/**
 * @author jan
 */



$(document).ready(function(){
	initCufon();
	initComponents();
	initSlideshow();
	initMessage();
	
	initHome();
	initAbout();
	initProducts();
	initBillingInfo();
	initYourBag();
});

$(window).load(function(){
	initHomeAfter();
});

function initCufon(){
	Cufon.replace('#menu li a', {hover:true});
}

function initComponents(){
	if($("input:radio").size()){
		$("input:radio").amaiRadio();
	}
	if($("input:checkbox").size()){
		$("input:checkbox").amaiCheck();
	}
	if($("select:visible").size()){
		$("select:visible").amaiSelect(100, 267);
	}
	if($(".submit_button").size()){
		var origX;
		$(".submit_button").live("mouseover", function(){
			origX = $(this).css("background-position-x");
			if(!$(this).hasClass("update_button") && !$(this).hasClass("go_back")){
				$(this).animate({backgroundPosition:"147px"}, {duration:400, easing:"easeOutExpo", queue:false});
			}
		});
		$(".submit_button").live("mouseout", function(){
			if(!$(this).hasClass("update_button") && !$(this).hasClass("go_back")){
				$(this).animate({backgroundPosition:"140px"}, {duration:400, easing:"easeOutExpo", queue:false});
			}
		});
	}
	if($("#update_button").size()){
		var interval;
		$("#update_button").live("mouseover", function(){
			interval = setInterval("rotateUpdateBackground()", 250);
		});
		$("#update_button").live("mouseout", function(){
			if(backgroundImageNormal == false){
				rotateUpdateBackground();
			}
			clearInterval(interval);
		});
	}
	if($(".go_back").size()){
		var interval;
		$(".go_back").live("mouseover", function(){
			origX = $(this).css("background-position-x");
			$(this).animate({backgroundPosition:"8px"}, {duration:400, easing:"easeOutExpo", queue:false});
		});
		$(".go_back").live("mouseout", function(){
			$(this).animate({backgroundPosition:"15px"}, {duration:400, easing:"easeOutExpo", queue:false});
		});
	}
	if($(".about_form_textbox input, .about_form_textbox textarea, #search_input, #add_product input[type='text']").size()){
		$(".about_form_textbox input, .about_form_textbox textarea, #search_input, #add_product input[type='text']").focus(function(){
			if(this.value == "*" || this.value.toLowerCase() == "search" || this.value.toLowerCase() == "zoeken"){
				this.value = "";
			}
			$(this).animate({borderBottomColor:"#000000", borderTopColor:"#000000", borderLeftColor:"#000000", borderRightColor:"#000000"}, 400, "linear");
		});
		$(".about_form_textbox input, .about_form_textbox textarea, #search_input, #add_product input[type='text']").blur(function(){
			if($(this).attr("id") == "search_input" && $(this).val() == ""){
				this.value = "Search";
			}
			$(this).animate({borderBottomColor:"#cdcecd", borderTopColor:"#cdcecd", borderLeftColor:"#cdcecd", borderRightColor:"#cdcecd"}, 600, "linear");
		});
	}
	if($("#search_input").size()){
		$("#search_input").autocomplete(autoCompleteData).result(function(){
			$(this).parent().submit();
		});
	}
	if($("#legals.left").size()){
		$("#legals.left a").click(function(){
			var distance = parseInt($($(this).attr("href")).offset().top - $("#header").height() - ($("#legals.left").height() / 2));
			if(!(distance>0)){
				distance = 0;
			}
			$.scrollTo(distance, 800, {easing:"easeOutExpo"});
			$("#legals.left").animate({marginTop:distance},{ease:"easeOutExpo", duration:800});
			return false;
		});
	}
}

	var backgroundImageNormal = true;
	function rotateUpdateBackground(){
		if(backgroundImageNormal){
			$("#update_button").css("background-image", "url(/media/img/update_arrows_rotate.jpg)");
			backgroundImageNormal = false;
		} else {
			$("#update_button").css("background-image", "url(/media/img/update_arrows.jpg)");
			backgroundImageNormal = true;
		}
	}

var currImage = 0;
var prevImage = 0;

