// JavaScript Document//clear text box on click
if (baseurl=='' || baseurl== null || baseurl== 'undefined') {
	var baseurl = window.location.protocol + '//' + window.location.host +'/2/reading';
}

function clearText(field){

	if (field.defaultValue == field.value) field.value = '';
	else if (field.value == '') field.value = field.defaultValue;

}

// CUFON
Cufon.replace('.fut-font', {  hover: true });
Cufon.replace('h1')('h2')('h3')('h4');
Cufon.replace('a.fut-nav-shadow', { hover: true, textShadow: '#312f2f 1px 1px'});


$(document).ready(function(){
	$('ul.nav').superfish({delay: 400}); 
	$('.styled').selectbox();

	// Carousel
	
	var selectors = [
		'.movieWrap .carouselWrap',
		'.movieWrap .carouselWrap2'
	];

	var selectorCount = selectors.length;

	
	el = $('.movieWrap .carouselWrap');
	if(el.size() > 0 && $('ul li', el).size() > 0) {
		el.jCarouselLite({
			btnNext: ".next",
			btnPrev: ".prev",
			circular: true,
			visible:1
		});
	}
	el = $('.movieWrap .carouselWrap2');
	if(el.size() > 0 && $('ul li', el).size() > 0) {
		el.jCarouselLite({
			btnNext: ".next2",
			btnPrev: ".prev2",
			circular: true,
			visible:1
		});
	}
	
	
	if($('.trailer-thumb .carouselWrap').size() && $('ul li', el).size() > 3) {
		$('.trailer-thumb .carouselWrap').jCarouselLite({
			btnNext: ".next",
			btnPrev: ".prev",
			circular: true,
			visible: 3
		})
	}

	if($('.movieWrap5 .carouselWrap').size()) {
		$('.movieWrap5 .carouselWrap').jCarouselLite({
			btnNext: ".next",
			btnPrev: ".prev",
			circular: true,

			visible: 5
		});
	}



	// Scheduled Movies Tabs
	/*
	$(".tab_content").hide(); //Hide all content
	$("ul.tabs li:first").addClass("active").show(); //Activate first tab
	$(".tab_content:first").show(); //Show first tab content
	$("ul.tabs li").click(function() {
		$("ul.tabs li").removeClass("active"); //Remove any "active" class
		$(this).addClass("active"); //Add "active" class to selected tab
		$(".tab_content").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(); //Fade in the active content
		return false;
	});
	*/


	// Features Tabs
	// Removed, as the "Features" are now on separate pages.
	var tab;
	   $(".theatre-details .feat-tab_content").hide(); //Hide all content
	   $(".theatre-details ul.features-tab li:first").addClass("active").show(); //Activate first tab
	   $(".theatre-details .cinema-features-cont > div:first").show(); //Show first tab content
	   $(".theatre-details ul.features-tab li").click(function() {
		   var activeTab = $(this).find("a").attr("href"); //Find the rel attribute value to identify the active tab + content
		   if(!$(activeTab).is(':visible')) {
			   $(".theatre-details ul.features-tab li").removeClass("active"); //Remove any "active" class
			   $(".theatre-details .cinema-features-cont > div").hide(); //Hide all tab content
			   $(activeTab).fadeIn(); //Fade in the active content
		   }
			$(this).addClass("active"); //Add "active" class to selected tab
		   return false;
	   });

	//toggle payment tag
	$("#ccard_select_btn").click(function() {
		$('#ccard_select_btn').addClass("active");
		$('#paypal_select_btn, #givex_select_btn').removeClass("active");
		$('#paypal_pay_block').hide();
		$('#credit_pay_block').show();
		$('#givex_pay_block').hide();
	});
	$("#paypal_select_btn").click(function() {
		$('#paypal_select_btn').addClass("active");
		$('#ccard_select_btn, #givex_select_btn').removeClass("active");
		$('#credit_pay_block').hide(); 
		$('#paypal_pay_block').show();
		$('#givex_pay_block').hide();
	});
	$("#givex_select_btn").click(function() {
		$('#givex_select_btn').addClass("active");
		$('#ccard_select_btn, #paypal_select_btn').removeClass("active");
		$('#credit_pay_block').hide(); 
		$('#paypal_pay_block').hide();
		$('#givex_pay_block').show();
	});

	// Cinema Location Session Times Tabs
	$(".sessiontimes-tab_content").hide(); //Hide all content
	$("ul.session-times-tab li:first").addClass("active").show(); //Activate first tab
	$(".sessiontimes-tab_content:first").show(); //Show first tab content
	$("ul.session-times-tab li").click(function() {
		$("ul.session-times-tab li").removeClass("active"); //Remove any "active" class
		$(this).addClass("active"); //Add "active" class to selected tab
		$(".sessiontimes-tab_content").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(); //Fade in the active content
		return false;
	});

	$('#cinemasection_container').live('change',function(){});

	if($('#slider').size() > 0) {
		$('#slider').nivoSlider({
			effect:'random', //Specify sets like: 'fold,fade,sliceDown'
			slices:10,
			animSpeed:500, //Slide transition speed
			pauseTime:5000,
			startSlide:0, //Set starting Slide (0 index)
				directionNav:true, //Next & Prev
			directionNavHide:true, //Only show on hover
			controlNav:true, //1,2,3...
			afterLoad: function(){ Cufon.refresh(); $('.nivo-caption h2').show(); }, 
			afterChange: function(){ Cufon.refresh(); $('.nivo-caption h2').show(); }
		});
	}

});

