function gup( name, urlToParse )
{
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regexS = "[\\?&]"+name+"=([^&#]*)";
	var regex = new RegExp( regexS );
	var results = urlToParse ? regex.exec( urlToParse ) : regex.exec( window.location.href );
	if( results == null ) {
		return "";
	} else {
		return results[1];
	}
}


$(document).ready(function() {
	
	//____________________________________________________________________________________________________MODAL FUNCTIONS
	var modalTitle = "";
	var modalDesc = "";
	var modalVideo = "";
	var modalImg = "";
	var modalSWF = "";
	var modalSWFwidth = "";
	var modalSWFheight = "";
	
	$("a.zoomLink").click(function () {
		modalTitle = $(this).attr("title");
		modalImg = gup("img", $(this).attr("href"));
		modalDesc = $(this).children(".zoomLinkImg").attr("alt");								  
	});

	$("a.modalVideoLink").click(function () {
		modalTitle = $(this).text();
		modalVideo = gup("flv", $(this).attr("href"));
		modalDesc =  $(this).attr("title");
	});

	$("a.modalDemoLink").click(function () {
		modalTitle = $(this).text();
		modalSWF = gup("swf", $(this).attr("href"));
		modalSWFwidth = gup("w", $(this).attr("href"));
		modalSWFheight = gup("h", $(this).attr("href"));
		modalDesc =  $(this).attr("title");
	});

	makeModal = function(id, data, triggerElem, loadFunc) {
		$("body").append("<div id=\"" + id + "\" class=\"jqmWindow\"></div>");
		$("#"+id).jqm({
			ajax: data, 
			trigger: triggerElem,
			modal: 'true',
			onLoad: loadFunc
		});
		if($.browser.msie && $.browser.version < 7) {
			//hide fancy modal borders
			$(".modalSupport #top_border").hide();
			$(".modalSupport #bot_border").hide();
		}
	}
	
 	if( $(".modalVideoLink").length > 0 ) {
		$.getScript("../js/swfobject_jq.js");
		makeModal("modalVideo", "@href", "a.modalVideoLink",  function (){
			if( $.browser.mozilla && parseFloat($.browser.version) < 3 && (navigator.appVersion.indexOf("Mac")!=-1) ) {
				$(".jqmOverlay").addClass("jqmOverlayFFMac");
			}
			
			$("div#modalTitle").text(modalTitle);
			$("div#modalDesc").text(modalDesc);
			
			var flashvars = {
				"vidPath" : "foo"
			}
			var params = {
				"wmode"	:	"transparent"
			};
			setAttributes = function (id) {
				var attributes = {
					"id"		:	id,
					"class"		: 	"flashCont",
					"name"	: id, 
					"quality" : "high",
					"align" : "middle",
					"play" : "true",
					"loop" : "true",
					"scale" : "showall",
					"wmode" : "transparent",
					"menu" : "true",
					"allowfullscreen" : "true",
					"allowscriptaccess" : "always"
				};
				return attributes;
			}
			$.swfobject.embedSWF("../swf/support_video_theatre.ccswf"+"?t=" +new Date().getTime(), "nonFlashVideoTheatre", "450", "300", "9.0.0", "../swf/expressInstall.ccswf", flashvars, params, setAttributes("swfVideoTheatre"));
			
			$(".modalSupport #swfVideoTheatre").bind("modalSwfLoad", function() {
				$(".modalSupport #swfVideoTheatre")[0].loadVideo(modalVideo);
			});
		});
	}
	
	if( $(".modalDemoLink").length > 0 ) {
		$.getScript("../js/swfobject_jq.js");
		makeModal("modalDemo", "@href", "a.modalDemoLink",  function (){
			if( $.browser.mozilla && parseFloat($.browser.version) < 3 && (navigator.appVersion.indexOf("Mac")!=-1) ) {
				$(".jqmOverlay").addClass("jqmOverlayFFMac");
			}
			
			$("div#modalTitle").text(modalTitle);
			$("div#modalDesc").text(modalDesc);
			
			var flashvars = {
				"vidPath" : "foo"
			}
			var params = {
				"wmode"	:	"transparent"
			};
			setAttributes = function (id) {
				var attributes = {
					"id"		:	id,
					"class"		: 	"flashCont",
					"name"	: id, 
					"quality" : "high",
					"align" : "middle",
					"play" : "true",
					"loop" : "true",
					"scale" : "showall",
					"wmode" : "window",
					"menu" : "true",
					"allowfullscreen" : "true",
					"allowscriptaccess" : "always"
				};
				return attributes;
			}
			$.swfobject.embedSWF("../swf/" + modalSWF +"?t=" +new Date().getTime(), "nonFlashSWFDemoCont", modalSWFwidth, modalSWFheight, "9.0.0", "../swf/expressInstall.ccswf", flashvars, params, setAttributes("swfDemo"));
			
		});
	}
	
	if( $(".modalLink").length > 0 ) { makeModal("modal_emailArticle", "@href", "a.modalLink"); }
	
	if( $(".zoomLink").length > 0 ) {
		makeModal("modalEquipmentImg", "@href", "a.zoomLink", function (){
			$("div#modalTitle").text(modalTitle);
			$("img#modalImg").attr("src", modalImg);
			$("p#modalDesc").text(modalDesc);
		});
	}

	$(".zoomLink").prepend("<img src=\"../img/icon_zoom.ccom\" width=\"19\" height=\"19\" class=\"zoomLinkIcon\" />");

	//____________________________________________________________________________________________________BROWSER WORKAROUNDS
	$("div.threeColCont div.colCont:last").addClass("colContLast");
	if($.browser.msie) {
		if($.browser.version < 7) {
			$("div.leftColCont.complexCorners div.content").parent().addClass("leftColCont-complexCorners");
			$("div.leftColBotCont.complexCorners div.content").parent().addClass("leftColBotCont-complexCorners");
			$("#supportMoving colCont").css("height", $("#supportMoving colCont:first").height());
			$(".clear").css("display","inline");
		}
	}
   
	//____________________________________________________________________________________________________EXPANDING TOPICS FUNCTIONS

	$("a.expandingTopicLink").click(function () {    
		var topic = $(this).parents(".expandingTopic");
		topic.toggleClass("expanded");
		topic.hasClass("expanded") ? topic.children("div:first").show() : topic.children("div:first").hide();
		topic.parent().children(".expandingTopic").length == topic.parent().children(".expandingTopic.expanded").length ? $(".expandAllTopicBtn").addClass("collapseState") : $(".expandAllTopicBtn").removeClass("collapseState");
		
		return false;
	});
	
	$(".expandAllTopicBtn").click( function() {
		if( $(this).hasClass("collapseState") ) {
			$(this).siblings(".expandingTopic").removeClass("expanded");
			$(this).siblings(".expandingTopic").each(function() {
				$(this).children("div:first").hide();
			});
		} else {
			$(this).siblings(".expandingTopic").addClass("expanded");
			$(this).siblings(".expandingTopic").each(function() {
				$(this).children("div:first").show();
			});
		}
		$(this).toggleClass("collapseState");
		return false;
	});
	
	$("ul.expandingQandA li p").hide();
	$("ul.expandingQandA li a").click(function () {
		$(this).toggleClass("selected");
		$(this).siblings("p").toggle();
		return false;	
	});
	
	//____________________________________________________________________________________________________VIDEO FUNCTIONS
	onSWFLoad = function() {
//		alert("Flash Loaded");
//		alert(document.swfVideoTheatre);
//		alert($("#swfVideoTheatre")[0]);
		$(".mediaTheatreDesc a, .mediaTheatreThn a").bind("click", function( e ) {
			$(".mediaTheatreItem").removeClass("currentVid")
			$(this).parents(".mediaTheatreItem").addClass("currentVid");
			$("#swfVideoTheatre")[0].loadVideo($(this).attr("href"));
			return false;
		});
		$(".mediaTheatreThn:first a").trigger("click");
		$(".modalSupport #swfVideoTheatre").trigger("modalSwfLoad");
	}
	
	//____________________________________________________________________________________________________TABS FUNCTIONS
	
	$("a.tablink").click(function () {
		var currTab = $(this).parents("div.tab");
		var currTxt = $(this).attr("href");

		$("div.tabContainer div.tab").removeClass("selected");	
		$("div.tabContent").removeClass("selected");;
		currTab.addClass("selected");
		$(currTxt).addClass("selected");
		
		$(this).blur();
		return false;	
	});
	
//	$("div.tabContainer div.tab:first-child").toggleClass("selected");
//	$("div.tabContainer div.tab:first-child a.tablink").click();
	
	$("a.subItemLink").click(function () {
		var currTxt = $(this).attr("href");
		var siblings = $(this).parents(".subItemLinks").children("li").children("a.subItemLink");		
		siblings.removeClass("selected");
		$(this).addClass("selected");
		siblings.each( function() { 
			$($(this).attr("href")).removeClass("selected"); 
		})
		$(currTxt).addClass("selected");
		$(this).blur();
		return false;	
	});
	
	//____________________________________________________________________________________________________ALERTS FUNCTIONS
	
	if(!($.browser.msie && $.browser.version < 7)) {
		$("#alertCont").prepend("<img class=\"ulCorner\" src=\"/CMS_IMAGES/cont_alerts_corner_ul.ccom\"/><img class=\"urCorner\" src=\"/CMS_IMAGES/cont_alerts_corner_ur.ccom\"/><img class=\"llCorner\" src=\"/CMS_IMAGES/cont_alerts_corner_ll.ccom\"/><img class=\"lrCorner\" src=\"/CMS_IMAGES/cont_alerts_corner_lr.ccom\"/>");
	}
	
	//____________________________________________________________________________________________________SEARCH RESULTS FUNCTIONS
	$("#searchResultsHeader img").bind("click", function() {
		$(this).siblings("img").removeClass("refine");
		$(this).addClass("refine");
	})
	
	//____________________________________________________________________________________________________RANKER FUNCTIONS
	resetRanks = function() {
		$(".ranker").each(function() {
			var avgRanking = Math.round($(this).attr("title"));
			$(this).addClass("ranked" + avgRanking);
		});
	}

	$(".ranker li a").hover( 
		function() {
			var theRanker = $(this).parents(".ranker");
			if( !theRanker.hasClass("user-ranked") ) {
				//$(this).parents(".ranker").filter( function() { return $(this).attr("class").match(/.*ranked[0-5]{1}/); } )
				var hoverRank = $(this).text();
				var classStr = $(this).parents(".ranker").attr("class");
				if( /.*rank[0-5]{1}/.test(classStr) ) {
					var newClassStr = classStr.replace(/rank[0-5]{1}/, "rank"+hoverRank);
					theRanker.attr("class",newClassStr);
				} else {
					theRanker.addClass("rank" + hoverRank);
				}
			}
		},
		function() {
			var hoverRank = $(this).text();
			//$(this).parents(".ranker").removeClass("rank" + hoverRank);
		}
	);
	
	$(".ranker li a").click( function() {
		$(this).blur();
		var theRanker = $(this).parents(".ranker");
		if( !theRanker.hasClass("user-ranked") ) {
			$.ajax({
				url: "../ajax/ajax_ranker.xml",
				datatype: "xml",
				data: { user_rank : $(this).text() },
				method: "GET",
				success: function( data, textStatus ) {
					var newRank = $("rank", data).text();
					if(!($.browser.msie && $.browser.version < 7)) {
						theRanker.attr("class", "ranker user-ranked ranked" + newRank);
					} else {
						theRanker.attr("class", "ranker ranked" + newRank + " user-ranked" + newRank);
					}
				}
			});
		}
		return false;
	});
	
	resetRanks();
	
});
