/**
 * Az oldalba ágyazott stíluslapok között vált
 * @package  alternateStyle
 * @author   Gyuris Gellért
 * @see      prototype_A, browserCheck, eventBinding, flash, attribute
 */
var alternateStyle = {
	skin : {},
	activeSkin : null,
	flash : null,
	checker : null,
	config : {
		swapFlash : true,               // ha tartozik az ui-hoz flash, cseréje-e? :: addAlternate( flash )
		swapJs : true,                  // ha tartozik az ui-hoz egyedi JS, cserélje-e? :: addAlternate( js )
		swapCss : true,                 // ha tartozik az ui-hoz CSS, cserélje-e? :: addAlternate( css )
		useChecker : true,              // ha van az oldalon CSS validator link, cserélje-e a linkjét? :: setChecker()
		useCookie : true,               // mentse-e sütibe a preferált témát?
		cookieDomain : '',              // a süti domain beállítása, pl. document.domain
		cookiePath : '',                // a süti útvonala, pl. '/'
		checkerUri : 'http://jigsaw.w3.org/css-validator/validator?uri=',
		checkerCommand : '&warning=1&profile=css2&usermedium=all'
	},
	Alternate : function( oAdd ) {
		this.id = oAdd.css;
		this.css = document.getElementById( oAdd.css ); // ez már létre kellett jöjjön
		this.assistantCss = ( oAdd.assistantCss ) ? document.getElementById( oAdd.assistantCss ) : null; // csak IE Conditional Comment technikával berakott segéd CSS
		this.js = ( oAdd.js && oAdd.js.length == 2 ) ? { start : oAdd.js[0], end : oAdd.js[1] } : null;  // ezek is már be vannak töltve
		this.flash = ( oAdd.flash ) ? oAdd.flash : null;
		return this;
	},
	/**
	 * @param sConfig String  A bellítás azonosítója
	 * @param bValue Boolean  A bellítás értéke
	 */
	setConfig : function( sConfig, bValue ) {
		this.config[sConfig] = bValue;
	},
	/**
	 * Csak belülről hívható
	 * @param aFlash Array  A kívánt ui-hez tartozó flash-ek szcriptelhetővé tétele.
	 */
	registerFlash : function( aFlash ) {
		this.flash = aFlash;
		loadEventBinding( window, function() { createFlashEmbed( aFlash ) } );
	},
	/**
	 * CSS validator linkjének beállítása
	 * @param sId String  A LinkElement id-je
	 */
	registerChecker : function( sId ) {
		this.checker = sId;
	},
	/**
	 * Egy téma hozzáadása
	 * @param oAdd Object  Az Object az alábbi módon kell felépüljön:
	 *                     css String           A téma (LinkElement) azonosítója
	 *                   ( assistantCss String  A téma IE kiegészítésének (LinkElement) azonosítója ) opcionális!
	 *                     js Array(2)          A kezdő és záró JS funkciók ( Funktion )
	 *                     flash Array(x)       A témához tartozó flash mozik URL-jei ( String )
	 */
	addAlternate : function( oAdd ) {
		this.skin[oAdd.css] = new this.Alternate( oAdd );
	},
	/**
	 * Csak belülről hívható
	 * @param sId  A téma belső azonosítója
	 */
	setActive : function( sId ) {
		var i, dExpire, elIframe, nNum, sCheckerId, sChecker;
		if ( this.activeSkin && this.activeSkin.id == sId ) // ugyanarra nem váltunk
			return false;
		if ( typeof this.skin[sId] == 'undefined' )
			return;
		// css csere
		if ( this.config.swapCss ) {
			for ( i in this.skin ) {
				this.skin[i].css.disabled = true; // csak IE sajátosság (?)
				if ( this.skin[i].assistantCss != null ) {
					this.skin[i].assistantCss.disabled = true;
				};
				if ( this.skin[i].id == sId ) {
					this.skin[i].css.disabled = false;
					if ( this.skin[i].assistantCss != null ) {
						this.skin[i].assistantCss.disabled = false;
					};
				};
			};
		};
		// javascript csere
		if ( this.config.swapJs ) {
			if ( this.activeSkin != null && this.activeSkin.js != null && typeof this.activeSkin.js.end == 'function' ) {
				this.activeSkin.js.end();
			};
			if ( this.skin[sId].js != null && typeof this.skin[sId].js.start == 'function' ) {
				this.skin[sId].js.start();
			};
		};
		// flash cserék
		if ( this.config.swapFlash ) {
			if ( this.flash != null &&  this.skin[sId].flash != null ) {
				for ( i = 0; i < this.flash.length; i++ ) {
					sendToFlash( this.flash[i], 'SetVariable', [ 'mySwf', this.skin[sId].flash[i] ] ); 
					sendToFlash( this.flash[i], 'TCallLabel', [ '/', 'skin' ] );
				};
			};
		};
		// cookie mentés
		if ( this.config.useCookie ) {
			dExpire = new Date();
			dExpire.setTime( dExpire.getTime() + ( 90 * 24 * 3600 * 1000 ) );
			document.cookie = 'skin=' + escape( this.skin[sId].id ) + '; expires=' + dExpire.toGMTString()
			                + (( this.config.cookieDomain != '' ) ? '; domain=' + this.config.cookieDomain : '' )
			                + (( this.config.cookiePath != '' ) ? '; path=' + this.config.cookiePath : '' )
		};
		// css ellenőrző átírása
		if ( this.config.useChecker && this.checker != null ) {
			nNum = ( document.location.pathname.lastIndexOf('/') > -1 ) ? document.location.pathname.lastIndexOf('/') + 1 : document.location.pathname.length;
			sChecker = this.config.checkerUri
					 + ( ( is.ie ) ? 'http://' + document.location.host + document.location.pathname.substring( 0, nNum ) + this.skin[sId].css.href : this.skin[sId].css.href )
					 +  this.config.checkerCommand;
			if ( document.getElementById( this.checker ) ) {
				document.getElementById( this.checker ).href = sChecker;
			}
			else { // XXX IE nem jut el ide, mintha már lezajlana a load; fix ötlet: setInterval, míg nem jön létre az objektum...
				loadEventBinding( window, function() {
					document.getElementById( alternateStyle.checker ).href = sChecker;
				} );
			};
		};
		// regisztrálás
		this.activeSkin = this.skin[sId];
		for ( i = 0; elIframe = document.getElementsByTagName('iframe')[i]; i++ ) {
			if ( is.ie ) {
				if ( window.frames[ elIframe.id ] || window.frames[ elIframe.name ] ) {
					elIframe.contentDocument = window.frames[ elIframe.id || elIframe.name ].document;
				};
			};
			if ( typeof elIframe.contentDocument == 'object' &&
			     typeof elIframe.contentDocument.alternateStyle == 'object' ) {
				elIframe.contentDocument.alternateStyle.setActive( sId );
			};
		};
	},
	/**
	 * A témák indítása
	 */
	initStyle : function() {
		var i, elLink, nStart, nEnd, sStoredStyleId;
		// default style lekeresése
		for ( i = 0; ( elLink = document.getElementsByTagName('link')[i] ); i++ ) {
    		if ( elLink.getAttribute('rel').indexOf('stylesheet' ) != -1 
    			 && elLink.getAttribute('rel').indexOf('alternate' ) == -1
    			 && elLink.getAttribute('media').indexOf('screen' ) != -1 ) {
       			this.activeSkin = ( isSpecified( elLink, 'id' ) ) ? this.skin[elLink.id] : null;
       		};
  		};
  		// ha van kedvenc skin, azt beállítjuk
		if ( this.config.useCookie ) {
			if ( document.cookie.length == 0 ) { 
				return;
			};
			nStart = document.cookie.indexOf( 'skin=' );
			if ( nStart == -1 ) { 
				return;
			};
			nEnd = document.cookie.indexOf( ';', nStart );
			if ( nEnd == -1 ) { 
				nEnd = document.cookie.length;
			};
			this.setActive( unescape( document.cookie.substring( nStart + 'skin='.length, nEnd ) ) );
		};
	}
};
document.alternateStyle = alternateStyle;
alternateStyle.setConfig( 'swapFlash', false );
alternateStyle.setConfig( 'swapJs', false );
alternateStyle.setConfig( 'useCookie', true );
alternateStyle.addAlternate( { css : 'skin-normal' } );
alternateStyle.addAlternate( { css : 'skin-small'  } );
alternateStyle.addAlternate( { css : 'skin-big'    } );
alternateStyle.initStyle();