
// Adjust image caching in IE.
if (navigator.appName.indexOf("Microsoft") != -1) {
	try {
		document.execCommand("BackgroundImageCache", false, true);
	} catch(err) {}
}



$(document).ready(function() {
	
	// Center the content in the window.
    $("#outerContainer").centerInClient();
	
	// Set up a callback for window resize.
	$(window).bind('resize', function() {
		$("#outerContainer").centerInClient();
	}); 
	
	// Prepare the Newsletter signup link to open up.
	$("#links .signup a").click(function(event) {
		$.jqURL.loc('newsletter.html', {w:605,h:445,wintype:'_blank'});
	});
	
	$("#links .signup a").click(function(event) {
							 
		event.preventDefault();
	});								 
	
	// Prepare the email popup link.
	$("#toolsNav .email a").click(function(event) {
		Utils.sharePage("send", null, null);
		event.preventDefault();
	});
	
	
	// Open subnav if we need to (if we're on a page with subs).
	switch (true) {
		case ($("body.zooey").length > 0):
			$("#subNav_zooey").show();
			break;
		case ($("body.miranda").length > 0):
			$("#subNav_miranda").show();
			break;
		case ($("body.jazmine").length > 0):
			$("#subNav_jazmine").show();
			break;
	}
	
	
	// Prepare 'other' tracking links.
	$(".zooey .mySpaceLink").click(function(event) {
		pageTracker._trackPageview("/TheFabricOfMyLife/Zooey-LinkMySpace");
	});
	$(".jazmine .mySpaceLink").click(function(event) {
		pageTracker._trackPageview("/TheFabricOfMyLife/Jazmine-LinkMySpace");
	});
	$(".zooey .lyricsLink").click(function(event) {
		pageTracker._trackPageview("/TheFabricOfMyLife/Zooey-LyricsDownload");
	});
	$(".miranda .lyricsLink").click(function(event) {
		pageTracker._trackPageview("/TheFabricOfMyLife/Miranda-LyricsDownload");
	});
	$(".jazmine .lyricsLink").click(function(event) {
		pageTracker._trackPageview("/TheFabricOfMyLife/Jazmine-LyricsDownload");
	});
	
	
	
	// Set the copyright information.
	var right_now=new Date();
	var right_year=right_now.getYear(); 
	if (right_year < 2000)  right_year = right_year + 1900;  
	var cr = "&copy;" + right_year + "&nbsp;Cotton Incorporated. All rights reserved; America's Cotton Producers and Importers.";
	$("#copyright").html(cr);
	
	
});



$.fn.centerInClient = function(options) {
	/// <summary>Centers the selected items in the browser window. Takes into account scroll position.
	/// Ideally the selected set should only match a single element.
	/// </summary>    
	/// <param name="fn" type="Function">Optional function called when centering is complete. Passed DOM element as parameter</param>    
	/// <param name="forceAbsolute" type="Boolean">if true forces the element to be removed from the document flow 
	///  and attached to the body element to ensure proper absolute positioning. 
	/// Be aware that this may cause ID hierachy for CSS styles to be affected.
	/// </param>
	/// <returns type="jQuery" />
	var opt = { forceAbsolute: false,
				container: window,    // selector of element to center in
				completeHandler: null
			  };
	$.extend(opt, options);
   
	return this.each(function(i) {
		var el = $(this);
		var jWin = $(opt.container);
		var isWin = opt.container == window;

		// force to the top of document to ENSURE that 
		// document absolute positioning is available
		if (opt.forceAbsolute) {
			if (isWin)
				el.remove().appendTo("body");
			else
				el.remove().appendTo(jWin.get(0));
		}

		// have to make absolute
		el.css("position", "absolute");

		// height is off a bit so fudge it
		var heightFudge = isWin ? 2.0 : 1.8;

		var x = (isWin ? jWin.width() : jWin.outerWidth()) / 2 - el.outerWidth() / 2;
		// var y = (isWin ? jWin.height() : jWin.outerHeight()) / heightFudge - el.outerHeight() / 2;

		el.css("left", x + jWin.scrollLeft());
		// el.css("top", y + jWin.scrollTop());

		// if specified make callback and pass element
		if (opt.completeHandler)
			opt.completeHandler(this);
	});
}

