﻿/*
  *
*/
function ManageCSS(doc)
{
	
	var myDoc = doc;
		
	this.ChangeCSS = function(title) {
			if (!title)
			{ 
				title='';
			}
			TemplateChange(title)
		}

	function TemplateChange(title){
		var ss;
		
		if(myDoc.styleSheets){
			ss = myDoc.styleSheets;
		}
		else{
			ss = GetAllStyleSheets() //Funziona utilizzata per OPERA e tutti i browser che non supportano document.styleSheets
		}
		
		if (ss && !ss.length){
			alert ('this operation is not admitted!');
			return;
		}
		// disabilita tutti i fogli di stile con un titolo 
		// tranne quello passato per argomento alla funzione
		for( var i = 0; i < ss.length; i++ ) {
			if (ss[i].title && ss[i].title == title) {
				ss[i].disabled = false;
			}
			else if (ss[i].title) {
				ss[i].disabled = true;
			}
		}
	}
	
	// Funzione per Opera
	function GetAllStyleSheets(){
		
		if( myDoc.getElementsByTagName ) {
			var linkTags = myDoc.getElementsByTagName('LINK');
			var styleTags = myDoc.getElementsByTagName('STYLE');
		} 
		else {
			return []; 
		}
		var os = [];
		var rel;
		for( var i = 0; i < linkTags.length; i++ ) {
		//controlla l'attributo rel per vedere se contiene 'style'
			if (linkTags[i].rel ) {
				rel = linkTags[i].rel;
			} 
			else if (linkTags[i].getAttribute ) {
				rel = linkTags[i].getAttribute('rel');
			} 
			else {
				rel = '';
			}	
			if(typeof(rel)=='string' && rel.toLowerCase().indexOf('style') > -1)
			{
				os[os.length] = linkTags[i];
			}
		}
		
	//include anche tutti i tags style e restituisce l'array
		for	( var i = 0;  i < styleTags.length; i++ ) {
			os[os.length] = styleTags[i];
		}
		return os;
	}
}

