$(document).ready( function() {
	$(".goleft").click(function (){
		$.widget.stop_timer();
		$.widget.scroll_left();
	});
	$(".goright").click(function (){
		$.widget.stop_timer();
		$.widget.scroll_right();
	});
	$(".doplay").click(function (){
		$.widget.start_timer();
	});	

	$("#logo").hover(function(){
		$(this).removeClass("logo_image").addClass("logo_image_hover");
	},function (){
		$(this).removeClass("logo_image_hover").addClass("logo_image");
	});
	
	/* if need logo act as toggle
	 * 
	 * $("#logo").toggle(function () {
		$.widget.close_help();
	},function(){
		$.widget.open_help();
	});*/
	
	$("#logo").click(function () {
		$.widget.open_help();
	});
	
	$("#help_c").html($("#helper_content").html());
	$("#close_helper").click(function(){
		$.widget.close_help();
	});
	
	$.widget.start();
	
	$.widget.set_days();
	
	$.preload("img", {onFinish: function(){
	      $('.preload_splash').remove();
    }});


});

(function($) {
	$.widget = {
		speed: 9000,	
		item_pos: 0,
		category_pos: 0,
		lock: false,
		timer_had_started: false,
		open_help: function(){
			$("#help").show();
			
		},
		close_help: function (){
			
			$("#help").hide();
		},
		set_days: function(){
			var zero_day = new Date("11 March 2010");
			var now = new Date();
			var days = Math.ceil((zero_day.getTime() - now.getTime())/(3600*24*1000))+1;
			var text = '';
			if (days>1)			
				text = days+" Days Remaining";
			else if (days == 1)
        text = "Today is World Plumbing Day!";
			/*	text = "1 Day Remaining";
			else if (days == 0)
				text = "Today is World Plumbing Day!";*/
			
			$("#remaining").text(text);
			
		},
		get_first: function(){
			return	$("#content div:eq(0) img:eq(0)");
		},
		get_next: function()
		{
			if ($("#content div:eq("+$.widget.category_pos+") img:eq("+$.widget.item_pos+")").is(":last-child"))
			{
				if ($("#content div:eq("+$.widget.category_pos+")").is(":last-child"))
				{
					$.widget.category_pos = 0;
					$.widget.item_pos = 0;
				}else
				{
					$.widget.category_pos++;
					$.widget.item_pos = 0;
				}
			}else
			{
				$.widget.item_pos++;
			}
			return	$("#content div:eq("+$.widget.category_pos+") img:eq("+$.widget.item_pos+")");
		},
		get_previous: function()
		{
			if ($("#content div:eq("+$.widget.category_pos+") img:eq("+$.widget.item_pos+")").is(":first-child"))
			{
				if ($("#content div:eq("+$.widget.category_pos+")").is(":first-child"))
				{
					$.widget.category_pos = $("#content div").length-1;
					$.widget.item_pos = $("#content div:eq("+$.widget.category_pos+") img").length-1;
				}else
				{
					$.widget.category_pos--;
					$.widget.item_pos = $("#content div:eq("+$.widget.category_pos+") img").length-1;
				}
			}else
			{
				$.widget.item_pos--;
			}
			return	$("#content div:eq("+$.widget.category_pos+") img:eq("+$.widget.item_pos+")");			
		},
		start: function(){
			var new_img = $.widget.get_first();
			
			$("#category_title").attr('class',new_img.parent().attr('class'));
			
			new_img.clone().appendTo("#frame");

			$.widget.start_timer();
			
		},
		start_timer: function(){
			if ($.widget.timer_had_started == false){
				$("#play_button").addClass("play_button_act");
				$("#frame").everyTime($.widget.speed, 'timer', function(i){
					$.widget.scroll_right();
				});
				$.widget.timer_had_started = true;
			}else
			{
				$.widget.stop_timer();
				
			}
		},
		stop_timer: function (){
			$("#play_button").removeClass("play_button_act");
			$("#frame").stopTime('timer');
			$.widget.timer_had_started = false;
		},
		change_category_class: function (to_class)
		{
			
		},
		scroll_left: function()
		{
			if ($.widget.lock == true)
				return $(this);
			
			var new_img = $.widget.get_previous();
			$("#category_title").attr('class',new_img.parent().attr('class'));
			
			new_img.clone().appendTo("#frame");
			$.widget.lock = true;
			$("#frame img:eq(1)").css({'opacity': 0, 'z-index' : 1});
			$("#frame img:eq(0)").animate({"opacity": 0}, 500, '', function (){
				$("#frame img:eq(1)").animate({"opacity": 1}, 500, '' );
				$(this).remove();
				$.widget.lock = false;
			});
		},
		scroll_right: function()
		{
			if ($.widget.lock == true)
				return $(this);
			
			var new_img = $.widget.get_next();
			$("#category_title").attr('class',new_img.parent().attr('class'));
			
			new_img.clone().appendTo("#frame");
			$.widget.lock = true;
			$("#frame img:eq(1)").css({'opacity': 0, 'z-index' : 1});
			$("#frame img:eq(0)").animate({"opacity": 0}, 500, '', function (){
				$("#frame img:eq(1)").animate({"opacity": 1}, 500, '' );
				$(this).remove();
				$.widget.lock = false;
			});
		}		
}})(jQuery);
