switcher = 
{
	themes:
	[
		{
			name: 'Openlogic - NEW',
			url: 'http://goo.gl/ZQ4es',
			thumbnail: 'http://s3.envato.com/files/1761243/0-preview-html.__large_preview.jpg'
		},
		{
			name: 'Magnetic',
			url: 'http://goo.gl/YCqVr',
			thumbnail: 'http://s3.envato.com/files/1595793/0-preview-html.__large_preview.jpg'
		},
		{
			name: 'ChilliBox',
			url: 'http://goo.gl/47NxA',
			thumbnail: 'http://s3.envato.com/files/1415277/00-preview-html.__large_preview.jpg'
		},
		{
			name: 'WebPoint',
			url: 'http://goo.gl/K18Fv',
			thumbnail: 'http://s3.envato.com/files/1225775/00_preview.__large_preview.jpg'
		},
		{
			name: 'Aurora',
			url: 'http://goo.gl/tgYLa',
			thumbnail: 'http://s3.envato.com/files/368343/screens/01_Preview.__large_preview.jpg'
		},
		{
			name: 'Designer',
			url: 'http://goo.gl/KzlhP',
			thumbnail: 'http://s3.envato.com/files/367184/screenshots/01_Theme_preview.__large_preview.jpg'
		},
		{
			name: 'DigitalArt',
			url: 'http://goo.gl/M8hAz',
			thumbnail: 'http://s3.envato.com/files/341417/screens%20html/01_Theme_preview.__large_preview.jpg'
		},
		{
			name: 'Creato',
			url: 'http://goo.gl/9fgSd',
			thumbnail: 'http://s3.envato.com/files/341438/screens%20html/01_theme_preview.__large_preview.jpg'
		}
	],
	sliders:
	[
		{
			name: 'Default',
			url: 'http://demo.themefields.net/openlogic/html/template/index.html'
		},
		{
			name: 'Carusel',
			url: 'http://demo.themefields.net/openlogic/html/template/slider-carousel.html'
		},
		{
			name: 'Nivo',
			url: 'http://demo.themefields.net/openlogic/html/template/slider-nivo.html'
		},
		{
			name: '3D',
			url: 'http://demo.themefields.net/openlogic/html/template/slider-3d.html'
		}
	],
	fonts:
	[
		'Allerta',
		'Allerta Stencil',
		'Arimo',
		'Arvo',
		'Bentham',
		'Cantarell',
		'Cardo',
		'Coda',
		'Cousine',
		'Covered By Your Grace',
		'Crisomo Text',
		'Cuprum',
		'Droid Sans Mono',
		'Droid Sans',
		'Droid Serif',
		'Geo',
		'IM Fell',
		'Inconsolata',
		'Jangerine',
		'Josefin Sans',
		'Josefin Slab',
		'Just Me Again Down Here',
		'Lobster',
		'Molengo',
		'Neucha',
		'Neuton',
		'Nobile',
		'OFL Sorts Mill',
		'Old Standard TT',
		'Philosopher',
		'PT Sans',
		'Puritan',
		'Raleway',
		'Reenie Beanie',
		'Tinos',
		'Unifraktur Cook',
		'Unifraktur Magunita',
		'Vollkorn',
		'Yanone Kaffeesatz'
	],
	font_pattern:
	[
		'h1',
		'h2',
		'h3',
		'h4',
		'h5',
		'h6'
	],
	color_background:
	{
		'background-color':
		[
			'.pricing table th',
			'blockquote.type-2',
			'q.pull-quote.type-2',
			'q.pull-quote.type-2:after',
			'#title.type-1 h2',
			'#featured',
			'#featured.type-3 .desc',
			'.comment',
			'.tooltip',
			'.tooltip:after',
			'.testimonials.type-2 blockquote',
			'.testimonials.type-2 blockquote:after',
			'#nav > ul > li > ul > li:first-child:before',
			'#nav ul ul li.parent:hover:after',
			'#nav ul ul span',
			'#nav ul ul li.parent > ul > li:first-child > a:before',
			'#nav ul ul li.parent > ul > li:first-child > a:after'
		],
		/*'color':
		[
			'blockquote.type-1 p',
			'q.pull-quote.type-1',
			'.button.default-2 span',
			'.testimonials.type-1 p'
		],*/
		'border-color':
		[
			'#featured',
			'.frame',
			'.frame-holder img',
			'.popular img',
			'.popular li:hover',
			'.popular li:hover img',
			'.featured img',
			'.testimonials.type-3 .meta a'
		],
		'border-top-color':
		[
			'#title.type-1'
		]
	},
	color_main:
	{
		'color':
		[
			'a',
			'h3',
			'h5',
			'.pricing strong.price',
			'.filter > a',
			'.popular li:hover span',
			'.menu-1 a:hover',
			'.menu-1 a:focus',
			'#nav > ul > li > a:hover',
			'#nav > ul > li > a:focus',
			'a.read-more',
			'.services h4 a',
			'.desc a',
			'.available-icons li a',
			'.post-nav a',
			'#twitter_div span a',
			'.alt-contact a',
			'.featured a',
			'.testimonials .meta a',
			'.menu-2 a',
			'#nav ul .current',
			'#nav > ul > li.current > a'
		],
		'background-color':
		[
			'.slider-nav.type-1 li a:hover',
			'.slider-nav.type-1 li a:focus',
			'.slider-nav.type-2 li a:hover',
			'.slider-nav.type-2 li a:focus',
			'.slider-nav.type-3 li a:hover',
			'.slider-nav.type-3 li a:focus',
			'.menu-1 li.current',
			'.menu-1 li.current:after',
			'.tags li a:hover',
			'.pricing table:hover th',
			'.button.default span'
		],
		'border-color':
		[
			'a.frame-holder:hover img',
			'.tags li a:hover'
		]
	},
	psd_url: 'http://goo.gl/FlCkL',
	html_url: '#',
	wp_url: 'http://twitter.com/#!/themefields',
	purchase_url: 'http://goo.gl/xbYiM'
};

