/*
    LoZooDiVenere / KtCN
	Stylesheets 1.0
	
	Copyright 2010 www.lozoodivenere.it, www.ktcn.it
*/
var kport_styleswitch = new Class({
	Implements: [Options, Events],
	options: {
		cookieDomain: "",
		cookieDays: 60,
		body_sheet_cn: "body_sheet",
		dim_sheet_cn: "dim_sheet",
		text_sheet_cn: "text_sheet",
		text_icons: Array(tmpl_dir+"img/icon_text_on.png", tmpl_dir+"img/icon_text.png"),
		act_text_icon: 1,
		hicontr_sheet_cn: "hicontr_sheet",
		hicontr_icons: Array(tmpl_dir+"img/icon_normal_contrast.png", tmpl_dir+"img/icon_hi_contrast.png"),
		hicontr_disable_stylesheets: [],
		act_hicontr_icon: 1
	},
	
	initialize: function(options) {
		this.setOptions(options);
		var selectedtitle=Cookie.read(this.options.body_sheet_cn);
		if (selectedtitle!=null)
			this.setStylesheet(selectedtitle, "body_");
		selectedtitle=Cookie.read(this.options.dim_sheet_cn);
		if (selectedtitle!=null)
			this.setStylesheet(selectedtitle, "main_tab_");
		selectedtitle=Cookie.read(this.options.text_sheet_cn);
		if (selectedtitle=="off") {
			var altsheets=this.get_stylesheets("text_");
			altsheets[0].setProperty('disabled',false);
			this.options.act_text_icon = 0;
  		}
		selectedtitle=Cookie.read(this.options.hicontr_sheet_cn);
		if (selectedtitle=="off") {
			//var altsheets=this.get_stylesheets("hicontr_");
			//altsheets[0].setProperty('disabled',false);
			//this.options.act_hicontr_icon = 0;
			var altsheets=this.get_stylesheets("hicontr_");
			this.hicontr_screen_on(altsheets[0]);
		}
	},
	
	setStylesheet: function(title, title_mask, randomize) {
		var i, cacheobj, altsheets=[""], t;
		$$("link").each(function(cacheobj,i) {
			var t = cacheobj.getProperty("title");
			if(cacheobj.getProperty("rel").toLowerCase().indexOf("alt")!=-1 && t) {
				if (t.indexOf(title_mask) != -1) {
					cacheobj.setProperty('disabled',true);
					altsheets.push(cacheobj);
					if (t == title)
						cacheobj.setProperty('disabled', false);
				}
			}
		});
	//return (typeof randomize!="undefined" && altsheets[randomnumber]!="")?altsheets[randomnumber].getAttribute("title"):"";
	},
	
	chooseStyle: function(styletitle, title_mask, cookiename) {
		this.setStylesheet(styletitle, title_mask);
		Cookie.write(cookiename, styletitle, {'domain':this.options.cookieDomain, 'duration': this.options.cookieDays});
	},
	
	get_stylesheets: function(title_mask) {
		var i, cacheobj, altsheets=new Array(), next=0;
		$$('link').each(function(cacheobj,i) {
			var t = cacheobj.getProperty("title");
			if (cacheobj.getProperty("rel").toLowerCase().indexOf("alt")!=-1 && t) {
				if (t.indexOf(title_mask) != -1) {
					cacheobj.setProperty('disabled', true);
					altsheets.push(cacheobj);
				}
			}
		});
		return altsheets;
	},
	
	next_stylesheet: function(cookie_name, title_mask) {
		var selectedtitle=Cookie.read(cookie_name);
		var i, cacheobj, altsheets=this.get_stylesheets(title_mask), next=0;
		if (selectedtitle==null)
			selectedtitle=altsheets[0].getProperty("title");
		for(i=0; i < altsheets.length; i++) {
			altsheets[i].setProperty('disabled', true);
			if (altsheets[i].getProperty("title") == selectedtitle) {
				next = i+1;
				if (next > (altsheets.length-1))
					next = altsheets.length-1;
			}
		}
		this.chooseStyle(altsheets[next].getProperty("title"), title_mask, cookie_name);
 	},
	
	prev_stylesheet: function(cookie_name, title_mask) {
		var selectedtitle=Cookie.read(cookie_name);
		var i, cacheobj, altsheets=this.get_stylesheets(title_mask), next=0;
		if (selectedtitle==null)
			selectedtitle=altsheets[0].getProperty("title");
		for(i=0; i < altsheets.length; i++) {
			altsheets[i].setProperty('disabled', true);
			if (altsheets[i].getProperty("title") == selectedtitle) {
				next = i-1;
				if (next < 0)
					next = 0;
			}
		}
		this.chooseStyle(altsheets[next].getProperty("title"), title_mask, cookie_name);
	},
	
	greater_font:function() {
		this.next_stylesheet(this.options.body_sheet_cn, "body_");
	},
	
	smaller_font:function() {
		this.prev_stylesheet(this.options.body_sheet_cn, "body_");
	},
	
	small_screen:function() {
		this.options.next_stylesheet(this.options.dim_sheet_cn, "main_tab_");
	},
	
	large_screen:function() {
		this.prev_stylesheet(this.options.dim_sheet_cn, "main_tab_");
	},
	
	text_screen_on:function(sheet) {
		var img = $("text_style_sheet_icon");
		sheet.setProperty('disabled', true);
		if (img)
			img.src=this.options.text_icons[0];
		this.options.act_text_icon = 0;
		Cookie.write(this.options.text_sheet_cn, "off", {'domain':this.options.cookieDomain, 'duration': this.options.cookieDays});
	},
	
	text_screen_off:function(sheet) {
		var img = $("text_style_sheet_icon");
		sheet.setProperty('disabled', true);
		if (img)
			img.src=this.options.text_icons[1];
		this.options.act_text_icon = 1;
		Cookie.write(this.options.text_sheet_cn, "on", {'domain':this.options.cookieDomain, 'duration': this.options.cookieDays});
	},
	
	text_screen:function() {
		var selectedtitle=Cookie.read(this.options.text_sheet_cn);
		var i, altsheets=this.get_stylesheets("text_");
		if ((selectedtitle==null) || (selectedtitle=="") || (selectedtitle=="off"))
		{ this.text_screen_off(altsheets[0]);
		}
		else
		{ this.text_screen_on(altsheets[0]);
		}
	},
	
	hicontr_screen_on:function(sheet) {
		var img = $("hicontr_style_sheet_icon");
		sheet.setProperty('disabled', false);
		/*$$("link").each(function(s,i) {
			var t = s.getProperty("title");
			if (t && (this.options.hicontr_disable_stylesheets.indexOf(t) != -1)) {
				s.setProperty('disabled', true);
			}
		}, this);*/
		if (img)
			img.set('src',this.options.hicontr_icons[0]);
		this.options.act_hicontr_icon = 0;
		Cookie.write(this.options.hicontr_sheet_cn, "off", {'domain':this.options.cookieDomain, 'duration': this.options.cookieDays});
	},
	
	hicontr_screen_off:function(sheet) {
		var img = $("hicontr_style_sheet_icon");
		sheet.setProperty('disabled', true);
		/*$$("link").each(function(s,i) {
			var t = s.getProperty("title");
			if (t && (this.options.hicontr_disable_stylesheets.indexOf(t) != -1)) {
				s.setProperty('disabled', false);
			}
		}, this);*/
		if (img)
			img.set('src',this.options.hicontr_icons[1]);
		this.options.act_hicontr_icon = 1;
		Cookie.write(this.options.hicontr_sheet_cn, "on", {'domain':this.options.cookieDomain, 'duration': this.options.cookieDays});
	},
	
	hicontr_screen:function() {
		var selectedtitle=Cookie.read(this.options.hicontr_sheet_cn);
		var i, altsheets=this.get_stylesheets("hicontr_");
		if ((selectedtitle==null) || (selectedtitle=="") || (selectedtitle=="off"))
		{ this.hicontr_screen_off(altsheets[0]);
		}
		else
		{ this.hicontr_screen_on(altsheets[0]);
		}
		top.document.location.reload();
	}
});