$(function(){

	if( $('#my-dropdown,#my-dropdown2, #my-dropdown3, #my-dropdown4').size() > 0) {
		$('#my-dropdown, #my-dropdown2, #my-dropdown3, #my-dropdown4').sSelect();
	}

	//for location only select
	if( $('#my-dropdown1').size() > 0) {
		$('#my-dropdown1').sSelect({defaultText:'Select a Cinema Location'});
	}

	//set max height
	//$('#my-dropdownCountries').sSelect({ddMaxHeight: '300px'});

	//set value on click
	//$('#setVal').click(function(){
	//$('#my-dropdown5').getSetSSValue('4');
	//});

	//get value on click
	//$('#getVal').click(function(){
	//alert('The value is: '+$('#my-dropdown5').getSetSSValue());
	//});

	//alert change event
	$('#my-dropdown').change(function(){	
		showallmovies();
	});

	$('#my-dropdown2').change(function(){
		showSessionTime();
	});

	$('#my-dropdown3').change(function(){
		showTime();
	});


	//glod lounge private booking form validation
	$('input#xxb').click(function(){
		var flag = true;
		var fields = '';
		/////////////////
		if($('select#my-dropdown1').val() == '' || $('div.selectedTxt').html() == 'Select a Cinema Location')
	{
		$('div#mydrp1').addClass('error-sel');
		flag = false;
		fields = '<li>Cinema Location</li>';
	}
		else
	{
		$('div#mydrp1').removeClass('error-sel');
	}
	////////////////////
	if($('input[name="fn"]').val() == '')
	{
		$('input[name="fn"]').addClass('textbox-error');
		if($('input[name="fn"]').parent().find('div.error-icon').length == 0)
	{
		$('input[name="fn"]').parent().append('<div class="error-icon"></div>');
	}
	flag = false;
	fields += '<li>First name</li>';
	}
	else
	{
		$('input[name="fn"]').removeClass('textbox-error');
		$('input[name="fn"]').parent().find('div.error-icon').remove();
	}
	///////////////////////
	if($('input[name="ln"]').val() == '')
	{
		$('input[name="ln"]').addClass('textbox-error');
		if($('input[name="ln"]').parent().find('div.error-icon').length == 0)
		{
			$('input[name="ln"]').parent().append('<div class="error-icon"></div>');
		}
		flag = false;
		fields += '<li>Last name</li>';
	}
	else
	{
		$('input[name="ln"]').removeClass('textbox-error');
		$('input[name="ln"]').parent().find('div.error-icon').remove();
	}
	////////////////////////
	if($('input[name="pc"]').val() == '')
	{
		$('input[name="pc"]').addClass('textbox-error');
		if($('input[name="pc"]').parent().find('div.error-icon').length == 0)
		{
			$('input[name="pc"]').parent().append('<div class="error-icon"></div>');
		}
		flag = false;
		fields += '<li>Postcode</li>';
	}
	else
	{
		$('input[name="pc"]').removeClass('textbox-error');
		$('input[name="pc"]').parent().find('div.error-icon').remove();
	}
	///////////////////////
	if($('input[name="cn"]').val() == '')
	{
		$('input[name="cn"]').addClass('textbox-error');
		if($('input[name="cn"]').parent().find('div.error-icon').length == 0)
		{
			$('input[name="cn"]').parent().append('<div class="error-icon"></div>');
		}
		flag = false;
		fields += '<li>Contact number</li>';
	}
	else
	{
		$('input[name="cn"]').removeClass('textbox-error');
		$('input[name="cn"]').parent().find('div.error-icon').remove();
	}

	//////////////////////
	if($('input[name="em"]').val() == '')
	{
		$('input[name="em"]').addClass('textbox-error');
		if($('input[name="em"]').parent().find('div.error-icon').length == 0)
		{	
			$('input[name="em"]').parent().append('<div class="error-icon"></div>');
		}
		flag = false;
		fields += '<li>Email address</li>';
	}
	else
	{
		var regex = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		if(regex.test($('input[name="em"]').val()))
		{
			$('input[name="em"]').removeClass('error-sel');
			$('input[name="em"]').parent().find('div.error-icon').remove();
		}
		else
		{
			$('input[name="cn"]').removeClass('textbox-error');
			if($('input[name="em"]').parent().find('div.error-icon').length == 0)
			{	
				$('input[name="em"]').parent().append('<div class="error-icon"></div>');
			}
			flag = false;
		}
	}

	//////////////////////
	if($('input[name="og"]').val() == '')
	{
		$('input[name="og"]').addClass('textbox-error');
		if($('input[name="og"]').parent().find('div.error-icon').length == 0)
		{
			$('input[name="og"]').parent().append('<div class="error-icon"></div>');
		}
		flag = false;
		fields += '<li>Organization/Group</li>';
	}
	else
	{
		$('input[name="og"]').removeClass('textbox-error');
		$('input[name="og"]').parent().find('div.error-icon').remove();
	}
	//////////////////////
	if($('#captcha').val() == '')
	{
		$('#captcha').addClass('textbox-error');
		if($('#captcha').parent().find('div.error-icon').length == 0)
		{
			$('#captcha').parent().append('<div class="error-icon"></div>');
		}
		flag = false;
		fields += '<li>Captcha</li>';
	}
	else
	{
		$('#captcha').removeClass('textbox-error');
		$('#captcha').parent().find('div.error-icon').remove();
	}

	if(flag == false)
	{
		showPopup('<span style="color:red">Required Field(s)</span>','<p>Please fill all the required fields and click submit.</p><br><div id="req-fields"><ul>'+fields+'</ul></div>');
		return false;
	}
	else
	{
		$('form#cpb').submit();
	}
	});

	//add options to select and update
	//$('#addOptions').click(function(){
	//$('#my-dropdown6').append('<option value="newOpt">New Option</option>').resetSS();
	//return false;
	//});


});