function load_script(path)
{
	$('head').append($('<script />').attr('type', 'text/javascript').attr('src', path));
}

function load_stylesheet(path)
{
	var style = $('<link />').attr('rel', 'stylesheet').attr('href', path).attr('type', 'text/css');
	document.getElementsByTagName('head')[0].appendChild(style[0]);
}

function hex(x)
{
	return ("0" + parseInt(x).toString(16)).slice(-2);
}
	
function rgb2hex(rgb)
{
	rgb = rgb.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);

	return hex(rgb[1]) + hex(rgb[2]) + hex(rgb[3]);
}

$( function()
{
	load_script('http://ajax.googleapis.com/ajax/libs/webfont/1/webfont.js');
	load_script('switcher/media/scripts/colorpicker.js');
	load_stylesheet('switcher/media/stylesheets/switcher.css');
	load_stylesheet('switcher/media/stylesheets/colorpicker.css');
	
	var $switcher = $('<div />').attr('id', 'switcher-holder').addClass('closed js').load('switcher/switcher.html #switcher-holder > *', function()
	{
		$('body').prepend($switcher);
		
		$('#switcher-holder li.toggle').click( function()
		{
			if ($('#switcher-holder').hasClass('closed'))
			{
				$('#switcher-holder').animate
				({
					'margin-top': 0
				}, 'slow', function()
				{
					$(this).removeClass('closed');
				});
				
				
				$(this).animate
				({
					'margin-top': 14,
					'margin-bottom': 0
				}, 'slow', function()
				{
					$(this).find('a').attr('title', 'Hide theme options');
				});
			} else
			{
					$('#switcher-holder').animate
				({
					'margin-top': -55
				}, 'slow', function()
				{
					$(this).addClass('closed');
				});
				
				$(this).animate
				({
					'margin-top': 55,
					'margin-bottom': -55
				}, 'slow', function()
				{
					$(this).find('a').attr('title', 'Customize theme');
				});
			}
		});
		
		$('#switcher li.psd a').attr('href', switcher.psd_url);
		$('#switcher li.html a').attr('href', switcher.html_url);
		$('#switcher li.wp a').attr('href', switcher.wp_url);
		$('#switcher a.purchase').attr('href', switcher.purchase_url);

		$.each(switcher.themes, function(i, theme)
		{
			$('#switcher li.select-theme ul.items').append($('<li />').append($('<a />').attr('href', theme.url).text(theme.name)).append($('<img />').attr('src', theme.thumbnail).attr('width', 590).attr('height', 300)));
		});
		
		$.each(switcher.sliders, function(i, slider)
		{
			$('#switcher li.change-slider ul.items').append($('<li />').append($('<a />').attr('href', slider.url).text(slider.name)));
		});

		$('#switcher li.change-font div.dropdown ul.items').css('overflow-x', 'hidden');
		
		$.each(switcher.fonts, function(i, font)
		{
			var $font = $('<li />');
			$font.append($('<a />').append($('<img />').attr('src', 'switcher/media/fonts/' + font.toLowerCase().replace(/ /g, '-') + '.png').attr('title', font).attr('alt', font.toLowerCase().replace(/ /g, '-'))));
			
			if (i == 0)
			{
				$font.addClass('selected');
			}
			
			$('#switcher li.change-font div.dropdown ul.items').append($font);
		});
		
		$('#switcher li.change-font').hover( function()
		{
			$(this).children('div.dropdown').show();
		}, function()
		{
			$(this).children('div.dropdown').hide();
		});

		$('#switcher li.change-font div.dropdown ul.items li').click( function()
		{
			$(this).parent('ul.items').children('li').removeClass('selected');
			$(this).addClass('selected');
				
			var style = '';
			var font = $(this).find('img').attr('title');
			var variants = 'regular';
				
			$('style.fonts').remove();
			$("link[href^='http://fonts.googleapis.com']").remove();
				
			if (font.toLowerCase() == 'diavlo')
			{

			} else
			{
				WebFont.load
				({
					google:
					{
						families: [ font.replace(/ /g,"+") + ':' + 'regular,bold' ]
					}
				});	
					
				$.each(switcher.font_pattern, function(index, selector)
				{
					style += selector + ' { font-family: \'' + font + '\';}';
				});

				$('<style />').attr('type', 'text/css').text(style).addClass('fonts').addClass('custom-rules').appendTo('head');
				$('#switcher li.change-font div.dropdown').hide();
			}
				
			return false;
		});

	});
	
});