function initHome(){
	//inislideshow();
	if($("#slideshow_text").size()){
		$("#slideshow_text").hide();
	}
}

function initHomeAfter(){
	if($("#slideshow_text").size()){
		$("#slideshow_text").show();
		$("#slideshow_text span").css({"padding-top": ($("#slideshow_text").height() - $("#slideshow_text span").height()) / 2, "display":"block"});
	}
}

function initSlideshow(){
	if($("#slideshow").size()){
		$("#slideshow li").hide();
		$("#slideshow li:first-child").show();
		$("#slideshow li:first-child").css("opacity", "0");
		$("#slideshow li:first-child").animate({opacity:1}, 4000, 'easeOutExpo');
		var tmr = setInterval("showNextImage()", 6000);
	}
}

	function showNextImage(){
		prevImage = currImage;
		if(currImage < $("#slideshow li").size() - 1){
			currImage++;
		} else {
			currImage = 0;
		}
		$("#slideshow li:nth-child("+(prevImage + 1)+")").css("z-index", "1");
		
		$("#slideshow li:nth-child("+(currImage + 1)+")").css("opacity", "0");
		$("#slideshow li:nth-child("+(currImage + 1)+")").css("z-index", "2");
		$("#slideshow li:nth-child("+(currImage + 1)+")").show();
		$("#slideshow li:nth-child("+(currImage + 1)+")").animate({opacity:1}, 4000, 'easeOutExpo', function(){$("#slideshow li:nth-child("+(prevImage + 1)+")").hide();});
	}
	
function initMessage(){
	if($(".message").size()){
		$('.message').hide();
		$('.message').fadeIn(200);
		setTimeout("$('.message').fadeOut(2000)", 3000);
	}
}
	
function initAbout(){
	if($("#map_canvas").size()){
		initializeGmap();	
	}
}

	function initializeGmap() {
		var latlng = new google.maps.LatLng(-34.397, 150.644);
		var myOptions = {zoom: 14, center: latlng, mapTypeId: google.maps.MapTypeId.ROADMAP, mapTypeControl: false, navigationControl:true};
		map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
		codeAddress();
	}
	
	function codeAddress() {
		geocoder = new google.maps.Geocoder();
		var address = "Vlaamsekaai 57, 2000 Antwerpen";
		geocoder.geocode( { 'address': address}, function(results, status) {
			if (status == google.maps.GeocoderStatus.OK) {
				map.setCenter(results[0].geometry.location);
				var marker = new google.maps.Marker({
					map: map, 
					position: results[0].geometry.location
				});
			} else {
				alert("Geocode was not successful for the following reason: " + status);
			}
		});
	}
	
var isUpdating = false;
	