function showallmovies(){
	var theatreid = $('#my-dropdown').val();

	$.getJSON(baseurl +'/expressticket/showMovie/'+theatreid, 
			function(data) {
				var moviesublist = "";
				$.each(data.movies, function(i,movie){
					moviesublist = moviesublist + '<option value="'+movie.Id+'">' + movie.Title + '</option>';
				});
				$('#my-dropdown2').html(moviesublist).resetSS();
			});
}


//e-ticket javascript

function showSessionTime(){
	var threatreid = $('#my-dropdown').val();
	var movieid = $('#my-dropdown2').val();
	var dateurl = baseurl + "/expressticket/showSessionTime/"+ threatreid + "/" + movieid;    	

	$.getJSON(dateurl, 
			function(data) {
				var datesublist = "";
				console.log(data);
				$.each(data.date, function(i,mydate){
					datesublist = datesublist + '<option value="' + mydate.mydate + '">' + mydate.mydatevalue + '</option>';
				});

				$('#my-dropdown3').html(datesublist).resetSS();
			});
}

function showTime(datevalue){
	var threatreid = $('#my-dropdown').val();
	var movieid = $('#my-dropdown2').val();

	var datevalue = $('#my-dropdown3').val();

	var timeurl = baseurl + "/expressticket/showBySessionTimeByDate/"+ threatreid + "/"+movieid+"/"+ datevalue;

	$.getJSON(timeurl, 
			function(data) {
				var timesublist = "";
				$.each(data.time, function(i,mytime){
					timesublist = timesublist + '<option value="' + mytime.mytime + '">' + mytime.mytimevalue + '</option>';
				});
				$('#my-dropdown4').html(timesublist).resetSS();
			});
}

