;(function($) {
	var snowing = false;
	if (!Array.prototype.indexOf) {  
		Array.prototype.indexOf = function (searchElement /*, fromIndex */ ) {  
			"use strict";  
			if (this === void 0 || this === null) {  
				throw new TypeError();  
			}  
			var t = Object(this);  
			var len = t.length >>> 0;  
			if (len === 0) {  
				return -1;  
			}  
			var n = 0;  
			if (arguments.length > 0) {  
				n = Number(arguments[1]);  
				if (n !== n) { // shortcut for verifying if it's NaN  
					n = 0;  
				} else if (n !== 0 && n !== (1 / 0) && n !== -(1 / 0)) {  
					n = (n > 0 || -1) * Math.floor(Math.abs(n));  
				}  
			}  
			if (n >= len) {  
				return -1;  
			}  
			var k = n >= 0 ? n : Math.max(len - Math.abs(n), 0);  
			for (; k < len; k++) {  
				if (k in t && t[k] === searchElement) {  
					return k;  
				}  
			}  
			return -1;  
		}  
	}
	var urlParams = (function() {
		var urlParams = {};
		var prms = window.location.search.substring(1).split('&');
		for(var pms = 0; pms < prms.length; pms++) {
			var pm1 = prms[pms].split('=');
			urlParams[pm1[0]] = pm1[1];
		}
		return urlParams;
	})();
	var themeChoices;
	var dawnTime = 6.5*60;
	var duskTime = 19.5*60;
	var currTime = (new Date().getHours() * 60) + new Date().getMinutes();
	var timeUntilNext = 0;
	themeChoices = ['default', 'dark'];
/*	if(currTime < dawnTime || currTime > duskTime) {
		//if(console && console.log) console.log("Night - "+currTime+" - " + new Date());
		themeChoices = ['dark','default'];
	} else {
		//if(console && console.log) console.log("Day - "+currTime+" - "  + new Date());
		themeChoices = ['default','dark'];
	}*/
	
	var CurrentTheme = themeChoices[0];
	if(urlParams.hasOwnProperty('theme')) {
		CurrentTheme = urlParams.theme;
	} else if(readCookie('theme')) {
		CurrentTheme = readCookie('theme');
		if(themeChoices.indexOf(CurrentTheme) == -1) {
			CurrentTheme = themeChoices[0];
		}
	}
	$('#themecss').attr('href', 'http://www.matchadesign.com/img/themes/'+CurrentTheme+'/theme.css');
$(function themeSwitch() {
	$('.counter').append('<div id="theme-switch"></div>');
	if(themeChoices.length <= 1) {
		$('#theme-switch').hide();
	}
	$('#theme-switch').click(function() {
		setTheme(themeChoices[(themeChoices.indexOf(CurrentTheme) + 1) % themeChoices.length]);
	});
	function getThemeItems(theme) {
		if($('#home').length > 0) {
			return getThemeItemsHome(theme);
		} else {
			return getThemeItemsSub(theme);
		}
	}
	function getThemeItemsHome(theme) {
		if(theme == 'easter') {
			return [
				{'side' : 'left', 'img' : '/img/themes/easter/decoration-easter-egg.png'},
				{'side' : 'right', 'img' : '/img/themes/easter/decoration-easter-egg-2.png'},
				{'side' : 'right', 'img' : '/img/themes/easter/decoration-easter-bunny.png'},
				{'side' : 'right', 'img' : '/img/themes/easter/decoration-flowers.png', 'offset' : '-29'}
				];
		}
		if(theme == 'dark') {
			return [
				{'side' : 'left', 'img' : '/img/themes/dark/decoration-fuel-pump.png', 'offset' : '15'},
				{'side' : 'left', 'img' : '/img/themes/dark/decoration-light.png', 'offset' : '-4'},
				{'side' : 'left', 'img' : '/img/themes/dark/decoration-cat.png'},
				{'side' : 'left', 'img' : '/img/themes/dark/decoration-bottles.png'},
				{'side' : 'right', 'img' : '/img/themes/dark/decoration-lightbulb.png'},
				{'side' : 'right', 'img' : '/img/themes/dark/decoration-sofa.png'},
				{'side' : 'right', 'img' : '/img/themes/dark/decoration-coffee.png'},
				{'side' : 'right', 'img' : '/img/themes/dark/decoration-sofa.png'}
				];
		}
		if(theme == 'autumn') {
			return [
			{'side' : 'left', 'img' : '/img/themes/autumn/decoration-cornucopia.png', 'offset' : '-246'},
			{'side' : 'left', 'img' : '/img/themes/autumn/decoration-pumpkins2.png', 'offset' : '-78'},
			{'side' : 'right', 'img' : '/img/themes/autumn/decoration-apples.png', 'offset' : '-24'},
			{'side' : 'right', 'img' : '/img/themes/autumn/decoration-thanksgiving.png', 'offset' : '-6'},
			{'side' : 'right', 'img' : '/img/themes/autumn/decoration-pumpkins.png', 'offset' : '-63'}
			];
		}
		if(theme == 'christmas') {
			return [
			[
				{'side' : 'left', 'img' : '/img/themes/christmas/decoration-tree.png', 'offset' : '-43'},
				{'side' : 'left', 'img' : '/img/themes/christmas/decoration-nutcracker.png', 'offset' : '23'},
				{'side' : 'left', 'img' : '/img/themes/christmas/decoration-stocking.png', 'offset' : '0'},
				{'side' : 'left', 'img' : '/img/themes/christmas/decoration-gift.png', 'offset' : '-24', 'css' : 'margin-bottom:-1px;'},
				{'side' : 'left', 'img' : '/img/themes/christmas/decoration-santa.png', 'offset' : '-33'},
				{'side' : 'left', 'img' : '/img/themes/christmas/decoration-nativity.png', 'offset' : '-104'},
				{'side' : 'left', 'img' : '/img/themes/christmas/decoration-nativity.png', 'offset' : '-104'},
				{'side' : 'left', 'img' : '/img/themes/christmas/decoration-nativity.png', 'offset' : '-104'}
			],
			[
				{'side' : 'right', 'img' : '/img/themes/christmas/decoration-wreath.png', 'offset' : '-37', 'css' : 'margin-bottom:144px;'}
			],
			[
				{'side' : 'right', 'img' : '/img/themes/christmas/decoration-msg1.png', 'offset' : '4', 'css' : 'margin-bottom:71px;'},
				{'side' : 'right', 'img' : '/img/themes/christmas/decoration-msg2.png', 'offset' : '8', 'css' : 'margin-bottom:46px;'},
				{'side' : 'right', 'img' : '/img/themes/christmas/decoration-msg3.png', 'offset' : '7', 'css' : 'margin-bottom:69px;'},
				{'side' : 'right', 'img' : '/img/themes/christmas/decoration-msg4.png', 'offset' : '11', 'css' : 'margin-bottom:42px;'}
			]];
		}
		return [
			{'side' : 'left', 'img' : '/img/decoration-bulb.png'},
			{'side' : 'left', 'img' : '/img/decoration-bottles.png'},
			{'side' : 'left', 'img' : '/img/decoration-cats.png'},
			{'side' : 'right', 'img' : '/img/decoration-lamp.png'},
			{'side' : 'right', 'img' : '/img/decoration-teapot.png'},
			{'side' : 'left', 'img' : '/img/decoration-apples.png', 'offset' : '-11'}/*,
			{'side' : 'left', 'img' : '/img/decoration-cornucopia.png', 'offset' : '218'},
			{'side' : 'left', 'img' : '/img/decoration-cornucopia.png', 'offset' : '218'},
			{'side' : 'left', 'img' : '/img/decoration-cornucopia.png', 'offset' : '218'}*/
		];
	}
	function getThemeItemsSub(theme) {
		if(theme == 'easter') {
			return [
				{'side' : 'right', 'img' : '/img/themes/easter/decoration-flowers.png', 'offset' : '-29'}
				];
		}
		if(theme == 'dark') {
			return [
				{'side' : 'right', 'img' : '/img/themes/dark/decoration-tree.png'}
				];
		}
		if(theme == 'autumn') {
			return [
				{'side' : 'right', 'img' : '/img/themes/autumn/decoration-phonograph.png', 'offset' : '-49'}
				];
		}
		if(theme == 'christmas') {
			return [
				{'side' : 'right', 'img' : '/img/themes/christmas/decoration-tree.png', 'offset' : '-60'}
			];
		}
		return [
			{'side' : 'right', 'img' : '/img/decoration-bonsai.png', 'offset' : '-77'}
			];
	}
	setTheme(CurrentTheme);
	function setTheme(th) {

		currTime = (new Date().getHours() * 60) + new Date().getMinutes();
		timeUntilNext = 0;
		if(currTime < dawnTime || currTime > duskTime) {
			timeUntilNext = dawnTime - currTime;
			if(timeUntilNext < 0) timeUntilNext += 24*60;
		} else {
			timeUntilNext = duskTime - currTime;
			if(timeUntilNext < 0) timeUntilNext += 24*60;
		}
		
		CurrentTheme = th;
		if(th == 'christmas' && !snowing) {
			if($('#home').length > 0) {
			$(document).snowfall(
				{
				flakeCount : 35, // number
				flakeColor : '#ffffff', // string
				flakeIndex: 600, // number
				minSize : 1, // number
				maxSize : 3, // number
				minSpeed : 4, // number
				maxSpeed : 5, // number
				round : false,                  // bool
				shadow : false,                  // bool
				/*collection : '.countertop',           // string*/
				collectionHeight : 20
				}
			);
			} else {
			$(document).snowfall(
				{
				flakeCount : 35, // number
				flakeColor : '#ffffff', // string
				flakeIndex: 600, // number
				minSize : 1, // number
				maxSize : 3, // number
				minSpeed : 4, // number
				maxSpeed : 5, // number
				round : false,                  // bool
				shadow : false,                  // bool
				collectionHeight : 20
				}
			);
			}
			snowing = true;
		} else if (snowing) {
			window.clearsnow();
			$('canvas').remove();
			snowing = false;
		}
		createCookie('theme', th, timeUntilNext);
		$('#themecss').attr('href', 'http://www.matchadesign.com/img/themes/'+th+'/theme.css');
		if(themeChoices.indexOf(CurrentTheme) >= 0) {
			$('#theme-switch').removeAttr('class').addClass('option-' + (themeChoices.indexOf(CurrentTheme)+1) + '-' + themeChoices.length);
		}
		if(th == 'autumn') {
			$('.img-scroll img').each(function() {
				var clr1 = {
					"r" : {"min" : 30, "max" : 206},
					"g" : {"min" : 15, "max" : 176},
					"b" : {"min" : 0, "max" : 101}};
				$(this).data('colors', clr1);
			});
		} else {
			$('.img-scroll img').each(function() {
				$(this).removeData('colors');
			});
		}
		if(th == 'dark' || th == 'autumn' || th == 'christmas') {
			$('#logo-area img').each(function() {
				$(this).attr('src', $(this).attr('src').replace(/logos\/(?!dark)/, "logos/dark/"));
			});
			$('.img-scroll img').each(function() {
				if($.fn.clearSaturation) {
					$(this).clearSaturation().attr('src', $(this).attr('src').replace(/logos\/(?!dark)/, "logos/dark/"));
				}
			});
		} else {
			$('#logo-area img').each(function() {
				$(this).attr('src', $(this).attr('src').replace("/dark/", "/"));
			});
			$('.img-scroll img').each(function() {
				if($.fn.clearSaturation) {
					$(this).clearSaturation().attr('src', $(this).attr('src').replace("/dark/", "/"));
				}
			});
		}
		$('.countertop').addRandomItem(getThemeItems(CurrentTheme));
	}
	
	
});

	function createCookie(name,value,minutes) {
		if (minutes) {
			var date = new Date();
			date.setTime(date.getTime()+(minutes*/*24*60**/60*1000));
			date.setSeconds(0,0);
			var expires = "; expires="+date.toGMTString();
		}
		else var expires = "";
		document.cookie = name+"="+value+expires+"; domain=.matchadesign.com; path=/";
	}
	
	function readCookie(name) {
		var nameEQ = name + "=";
		//if(console && console.log) console.log(document.cookie);
		var ca = document.cookie.split(';');
		for(var i=0;i < ca.length;i++) {
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substr(1);
			if (c.indexOf(nameEQ) == 0) {
				//if(console && console.log) console.log("[" + c.substr(nameEQ.length) + "]");
				return c.substr(nameEQ.length);
			}
		}
		return null;
	}
	
	function eraseCookie(name) {
		createCookie(name,"",-1);
	}
})(jQuery);
