/**
 * @author Eden Design & Communication, 2008
 */

// Stylesheet (javascript enabled specific styles) Extended
$('head').append('<link rel="stylesheet" href="../static/css/js-enabled.css" type="text/css" />');


// Global Fn initializes to be loaded on document ready event
$(document).ready(function(){
	// init Fn's
	
	
	
	replaceHeadings();		// IN SIFR CONFIG.JS
	FnTextSize.init();
	FnInputValue.init();
	FnToggleSubmitHovers.init();
	FnMoviePlayer.init();
	
	
	$('a.button-print').click(function() {
		window.print();
		return false;
	});
});


// Functions
FnMoviePlayer = {
	qty: 0,
	init: function() {
		if($('.movie-player-container').length > 0)	{
			
			$('.movie-player-container').each(function(){
				var src = $(this).find('.video').attr('title');
				var ratio = (180/290); 
				var w = $(this).width();
				var h = (w*ratio);
						
				var flashvars = {
					file: src,
					skin: '../static/swf/xusal-skin.swf',
					screencolor: '#a8b0b7',
					icons: false,
					fullscreen: true
				};
				var params = {
					menu: false,
					allowFullScreen: true
				};
				var attributes = {
				  id: "movie-player",
				  name: "XusalVideo"
				};
				$(this).find('.video').find('img').wrap('<div class="holder"></div>')
				$(this).find('.holder').attr('id','video'+FnMoviePlayer.qty);
				var holderId = 'video'+FnMoviePlayer.qty;
				
				swfobject.embedSWF("../static/swf/player.swf", holderId, w, h, "9.0.0","expressInstall.swf", flashvars, params, attributes);	
				FnMoviePlayer.qty++;
			})
		}
		
	}
}

FnToggleSubmitHovers = {
	init: function() {
		$(':submit').each(function() {
			$(this).hover(function(){
				$(this).addClass('hover');
			},
			function(){
				$(this).removeClass('hover');
			});
		});
	}
}

FnInputValue = {
	init:function(){	
		$('input:text').each(function(){
			var initValue = $(this).val();
			if(initValue == '') return;
			$(this).focus(function(){
				if($(this).val() == initValue) $(this).val('');
			})
			$(this).blur(function(){
				if($(this).val() == '') $(this).val(initValue);
			})
		})
		
	}	
}

//	:::::::::::::::::::::::::::::: Start Text resizing functionality
FnTextSize = {
	
	init: function() {
		$('.top-actions a.button-textsize-plus').click(function() {
			var currentSize = FnTextSize.getTextSize();
			currentSize = Number(currentSize);
			if (currentSize < 5) {
				var newTextSize = 0;
				newTextSize=currentSize+1;
				FnTextSize.setTextSize(newTextSize);
			}
			return false;
		});
		$('.top-actions a.button-textsize-min').click(function() {
			var currentSize = FnTextSize.getTextSize();
			currentSize = Number(currentSize);
			if (currentSize > 1) {
				var newTextSize=currentSize - 1;
				FnTextSize.setTextSize(newTextSize);
			}
			return false;
		});
		
		$('<link rel="alternate stylesheet" type="text/css" href="../static/css/fontsize-1.css" title="1" />').appendTo($('head')[0]);
		$('<link rel="alternate stylesheet" type="text/css" href="../static/css/fontsize-2.css" title="2" />').appendTo($('head')[0]);
		$('<link rel="alternate stylesheet" type="text/css" href="../static/css/fontsize-3.css" title="3" />').appendTo($('head')[0]);
		$('<link rel="alternate stylesheet" type="text/css" href="../static/css/fontsize-4.css" title="4" />').appendTo($('head')[0]);
		$('<link rel="alternate stylesheet" type="text/css" href="../static/css/fontsize-5.css" title="5" />').appendTo($('head')[0]);
		
		var cookie = FnTextSize.handleCookie.get("XusalTextSize");
		var title = cookie ? cookie : FnTextSize.getSavedTextSize();
		if (title!=null) {
			FnTextSize.setTextSize(title);
		} else {
			FnTextSize.setTextSize('3');
		}

		
	},
	setTextSize: function(title) {
		$('link[@rel*=style][@title]').each(function(i) 
		{
			this.disabled = true;
			if ($(this).attr('title') == title) this.disabled = false;
		});
  		FnTextSize.handleCookie.set("XusalTextSize", title, 365);
	},
	getTextSize:function() {
		var i, a;
		for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
			if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
		}
		return null;
	},
	getSavedTextSize: function() {
		var i, a;
		for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
			if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("rel").indexOf("alt") == -1 && a.getAttribute("title") ) return a.getAttribute("title");
		}
		return null;
	},
	handleCookie: {
		set: function(name, value, days) {

			if (days) {
				var date = new Date();
				date.setTime(date.getTime()+(days*24*60*60*1000));
				var expires = "; expires="+date.toGMTString();
			}
			else expires = "";
			document.cookie = name+"="+value+expires+"; path=/";	
		},
		get: function(name) {
			var nameEQ = name + "=";
			var ca = document.cookie.split(';');
			for(var i=0;i < ca.length;i++) {
				var c = ca[i];
				while (c.charAt(0)==' ') c = c.substring(1,c.length);
				if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
			}			
		}
	}
}
//	:::::::::::::::::::::::::::::: End Text resizing functionality

