$(document).ready(function(){    
    
	//to on change drop down options for movies and dates
	$('.autopop').change(function(){
		var id = $(this).attr("id");
		// checking not null
		var cityId = $("#"+id).val();
		var id_flg = $(this).attr("alt");
		homeSearchDropdowns(id,cityId,id_flg,'');
		
	});
	
	//start script for search movies by movie or theatre tab
	//Default Action for featured 
	$(".selectmenu").hide(); //Hide all content
	$("div.heading ul li:first").addClass("current").show(); //Activate first tab
	$(".selectmenu:first").show(); //Show first tab content
	
	//On Click Event
	$("div.heading li").click(function() {
		$("div.heading li").removeClass("current"); //Remove any "active" class
		$(this).addClass("current"); //Add "active" class to selected tab
		$(".selectmenu").hide(); //Hide all tab content
		var activeTab = $(this).find("a").attr("href"); //Find the rel attribute value to identify the active tab + content
		
		$(activeTab).fadeIn('slow'); //Fade in the active content
		return false;
	});
	//end script for search movies by movie or theatre tab
	
	//start script for now running and cominig soon tab
	//Default Action for featured 
	$(".now_cont").hide(); //Hide all content
	$("div.nav_bottom ul li:first").addClass("current").show(); //Activate first tab
	$(".now_cont:first").show(); //Show first tab content
	
	//On Click Event
	$("div.nav_bottom li").click(function() {
		$("div.nav_bottom li").removeClass("current"); //Remove any "active" class
		$(this).addClass("current"); //Add "active" class to selected tab
		var img_flg =$(this).attr("rel");
		if(img_flg[0] =='n'){
			var img_src =$("#img"+img_flg).attr("rel");
			$("#img"+img_flg).attr("src",img_src);
		}else{
			var img_src =$("#img"+img_flg).attr("rel");
			$("#img"+img_flg).attr("src",img_src);
		}
		$(".now_cont").hide(); //Hide all tab content
		$(".coming_cont").hide(); //Hide all tab content
		var activeTab = $(this).find("a").attr("href"); //Find the rel attribute value to identify the active tab + content
		
		$(activeTab).fadeIn('slow'); //Fade in the active content
		return false;
	});
	
	//Default Action for featured tabs
	$(".nowplaying").hide(); //Hide all content
	$("ul.featured li:first").addClass("current").show(); //Activate first tab
	$(".nowplaying:first").show(); //Show first tab content
	
	//On Click Event
	$("ul.featured li").click(function() {
		$("ul.featured li").removeClass("current"); //Remove any "active" class
		$(this).addClass("current"); //Add "active" class to selected tab
		if($(this).attr("id")=='comingfeature'){
			$(".now_cont").hide(); //Hide all content
			$(".coming_cont").hide(); //Hide all content
			$(".coming_cont:first").show(); //Show first tab content
		}
		else{
			$(".coming_cont").hide(); //Hide all content
			$(".now_cont").hide(); //Hide all content
			$(".now_cont:first").show(); //Show first tab content
		}
		$(".nowplaying").hide(); //Hide all tab content
		var activeTb = $(this).find("a").attr("href"); //Find the rel attribute value to identify the active tab + content
		$(activeTb).fadeIn('slow'); //Fade in the active content
		return false;
	});
	
	//end script for now running and cominig soon tab
});


function submitMovieSearch(frm){
	if(frm.cityId.value == ""){
		alert("Please select city");
		frm.cityId.focus();
		return false;
	}
	if(frm.movieId.value == ""){
		alert("Please select movie");
		frm.movieId.focus();
		return false;
	}
	if(frm.sel_date.value == ""){
		alert("Please select date");
		frm.sel_date.focus();
		return false;
	}
	frm.submit();
}

function submitTheatreSearch(frm){
	if(frm.cityId.value == ""){
		alert("Please select city");
		frm.cityId.focus();
		return false;
	}
	if(frm.theatreId.value == ""){
		alert("Please select theatre");
		frm.theatreId.focus();
		return false;
	}
	if(frm.selT_date.value == ""){
		alert("Please select date");
		frm.selT_date.focus();
		return false;
	}
	frm.submit();
}

