var DTD = DTD || {};

$j(function () {
	DTD.FieldhintInput();
    DTD.Tabulator();
	DTD.flyoutMenu();
    DTD.FixSearchSubmit();
	
	$j("#basic").addClass("visuallyhidden");
	$j("#enhanced").removeClass("visuallyhidden");
});

(function(w,d,u){
	
	function testMediaQuery(mq) {

		var st = d.createElement('style'),
			div = d.createElement('div'),
			ret, docElement = d.documentElement,
			docHead = d.head || d.getElementsByTagName('head')[0];

	  st.textContent = mq + '{#mq{height:3px}}';
	  docHead.appendChild(st);
	  div.id = 'mq';
	  docElement.appendChild(div);

	  ret = div.offsetHeight === 3;

	  st.parentNode.removeChild(st);
	  div.parentNode.removeChild(div);

	  return !!ret;

	}

	(function fakeMediaQuery() {
		if(!testMediaQuery("@media only all")) {
			$j(w).bind("resize",function(){
				checkSize($j(this));
			});
			checkSize($j(w));
		}
	})();

	function checkSize(win) {
		var winSize = win.width(),
			docSize = $j(document).height(),
			mClass = "fake-mediaquery", 
			docElem = d.documentElement;
				
		if(winSize >= 1390) {
			$j(docElem).addClass(mClass+"-1390px");
		} else if(winSize < 1390) {
			$j(docElem).removeClass(mClass+"-1390px");
		}
		
		if(winSize <= 981) {
			$j(docElem).addClass(mClass+"-980px");
		} else if(winSize > 980) {
			$j(docElem).removeClass(mClass+"-980px");
		}
		
		if(docSize >= 1700) {
			$j(".pos01").addClass("pos07");
		}
	}
	
	function FieldhintInput() {
		var f = d.createElement("input"),
			isNative = !!("placeholder" in f), hint;
		 
		if(!isNative) {
			$j(".fieldhint").each(function(i, el) {
				hint = $j(el).attr("placeholder");
				$j(el).val(hint);
	 
				$j(el).bind({
					"focusin": function(e) {
						hint = $j(el).attr("placeholder");
						if ($j(el).val() === hint) {
							$j(el).val("");
						}
					},
					"focusout": function(e) {
						hint = $j(el).attr("placeholder");
						if ($j(el).val() === "") {
							$j(el).val(hint);
						}
					}
				});
			});
            
		}
        $j(".scfSingleLineTextLabel, .scfTelephoneLabel, .scfEmailLabel, .scfMultipleLineTextLabel, .scfNumberLabel").each(function(i, el) {
			hint = $j(el).html();
            thisFor = $j(el).attr("for");
			$j('#' + thisFor).val(hint);
            
            $j('#' + thisFor).attr("placeholder", hint);
	 
			$j('#' + thisFor).bind({
				"focusin": function(e) {
                    
					hint = $j(this).attr("placeholder");
					if ($j(this).val() === hint) {
						$j(this).val("");
					}
				},
				"focusout": function(e) {
					hint = $j(this).attr("placeholder");
					if ($j(this).val() === "") {
						$j(this).val(hint);
					}
				}
			});
		});
	}
	
	function Tabulator() {
		var tabs = $j(".tabs"),
			tabcontent = $j(".tabcontent");
		
		tabcontent.not(":lt(1)").hide();
		
		tabs.find("a").bind("click",function(e) {
			var tab = $j(this),
				tabID = tab.attr("href"),
				content = tabs.siblings(tabID);
			
			tabs.find("li").removeClass("active");
			tab.parent("li").addClass("active");
			tabcontent.hide();	
			content.show();
			
			e.preventDefault();
		});
	}

    var hasEnterBeenPressed = false;

    function FixSearchSubmit() {
        $j('.searchbox input:text').keypress(function (e) {       
            if (e.which == 13){
                e.stopPropagation();
                LaunchSearchQuery();
            }
        });

        $j('.searchbox input:text').focusin(function (e) {
            hasEnterBeenPressed = false;
        });
        
        $j('.searchbox input:text').focusout(function (e) {
            if (hasEnterBeenPressed)
            {
                LaunchSearchQuery();
            }
        });

         $j('.searchbox input:text').keypress(function (e) {
            hasEnterBeenPressed = e.keyCode == 13;
        });
    }

    function LaunchSearchQuery()
    {
        var query = $j('.searchbox input:text').val();
        if (query.length > 0) {
            location.href = "/search/?query=" + query;
        }
    }
	
	function flyoutMenu() {
		$j("a.flyout").bind("click",function(e){
			var flyout = $j(this).next(".flyout");
			
			flyout.toggle();
			$j(".module04").bind('click',function(e){e.stopPropagation();});
			$j("body").one("click",function(e){
				flyout.hide();
			});
			
			e.stopPropagation();
			e.preventDefault();
		});
	}
	
	DTD["FieldhintInput"] = FieldhintInput;
	DTD["Tabulator"] = Tabulator;
	DTD["flyoutMenu"] = flyoutMenu;
    DTD["FixSearchSubmit"] = FixSearchSubmit;

})(this, this.document);


