// JavaScript Document
$(document).ready(function(){						   
	
	
	// shadow
	
/*	var maxHeight = $("#wrap").height();
	console.log(maxHeight);
	$(".shadow").css("height",maxHeight+"px");*/
	$(".changePressLink").remove();
	$("#press-room #cont-press #addStory").remove();
	// CHROME & SAFARI
	
	if($.browser.webkit || $.browser.safari)
    {
        $('#promo-reg #registration form#register table tr td').css('padding-top','2px');
        $('#standar-main .tab-content #registration form#register table tr td').css('padding-top','2px');     
    }
	
	// opacity INTERNET EXPLORER
       	if ( $.browser.msie ) {
			$('.black-screen').css('opacity','0.7');
		}
	
	/****************************************************************
							     INDEX
	*****************************************************************/
	
	// video - promo
	
	$("#promo-reg #watch-promo").click(function(){
		$(".black-screen").fadeIn();
		$(".screen-home").fadeIn();
		$("#popup-home").fadeIn();
	});
	
	// register form
	var $td = $('form#register tr td:odd');
	$td.addClass('resalt');
	
	// Register form
	var $form = $('#birthday');
	
	// add class 'current'
	
	var pageClass = $('#wrap').attr('class');
	//alert (pageClass);
	$("#wrap ul#nav #"+pageClass).addClass('current');
	
	//Cufon.refresh();
	
	/****************************************************************
							   TAB NAVIGATION
	*****************************************************************/
	// show 
	//alert (location.hash);
	
	var hash = location.hash.slice(1);
	if (hash != ""){
		var currentTabID = "tab-"+hash;
		var currentContID = hash;
	}else{
		if(pageClass != "home" && pageClass != "room" && pageClass != "franchisse-info")
		{
		var currentTabID = $("#nav-content").children().attr("id");
		var currentContID = currentTabID.slice(4);
		}
	}
	//alert ($currentTabID);
	
	$("#"+currentTabID).addClass('current')
	$("#"+currentContID).show();
	
	$('.tab-nav').click(function(){
		// remove class current
		var $tabCurrent = $('#nav-content').find('.current');
		$tabCurrent.removeClass("current");
		// add class to a new current
		$(this).addClass("current");
		// id new currrent content
		var $newContent = $(this).attr("id").slice(4);
		$oldContent = $tabCurrent.attr("id").slice(4);
		$("#"+$oldContent).hide();		
		$("#"+$newContent).fadeIn();
		location.hash = "#"+$newContent;
		
		// change height border shadow
		
	   	$(".shadow").css("height",0+"px");        
	   	var maxHeight = $(document).height();
	   	$(".shadow").css("height",maxHeight+"px");
	});
	
	// Goto internal links
	
	$(".goto").live("click", function(){
		var goto = $(this).attr("id");
		goto = goto.slice(5);
		var tab = "#tab-"+goto;
		$(tab).addClass("current");	
		var newContent = "#"+goto;
		//$oldContent = $oldCurrent.attr("id").slice(4);
		var hash = location.hash.slice(1);
		$("#tab-"+hash).removeClass("current");
		$("#"+hash).hide();
		$(newContent).fadeIn();
		location.hash = "#"+newContent;
	});
	
	/****************************************************************
					     POPUPS VIDEOS & IMAGES
	*****************************************************************/
	
	$("li.show-popup a, ul.img-list li img").click(function(){
		var id = $(this).parent().attr("id");
		//alert (id);
		$(".screen").fadeIn();
		$("#popup-"+id).fadeIn();
	});
	
	// close popup

	$("#close-BS").click(function(){
		$(".popup > li").fadeOut();
		$(".screen").fadeOut();
	});
	
	$("#close-BS-home").click(function(){
		$("#popup-home").hide();
		$(".screen").fadeOut();
	});
	
	// GALLERY - FRAN OPP. Bigger is better
	
	$(".img-gallery li img, .img-gallery li p").click(function(){
		var $parent = $(this).parent();
		var parentID = $parent.attr("id");
		//alert (parentID);
		$(".screen").fadeIn();
		
		var popupID = "#popup-"+parentID;
		
		var widthPopUp = $(popupID + " img").width() /*- 22 for padding 10px left and right AND borders*/;
		var heightPopUp = $(popupID + " img").height() - 22 /* for padding 10px top and bottom AND boders*/;
		//$parent.css("width",widthPopUp+"px");
		//widthPopUp = (widthPopUp / 2);
		$(".details div.tab-details").css("width",widthPopUp+"px");
		var newWidth = (widthPopUp/2) - 20;
		$(".details div.tab-details ul").css("width",newWidth+"px");
		//$(".details div.tab-details").css("min-height",heightPopUp+"px");
		$(".details div.tab-details ul").css("height",heightPopUp+"px");
		//alert("anocho: "+widthPopUp+" alto: "+heightPopUp);
		$("#popup-"+parentID).fadeIn();
	});
	
	$(".more-details").click(function(){
		//alert ("a");
		$(this).parent().parent().find(".tab-details").fadeIn();
		//$(this).hide();
	});

	$(".back-to-image").click(function(){
		//alert ("a");
		$(this).parent().parent().find(".more-details").show();
		$(this).parent().fadeOut();
	});
		
	// Remove default events
	
	$(".tab-nav a, li.show-popup a, #popupterms, #popuppolicy, #watch-promo").click(function(event){
		event.preventDefault();	
	});
	
	// Popups terms & conditions
	
	$("#popupterms, #popuppolicy").click(function(){
		var id = $(this).attr("id");
		//alert (id);
		$(".black-screen, .screen-terms").fadeIn();
		$("."+id).fadeIn();
	});
	
	
	$(".closepopup").click(function(){
		$(".black-screen, .popupterms, .popuppolicy, .screen-terms").fadeOut();
	});	
	
	/********************************************************
								AJAX 
	********************************************************/
	
	$("#register").submit(function(event){
		event.preventDefault(); 
		if ($(this).find("#control").val() == "error"){
			var control = validate();
			if (control){
				$(this).find("#control").val("ok");
				var data = $(this).serialize();
				var url = $(this).attr("action");
				ajaxRegister(url,data);
			}else{
				alert ("missing data");	
			}
		}
		/*if ($(this).children().hasClass(".error")){
			alert ("error");
		}else{
			var data = $(this).serialize();
			var url = $(this).attr("action");
			ajaxRegister(url,data);
		}		*/
	});
		
	function ajaxRegister(url, data){
		$.ajax({
		   type: "POST",
		   url: url,
		   data: data,
		   success: function(msg){
			   //console.log(msg);
			   alert("the request has completed successfully");
		   }
		});
	}
	
	/********************************************************
							VALIDATE 
	********************************************************/
	
	function validate(){
		/*var name = $this.find("#name").val();
		var phone = $this.find("#phone").val();
		var email = $this.find("#email").val();
		var zipcode = $this.find("#zipcode").val();
		var guest = $this.find("#guest").val();*/
		
		//alert (name);
		//$("")
		/*validateString(name);
		validatePhone(phone);
		validateEmail(email);
		validateZipcode(zipcode);
		validateString(guest);*/
		
		var inputArray = $("#register input:text");
		var control = true;
		for (var i = 0 ; i < inputArray.length ; i++){
			if (inputArray[i].value == ""){
				$("#"+inputArray[i].name).css("background-color","#FFE703");
				control = false;
			}else{
				$("#"+inputArray[i].name).css("background-color","#FFF");
			}
		}
		return control;
		//alert (inputArray(0).val());
	}
	
	$("#why-this-industry").click(function(){
		//alert ($(this).html());
	});
	
	/********************************************************
							HEADER SLIDESHOW
	********************************************************/
	
/*	function slideSwitch() {
		var $active = $('#slideshow img.active');
	
		if ( $active.length == 0 ) $active = $('#slideshow img:last');
	
		// use this to pull the images in the order they appear in the markup
		var $next =  $active.next().length ? $active.next()
			: $('#slideshow img:first');
	
		// uncomment the 3 lines below to pull the images in random order
		
		// var $sibs  = $active.siblings();
		// var rndNum = Math.floor(Math.random() * $sibs.length );
		// var $next  = $( $sibs[ rndNum ] );
		console.log("in");
	
		$active.addClass('last-active');
	
		$next.css({opacity: 0.0})
			.addClass('active')
			.animate({opacity: 1.0}, 1000, function() {
				$active.removeClass('active last-active');
			});
	};
	
	setInterval( "slideSwitch()", 5000 );*/
	
	/********************************************************
							PRESS ROOM
	********************************************************/
	
	//$("#press-room #cont-press ul#press-list li:even").addClass("back-opacity");
	$("#press-room #cont-press ul#press-list li").removeClass("back-opacity");
	$("#press-room #cont-press ul#press-list li").each(function (index) {
		if (index % 2 == 0) $(this).addClass("back-opacity");
	});
});