function expressSubmit(){
	var theatre = $('#my-dropdown').val();
	var movieselect = $('#my-dropdown2').val();
	var sessiondate = $('#my-dropdown3').val();
	var sessiontime = $('#my-dropdown4').val();

	if(theatre != "" && movieselect != "" && sessiondate != "" && sessiontime != ""){

		$('#expressform').submit();
	}
	else if(theatre == ""){

		window.location.replace(baseurl + "session_times/locations/");

	}else if(movieselect == ""){
		
		window.location.replace(baseurl + "session_times/movies/");
	}
	else if(sessiondate == "" || sessiontime == ""){
		
		window.location.replace(baseurl + "session_times/time");
	}	
}

//eticket step 3 payment arrangement.
function showSection(id){

	var numberid = id;

	if(id == "paysection1"){
		$('#payformsection1').show();
		$('#payformsection2').hide();
		$('#payformsection3').hide();
	}
	if(id == "paysection2"){
		$('#payformsection2').show();
		$('#payformsection1').hide();
		$('#payformsection3').hide();
	}
	if(id == "paysection3"){
		$('#payformsection3').show();
		$('#payformsection2').hide();
		$('#payformsection1').hide();
	}

}

//session time javascript for submiting to E-tix



//search movie
function searchMovie(){
	var location = $('#selectmovie').val();
	var title = $('#searchtitle').val();

	if(title == "Movie title..."){
		title = "";
	}
	$('#movielist').fadeOut('fast').html('<h2>Searching...</h2>').fadeIn('fast');

	$.ajax({
		url: "./movies/searchBylocationAndTitle/"+location+"/"+title,
		type: "GET",
		error:function(){
			$('#movielist').fadeOut('fast');
			$('#movielist').html('Oops! something went wrong..');
		    $('#movielist').fadeIn('fast');
			return false;
		},	  
		success: function (repsonText) {
			 $('#movielist').fadeOut('fast', function() {;
			 $('#movielist').html(repsonText);
			 $('#movielist').fadeIn()});
		 }
	});
}

function showFeatureSessions(theatre, feature, date) {
	
	$('#sessiontime').show();
	$('#stab').show().addClass('load-spinner').html('');

	$.ajax({
		url: './cinemafeature/get_theatre_sessions/' + theatre  + '/' + feature + '/' +  date,
		type: 'POST',
		dataType: 'html',
		success: function (data) {
			$('#stab').removeClass('load-spinner').html(data);
		}
	});
	return false;	

}

// TIPSY TOOLTIP RED
$(function() {
    $('.tooltip-sw').tipsy({gravity: 'sw'});
	$("a.inline_lightbox").fancybox({"showCloseButton":true,"onStart": function(){}});
});

function shareURL(url) {
	$('#fb_share_link').attr('href', 'http://www.facebook.com/share.php?u='+encodeURIComponent(url));	
	$('#twitter_share_link').attr('href', 'https://twitter.com/share?source=tweetbutton&text=Reading%20Cinema&url='+encodeURIComponent(url));
}

$(document).ready(function(){
    $('a.inline_lightbox').live('mouseenter', function(){
        $(this).fancybox({'type':'inline'});
    });
});

function showPopup(title, content) {
	$('#general_popup .overlay-title').html(title);
	$('#general_popup .overlay-content').html(content);
	$('#popup_link').click();
}