$(window).load( function()
{
	$('#switcher li.change-color-1').data('color', '#000000')
	$('#switcher li.change-color-2').data('color', '#000000')

	$('#switcher li.change-color-1 div.dropdown').ColorPicker
	({
		flat: true,
		onSubmit: function(hsb, hex, rgb, el)
		{
			
		},
		onBeforeShow: function()
		{
			$(this).ColorPickerSetColor($('#switcher li.change-color-1').data('color'));
		},
		onChange: function(hsb, hex, rgb)
		{
			var style = '';
			var color = '#' + hex;

			$.each(switcher.color_background, function(key, elements)
			{
				$.each(elements, function(index, selector)
				{
					style += selector + ' {' + key + ':' + color + (selector == '.tip:after' ? ' !important' : '') + ';}';
				});
			});

			$('style.colors1').remove();
			$('<style />').attr('type', 'text/css').text(style).addClass('colors1').addClass('custom-rules').appendTo('head');
			$('#switcher li.change-color-1').data('color', color);
		}
	}).hover( function()
	{
		//$(this).ColorPickerShow();
	}, function()
	{
		//$(this).ColorPickerHide();
	});
	
	$('#switcher li.change-color-2 div.dropdown').ColorPicker
	({
		flat: true,
		onSubmit: function(hsb, hex, rgb, el)
		{
			
		},
		onBeforeShow: function()
		{
			$(this).ColorPickerSetColor($('#switcher li.change-color-2').data('color'));
		},
		onChange: function(hsb, hex, rgb, el)
		{
			var style = '';
			var color = '#' + hex;

			$.each(switcher.color_main, function(key, elements)
			{
				$.each(elements, function(index, selector)
				{
					style += selector + ' {' + key + ':' + color + (selector == '.testimonials .read-more:after' ? ' transparent' : '') + ';}';
				});
			});

			$('style.colors2').remove();
			$('<style />').attr('type', 'text/css').text(style).addClass('colors2').addClass('custom-rules').appendTo('head');
			$('#switcher li.change-color-2').data('color', color);
		}
	}).hover( function()
	{
		//$(this).ColorPickerShow();
	}, function()
	{
		//$(this).ColorPickerHide();
	});
		
	$('div#switcher').show();
	$('#switcher-holder').removeClass('closed');
});

