/* jQuery plugin themeswitcher
---------------------------------------------------------------------*/
$.fn.themeswitcher = function(settings){
	var options = jQuery.extend({
		loadTheme: null,
		initialText: 'Switch Theme',
		width: 150,
		height: 300,
		buttonPreText: 'Theme: ',
		closeOnSelect: true,
		buttonHeight: 14,
		cookieThemeName: 'Photonization-theme',
		cookieWallName: 'Photonization-wall',
		onOpen: function(){},
		onClose: function(){},
		onSelect: function(){}
	}, settings);

	//markup
	var style1Ref = "css/main1.css";
	var style2Ref = "css/main2.css";
	var style3Ref = "css/main3.css";
	var currentWallHref = "";
	var button = $('<a href="#" class="jquery-ui-themeswitcher-trigger themeSwitcher"><span class="jquery-ui-themeswitcher-icon"></span><span class="jquery-ui-themeswitcher-title">'+ options.initialText +'</span></a>');
	//var switcherpane = $('	<div class="jquery-ui-themeswitcher">	<div id="themeGallery">		<ul>	<li><a href="' + style1Ref + '">			<img src="images/themeRoller/gallery/lightness.png" alt="Lightness" title="Lightness" /><span class="themeName">Lightness</span>		</a></li><li><a href="' + style2Ref + '">			<img src="images/themeRoller/gallery/transparency.png" alt="Transparency" title="Transparency" /><span class="themeName">Transparency</span>		</a></li></ul></div></div>').find('div').removeAttr('id');
	
	var switcherpane = $('<div class="jquery-ui-themeswitcher">	<div id="themeGallery">'+
			'<ul><li><a id="css1" href="' + style1Ref + '"><img src="images/themeRoller/gallery/lightness.png" title="Lightness" /><span class="themeName">Lightness</span></a></li>' +
			'<li><a id="css3" href="' + style3Ref + '"><img src="images/themeRoller/gallery/darkness.png" title="Darkness" /><span class="themeName">Darkness</span></a></li>' +
			'<li><a id="css2" href="' + style2Ref + '"><img src="images/themeRoller/gallery/transparency.png" title="Transparency" /><span class="themeName">Transparency</span></a></li>' +
			'</ul></div></div>').find('div').removeAttr('id');
	
	//button events
	button.click(
		function(){
			if(switcherpane.is(':visible')){ switcherpane.spHide(); }
			else{ switcherpane.spShow(); }
			return false;
		}
	);
	
	//menu events (mouseout didn't work...)
	switcherpane.hover(
		function(){},
		function(){if(switcherpane.is(':visible')){$(this).spHide();}}
	);

	//show/hide panel functions
	$.fn.spShow = function(){ $(this).css({top: button.offset().top + options.buttonHeight + 6, left: button.offset().left}).slideDown(50); button.css(button_active); options.onOpen(); }
	$.fn.spHide = function(){ $(this).slideUp(50, function(){options.onClose();}); button.css(button_default); }
	
	$('.iconWall').click(function(){
		$('.iconWall img').removeClass('iSelected');
		$(this).find('img').addClass('iSelected');
		updateWall($(this).attr('href'));
		$.cookie(options.cookieWallName, $(this).attr('href'));
		return false;
	});
	
	function iconsContainer(containerId){
		$(".iconsContainer").hide();
		$(containerId).css('display', 'block');
		return false;
	}
	
	/* Theme Loading
	---------------------------------------------------------------------*/
	switcherpane.find('a').click(function(){
		var href = $(this).attr('href');
		var walls = $('#icons' + $(this).attr('id').substr(3,4));
		updateTheme(href);
		var id = $(this).attr('id');
		var containerId = "#icons" + id.substr(3); 
		var themeName = $(this).find('span').text();
		if (themeName == 'Darkness'){
			mbContainerSkin = 'black';
			$('.close').attr('src', 'images/mbContainer/black/close.png');
			$('.iconizeContainer').attr('src', 'images/mbContainer/black/iconize.png');
			$('.collapsedContainer').attr('src', 'images/mbContainer/black/min.png');
		} else {
			$('.close').attr('src', 'images/mbContainer/default/close.png');
			$('.iconizeContainer').attr('src', 'images/mbContainer/default/iconize.png');
			$('.collapsedContainer').attr('src', 'images/mbContainer/default/min.png');
		}
		button.find('.jquery-ui-themeswitcher-title').text( options.buttonPreText + themeName );
		$.cookie(options.cookieThemeName, themeName);
		options.onSelect();
		if(options.closeOnSelect && switcherpane.is(':visible')){ switcherpane.spHide(); }
		
		var found = false;
		walls.find('a').each(function(){
			if ($(this).attr('href') == currentWallHref)
				found = true;
		});
		if (found == false){
			var wall = walls.find('a:first');
			$('.iconWall img').removeClass('iSelected');
			wall.find('img').addClass('iSelected');
			updateWall(wall.attr('href'));
			$.cookie(options.cookieWallName, wall.attr('href'));
		} else {
			$('.iconWall img').removeClass('iSelected');
			walls.find("a[href='" + currentWallHref + "']").find('img').addClass('iSelected');
		}
		iconsContainer(containerId);
		return false;
	});
	
	function updateTheme(locStr){
		var cssLink = $('<link href="'+locStr+'" type="text/css" rel="stylesheet" class="theme" />');
		$("head").append(cssLink);
		if( $("link.theme").size() > 1){
			$("link.theme:first").remove();
		}	
	}	
	
	function updateWall(locStr){
		currentWallHref = locStr;
		var cssLink = $('<link href="'+locStr+'" type="text/css" rel="stylesheet" class="wall" />');
		$("head").append(cssLink);
		if( $("link.wall").size() > 1){
			$("link.wall:first").remove();
		}
	}	
	
	/* Inline CSS 
	---------------------------------------------------------------------*/
	var button_default = {
		fontFamily: 'Trebuchet MS, Verdana, sans-serif',
		fontSize: '11px',
		//color: '#666',
		//background: '#eee url(images/themeRoller/buttonbg.png) 50% 50% repeat-x',
		border: '1px solid #ccc',
		'border-radius': '6px',
		'-moz-border-radius': '6px',
		'-webkit-border-radius': '6px',
		textDecoration: 'none',
		padding: '3px 3px 3px 8px',
		width: options.width - 11, //minus must match left and right padding 
		display: 'block',
		height: options.buttonHeight,
		outline: '0'
	};
	var button_hover = {
		'borderColor':'#bbb',
		//'background': '#f0f0f0',
		cursor: 'pointer'
		//color: '#444'
	};
	var button_active = {
		//color: '#aaa',
		//background: '#000',
		border: '1px solid #ccc',
		borderBottom: 0,
		'border-radius-bottomleft': 0,
		'-moz-border-radius-bottomleft': 0,
		'-webkit-border-bottom-left-radius': 0,
		'border-radius-bottomright': 0,
		'-moz-border-radius-bottomright': 0,
		'-webkit-border-bottom-right-radius': 0,
		outline: '0'
	};
	
	//button css
	button.css(button_default)
	.hover(
		function(){ 
			$(this).css(button_hover); 
		},
		function(){ 
		 if( !switcherpane.is(':animated') && switcherpane.is(':hidden') ){	$(this).css(button_default);  }
		}	
	)
	.find('.jquery-ui-themeswitcher-icon').css({
		'float': 'right',
		'width': '16px',
		'height': '16px',
		'background': 'url(images/themeRoller/icon_color_arrow.gif) 50% 50% no-repeat'
	});	
	//pane css
	switcherpane.css({
		'position': 'absolute',
		'float': 'left',
		'fontFamily': 'Trebuchet MS, Verdana, sans-serif',
		'fontSize': '12px',
		'background': '#000',
		'color': '#fff',
		'padding': '8px 3px 3px',
		'border': '1px solid #ccc',
		'border-radius-bottomleft': '6px',
		'-moz-border-radius-bottomleft': '6px',
		'-webkit-border-bottom-left-radius': '6px',
		'border-radius-bottomright': '6px',
		'-moz-border-radius-bottomright': '6px',
		'-webkit-border-bottom-right-radius': '6px',
		'borderTop': 0,
		'zIndex': 999999,
		'width': options.width-6 //minus must match left and right padding
	})
	.find('ul').css({
		'listStyle': 'none',
		'margin': '0',
		'padding': '0',
		'overflow': 'auto',
		'height': options.height
	}).end()
	.find('li').hover(
		function(){ 
			$(this).css({
				'borderColor':'#555',
				'background': 'url(images/themeRoller//menuhoverbg.png) 50% 50% repeat-x',
				'cursor': 'pointer'
			}); 
		},
		function(){ 
			$(this).css({
				'borderColor':'#111',
				'background': '#000',
				'cursor': 'auto'
			}); 
		}
	).css({
		'width': options.width-30,
		'height': '',
		'padding': '2px',
		'margin': '1px',
		'border': '1px solid #111',
		'border-radius': '4px',
		'-moz-border-radius': '4px',
		'-webkit-border-radius': '4px',
		'clear': 'left',
		'float': 'left'
	}).end()
	.find('a').css({
		'color': '#aaa',
		'textDecoration': 'none',
		'float': 'left',
		'width': '100%',
		'outline': '0'
	}).end()
	.find('img').css({
		'float': 'left',
		'border': '1px solid #333',
		'margin': '0 2px'
	}).end()
	.find('.themeName').css({
		'float': 'left',
		'margin': '3px 0'
	}).end();
	
	$(this).append(button);
	$('body').append(switcherpane);
	switcherpane.hide();
	var themeId = '1';
	var themeName = $.cookie(options.cookieThemeName);
	var wallName = $.cookie(options.cookieWallName);
	if (themeName != null && themeName != ""){
		if (themeName == "Lightness")
			themeId = 1;
		else if (themeName == "Transparency")
			themeId = 2;
		else if (themeName == "Darkness")
				themeId = 3;
	} else {
		themeName = "Lightness";
		themeId = 1;
	}
	if(themeName || options.loadTheme ){
		if (themeName == 'Darkness')
			mbContainerSkin = 'black';
		var themeName = themeName || options.loadTheme;
		var elt = switcherpane.find('a:contains('+ themeName +')');
		elt.trigger('click');
		themeId = elt.attr('id').substr(3);
	}
	if(wallName && themeName){
		var elt = $('#subStyles').find('div[id=icons' + themeId + ']').find('a[href=' + wallName + ']:eq(0)');
		elt.trigger('click');
		elt.find('img').addClass('iSelected');
		currentWallHref = elt.attr('href');
	}
	
	
	return this;
};

