$(document).ready(function(){
	$('table.zebra tr:even').addClass('even');
	$('table.zebra tr:odd').addClass('odd');
	$('ul.zebra>li:even').addClass('even');
	$('ul.zebra>li:odd').addClass('odd');
	$('#subscribe_form_page table input[type=text],#subscribe_form_page table input[type=password]').css({'width':'306px','border':'1px solid #cbcbcb'});
	$('#subscribe_form_page table td').attr('valign','top');
	$('#subscribe_form_page #countrySelect,#subscribe_form_page #stateSelect').css({'width':'306px'});
	
	//when click letter,run function updateDirectorByLetter
	$('#director_listing ul.alpha_list li a').click(updateDirectorByLetter);
	$('#release #viewBy li a').click(updateMovieListing);
	$('#release #timepager li a').click(updateMovieListing);
	$('#distributor #presentselect').change(updatePresentSelection);
	$('#distributor #pastselect').change(updatePastSelection);
	$('#distributor #futureselect').change(updateFutureSelection);


	//when click letter,run function updateDirectorByNumber
	$('#director_listing ul.pager li a').click(updateDirectorByPage);
	//when click letter,run function updateActorByLetter
	$('#actor_listing ul.alpha_list li a').click(updateActorByLetter);
	//when click letter,run function updateActorByNumber
	$('#actor_listing ul.pager li a').click(updateActorByPage);
	
	
	function updateDirectorByLetter(){
		var url=$(this).attr('href');
		var a=url.split('/');
		var lastpage=a[5];
		var letter=a[6];
		//this make ajax call to /director/getDirectors/lastpage/letter
		$.get(url,function(data){
			$('#directorlist').html(data);//update director table
			//two lines below style director table into zebra effect
			$('table.zebra tr:even').css({"background":"#fff"});
			$('table.zebra tr:odd').css({"background":"#f2f2f2"});
			//this update page title
			$('#directorletter').html('<h1 class="main-headline">Directors | ' + letter + '</h1>');
		});
		
		//this make ajax call to /director/updatePages/lastpage/letter
		var url2='/director/updatePages/'+lastpage+'/'+letter;
		$.get(url2,function(data){
		    //two lines below update the number list with ajax back content
			$('#dirpages').html(data);
            $('#dirpages2').html(data);
			//this rebind function updateDirectorByPage to the pager links,so that when you click it,it can still update content
			$('#director_listing ul.pager li a').click(updateDirectorByPage);
		});

		return false;
	};

	function updateMovieListing(){
		var url=$(this).attr('href');
		//this make ajax call to /schedule/updateListing
		var url=$(this).attr('href');
		var pathArray = url.split("/");
		if (pathArray[4] != "this_year"){
			$('#release #timepager').css("display", "inline");
			updateTimePager(pathArray[3], pathArray[4]);
		}
		else {
			$('#release #timepager').toggle();
		}
		

		$.get(url,function(data){
			$('#release_listings').html(data);//update movie listing
			//two lines below style director table into zebra effect
			if ( $('ul.zebra>li:even').length > 0)
			{
				$('ul.zebra>li:even').css({"background":"#fff"});
				$('ul.zebra>li:odd').css({"background":"#f2f2f2"});
			}
			else
			{
				$('table.zebra tr:even').css({"background":"#fff"});
				$('table.zebra tr:odd').css({"background":"#f2f2f2"});
			}	
			$('#release #timepager li a').click(updateMovieListing);

		});

		return false;
	};

	function updateTimePager(mday, type)
	{
		mday = parseInt(mday);
		if (type == "this_week"){
			var nextmday = mday+7;
			$('#release #timepager').html('<li><a href="/schedule/updateListing/' + nextmday + '/' + type + '"> Next Week >></a></li>');
		} else {
			var nextmday = mday+1;
			$('#release #timepager').html('<li><a href="/schedule/updateListing/' + nextmday + '/' + type + '"> Next Month >></a></li>');
		}
		return false;
	}

	function updatePastSelection(){
		var pastid=$('#distributor #pastselect option:selected').val();
		$('#distributor #presentselect option[name=presentopt]').attr("selected","selected");
		$('#distributor #futureselect option[name=futureopt]').attr("selected","selected");
		if (pastid != "Past")
			updateDistListing(pastid);
	}

	function updatePresentSelection(){
		var presentid=$('#distributor #presentselect option:selected').val();
		$('#distributor #futureselect option[name=futureopt]').attr("selected","selected");
		$('#distributor #pastselect option[name=pastopt]').attr("selected","selected");
		if (presentid != "Present")
			updateDistListing(presentid);
	}

	function updateFutureSelection(){
		var futureid=$('#distributor #futureselect option:selected').val();		
		$('#distributor #presentselect option[name=presentopt]').attr("selected","selected");
		$('#distributor #pastselect option[name=pastopt]').attr("selected","selected");
		if (futureid != "Future")
			updateDistListing(futureid);

	}

	function updateDistListing(rangeid){
		//this make ajax call to /schedule/updateListing
		//var sortid = "1";
		var distid = $('#distributor #distid').val();

		var url = '/distributor/updateList/' + rangeid + "/" + distid;
		$.get(url,function(data){
			$('#movielist').html(data);//update movie listing
			//two lines below style director table into zebra effect
			$('ul.zebra>li:even').css({"background":"#fff"});
			$('ul.zebra>li:odd').css({"background":"#f2f2f2"});
		});

		return false;
	};
	
	function updateDirectorByPage(){
		var url=$(this).attr('href');
		var a=url.split('/');
		var lastpage=a[5];
		var letter=a[6];
		var url2='/director/getDirectors/'+lastpage+'/'+letter;
		$.get(url2,function(data){
			$('#directorlist').html(data);
			$('table.zebra tr:even').css({"background":"#fff"});
			$('table.zebra tr:odd').css({"background":"#f2f2f2"});
			$('#directorletter').html('<h1 class="main-headline">Directors | ' + letter + '</h1>');
		});
		$.get(url,function(data){
			$('#dirpages').html(data);
            $('#dirpages2').html(data);	
			$('#director_listing ul.pager li a').click(updateDirectorByPage);
		});
		return false;
	}

	function updateActorByLetter(){
		var url=$(this).attr('href');
		var a=url.split('/');
		var lastpage=a[5];
		var letter=a[6];
		//this make ajax call to /actor/getDirectors/lastpage/letter
		$.get(url,function(data){
			$('#actorlist').html(data);//update director table
			//two lines below style director table into zebra effect
			$('table.zebra tr:even').css({"background":"#fff"});
			$('table.zebra tr:odd').css({"background":"#f2f2f2"});
			//this update page title
			$('#actorletter').html('<h1 class="main-headline">Actors | ' + letter + '</h1>');
		});
		
		//this make ajax call to /director/updatePages/lastpage/letter
		var url2='/actor/updatePages/'+lastpage+'/'+letter;
		$.get(url2,function(data){
		    //two lines below update the number list with ajax back content
			$('#actorpages').html(data);
            $('#actorpages2').html(data);
			//this rebind function updateDirectorByPage to the pager links,so that when you click it,it can still update content
			$('#actor_listing ul.pager li a').click(updateActorByPage);
		});

		return false;
	};
	
	function updateActorByPage(){
		var url=$(this).attr('href');
		var a=url.split('/');
		var lastpage=a[5];
		var letter=a[6];
		var url2='/actor/getActors/'+lastpage+'/'+letter;
		$.get(url2,function(data){
			$('#actorlist').html(data);
			$('table.zebra tr:even').css({"background":"#fff"});
			$('table.zebra tr:odd').css({"background":"#f2f2f2"});
			$('#actorletter').html('<h1 class="main-headline">Actors | ' + letter + '</h1>');
		});
		$.get(url,function(data){
			$('#actorpages').html(data);
            $('#actorpages2').html(data);	
			//this rebind function updateDirectorByPage to the pager links,so that when you click it,it can still update content
			$('#actor_listing ul.pager li a').click(updateActorByPage);
		});
		return false;
	}
});