var focalOffset = { _x: -543, _y: -65};
var focalSize = {w: 636, h: 1010};
var viewPort = {w: $(window).width(), h: $(window).height()};

// Project Viewer!
//settings
var sliderSpeed = 250;
var fadeSpeed = 250;
//globals
var currentIndex = 0;
var liwidth = 0;
var maxleft = 0;



$(function(){
	//initinal setup

	$(window).bind('resize', recenter);
	scrollSlide(0);
	
	$(".logo a").click(function(){
		focalOffset = { _x: -543, _y: -65};
		focalSize = {w: 636, h: 1010};
		navClick($("#item-1"));
		return false;
	});
	
	$("#item-1").click(function(){
		focalOffset = { _x: -543, _y: -65};
		focalSize = {w: 636, h: 1010};
		navClick(this);
		return false;
	});
	
	$("#item-2").click(function(){
		focalOffset = {_x: -3207, _y: 0};
		focalSize = {w: 1029, h: 1010};
		navClick(this);
		return false;
	});
	
	$("#item-3").click(function(){
		focalOffset = {_x: -5256, _y: 0};
		focalSize = {w: 1010, h: 810};
		navClick(this);
		return false;
	});
	
	$(".get-to-know").click(function(){
		focalOffset = {_x: -5256, _y: 0};
		focalSize = {w: 1010, h: 810};
		navClick($("#item-3"));
		return false;
	});
	
	$("#item-4").click(function(){
		focalOffset = {_x: -7649, _y: 0};
		focalSize = {w: 1010, h: 1010};
		navClick(this);
		return false;
	});
	
	
	
	
	
	// Project Viewer!
	// startProjViewer();
	// Load in initinal project
	
	$("#submit").click(function(){
		var err = "";
		// CHECK fields!
		if( (!($('#name').val())) || ($('#name').val() === "")){
			err += "name is empty";
		}
		if( (!($('#email').val())) || ($('#email').val() === "")){
			err += "email is empty";
		}
		if((!($('#message').val())) || ($('#message').val() === "")){
			err += "Message is empty";
		}
		if(err !== ""){
			alert("You missed some fields");
			return false;
		}
		var name = $('#name').val();
		var email = $('#email').val();
		var message = $('#message').val();
		console.log(name + " " + email + " " + message);
		// Send the trquest
		var div = $(this);
		var oldHTML = $(this).html();
		$(div).parent().html('<div class="responce"><img src="img/ajax.gif" class="loading" width="32" height="32" alt="Ajax"></div>');
		// $(this).html('');
		$.ajax({
			type: "POST",
			url: "contact.php",
			data: "name=" + name + "&email=" + email + "&message=" + message,
			success: function(msg){
			 setTimeout(function(){ $(".responce").html(msg) } , 250);
			}
			// error: function(XMLHttpRequest, textStatus, errorThrown){
			// 	alert( "Error: " + textStatus );
			// 	$(div).html(oldHTML);
			// }
		});
		return false;
	});
	
});

function startProjViewer(){
	liwidth = ($(".image_mod_list li").outerWidth(true));
	maxleft = (liwidth * $(".image_mod_list li").length) * -1;
	$(".image_module .next").live('click', next_btn);
	$(".image_module .prev").live('click', prev_btn);
	construct_index();
	$('.project-selecter li a').click(function(){ load_project(this); return false; })
	fancyboxSetUp();
}

function fancyboxSetUp(){
	$('a.port-enlarge').fancybox({
		overlayColor: '#000',
		hideOnContentClick : true,
		titleShow: false
	});
}

function load_project(element){
	$(".image_module .next").die();
	$(".image_module .prev").die();
	destroy_index();
	
	var request = $(element).attr('title');
	$(element).parent().parent().find(".active").removeClass("active");
	$(element).addClass("active");
	
	// console.log(request);
	$.ajax({
		url: "projects.php",
	  cache: false,
		type: "POST",
		data: ({'request' : request}),
	  success: function(html){
			$(".project").html(html);
			maxleft = (liwidth * $(".image_mod_list li").length) * -1;
			$(".image_module .next").live('click', next_btn);
			$(".image_module .prev").live('click', prev_btn);
			construct_index();
			Cufon.refresh(".fancy-sans");
			fancyboxSetUp();
	  },
		error: function(XMLHttpRequest, textStatus, errorThrown){
			alert("Error: " + textStatus);
		}
	});
	
}

