/* /sites/MPSI/_resources/js/global.js*/ /// <reference path="/_resources/js/jquery-1.2.6.min.js" />
/// <reference path="/_resources/js/universal.js" />

/* 
MPSI - global.js
*/

// IE6 flickering
try { document.execCommand("BackgroundImageCache", false, true); } catch (err) { }

$j(document).ready(function() {

	$j("a[rel^='prettyPhoto']").prettyPhoto({animationSpeed:'fast',slideshow:10000, social_tools:'', show_title:false, overlay_gallery:false});
	
	// Change all a.video tags to swf video player objects
	insertVideo();

	// add class to drop downs and buttons for IE <6
	if (document.all) {
		$j("#nav li").hover(
			function() { $j(this).addClass("iehover"); },
			function() { $j(this).removeClass("iehover"); }
	    );
	    
	} // if document.all

    // Language Select drop down
	$j("#lang-select").click(function() {
		pullDownLang();
	});
	$j("#lang > ul > li > a").click(function() {
		pullDownLang();
	});

});   // document ready / end jquery


// Usage: <a id="{some id}" class="video {height} {width} {videoid}" href="#" />
var insertVideo = function() {
	$j("a.video").each(function(i) {
		var $this = $j(this);
		var params = $this.attr("class").split(" ");

		// params[1] = height
		// params[2] = width
		// params[3] = index of video to play
		var flashVars = {
			showMovie: (3 in params) ? params[3].toString() : "0"
		};
		var objectVars = {
			quality: "high"
		};

		// provide sane defaults if no size parameters are provided.
		var height = (1 in params) ? params[1] : 309;
		var width = (2 in params) ? params[2] : 346;
		swfobject.embedSWF("/sites/MPSI/_resources/flash/mps_pt_viewer.swf", $this.attr("id"), width, height, "8", undefined, flashVars, objectVars, undefined);
	});
};


function pullDownLang() {
	var list = $j("#lang-list");
	if (list.length > 0) {
		if (list.attr('class').indexOf('pulled-out') != -1) {
			list.slideUp("slow").removeClass('pulled-out');
		} else {
			list.slideDown("slow").addClass('pulled-out');
		}
	}
}

// Cookies get/set/delete
function getCookie(name) {
	var start = document.cookie.indexOf(name + "=");
	var len = start + name.length + 1;
	if ((!start) && (name != document.cookie.substring(0, name.length))) {
		return null;
	}
	if (start == -1) return null;
	var end = document.cookie.indexOf(";", len);
	if (end == -1) end = document.cookie.length;
	return unescape(document.cookie.substring(len, end));
}
function setCookie(name, value, expires, path, domain, secure) {
	var today = new Date();
	today.setTime(today.getTime());
	if (expires) {
		expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date(today.getTime() + (expires));
	document.cookie = name + "=" + escape(value) +
		((expires) ? ";expires=" + expires_date.toGMTString() : "") + //expires.toGMTString()
		((path) ? ";path=" + path : "") +
		((domain) ? ";domain=" + domain : "") +
		((secure) ? ";secure" : "");
}
function deleteCookie(name, path, domain) {
	if (getCookie(name)) document.cookie = name + "=" +
			((path) ? ";path=" + path : "") +
			((domain) ? ";domain=" + domain : "") +
			";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

// Text size widget
$j(window).load(function() {
	
	//if($j(".homepage")[0]) return false; 
	
	var baseSize = parseFloat($j('html').css('font-size'), 10);
	var textsize = getCookie("textsize");
	var coefficient = 1.0;
	if (textsize) {
		coefficient = parseFloat(textsize, 10);
	} else {
		setCookie("textsize", 1.0);
	}

	var FontController = {
		BaseSize: baseSize,
		Coefficient: coefficient,
		IncrementBy: 0.2,
		Minimum: .6,
		Maximum: 1.4,

		SetSize: function() {
			var newSize = this.BaseSize * this.Coefficient;
			setCookie("textsize", this.Coefficient);
			$j('html').css({ fontSize: newSize });
			// prevent change rendering bug in IE <6 on search results page
			if (document.all) {
				$j(".search button").focus().blur();
			} // if document.all			
		},

		Increment: function() {
			var newCo = this.Coefficient + this.IncrementBy;
			if (newCo <= this.Maximum) {
				this.Coefficient = newCo;
				this.SetSize();
			}
		},

		Decrement: function() {
			var newCo = this.Coefficient - this.IncrementBy;
			if (newCo >= this.Minimum) {
				this.Coefficient = newCo;
				this.SetSize();
			}
		},

		Reset: function() {
			this.Coefficient = 1.0;
			this.SetSize();
		}

	};

	if (coefficient != 1.0) {
		FontController.SetSize();
	}

	// Set up onclick functions	
	$j("#regular").click(function() {
		FontController.Reset();
		return false;
	});
	// Increase Font Size
	$j("#large").click(function() {
		FontController.Increment();
		return false;
	});
	// Decrease Font Size
	$j("#small").click(function() {
		FontController.Decrement();
		return false;
	});
});

