/* jQuery plugin themeswitcher
---------------------------------------------------------------------*/
$.fn.themeswitcher = function(settings){
	var options = jQuery.extend({
		loadTheme: null,
		initialText: 'Joomla Templates',
		width: 250,
		height: 200,
		buttonPreText: 'Theme: ',
		closeOnSelect: true,
		buttonHeight: 14,
		cookieName: 'jquery-ui-theme',
		onOpen: function(){},
		onClose: function(){},
		onSelect: function(){}
	}, settings);

	//markup 
	var button = $('<a href="#" class="jquery-ui-themeswitcher-trigger"><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="http://demo.hotjoomlatemplates.com/index.php?template=hotel"><img src="images/template_hotel.jpg" alt="Hotel" title="Hotel" /><span class="themeName">Hotel (Jan 2012)</span></a></li><li><a href="http://demo.hotjoomlatemplates.com/index.php?template=model_agency"><img src="images/template_model_agency.jpg" alt="Model Agency" title="Model Agency" /><span class="themeName">Model Agency (Dec 2011)</span></a></li><li><a href="http://demo.hotjoomlatemplates.com/index.php?template=flashes"><img src="images/template_flashes.jpg" alt="Flashes" title="Flashes" /><span class="themeName">Flashes (Nov 2011)</span></a></li><li><a href="http://demo.hotjoomlatemplates.com/index.php?template=betting"><img src="images/template_betting.jpg" alt="Betting" title="Betting" /><span class="themeName">Betting (Oct 2011)</span></a></li><li><a href="http://demo.hotjoomlatemplates.com/index.php?template=host"><img src="images/template_host.jpg" alt="Host" title="Host" /><span class="themeName">Host (Sep 2011)</span></a></li><li><a href="http://demo.hotjoomlatemplates.com/index.php?template=events"><img src="images/template_events.jpg" alt="Events" title="Events" /><span class="themeName">Events (Aug 2011)</span></a></li><li><a href="http://demo.hotjoomlatemplates.com/index.php?template=cosmetics"><img src="images/template_cosmetics.jpg" alt="Cosmetics" title="Cosmetics" /><span class="themeName">Cosmetics (July 2011)</span></a></li><li><a href="http://demo.hotjoomlatemplates.com/index.php?template=destinations"><img src="images/template_destinations.jpg" alt="Destinations" title="Destinations" /><span class="themeName">Destinations (June 2011)</span></a></li><li><a href="http://demo.hotjoomlatemplates.com/index.php?template=chameleon"><img src="images/template_chameleon.jpg" alt="Chameleon" title="Chameleon" /><span class="themeName">Chameleon (May 2011)</span></a></li><li><a href="http://demo.hotjoomlatemplates.com/index.php?template=vortex"><img src="images/template_vortex.jpg" alt="Vortex" title="Vortex" /><span class="themeName">Vortex (Apr 2011)</span></a></li><li><a href="http://demo.hotjoomlatemplates.com/index.php?template=fashion_store"><img src="images/template_fashionstore.jpg" alt="Fashion Store" title="Fashion Store" /><span class="themeName">Fashion Store (Mar 2011)</span></a></li><li><a href="http://demo.hotjoomlatemplates.com/index.php?template=leafs"><img src="images/template_leafs.jpg" alt="Leafs" title="Leafs" /><span class="themeName">Leafs (Feb 2011)</span></a></li><li><a href="http://demo.hotjoomlatemplates.com/index.php?template=wellness"><img src="images/template_wellness.jpg" alt="Wellness" title="Wellness" /><span class="themeName">Wellness (Dec 2010)</span></a></li><li><a href="http://demo.hotjoomlatemplates.com/index.php?template=cars"><img src="images/template_cars.jpg" alt="Cars" title="Cars" /><span class="themeName">Cars (Nov 2010)</span></a></li><li><a href="http://demo.hotjoomlatemplates.com/index.php?template=ecommerce"><img src="images/template_ecommerce.jpg" alt="eCommerce" title="eCommerce" /><span class="themeName">eCommerce (Oct 2010)</span></a></li><li><a href="http://demo.hotjoomlatemplates.com/index.php?template=design_now"><img src="images/template_designnow.jpg" alt="DesignNow" title="DesignNow" /><span class="themeName">DesignNow (Sep 2010)</span></a></li><li><a href="http://demo.hotjoomlatemplates.com/index.php?template=sportal"><img src="images/template_sportal.jpg" alt="Sportal" title="Sportal" /><span class="themeName">Sportal (Aug 2010)</span></a></li><li><a href="http://demo.hotjoomlatemplates.com/index.php?template=furniture_store"><img src="images/template_furniturestore.jpg" alt="Furniture Store" title="Furniture Store" /><span class="themeName">Furniture Store (Jul 2010)</span></a></li><li><a href="http://demo.hotjoomlatemplates.com/index.php?template=restaurant"><img src="images/template_restaurant.jpg" alt="Restaurant" title="Restaurant" /><span class="themeName">Restaurant (Jun 2010)</span></a></li><li><a href="http://demo.hotjoomlatemplates.com/index.php?template=real_estate"><img src="images/template_realestate.jpg" alt="Real Estate" title="Real Estate" /><span class="themeName">Real Estate (May 2010)</span></a></li><li><a href="http://demo.hotjoomlatemplates.com/index.php?template=photo_portfolio"><img src="images/template_photoportfolio.jpg" alt="Photo Portfolio" title="Photo Portfolio" /><span class="themeName">Photo Portfolio (May 2010)</span></a></li><li><a href="http://demo.hotjoomlatemplates.com/index.php?template=coffeine"><img src="images/template_coffeine.jpg" alt="Coffeine" title="Coffeine" /><span class="themeName">Coffeine (Apr 2010)</span></a></li><li><a href="http://demo.hotjoomlatemplates.com/index.php?template=rockin"><img src="images/template_rockin.jpg" alt="Rockin" title="Rockin" /><span class="themeName">Rockin\' (Apr 2010)</span></a></li><li><a href="http://demo.hotjoomlatemplates.com/index.php?template=boutique"><img src="images/template_boutique.jpg" alt="Boutique" title="Boutique" /><span class="themeName">Boutique (Mar 2010)</span></a></li><li><a href="http://demo.hotjoomlatemplates.com/index.php?template=architecture"><img src="images/template_architecture.jpg" alt="Architecture" title="Architecture" /><span class="themeName">Architecture (Feb 2010)</span></a></li><li><a href="http://demo.hotjoomlatemplates.com/index.php?template=snowflake"><img src="images/template_snowflake.jpg" alt="Snowflake" title="Snowflake" /><span class="themeName">Snowflake (Jan 2010)</span></a></li><li><a href="http://demo.hotjoomlatemplates.com/index.php?template=software"><img src="images/template_software.jpg" alt="Software" title="Software" /><span class="themeName">Software (Dec 2009)</span></a></li><li><a href="http://demo.hotjoomlatemplates.com/index.php?template=mobility"><img src="images/template_mobility.jpg" alt="Mobility" title="Mobility" /><span class="themeName">Mobility (Dec 2009)</span></a></li><li><a href="http://demo.hotjoomlatemplates.com/index.php?template=skies"><img src="images/template_skies.jpg" alt="Skies" title="Skies" /><span class="themeName">Skies (Nov 2009)</span></a></li><li><a href="http://demo.hotjoomlatemplates.com/index.php?template=fireworks"><img src="images/template_fireworks.jpg" alt="Fireworks" title="Fireworks" /><span class="themeName">Fireworks (Oct 2009)</span></a></li><li><a href="http://demo.hotjoomlatemplates.com/index.php?template=itstarts"><img src="images/template_itstarts.jpg" alt="It Starts!" title="It Starts!" /><span class="themeName">It Starts! (Sep 2009)</span></a></li><li><a href="http://demo.hotjoomlatemplates.com/index.php?template=relief"><img src="images/template_relief.jpg" alt="Free Joomla Template" title="Free Joomla Template" /><span class="themeName">Relief (Bonus)</span></a></li><li><a href="http://demo.hotjoomlatemplates.com/index.php?template=test"><img src="images/template_extensions.jpg" alt="Extensions" title="Extensions" /><span class="themeName">Extensions Demo</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); }
	

	
	/* Inline CSS 
	---------------------------------------------------------------------*/
	var button_default = {
		fontFamily: 'Trebuchet MS, Verdana, sans-serif',
		fontSize: '11px',
		color: '#fff',
		background: '#eee url(images/buttonbg.png) 50% 50% repeat-x',
		border: '1px solid #ccc',
		'-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': '#6d2627',
		cursor: 'pointer',
		color: '#fff'
	};
	var button_active = {
		color: '#aaa',
		background: '#000',
		border: '1px solid #ccc',
		borderBottom: 0,
		'-moz-border-radius-bottomleft': 0,
		'-webkit-border-bottom-left-radius': 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/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',
		'-moz-border-radius-bottomleft': '6px',
		'-webkit-border-bottom-left-radius': '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/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',
		'-moz-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 5px'
	}).end()
	.find('.themeName').css({
		float: 'left',
		margin: '3px 0'
	}).end();
	


	$(this).append(button);
	$('body').append(switcherpane);
	switcherpane.hide();
	if( $.cookie(options.cookieName) || options.loadTheme ){
		var themeName = $.cookie(options.cookieName) || options.loadTheme;
		switcherpane.find('a:contains('+ themeName +')').trigger('click');
	}

	return this;
};




/**
 * 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
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        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;
    }
};


