/**
* Styleswitch stylesheet switcher built on jQuery
* Under an Attribution, Share Alike License
* By Kelvin Luck ( http://www.kelvinluck.com/ )
**/

$(document).ready(function() {
	$('.styleswitch').click(function()
	{
		switchStylestyle(this.getAttribute("rel"));
		return false;
	});
	var c = readCookie('catstyle');
	//alert(c);
	if (c) switchStylestyle('catstyle',c);
	var d = readCookie('catpstyle');
	if (d) switchStylestyle('catpstyle',d);

	if(c.match(/(on){1}/gi)!=null){
		$("img.onbut").src("/style/EPAL/img/share_buttons/funview_on.gif");
		$("img.offbut").src("/style/EPAL/img/share_buttons/funview_off.gif");

		}else{
		$("img.onbut").src("/style/EPAL/img/share_buttons/funview_off.gif");
		$("img.offbut").src("/style/EPAL/img/share_buttons/funview_on.gif");		
			}
	
});
function switchStylestyle(cn,styleName)
{
	//alert(cn);
	$('link[@rel*=style][@title]').each(function(i) 
	{
		if(this.getAttribute('title')!=null){
		//  alert(this.getAttribute('title').match(/^[a-z]+/gi));
			if(this.getAttribute('title').match(/^[a-z]+/gi)== cn){this.disabled = true;}			
			if (this.getAttribute('title') == styleName){
			//	alert(document.cookie+"\ncookie:"+readCookie(cn)+"\ngoto:"+styleName);
				this.disabled = false;} 					
			}
	});
	
	createCookie(cn, styleName);
}

// cookie functions http://www.quirksmode.org/js/cookies.html
function createCookie(name,value,days)
{
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+escape(value)+expires+";";
}
function readCookie(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);
	}
	return null;
}
function eraseCookie(name)
{
	createCookie(name,"",-1);
}
// /cookie functions
function dismode(mode){
	var c = readCookie('catstyle');
	var d = readCookie('catpstyle');
	if(c.match(/(on){1}/gi)!=null){
		switchStylestyle('catstyle','catstyle.'+mode+'imgon');
	}else if(c.match(/(off){1}/gi)!=null){
		switchStylestyle('catstyle','catstyle.'+mode+'imgoff');
	}
	if(d.match(/(on){1}/gi)!=null){
		switchStylestyle('catpstyle','catpstyle.'+mode+'imgon');
	}else if(d.match(/(off){1}/gi)!=null){
		switchStylestyle('catpstyle','catpstyle.'+mode+'imgoff');
	}else{
		switchStylestyle('catpstyle','catpstyle.nolist');
	}
}
function toggle_image_on(){
	var c = readCookie('catstyle');
	var d = readCookie('catpstyle');
	$("img.onbut").src("/style/EPAL/img/share_buttons/funview_on.gif");
	$("img.offbut").src("/style/EPAL/img/share_buttons/funview_off.gif");
		

	
	if(c.match(/^catstyle.box/gi)!=null){
		switchStylestyle('catstyle','catstyle.boximgon');
		}else{
		switchStylestyle('catstyle','catstyle.listimgon');
		}
	if(d.match(/^catpstyle.box/gi)!=null){
			switchStylestyle('catpstyle','catpstyle.boximgon');
		}else if(d.match(/^catpstyle.nolist/gi)!=null){
			//do nothing thing
		}else{
			switchStylestyle('catpstyle','catpstyle.listimgon');
		}		
	}
function toggle_image_off(){
	var c = readCookie('catstyle');
	var d = readCookie('catpstyle');
	$("img.onbut").src("/style/EPAL/img/share_buttons/funview_off.gif");
	$("img.offbut").src("/style/EPAL/img/share_buttons/funview_on.gif");
		
	if(c.match(/^catstyle.box/gi)!=null){
		switchStylestyle('catstyle','catstyle.boximgoff');
		}else{
		switchStylestyle('catstyle','catstyle.listimgoff');
		}
	if(d.match(/^catpstyle.box/gi)!=null){
			switchStylestyle('catpstyle','catpstyle.boximgoff');
		}else if(d.match(/^catpstyle.nolist/gi)!=null){
			//do nothing thing
		}else{
			switchStylestyle('catpstyle','catpstyle.listimgoff');
		}		
	}	