Utils = {

	popImgs: [{img:"beltBuckles.jpg", dims:{w:526, h:441}},
			  {img:"blueDress.jpg", dims:{w:384, h:584}},
			  {img:"collar.jpg", dims:{w:526, h:441}},
			  {img:"cowboyBoots.jpg", dims:{w:526, h:441}},
			  {img:"delilah.jpg", dims:{w:526, h:441}},
			  {img:"flowerSundress.jpg", dims:{w:384, h:584}},
			  {img:"greenDress.jpg", dims:{w:384, h:584}},
			  {img:"greenSweatSuit.jpg", dims:{w:384, h:584}},
			  {img:"jeanJacket.jpg", dims:{w:384, h:584}},
			  {img:"jeans.jpg", dims:{w:384, h:584}},
			  {img:"journal.jpg", dims:{w:526, h:441}},
			  {img:"myDogs.jpg", dims:{w:526, h:584}},
			  {img:"pinkGuitar.jpg", dims:{w:384, h:584}},
			  {img:"redStripeDress.jpg", dims:{w:384, h:584}},
			  {img:"tangerineDress.jpg", dims:{w:384, h:584}},
			  {img:"vest.jpg", dims:{w:384, h:584}}],
	
	popWin:null,

	popImage: function(imgPath) {
		var imgName = imgPath.substring(imgPath.lastIndexOf('/') + 1);
		var dims = Utils.getDimsFromName(imgName);
		var isSlideShow = (imgName == "myDogs.jpg");
		if (!dims) return;
		var fileName = isSlideShow ? "miranda_closet_slideshow.html" : "miranda_closet_image.html?imgPath=" + encodeURIComponent(imgPath);
		if ((!Utils.popWin) || (Utils.popWin  && Utils.popWin.closed)) {
			// Means no window is open.
			Utils.popWin = window.open(fileName, "MirandasCloset", 
						"menubar=0,resizable=1,status=0,width=" + dims.w + ",height=" + dims.h);
		} else {
			// Resize existing.
			Utils.popWin.location.href = fileName;
			Utils.popWin.resizeTo (dims.w + 16, dims.h + 64);
			Utils.popWin.focus();
		}
		
	},
	
	getDimsFromName: function(imgName) {
		for (var i = 0; i < Utils.popImgs.length; i++) {
			if (Utils.popImgs[i].img == imgName) {
				return Utils.popImgs[i].dims;
			}
		}
		return null;
	},
	
	launchFile: function(filename) {
		alert (filename);
		$.jqURL.loc(filename, {w:800,h:600,wintype:'_blank'});
	},
	
	getSwfReference: function(movieName) {
		if (navigator.appName.indexOf("Microsoft") != -1) {
			return window[movieName];
		} else {
			return document[movieName];
		}
	},
	
	sharePage:function(shareType, title, params) {
		var returnUrl = encodeURIComponent($.jqURL.strip());
		returnUrl += (params == null) ? '' : encodeURIComponent('?' + params);
		title = ((title == null)||(title == 'null')) ? encodeURIComponent("TheFabricOfMyLife.com") : encodeURIComponent(title);
		if (shareType == "send") {
			var offset = 0;
			if (navigator.appName.indexOf("Microsoft") != -1) {
				offset = 18;
			} 
			$.jqURL.loc('share.html?url=' + returnUrl, {w:605 + offset,h:555,wintype:'_blank'});

		} else {
			switch (shareType) {
				case "facebook":
					url = 'http://www.facebook.com/sharer.php?u=' + returnUrl + '&t=' + title;
					break;
				case "digg":
					url = 'http://digg.com/submit?url=' + returnUrl + '&title=' + title;
					break;
				case "yahoo":
					url = 'http://buzz.yahoo.com/buzz?targetUrl=' + returnUrl + '&headline=' + title;
					break;
				case "delicious":
					url = 'http://delicious.com/save?v=5&amp;noui&amp;jump=close&amp;url=' + returnUrl + '&amp;title=' + title;
					break;
				case "twitter":
					url = 'http://www.twitter.com/home?status=' + returnUrl;
					break;
			}
					
			$.jqURL.loc(url, {w:800,h:440,wintype:'_blank'});
			
		}
	}
}