/************************************************************************ 
 * Copyright 16th Street Ventures, LLC
 * ============================================= 
 *  Last edited by: $$Author: mqumhieh $$ 
 *              on: $$Date: 2011-12-13 10:02:33 $$ 
 *        Filename: $$RCSfile: home.js,v $$ 
 *        Revision: $$Revision: 1.24 $$ 
 * 
 */
var tempCheckedbox = new Array();
$(function() {
	/* Best restaurants container 
	----------------------------------------------------------------------------------------------------------------- */
	// Show datepicker
	$("#reservation input[name=d]").datepicker({ 
		showAnim: 'fadeIn',
		dateFormat: 'dd/mm/yy',
		minDate: 'dd/mm/yy',
		maxDate: '+6m'});
	
	// tabs on mouse click
	$("#best-venues-tabs div > div").live("click", function(){
		// Change its background image
		$("#best-venues-tabs .selected").removeClass("selected");
		$(this).parent().addClass("selected");
		// Show the right li content in best venues body
		$("#best-venues-body li").each(function(){
			// Hide the visible li
			if($(this).is(":visible")){
				$(this).hide();
				return false;
			}
		});
		// Show the correct li
		$("#best-venues-body li."+$(this).children("a").attr("id")).show();
	});
	
	/* Slideshow
	----------------------------------------------------------------------------------------------------------------- */
	// Hover venue name on SLIDESHOW
	$("#slideshow-container .info .header a").live("mouseover", function(){
		$(this).animate().stop();
		$(this).animate({
			color: "#ee0000"
		});
	});
	$("#slideshow-container .info .header a").live("mouseout",function(){
		$(this).animate().stop();
		$(this).animate({
			color: "#ffffff"
		});
	});
	
	// Start the slide show
	$("#slideshow-container").tabs({ fx: { opacity: 'toggle' } }).tabs('rotate', 10000, false);
	$("#slideshow-container").tabs('rotate', 0, false);
	
	var startTime,
		endTime, 
		result,
		tabCounter = 1,
		clicked = false;
	
	// Load slide show images
	loadImg();
	
	function loadImg(){
		// check if the image exist
		if($("#slide-img-"+ tabCounter).length != 0){
			var currentImg = $("#slide-img-" + tabCounter);
			if(currentImg.hasClass("loading")){
				currentImg.removeClass("loading");
				startTime = (new Date()).getTime();
				// after image loaded
				currentImg.attr("src", currentImg.attr("alsrc")).load(function(){
					currentImg.show();
					endTime = (new Date()).getTime();
					result = endTime - startTime;
					if(result < 10000){
						setTimeout(function(){
							if(!clicked){
								$("#slideshow-container").tabs({ fx: { opacity: 'toggle' } }).tabs('select', tabCounter++);
							}
							loadImg();
						}, 10000 - result);
					}else{
						if(!clicked){
							$("#slideshow-container").tabs({ fx: { opacity: 'toggle' } }).tabs('select', tabCounter++);
						}
						loadImg();
					}					
				});
			}else{
				tabCounter ++;
				loadImg();
			}
		}else{
			$("#slideshow-container .ui-tabs-panel").each(function(){
				$(this).css("background","#000");
			})
			setTimeout(function(){
				if(!clicked){
					$("#slideshow-container").tabs({ fx: { opacity: 'toggle' } }).tabs('rotate', 10000, false);
				}
			}, 1000);
		}
	}
	
	// click on specific image on slider
	$("#slideshow-container .ui-tabs-nav a").click(function(){
		clicked = true;
		var currentImg = $(""+ $(this).attr("href") +"").children("img");
		if(currentImg.hasClass("loading")){
			currentImg.removeClass("loading");
			currentImg.attr("src", currentImg.attr("alsrc")).load(function(){
				currentImg.fadeIn(500);
			});
		}
	});
	
	
	/* main search container
	----------------------------------------------------------------------------------------------------------------- */
	// search function
	$("#main-search").submit(function(){
		var time = getTimeObj($("#availability-container .ca-time").val());
		$(this.h).val(time.hour);
		$(this.m).val(time.minute);
		if ($("#advance-search-container").is(":hidden")){
			$("#advance-search-container input[name=cl]").removeAttr("name");
			$("#advance-search-container input[name=pl]").removeAttr("name");
			$("#advance-search-container input[name=nl]").removeAttr("name");
		} else {
			$("#search-criteria").removeAttr("name");
		}
	});
	
	// checkbox wrapping
	$(".checkbox-wrapping").live("click", function(){
		var checkboxEle = $(this).siblings("input[type=checkbox]");
		checkboxEle.trigger('change');
		if(checkboxEle.is(":checked")){
			checkboxEle.prop("checked", false);
		} else {
			checkboxEle.prop("checked", true);
		}
	});

	// show advanced
	$("#show-advance-search a").click(function(){
		if ($("#advance-search-container").is(":hidden")){
			$("#show-advance-search").removeClass("inactive").addClass("active");
			$("#search-criteria").attr("disabled", "disabled");
			$("#search-criteria").addClass("disable-quick-search");
			$("#advance-search-container").slideDown(500);
		} else {
			$("#advance-search-container").slideUp(500, function(){
				$("#show-advance-search").removeClass("active").addClass("inactive");
				$("#search-criteria").removeAttr("disabled"); 
				$("#search-criteria").removeClass("disable-quick-search");
			});
		}
	});
	
	// see all cuisines/neighrbohoods
	$("#advance-search-container .see-all").live('click', function(){
		tempCheckedbox = [];
		var eleName = $(this).attr("name");
		// filling checked boxes into temp cache
		$("#"+ eleName +" input[type=checkbox]").each(function(){
			if($(this).is(":checked")){
				tempCheckedbox.push($(this).attr("value"));
			}
		});
		$("#loading-div").fadeIn(500 ,function(){
			$("#all-ele-wrapper").show();
			$("#all-"+ eleName).show();
		});
	});
	// submit all ele wrapper div
	$(".all-ele-wrapper .search-button").click(function(){
		var mainParent = $(this).closest(".all-ele-wrapper").attr("id"),
			checkboxName = $(this).prev("input[type=hidden]").val(),
			counter = 0,
			layout = "";
		$("#" + mainParent + " input:checked").each(function(){
			if (counter > 4){
				layout += "<li class=\"hidden\"><input type=\"checkbox\" name=\""+ checkboxName +"\" checked=\"true\" value=\""+ $(this).val() +"\" /><span class=\"checkbox-wrapping\">"+ $(this).val() +"</span></li>";
			} else {
				layout += "<li><input type=\"checkbox\" checked=\"true\" name=\""+ checkboxName +"\" value=\""+ $(this).val() +"\" /><span class=\"checkbox-wrapping\">"+ $(this).val() +"</span></li>";
			}
			counter++;
		});
		if (counter < 5) {
			$("#" + mainParent + " input:not(:checked)").each(function(){
				if ( counter > 4) {return false;}
				layout += "<li><input type=\"checkbox\" name=\""+ checkboxName +"\" value=\""+ $(this).val() +"\" /><span class=\"checkbox-wrapping\">"+ $(this).val() +"</span></li>";
				counter++;
			});
		}
		layout += "<li><a class=\"see-all\" name=\""+ $(this).attr("name") +"\">see all</a></li>";
		$("#" + $(this).attr("name") + " li").remove();
		$("#" + $(this).attr("name")).append(layout);
		// hide container
		$("#" + mainParent).hide();
		$("#loading-div").fadeOut(500);
		
	});
	// close all cuisines/neighborhoods
	$(".all-ele-wrapper .close").click(function(){
		var eleName = $(this).attr("name");
		$("#" + eleName).hide();
		$("#loading-div").fadeOut(500);
		$("#"+ eleName +" li input[type=checkbox]").attr("checked",false);
		for(var i = 0; i < tempCheckedbox.length; i++){
			$("#"+ eleName +" li input[value=\""+ tempCheckedbox[i] +"\"]").attr("checked",true);
		}
	});
	// normal check box select
	$("#advance-search-container input[type=checkbox]").live("change", function(){
		var parName = $(this).closest("ul").attr("id");
		if ($("#"+ parName + " .see-all").length > 0){
			$("#all-" + parName + " input[value=\""+ $(this).val() +"\"]").attr('checked', !$(this).is(':checked'));
		}
		// check if there is hidden <li>
		var closestChildren = $(this).closest("ul").children(".hidden");
		if (closestChildren.length > 0){
			$(this).parent("li").remove();
			$(closestChildren.get(0)).removeClass("hidden");
		}
	});
});