function update_index(){
	$('.image_mod_index a.active').removeClass("active");
	$('.image_mod_index li#index-item-' + currentIndex + " a").addClass("active");
}

function construct_index(){
	var ulWidth = 0;
	currentIndex = 0;
	for(i = 0; i < $(".image_mod_list li").length ; i ++){
		var html = '<li id="index-item-' + i + '"';
		if(i == ($(".image_mod_list li").length - 1)) html = html + ' class="last"';//add last class
		html = html + '><a href="#"';
		if(i == 0) html = html + ' class="active"';//add active class to first
		html = html + '>'+ i + '</a></li>';
		$('.image_mod_index').append(html);
		ulWidth += ($('#index-item-' + i).outerWidth(true));
		$('#index-item-' + i + " a").click(function(){ select_index($(this).html()); return false; });
	}
	$('.image_mod_index').css("width", ulWidth + "px");
	$('.image_module .prev').fadeTo(fadeSpeed, 0.3);
}

function select_index(request){
	$(".image_mod_list").animate({
		left: ('-' + (request * liwidth) + 'px')
	}, sliderSpeed, function(){ });
	
	if((parseInt(request) + 1) == $(".image_mod_list li").length ){
		$('.image_module .next').fadeTo(fadeSpeed, 0.3);
		$('.image_module .prev').fadeTo(fadeSpeed, 1);
	}else if(request == 0){
		$('.image_module .prev').fadeTo(fadeSpeed, 0.3);
		$('.image_module .next').fadeTo(fadeSpeed, 1);
	}else{
		$('.image_module .next').fadeTo(fadeSpeed, 1);
		$('.image_module .prev').fadeTo(fadeSpeed, 1);
	}
	currentIndex = request;
	update_index();
}

function destroy_index(){
	var currentIndex = 0;
	$('.image_mod_index').html('');
}

function next_btn(){
	$('.image_module .prev').fadeTo(fadeSpeed, 1);
	//console.log("maxleft = " + maxleft);
	if( !((parseInt($(".image_mod_list").css('left')) - liwidth) <= maxleft)){
		$(".image_module .next").die();
		if((parseInt($(".image_mod_list").css('left')) - (liwidth * 2)) <= maxleft){
			$('.image_module .next').fadeTo(fadeSpeed, 0.3);
		}
		// console.log("maxleft = " +maxleft);
		$(".image_mod_list").animate({
			left: ('-=' + liwidth + 'px')
		}, sliderSpeed, function(){ $(".image_module .next").live('click', next_btn); });
		currentIndex ++;
		update_index();
	}
	return false;
}

function prev_btn(){
	$('.image_module .next').fadeTo(fadeSpeed, 1);
	
	if((parseInt($(".image_mod_list").css('left'))) - (liwidth * -1) >= 0){
		$('.image_module .prev').fadeTo(fadeSpeed, 0.3);
	}else{
		$('.image_module .prev').fadeTo(fadeSpeed, 1);
	}

	if( (parseInt($(".image_mod_list").css('left'))) < 0 ){
		$(".image_module .prev").die();
		$(".image_mod_list").animate({
			left: '+=291px'
		}, sliderSpeed, function(){ $(".image_module .prev").live('click', prev_btn); });
		currentIndex --;
		update_index();
	}
	return false;
}

function navClick(element){
	scrollSlide(1000);
	$("li.active").removeClass("active");
	$(element).parent().addClass("active");
	Cufon.refresh(".fancy-sans");
}

function recenter(){
	viewPort.w = $(window).width();
	viewPort.h = $(window).height();
	
	scrollSlide(500);
}

function scrollSlide(speed){
	var windowCenter = (viewPort.w/2) * -1;
	var intrestCenter = focalOffset._x - (focalSize.w/2);
	
	var neededLeft = intrestCenter - windowCenter;
	var neededTop = focalOffset._y;
	
	//console.log("intrestCenter = " + intrestCenter + " | windowCenter = " + windowCenter );
	
	$(".slides").clearQueue().stop().animate({
		left : neededLeft,
		top : neededTop
	}, speed);
}
