/**
 * Cookie plugin
 *
 * Copyright (c) 2006 Klaus Hartl (stilbuero.de)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 */

jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};

(function($){
	var initLayout = function() {

		$.browser.webkit = /applewebkit/.test(navigator.userAgent.toLowerCase());		
		
		// Cookie array
		var hexValues = [ "body-text", "header-text", "body-bg" ];
		
		$.each(hexValues,function(i,v){
			hex = $.cookie(v);
			if(hex != null && hex != 'X'){
				formatLayout(hex,v);
			}
		});
		
		$('a.changebg').click(function(){
			$('body').removeAttr('style');
			$.cookie('body-bg', 'X', { path: '/' });
		});
		
		$('a.reset-styles').click(function(e){
			resetLayout();
			$.each(hexValues,function(i,v){
				$.cookie(v, 'X', { path: '/' });
			});
			if($.browser.webkit){
				var url = document.URL;
				window.location.href = url;
			}
			e.preventDefault();
		});
		

		
		$('#colorpickerHolder').ColorPicker({flat: true});

		$('#colorSelector1').ColorPicker({
			color: '#0000ff',
			onShow: function (colpkr) {
				$(colpkr).fadeIn(500);
				return false;
			},
			onHide: function (colpkr) {
				$(colpkr).fadeOut(500);
				return false;
			},
			onChange: function (hsb, hex, rgb) {
				$('#colorSelector1 div').css('backgroundColor', '#' + hex);
				formatLayout(hex,'body-text');
			}
		});
		
		$('#colorSelector2').ColorPicker({
			color: '#0000ff',
			onShow: function (colpkr) {
				$(colpkr).fadeIn(500);
				return false;
			},
			onHide: function (colpkr) {
				$(colpkr).fadeOut(500);
				return false;
			},
			onChange: function (hsb, hex, rgb) {
				$('#colorSelector2 div').css('backgroundColor', '#' + hex);
				formatLayout(hex,'body-bg');
			}
		});
		
		$('#colorSelector3').ColorPicker({
			color: '#0000ff',
			onShow: function (colpkr) {
				$(colpkr).fadeIn(500);
				return false;
			},
			onHide: function (colpkr) {
				$(colpkr).fadeOut(500);
				return false;
			},
			onChange: function (hsb, hex, rgb) {
				$('#colorSelector3 div').css('backgroundColor', '#' + hex);
				formatLayout(hex,'header-text');
			}
		});
		
		$('#colorSelector4').ColorPicker({
			color: '#0000ff',
			onShow: function (colpkr) {
				$(colpkr).fadeIn(500);
				return false;
			},
			onHide: function (colpkr) {
				$(colpkr).fadeOut(500);
				return false;
			},
			onChange: function (hsb, hex, rgb) {
				$('#colorSelector4 div').css('backgroundColor', '#' + hex);
				formatLayout(hex,'sidebar-bg');
			}
		});
		
		
		$('#colorSelector5').ColorPicker({
			color: '#0000ff',
			onShow: function (colpkr) {
				$(colpkr).fadeIn(500);
				return false;
			},
			onHide: function (colpkr) {
				$(colpkr).fadeOut(500);
				return false;
			},
			onChange: function (hsb, hex, rgb) {
				$('#colorSelector5 div').css('backgroundColor', '#' + hex);
				formatLayout(hex,'link-text');
			}
		});

		
	};
	
	function formatLayout(hex,layout){
	
		switch(layout){
			
			case 'body-text':
			$('#colorSelector1 div').css('backgroundColor', '#' + hex);
			$('body, .intro').css('color', '#' + hex);
			break;
			case 'header-text':
			$('#colorSelector3 div').css('backgroundColor', '#' + hex);
			$('h1, h2, h3, h4, h5, h6, a.button, .forms fieldset .btn-submit, #filtering-nav li a, ul.tabs li a, h2.trigger, .jta-tweet-list-controls-button, .footer-box ul.popular-posts li h5 a, .secondary .sidebox ul.popular-posts li h5 a, h3.title a, .box h4 a, .jta-tweet-list-controls span').css('color', '#' + hex);
			break;
			case 'body-bg':
			$('#colorSelector2 div, body, .e-showcase-block .inner, .loader').css('backgroundColor', '#' + hex);
			$('body, .e-showcase-block .inner, .loader').css({backgroundImage: 'none'});
			break;
			case 'sidebar-bg':
			$('#colorSelector4 div').css('backgroundColor', '#' + hex);
			$('#sidebar, .menu-v ul li ul').css('backgroundColor', '#' + hex);
			break;
			case 'link-text':
			$('#colorSelector5 div').css('backgroundColor', '#' + hex);
			$('a').css('color', '#' + hex);
			break;
		}
		$.cookie(layout, hex, { path: '/' });
	}
	
	function resetLayout(){
	
		$('#colorSelector1 div, #colorSelector2 div, #colorSelector3 div, #colorSelector4 div, #colorSelector5 div').css('backgroundColor', '#0000ff');
			$('body, h1, h2, h3, h4, h5, h6, a.button, .forms fieldset .btn-submit, #filtering-nav li a, ul.tabs li a, h2.trigger, .jta-tweet-list-controls-button, .footer-box ul.popular-posts li h5 a, .secondary .sidebox ul.popular-posts li h5 a, h3.title a, .box h4 a, .jta-tweet-list-controls span, .menu ul a, .e-showcase-block .inner, .loader, .intro').removeAttr('style');
	}
	
	EYE.register(initLayout, 'init');
})(jQuery)
