$(document).ready(function() {	

	//select all the a tag with name equal to modal
	$('a[name=modal]').click(start_modal);
	
	//if close button is clicked
	$('.window .close').click(function (e) {
		//Cancel the link behavior
		e.preventDefault();
		$('#mask, .window').hide();
	});		
	
	//if mask is clicked
	$('#mask').click(function () {
		$(this).hide();
		$('.window').hide();
	});		
	
	// Modal box watchlist release outputs.
	$(".release-wrapper .option_bar").click(OpenWatch);
	
	// Add watchlist(movies) formats.
	$(".watchlist-tag").live("click",AddFormatWatchlistMovies);
	
	// Remove watchlist ( movies) formats.
	$('.watchlist-tag-selected').live("click", RemoveFormatWatchlistMovies);
	 
	// all good formats, add new entry watchlist movies.
	$(".add-movie-watchlist-submit").live("click",AddWatchModal);
	
	// close watchlist.
	$("#close_watch").live("click",CloseWatch);
	
	$("a#working").click(working);
});

function open_vip(tekst){

	$("#dialog p:eq(0)").text(tekst);
	$("#dialog .modal-title").text('Sorry Vip members only');
	
	 
	var e ='';
	start_modal(e);
	
	
}


function working(evt){
	
	$("#dialog p:eq(0)").text('We are working on it.. bear with us.');
	$("#dialog .modal-title").text('This Function is still in development');

	var e ='';
	start_modal(e);

	
	
};




function start_modal(e){
		//Cancel the link behavior
		
		//Get the A tag
		var id = '#dialog';
	
		//Get the screen height and width
		var maskHeight = $(document).height();
		var maskWidth = $(window).width();
	
		//Set height and width to mask to fill up the whole screen
		$('#mask').css({'width':maskWidth,'height':maskHeight});
		
		//transition effect		
		//$('#mask').fadeIn(500);	
		$('#mask').fadeTo(500,0.8);	
		
		
	
		//Get the window height and width
		var winH = $(window).height();
		var winW = $(window).width();
        var aScroll=$(window).scrollTop();
		
		
		//Set the popup window to center
		$(id).css('top',  winH/2-$(id).height()/2 + aScroll);
		$(id).css('left', winW/2-$(id).width()/2);
	
			
		
		//transition effect
		$(id).fadeIn(500); 
		return false;
	
};


function OpenWatch(evt){
	
	var acParent =$(this).parent();
	var acH3	= $("h3" ,acParent);
	var positionWrap=$(".global-center").position();
	var position=$(acH3).position();
	
	
	var acLeft	=(position.left-10)+ positionWrap.left;

	var acTop	=position.top+255;
	var imdb	=$(this).attr("id");
	var content	=$(".addWatch #content");
	
	var htmlc='<div style="margin-left:250px; margin-top:60px;">';
	htmlc+='<img  src="http://www.nzbmovieseeker.com/images/loader_trailer.gif" /> Loading Watchlist';
	htmlc+='</div>';				
		
	
	$(content).html(htmlc);

	$(".addWatch").css({'top':"0px", 'left' :"0px"});
	
	$(".addWatch").css({'top':acTop+"px" , 'left' :acLeft+"px"});
	$(".addWatch").show();

	$.get('http://www.nzbmovieseeker.com/sql/add_watch_list-debug.php',{ imdb: imdb }, function(data) {
		  $(content).html(data);
		 
	});
	
	
};


function AddWatchModal(evt){

	// No format selected ?
	if( !$('input[name="formats_wl[]"]').length ){
		alert ( ' No formats selected' );
		return false;
	}
	
	var imdb	=$('input[name="imdb"]').val();
	
	// to much code , smarter would be to rewrite script.
	
	
	// Loop over each input and create url.
	var formats ='';
	$('input[name="formats_wl[]"]').each(function (index,value) {
	
		formats	+='f'+$(this).val();
	});
	

	var content	=$(".addWatch #content");
	
	var htmlc='<div style="margin-left:180px; margin-top:60px;">';
	htmlc+='<img  src="http://www.nzbmovieseeker.com/images/loader_trailer.gif" /> Adding movie to your Watchlist';
	htmlc+='</div>';	
	$(content).html(htmlc);


	$.get('http://www.nzbmovieseeker.com/sql/process_watchlist_movies.php',{ imdb: imdb , formats : formats }, function(data) {
		
			if(data=="vip"){
					open_vip('Sorry only VIP members are allowed to have more then 2 items in there watchlist');
					 $(".addWatch").hide();
					return false;
				
			}else{
		 		 $(content).html(data);
			}
			
	});

	
	return false;
};


function CloseWatch(evt){
	
	$(".addWatch").hide();
	
	return false;
};


function Working(evt){
	
	
}


/*
	Select format watchlist movies.

*/
function AddFormatWatchlistMovies(evt){
	
	var watchlist_tag_id	=$(this).attr('id');
	
    // get format id from watchlist_tag_id.
	var format_id	=watchlist_tag_id.replace(/watchlist-mItem-/g,'');
	
	 //insert forum input after link.
	var html	='<input type="hidden" name="formats_wl[]" value="'+format_id+'" > ';
	$(this).after(html);
	
	//change class.
	$(this).removeClass('watchlist-tag').addClass('watchlist-tag-selected');
		

	return false;
	
}




/*
	Remove format watchlist movies.

*/
function RemoveFormatWatchlistMovies(evt){
	
	// removre formum input.
	$(this).next().remove();
	
	//change class.
	$(this).removeClass('watchlist-tag-selected').addClass('watchlist-tag');
	
	return false
	
}


function OpenCastWatch(evt,type,special_id){
	var actor_id =$(evt).attr('title');
	
	
	var acParent =$('h1').parent();
//	var acH3	= $(".movie-info" ,acParent);
	var positionWrap=$(".global-center").position();
	var position=$(acParent).position();
	
	
	var acLeft	=(position.left+15);

	var acTop	=position.top+50;
	
	var content	=$(".addWatch .content2");
	
	//var content	=$("#content");
	
	var htmlc='<div style="margin-left:250px; margin-top:60px;">';
	htmlc+='<img  src="http://www.nzbmovieseeker.com/images/loader_trailer.gif" /> Loading Watchlist';
	htmlc+='</div>';				
					
	
	$(content).html(htmlc);
	
	

	$(".addWatch").css({'top':"0px", 'left' :"0px"});
	
	$(".addWatch").css({'top':acTop+"px" , 'left' :acLeft+"px"});
	$(".addWatch").show();
	
	$.get('http://www.nzbmovieseeker.com/sql/process_watchlist_cast.php',{actor_id: actor_id ,type:type,special_id:special_id  }, function(data) {
		
			if(data=="vip"){
					open_vip('Sorry only VIP members are allowed to have more then 2 items in there watchlist');
					 $(".addWatch").hide();
					return false;
				
			}else{
		 		 $(content).html(data);
			}
			
	});
	
	
	return false;
	
	
	
};
	

