﻿String.prototype.contains = function(val){
	return this.indexOf(val) > -1;
}

jQuery.noConflict();

jQuery(function($) {
	overlayHover();

	//toggle txtSearch default text
	var txtSearch = $("input.txtSearch");

	txtSearch.focus(function() {
		if ($(this).val() === $(this).attr("title")) {
			$(this).val("");
		}
	});

	txtSearch.blur(function() {
		if ($(this).val() === "") {
			$(this).val($(this).attr("title"));
		}
	});


	//imgBtnSearch click listener
	$("input.imgBtnSearch").click(function() {
		if (txtSearch.val() === txtSearch.attr("title")) {
			alert("Please enter a search term.");
			return false;
		}
	});

	//highlights_wrapper initial values
	var hw = $("div.highlights_wrapper");
	var ulServices = $("ul.services");
	var allow = true;

	//left and right arrows
	$("a.left_arrow").click(function() {
		var mylast = $("ul.services li:last");
		var hwWidth = hw.width();
		if (allow == true) {
			allow = false;
			mylast.clone().prependTo(ulServices);
			hw.width(hwWidth + 232);
			hw.animate({ "left": "-=232px" }, 0);
			hw.stop(true, true).animate({ "left": "+=232px" }, 500, function() {
				mylast.remove();
				allow = true;
			});
		}
		overlayHover();

		//DD_belatedPNG.fix(".fix_png");
		//alert("test");

		return false;
	});

	$("a.right_arrow").click(function() {
		var myfirst = $("ul.services li:first");
		var hwWidth = hw.width();
		if (allow == true) {
			allow = false;
			myfirst.clone().appendTo(ulServices);
			hw.width(hwWidth + 232);
			hw.stop(true, true).animate({ "left": "-=232px" }, 500, function() {
				hw.animate({ "left": "+=232" }, 0);
				myfirst.remove();
				allow = true;
				//alert(typeof DD_belatedPNG.fix);

				if (typeof DD_belatedPNG !== "undefined" && typeof DD_belatedPNG.fix === "function") {
					//DD_belatedPNG.fix(".services .fix_png");
					//alert($(".services .fix_png").length);
				}
			});
		}
		overlayHover();

		//
		//alert(typeof DD_belatedPNG);

		return false;
	});

	//animate the overlay movement
	function overlayHover() {
		$("ul.services li a").hover(
		    function() {
		    	$(this).find("span.overlay").stop().animate({ "margin-top": "0px", "padding-top": "10px" }, 250);
		    },

		    function() {
		    	$(this).find("span.overlay").stop().animate({ "margin-top": "44px", "padding-top": "3px" }, 500);
		    }
	    );
	}

	//animate solution button overlay movement
	$(".solution_nav a").hover(function() {
		$(this).find(".solution_nav_arrow").css("background-image", "url(../BZResults/images/solution_nav_arrow_down.png)");
		$(this).find(".solution_nav_frame").css("background-image", "url(../BZResults/images/solution_nav_active.png)");
		$(this).find(".solution_nav_frame_first").css("background-image", "url(../BZResults/images/solution_nav_active.png)");
		$(this).find(".solution_nav_shadow").css("top", "20px");
		$(this).parent("li").stop().animate({ "margin-top": "0" }, 200);
	}, function() {
		$(this).find(".solution_nav_arrow").css("background-image", "url(../BZResults/images/solution_nav_arrow_up.png)");
		$(this).find(".solution_nav_frame").css("background-image", "url(../BZResults/images/solution_nav_inactive.png)");
		$(this).find(".solution_nav_frame_first").css("background-image", "url(../BZResults/images/solution_nav_inactive_first.png)");
		$(this).find(".solution_nav_shadow").css("top", "0");
		$(this).parent("li").stop().animate({ "margin-top": "20px" }, 200);
	});

	//$(".solution div[rel=cnt_0]").fadeIn(300);

	// animate solution swap
	$(".solution_nav a").click(function() {
		var n = $(this).attr("rel").substring(4);
		var target = $(".solution div[rel=cnt_" + n + "]");
		var active = $(".solution .active");
		var clicked = active.attr("rel").substring(4);

		if (!(n == clicked)) {
			active.stop().fadeOut(300, function() {
				$(this).removeClass("active");
				target.stop().fadeIn(300).addClass("active");
			});
		}
	});

	// content tabs click functions
	$(".content_tabs a").click(function() {
		var clicked = $(".content_tabs li");
		var target = $(this).parent();
		var getFirst = $(".content_tabs li:first");
		var getLast = $(".content_tabs li:last");

		clicked.removeClass("active");
		if (target.attr("class").indexOf("first") > -1 || target.attr("class").indexOf("first active") > -1) {
			getFirst.find("img.first").attr("src", "../BZResults/images/content_nav_tab_cap_left_active.png");
		}
		else {
			getFirst.find("img.first").attr("src", "../BZResults/images/content_nav_tab_cap_left_inactive.png");
		}
		if (target.attr("class").indexOf("last") > -1 || target.attr("class").indexOf("last active") > -1) {
			getLast.find("img.last").attr("src", "../BZResults/images/content_nav_tab_cap_right_active.png");
		}
		else {
			getLast.find("img.last").attr("src", "../BZResults/images/content_nav_tab_cap_right_inactive.png");
		}
		target.addClass("active");

		var target_cnt_index = $(this).attr("class").substring($(this).attr("class").indexOf("btn") + 3);
		var target_cnt = $("div.content_body > div.cnt" + target_cnt_index);
		var target_cnt_class = target_cnt.attr("class");

		$("div.content_body > div").each(function() {
			var tempCnt = $(this);
			var tempCnt_class = tempCnt.attr("class");

			if (tempCnt_class.indexOf(target_cnt_class) === -1) {
				if (tempCnt_class.indexOf("tabContentBlock_active ") > -1) {
					tempCnt.attr("class", tempCnt_class.replace("tabContentBlock_active ", "tabContentBlock "));
				}
			}
			else {
				tempCnt.attr("class", target_cnt_class.replace("tabContentBlock ", "tabContentBlock_active "));
			}
		});
		
		if($.browser.msie) {
			var mainSection_innerWrapper = $(".mainSection_innerWrapper");
			
			var scroller_left = mainSection_innerWrapper.offset().left;
			var scroller_top = mainSection_innerWrapper.offset().top + mainSection_innerWrapper.height();
			
			var scroller_position = {
				left: scroller_left,
				top: scroller_top
			}
			
			if($.browser.version != "6.0") {
				$(".highlights_outter_frame").css({
					"top": scroller_position.top,
					"left": scroller_left + 19
				});
			}
		}
		
		SetMainContentHeight();
	});



	// solution side nav functions
	$(".content_side_nav a").hover(function() {
		$(this).find("span").css("color", "#f6a21f");
	}, function() {
		if (!($(this).find("span").attr("class") == "title")) {
			$(this).find("span").css("color", "#fff");
		}
	});

	$(".content_side_nav a").click(function() {
		var clickedA = $(".content_side_nav a");
		var clickedS = $(".content_side_nav span");
		var targetA = $(this);
		var targetS = $(this).find("span");

		clickedA.removeClass("active");
		clickedS.removeClass("title");
		targetA.addClass("active");
		targetS.addClass("title");
	});



	//related content
	var relCnt_inBody = $("div.relatedContent_innerBody");

	if (relCnt_inBody.height() < 177) {
		relCnt_inBody.height(177);
	}

	$("div.relatedContent_outerWrapper").css("visibility", "visible");
	
	function SetMainContentHeight() {
		//set height of main body
		if($(".content_side_wrapper").length > 0) {
			var sidebar_height = $(".content_side_wrapper").outerHeight();
			
			var content_body_wrapper = $(".content_body_wrapper");
			var body_height = content_body_wrapper.outerHeight();
			
			if(body_height < sidebar_height) {			
				var content_body_top_height = content_body_wrapper.find(".content_body_top").height();
				var content_body_bot_height = content_body_wrapper.find("content_body_bot").height();
				
				var content_body = content_body_wrapper.find(".content_body");
				var content_body_padding_top = parseInt(content_body.css("padding-top").replace("px", ""));
				var content_body_padding_bot = parseInt(content_body.css("padding-bottom").replace("px", ""));
				
				var target_height = sidebar_height - content_body_padding_top - content_body_padding_bot;
				content_body.css("min-height", target_height);
				
				//alert("sidebar_height: " + sidebar_height);
				//alert("body_height: " + body_height);
				//alert("target_height: " + target_height);
			}
		}
		
		SetSliderPosition();
	}
	
	SetMainContentHeight();
	
	function SetSliderPosition() {
		if($.browser.msie) {
			var currentTemplate = $("#currentTemplate");
			
			if(currentTemplate.val() === "Homepage") {
				$(".highlights_outter_frame").css("position", "relative");
				$(".pdWrapper").css("padding-top", 0);
			}
			else {
				var mainSection_innerWrapper = $(".mainSection_innerWrapper");
				
				var scroller_left = mainSection_innerWrapper.offset().left + 19;
				var scroller_top = mainSection_innerWrapper.offset().top + mainSection_innerWrapper.height();
				
				var scroller_position = {
					left: scroller_left,
					top: scroller_top
				}
				
				if($.browser.version != "6.0") {
					$(".highlights_outter_frame").css({
						"top": scroller_position.top,
						"left": scroller_left
					});
				}
			}
		}
	}
	
	$(window).resize(function () {
		SetSliderPosition();
	});
});