function homeSearchDropdowns(id,cityId,id_flg,selected){		
		if(id=='cityId' || id=='TcityId'){
			
			if(id_flg=="T"){
				$("#cityId").val(cityId);
				var dmovieId="theatreId";
				var dsel_date="selT_date";
				var dates="datesT";
				var movie="theatre";
			}
			else{
				$("#TcityId").val(cityId);
				var dmovieId="movieId";
				var dsel_date="sel_date";
				var dates="dates";
				var movie="movie";
				
			}
			
			var obj = document.getElementById('movieId');
			var obj2 = document.getElementById('theatreId');
			if(cityId != '' && cityId != 0){
			$("select#movieId option").remove();
			$("select#movieId optgroup").remove();
			$("#movieId").attr("disabled","disabled");
			$("select#theatreId option").remove();
			$("#theatreId").attr("disabled","disabled");			
			obj.options[obj.options.length] = new Option('Loading...','',true,true);
			obj2.options[obj2.options.length] = new Option('Loading...','',true,true);
			var post_string = "cityId=" + cityId+"&flag=movies&flag2=theatres&"+movie+"="+selected;
			$.ajax({
				type: "GET",
		        data: post_string,
		        dataType: "text",
				cache: true,  
		        url: siteURL + 'scripts/search_serv_new.php',
		        timeout: 60000,
		        error: function() {
		        	//alert("Failed to submit");
		        },
		        success: function(data) {		         
			       $("select#movieId option").remove();
			       $("select#movieId optgroup").remove();
			      
			       $("select#sel_date option").remove();
			       $("<option value=''>Select Date</option> ").appendTo("select#sel_date");
			        $("select#selT_date option").remove();
			       $("<option value=''>Select Date</option> ").appendTo("select#selT_date");
			       
			       $("#movieId").attr("disabled","");
			       $("select#theatreId option").remove();
			       $("#theatreId").attr("disabled","");
			       eval(data);
		        }
			});
			if(selected!=''){
				var obj3 = document.getElementById(dsel_date);
			}
		}// checking not null
		else{
			$("select#movieId optgroup").remove();
			$("select#movieId option").remove();
			$("select#theatreId option").remove();
			$("<option value=''>Select Movie</option> ").appendTo("select#movieId");
			$("<option value=''>Select Theatre</option> ").appendTo("select#theatreId");
			$("select#sel_date option").remove();
			$("<option value=''>Select Date</option> ").appendTo("select#sel_date");
		}
	}// end of city
	else if(id=='movieId' || id=='theatreId'){
			if(id=='theatreId'){
				var sel_date='selT_date';
				var cityId = $("#TcityId").val();
				var flg_d= "datesT";
				var th_flg="theatreId";
			}
			else{
				var sel_date='sel_date';
				var cityId = $("#cityId").val();
				var flg_d= "dates";
				var th_flg="movieId";
			}
			var movieId = $("#"+id).val();
		if(movieId != '' && movieId != 0){
			getDatesJquery(cityId,th_flg,movieId,sel_date,flg_d);
			
		}// checking not null
		else{
			$("select#"+sel_date+" option").remove();
			$("<option value=''>Select Date</option> ").appendTo("select#"+sel_date);
		}
	}
}
function getDatesJquery(cityId,th_flg,movieId,sel_date,flg_d){

	
	var obj3 = document.getElementById(sel_date);
	$("select#"+sel_date+" option").remove();
	$("#"+sel_date).attr("disabled","disabled");			
	obj3.options[obj3.options.length] = new Option('Loading...','',true,true);
	var post_string = "cityId=" + cityId+"&"+th_flg+"=" + movieId+"&flag="+flg_d;
	$.ajax({
		type: "GET",
        data: post_string,
        dataType: "text",
		cache: true,  
        url: siteURL + 'scripts/search_serv_new.php',
        timeout: 60000,
        error: function() {
        	//alert("Failed to submit");
        },
        success: function(data) {		         
       $("select#"+sel_date+" option").remove();
       $("#"+sel_date).attr("disabled","");
        eval(data);
        }
	});
}
function ChangeTable(id)
{
	if(id==1){
	document.getElementById('movie_search_table').style.display='';
	document.getElementById('theatre_search_table').style.display='none';
	}else {
	document.getElementById('movie_search_table').style.display='none';
	document.getElementById('theatre_search_table').style.display='';
	}
}
		