function initProducts(){
	if($("#products_list").size()){
		bindProductListEvents();
	 /*	$(window).scroll(function(){
	        if($(window).scrollTop() > $(document).height() - $(window).height() - 200){
	        	if($("#products_list li").size() < $("#num").text() && !isUpdating){
	        		isUpdating = true;
	        		$(".price").parent().find(".priceHover").remove();
	        		$("#products_list li").unbind("mouseenter");
	        		$("#products_list li").unbind("mouseleave");
	        		$.post("http://dev.espoo.be/nl/shop/temp", function(html){$("#products_list").append(html); bindProductListEvents(); isUpdating = false;});
	        		
	        	}
	        }
		}); */ 
		// MOVED TO views/espoo.php
	}
	if($("a.fullscreen").size()){
		$("a.fullscreen").click(function(){
			var arrURLS = new Array();
			var self = $(this);
			var teller = 0;
			var currentIndex = 0;
			$("a.fullscreen").each(function(){
				if(self.attr("href") == $(this).attr("href")){
					currentIndex = teller;
				}
				teller ++;
				arrURLS.push($(this).attr("href"));
			});
			$.fullscreenSlideshow(arrURLS, currentIndex);
			return false;
		});
	}
	if($("#product_slideshow_thumb").size()){
		$("#product_slideshow_big li").hide();
		$("#product_slideshow_big li:first-child").show();
		$("#product_slideshow_thumb li a").click(function(){
			$("#product_slideshow_big li").hide();
			$($(this).attr("href")).css("opacity", "0");
			$($(this).attr("href")).show();
			$($(this).attr("href")).animate({opacity:1},{duration:1000, easing:'easeOutExpo', queue:false});
			return false;
		});
	}
}

	function bindProductListEvents(){
		$(".price").each(function(){
			var pricehover = $("<span></span>").addClass("priceHover").html($(this).html());
			pricehover.hide();
			$(this).after(pricehover);
		});
		$("#products_list li").bind("mouseenter", function(){
			$(this).find(".priceHover").css("opacity", "0");
			$(this).find(".priceHover").show();
			$(this).find(".priceHover").stop();
			$(this).find(".priceHover").animate({opacity:1}, {duration:200, easing:'easeOutExpo', queue:false});
		});
		$("#products_list li").bind("mouseleave", function(){
			var self = $(this).find(".priceHover");
			$(this).find(".priceHover").stop();
			$(this).find(".priceHover").animate({opacity:0}, {duration:4000, easing:'easeOutExpo', queue:false, onComplete:function(){$(self).hide()}});
		});
		
		//just for the promos
		$(".pricepromo").each(function(){
      var pricehover = $("<span></span>").addClass("priceHoverPromo").html($(this).html());
      pricehover.hide();
      $(this).after(pricehover);
    });
    $("#products_list li").bind("mouseenter", function(){
      $(this).find(".priceHoverPromo").css("opacity", "0");
      $(this).find(".priceHoverPromo").show();
      $(this).find(".priceHoverPromo").stop();
      $(this).find(".priceHoverPromo").animate({opacity:1}, {duration:200, easing:'easeOutExpo', queue:false});
    });
    $("#products_list li").bind("mouseleave", function(){
      var self = $(this).find(".priceHover");
      $(this).find(".priceHoverPromo").stop();
      $(this).find(".priceHoverPromo").animate({opacity:0}, {duration:4000, easing:'easeOutExpo', queue:false, onComplete:function(){$(self).hide()}});
    });
	}

function initBillingInfo(){
	if($("#form_shipping_address").size()){
		if(!$("#different_shipping_address").is(":checked")){
			$("#form_shipping_address").parent().parent().find(".form_left").css("padding-top", "0px");
			$("#form_shipping_address").hide();
		}
		$("#different_shipping_address").change(function(){
			if(!$("#different_shipping_address").is(":checked")){
				$("#form_shipping_address").parent().parent().find(".form_left").css("padding-top", "0px");
				$("#form_shipping_address").hide();
			} else {
				$("#form_shipping_address").slideDown(200);
				$("#form_shipping_address").parent().parent().find(".form_left").animate({"padding-top": "150px"}, 200);
			}
		});
		if(!$("#customer_business").is(":checked")){
			$(".business_container").hide();
		}
		$("#subject_radios input").change(function(){
			if($("#subject_radios input:checked").val() == "private" ){
				$(".business_container").hide();
			} else {
				$(".business_container").slideDown();
			}
		});
	}
}

var currentPopupId;

var moveBlock = function(){
	var id = currentPopupId;
	$(id).css("top", (($(window).height() - $(id).outerHeight()) / 2) + $(document).scrollTop() + 20 + "px");
}

function initYourBag(){
	if($("#shipping_info, #payment_info").size()){
		$("#shipping_info, #payment_info").parent().hide();
		$(document).click(function(){
			$("#shipping_info, #payment_info").parent().hide();
			$(document).unbind("scroll", moveBlock);
		});
		$("#shipping_info_button, #payment_info_button").click(function(e){
			e.stopPropagation();
			$("#shipping_info, #payment_info").parent().hide();
			var id = $(this).attr("href");
			$(id).parent().height($(document).height());
			$(id).parent().fadeIn(200);
			$(id).css("top", (($(window).height() - $(id).outerHeight()) / 2) + $(document).scrollTop() + 20 + "px");
			$(id).css("left", (($(document).width() - $(id).outerWidth()) / 2) + "px");
			currentPopupId = id;
			$(document).bind("scroll", moveBlock);
			return false;
		});
	}
}

