$(document).ready(function(){
	// Navigation :: Primary Nav
	if(window.XMLHttpRequest=='function(){return new ActiveXObject("Microsoft.XMLHTTP")}') {
		$("#menu li").hover(
			function() { $("ul", this).show("fast"); $(this).addClass("hover");}, 
			function() { $(this).removeClass("hover"); }
		);
	} else {
		$("#menu li ul").fadeTo("normal", 0);
		$("#menu li").hover(
			function() { $("ul", this).fadeTo(400, 0.80).css("display", "block"); $(this).addClass("hover");}, 
			function() { $("ul", this).fadeTo(1, 0).css("display", "none"); $(this).removeClass("hover");}
		);
	}
	if (document.all) {
		$("#menu li").hoverClass("hover");
	}
	
	// Accessible Target links
	$(".external-window").click( function(e) {
		// Abort if a modifier key is pressed
		if (e.shiftKey || e.altKey || e.ctrlKey || e.metaKey) {
			return true;
		} else {
			// Change "_blank" to something like "newWindow" to load all links in the same new window
		    var newWindow = window.open(this.getAttribute('href'), '_blank');
			if (newWindow) {
				if (newWindow.focus) {
					newWindow.focus();
				}
				return false;
			}
			return true;
		}
		
	});
	$(".external-window").attr("title", "Link opens in an external window");
	
	
	//expanable list
	$("ul.expand-list a.expander").click(
		function() {
			if($(this).parents("li").attr("class")) {
				$(this).parents("li").toggleClass("selected");
			} else {
				$(this).parents("ul").children(".selected").removeClass("selected");
				$(this).parents("li").addClass("selected");
			}
			
			return false;
		}
	);
	
	//add in carousel
	if(document.getElementById("tvcCarousel")) {
		jQuery(".jcarousel-scope").jcarousel({
			orientation: "vertical",
			itemVisible: 2,
			itemScroll: 1,
			scrollAnimation: "slow"
		});
	}
	
	//carousel funcitonalit/
	$(".tvc-list li div").click(
		function() {			
			var movieToLoad;
			
			$(this).addClass("selected");
			movieToLoad = $(this).attr("id");
			
			var so = new SWFObject("/Structure_Files/flv-player.swf", "mediaPlayerINS", "350", "290", "8", "#ffffff");
			//var so = new SWFObject("/basemedia/flash/flv-player.swf", "mediaPlayerINS", "350", "290", "8", "#ffffff");
			so.addParam("wmode", "transparent");
			so.addParam("allowScriptAccess", "sameDomain");
			so.addVariable("flvPath", movieToLoad);
			//so.addVariable("flvPath", "/media/"+$(this).attr('id').replace("tvcid", "")+"/"+$(this).attr('class').replace(" selected", "")+".flv");
			so.addVariable("bufferingTime", "5");
			
			so.write("mediaPlayer");
			$("#mediaPlayer").addClass("has-flash");
			return false;
		}
	);
	
	//putting alt class on every second row in a table
	var counter = 0;
	$("table.table tbody tr").each(
		function(i) {
			if(counter == 1) {
				$(this).addClass("alt");
				counter = 0;
			} else {
				counter++;
			}
		}
	);
	
	//profiles menu on load
	if(document.getElementById("profiles-menu")) {
		var maxh = $("ul.profiles-menu").height();
		$("ul.profiles-menu ul").each(
			function(i) {
				h = $(this).height()
				if(h > maxh) {
					maxh = h;
				}
			}
		);
		//check the info div to see if they are also too large to fit they or that they may overlap the cnr span
		var cnrh = $("ul.profiles-menu span.bottom-cnr").height();
		$("ul.profiles-menu div.info").each(
			function(i) {
				h = $(this).height();
				if(h > maxh) {
					maxh = h + cnrh;
				}
			}
		);
		$("ul.profiles-menu").css("height", maxh+"px");
		
		$("ul.profiles-menu ul").each(
			function() {
				$("li:first", this).addClass("first-item");
			}
		);
		
		$("ul.profiles-menu li:first").addClass("selected");
		$("ul.profiles-menu li:first ul li:first").addClass("selected");
		
		//determine if something should be expanded for reddot edit
		if($("ul.profiles-menu p:contains('Click to edit position')").length>0) {
			$("ul.profiles-menu li").removeClass("selected"); // remove any currently selected
			$("ul.profiles-menu p:contains('Click to edit position')").parents("li").addClass("selected")
		} else {
			//add first if on live site / non reddot
		}
		
		
	}
	
	
	//profiles-menu behaviour
	$("ul.profiles-menu li a").not(":contains('click to')").hover(
		function() {
			if($(this).parents("div").attr("class").match("info")) {
				// nothing
			} else if($(this).parent().parent().attr("class")) {
				$("ul.profiles-menu li").removeClass("selected");
				$(this).parent().addClass("selected");
			} else {
				//2nd level);
				$("ul.profiles-menu ul li").removeClass("selected");
				$(this).parent().addClass("selected");
			}
		},
		function() {}
	);
	
	/*insert swf's*/
	if(document.getElementById("flashRotator")) {
		var rel = $("#flashRotator").attr("rel");
		var so = new SWFObject("/Structure_Files/rotator.swf", "sotester", "475", "200", "8", "#FF6600");
		so.addParam("wmode", "transparent");
		so.addVariable("xmlPath", rel);
		so.write("flashRotator");
	}
	
	$("ul.listing-split-content li").each(
		function(i)
		{
			$(this).children("div").vjustify();
		}
	);
	
	if(document.getElementById("shadowbox-links"))
	{ 
		Shadowbox.init();
		
		//shadowbox from input
		var links = [];
		$(".shadowbox").each(
			function(i)
			{
				links[i] = this;
			}
		);
		Shadowbox.setup(links, {});
		
	}
		
	
	/*reddot resets*/
	if($(".rededit").length>0) {
		$(".jcarousel-list li").css("height", "auto");
		$("#tvcCarousel").css("height", "auto").css("overflow", "visible");
		$(".jcarousel-list").css("height", "auto");
		$(".jcarousel-clip").css("overflow", "visible");
		$(".jcarousel-next").css("bottom", "-39px");
		
		$(".image-gallery-reddot li").vjustify();
	}
	
	
});

