/**
 * @author msandow
 */

$.timeWallpaper = function(options){	
	
	var time = new Object();

	time.now = new Date();
	
	$.getPaper = function(){
		time.hours = time.now.getHours();
		time.count = options.times.length;
		time.rgb = $('body').css('background-color');
		
		
		if($(window).width() > $(document).width()){
			time.width = $(window).width();
		}else{
			time.width = $(document).width();
		}
						
		time.path = document.URL.substring(0, document.URL.lastIndexOf("/"));
		time.path = time.path.replace(/https:\/\/admin.searchoptics.com\//,'http://ww3.ldvis.com/');
		
		if($.browser.mozilla || $.browser.safari){
		    //if mozilla or safari
			time.rgb = time.rgb.substring(4,(time.rgb.length - 1));
		
			function toHex(N) {
				if (N==null) return "00";
				N=parseInt(N); if (N==0 || isNaN(N)) return "00";
				N=Math.max(0,N); N=Math.min(N,255); N=Math.round(N);
				return "0123456789ABCDEF".charAt((N-N%16)/16) + "0123456789ABCDEF".charAt(N%16);
			}
			
			time.colorArray = new Array();
	
			time.colorArray.push(Number(time.rgb.substring(0, time.rgb.indexOf(","))));
			time.colorArray.push(Number(time.rgb.substring(time.rgb.indexOf(",") + 2, time.rgb.lastIndexOf(","))));
			time.colorArray.push(Number(time.rgb.substring(time.rgb.lastIndexOf(",") + 2)));
			
			time.color = toHex(time.colorArray[0])+toHex(time.colorArray[1])+toHex(time.colorArray[2]);
			
		}else{
		 	//if IE
			time.color = time.rgb.substring(1);
			
	 	}
		
	
		if (time.count > 1) {
			for (time.cycle = 0; time.cycle < time.count; time.cycle++) {
				if (time.cycle != (time.count - 1)) {
					if (time.hours >= options.times[time.cycle] && time.hours < options.times[time.cycle + 1]) {
						time.src = 'common/phpthumb/phpThumb.php?src=' + time.path + '/' + options.srcs[time.cycle] + '&w=' + (time.width+20) + '&f=jpg&aoe=1&f=jpeg&fltr[]=bvl|10|'+time.color+'|'+time.color;
						$('body').css('background', '#'+time.color + ' url(' + time.src + ') top center no-repeat fixed');
					}
				}
				else {
					if (time.hours >= options.times[time.cycle]) {
						time.src = 'common/phpthumb/phpThumb.php?src=' + time.path + '/' + options.srcs[time.cycle] + '&w=' + (time.width+20) + '&f=jpg&aoe=1&f=jpeg&fltr[]=bvl|10|'+time.color+'|'+time.color;
						$('body').css('background', '#'+time.color + ' url(' + time.src + ') top center no-repeat fixed');
					}
				}
			}
		}
		else {
			if (time.hours >= options.times[0]) {
				time.src = 'common/phpthumb/phpThumb.php?src=' + time.path + '/' + options.srcs[0] + '&w=' + (time.width+20) + '&f=jpg&aoe=1&f=jpeg&fltr[]=bvl|10|'+time.color+'|'+time.color;
				$('body').css('background', '#'+time.color + ' url(' + time.src + ') top center no-repeat');
			}
		}
		
				
		
	};
	
	$.getPaper();
	
	$(window).bind('resize',function(){
		$.getPaper();
	});
	
};