google.load("jquery", "1.3.2");
google.load("jqueryui", "1.7.2");

google.setOnLoadCallback(function() {
    $(document).ready(function() {
        initRotator();
		initImageSelect();
		initThreeCols();
		initContact();
    });
});

var contactInaction = false;

function initContact(){
	
	var width = $("#contactConatiner .col.one").width() + $("#contactConatiner .col.two").width() + 20;
	
	
	$("#contactConatiner").width(40);
	
	$("#contactConatiner").hover(
		function(){

				$("#contactConatiner").stop().animate({width: width}, 'slow')
		},
		function(){
				$("#contactConatiner").stop().animate({width: 40}, 'slow')
		}
	);
}


var colCount = -1;

function initThreeCols(){
	var allCols = $(".portfolio .col").length;
	colCount = colCount + 3;
	
	if(colCount < allCols)
	{
		$("<div class='colRow'></div>").insertAfter($(".portfolio .col").eq(colCount));
		initThreeCols();
	}
}

function initImageSelect(){
	
	$(".image").each(function(){
										   
		var that = this;
		$(that).find("img").hide();
		$(that).find("img:first").show();
		
		var count = 0;
		$(that).find("img").each(function(){
			$(that).find(".imageSelector").append("<div><p>" + count + "</p></div>");
			count++;
			$(that).find(".imageSelector").width(15 * count);
		});
		
		$(that).find(".imageSelector div:first").addClass("on");
			
	});
	
	rigSelectClick();
	
}

function rigSelectClick(){
	$(".imageSelector div").click(function(){
		$(this).parent().find("div").removeClass("on");
		$(this).addClass("on");
		$(this).parents(".image").find("img:visible").hide();
		$(this).parents(".image").find("img").eq($(this).find("p").text()).show();
	});
	
	$(".image img").click(function(){
		var imageCount = $(this).parents(".image").find("img").length;	
		var current = $(this).parents(".image").find(".imageSelector").find(".on").text();
		
		$(this).hide();
		
		current++;
		
		if(current >= imageCount){
			$(this).parents(".image").find("img:first").show();
			current = 0;
		}
		else
		{
			$(this).next("img").show();
		}
		
		$(this).parents(".image").find(".imageSelector").find(".on").removeClass("on");
		$(this).parents(".image").find(".imageSelector div").eq(current).addClass("on");
	});


}


function initRotator() {
	slider();
	$("#homeContentWrapper").width($(".homeContent").width() * $(".homeContent").length);
}

var currentVal;
var speed = 13000;
var run = setInterval('rotate()', speed);

function slider() {

    currentVal = 1;

    var item_width = $('#overlayTrigger .homeContent').outerWidth();
    var left_value = item_width * (-1);


    $('#overlayTrigger .homeContent:first').before($('#overlayTrigger .homeContent:last'));
    $('#overlayTrigger #homeContentWrapper').css({ 'left': left_value });

    $('#Prev').click(function() {
		clearStart();
        var left_indent = parseInt($('#overlayTrigger #homeContentWrapper').css('left')) + item_width;
        $('#overlayTrigger #homeContentWrapper:not(:animated)').animate({ 'left': left_indent }, 1700, function() {
            $('#overlayTrigger .homeContent:first').before($('#overlayTrigger .homeContent:last'));
            $('#overlayTrigger #homeContentWrapper').css({ 'left': left_value });
        });
        return false;
    });

    $('#Next').click(function() {
		clearStart();
        var left_indent = parseInt($('#overlayTrigger #homeContentWrapper').css('left')) - item_width;
        $('#overlayTrigger #homeContentWrapper:not(:animated)').animate({ 'left': left_indent }, 1700, function() {
            $('#overlayTrigger .homeContent:last').after($('#overlayTrigger .homeContent:first'));
            $('#overlayTrigger #homeContentWrapper').css({ 'left': left_value });
        });
        return false;
    });

    /*$('#overlayTrigger').hover(
    	function() {
    	    clearInterval(run);
    	},
    	function() {
    	    run = setInterval('rotate()', speed);
    	}
    );*/
}

function rotate() {
    $('#Next').click();
}

function clearStart(){
	clearInterval(run);
	run = setInterval('rotate()', speed);
}

function stopRotator(){
	clearInterval(run);	
}

function startRotator(){
	run = setInterval('rotate()', speed);
}