/*
	** Global functions
*/
function showUpdateCart() {
	$("div.subright div.cart a").css("display", "none");
	//console.log("clicked");
	$("div.subright div.cart a").show("slow");
}

jQuery.fn.log = function (msg) {
	console.log("%s: %o", msg, this);
	return this;
};

$.fn.hoverClass = function(c) {
	return this.each(function(){
		$(this).hover( 
			function() { $(this).addClass(c);  },
			function() { $(this).removeClass(c); }
		);
	});
};

jQuery.fn.vjustify=function() {
    var maxHeight=0;
    this.each(function(){
        if (this.offsetHeight>maxHeight) {maxHeight=this.offsetHeight;}
    });
    this.each(function(){
        $(this).height(maxHeight + "px");
        if (this.offsetHeight>maxHeight) {
            $(this).height((maxHeight-(this.offsetHeight-maxHeight))+"px");
        }
    });
};

function enableJSClass() {
	document.body.className = "jsEnabled";
}

function printPage() { 
	if (window.print) { 
		window.print() ; 
	} else { 
		var WebBrowser = '<OBJECT ID="WebBrowser1" WIDTH=0 HEIGHT=0 CLASSID="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></OBJECT>'; 
		document.body.insertAdjacentHTML('beforeEnd', WebBrowser); 
		WebBrowser1.ExecWB(6, 2);//Use a 1 vs. a 2 for a prompting dialog box WebBrowser1.outerHTML = ""; 
	}
} 