//---------------------------------------------------------------------------
// SaaS KanaboStyle JavaScript for Client
//
// (C)Copyright WAC.com inc. All Rights Reserved.
//---------------------------------------------------------------------------

function KanaboStyleClass()
{
	// Font Size Setting
	this.fontSizeUnit = "%"; 	// Need double quotation, "DO NOT MODIFY"
	this.defaultSize = 100;		// Do not use double quotation "DO NOT MODIFY"
	this.perOrder = 20;		 	// Do not use double quotation
	
	this.defaultSelector = 'body';
	this.currentSelector = this.defaultSelector;
	// Current Size
	this.currentSize = this.defaultSize; 
	

	// Set Cookie Name
	this.cookieNameOfFontSize = "kbsFontSize";
	this.cookieNameOfSelector = "kbsSelector";	
	this.cookieNameOfColorTheme = "kbsColor";
	

	//---------------------------------------------
	// Change StyleSheet
	//---------------------------------------------
	this.setActiveStyleSheet = function( title )
	{
		var i, cssItem, main;
		for( i = 0 ; ( cssItem = document.getElementsByTagName("link")[i] ) ; i++ ) 
		{
			if( cssItem.getAttribute("rel").indexOf("style") != -1 && cssItem.getAttribute("title") ) 
			{
				cssItem.disabled = true;
				if( cssItem.getAttribute("title") == title ) cssItem.disabled = false;
			}
		}
		
		// Save Seleted CSS Title to Cookie
		var cssTitle = this.getTitleActiveStyleSheet();
		this.cookie.setValue( this.cookieNameOfColorTheme, cssTitle, 365);		
	};
	
	//---------------------------------------------
	// Get Title of selected StyleSheet for Cookie
	//---------------------------------------------
	this.getTitleActiveStyleSheet = function()
	{
		var i, cssItem;
		for( i = 0 ; ( cssItem = document.getElementsByTagName("link")[i] ) ; i++) 
		{
 			if( cssItem.getAttribute("rel").indexOf("style") != -1 && cssItem.getAttribute("title") && !cssItem.disabled) 
 			{
	 			return cssItem.getAttribute("title");
	 		}
		}
		return null;
	};
	
	//---------------------------------------------
	// Change Font Size
	//---------------------------------------------
	this.changeFontSize = function( cmd )
	{
		// Set larger size than now 	
		if( cmd == "larger" )
		{
			this.currentSize = Number( this.currentSize + this.perOrder );
			this.cookie.setValue( this.cookieNameOfFontSize, this.currentSize, 365);
		}
		
		// Set smaller size than now 
		if( cmd == "smaller" )
		{
			if( this.currentSize != this.perOrder )
			{
				this.currentSize = Number( this.currentSize - this.perOrder );
				this.cookie.setValue( this.cookieNameOfFontSize, this.currentSize, 365);
			}
			else
			{
				this.currentSize = Number( this.currentSize ); // ????
			}
		}
		this.cookie.setValue(this.cookieNameOfSelector, this.currentSelector, 365);		
		
		// Set Default Size
		if( cmd == "default" )
		{
			this.cookie.setValue( this.cookieNameOfFontSize, this.defaultSize, 0);
			this.cookie.setValue(this.cookieNameOfSelector, this.defaultSelector, 0);
		}

		location.reload();
	};
	
	//---------------------------------------------
	// Set Font Size
	//---------------------------------------------	
	this.setFontSize = function( selector, fontSize )
	{
		document.writeln( '<style type="text/css">' );
		var s = selector + '{font-size:' + fontSize + this.fontSizeUnit + '}';
		document.write( s );
		document.writeln( '</style>' );
	};

			
	this.addSelector = function( selector )
	{
		this.selectors[this.selectors.length] = selector;
	};

	//---------------------------------------------
	// constructor (Set Color theme and Font size)
	//---------------------------------------------	
	{
		// Create CookieClass Object	
		this.cookie = new CookieClass();
				
		// for change color theme		
		var cssTitleFromCookie = this.cookie.readValue(this.cookieNameOfColorTheme);
		var cssTitle = cssTitleFromCookie ? cssTitleFromCookie : "base";
		this.setActiveStyleSheet(cssTitle);

		
		// for change font size
		var fontSizeFromCookie = this.cookie.readValue(this.cookieNameOfFontSize);
		this.currentSize = fontSizeFromCookie ? eval( fontSizeFromCookie ) : this.defaultSize;
		
		var baseSelectorFromCookie = this.cookie.readValue( this.cookieNameOfSelector );				
		this.currentSelector = baseSelectorFromCookie ? baseSelectorFromCookie : this.defaultSelector;
		
		this.setFontSize( this.currentSelector, this.currentSize );
	};

} // End of KanaboStyleClass


//===============================================
// Cookie Control Class
//===============================================
function CookieClass()
{
	// Cookie Settings
	this.host_id = "";
	this.path = "/";
	// Cookie avaliable duration (Default 2days)
	this.avaliableDuration = 2;

	
	//---------------------------------------------
	// Create New Cookie
	//---------------------------------------------	
	this.setValue = function( name, value, days)
	{
		if (!days)
		{
			days = this.avaliableDuration;	
		}
			
		var date = new Date();
		date.setTime( date.getTime() + ( days * 24 * 60 * 60 * 1000 ) );
		var expires = "; expires=" + date.toGMTString();
		
		document.cookie = name + "=" + escape(value) + expires + "; path=" + this.path ;
	};
	
	//---------------------------------------------
	// Read item data from Cookie
	//---------------------------------------------	
	this.readValue = function( name )
	{
		var cookieName = name + "=";
		var cookieDataArray = document.cookie.split(';');
	  
		for(var i=0 ; i < cookieDataArray.length ; i++) 
		{
			var cookieItem = cookieDataArray[i];
			while (cookieItem.charAt(0)==' ')
			{
				cookieItem = cookieItem.substring(1, cookieItem.length);
			}
	    
			if (cookieItem.indexOf(cookieName) == 0) 
			{
				return cookieItem.substring(cookieName.length, cookieItem.length);
			}
		}
	  
		return null;
	};
	
	//---------------------------------------------
	// Delete item data from Cookie
	//---------------------------------------------		
	this.deleteValue = function( name )
	{

		if(this.readValue(name))
		{
			document.cookie = name + '=; expires=Thu, 01-Jan-70 00:00:01 GMT;path=' + this.cookiePath;
		}
	};
	
	//---------------------------------------------
	// Get item from Cookie
	//---------------------------------------------
	this.getItem = function( startPosition )
	{
		var endPosition = document.cookie.indexOf(";", startPosition);
		if(endPosition == -1)
		{
			endPosition = document.cookie.length;
		}
		return unescape(document.cookie.substring(startPosition, endPosition));
	};
}

// Make Object
var KanaboStyle = new KanaboStyleClass();