//SET CUSTOM JAVASCRIPT METHODS
function in_array( what, where ){
	var a=false;
	for(var i=0;i<where.length;i++){
		if(what == where[i]){
			a=true;
			break;
		}
	}
	return a;
}

var j = jQuery.noConflict();

// history.navigationMode = 'compatible';

// window.onload = function(){alert('onload')};

j(document).ready(function(){	


	if( j("a[rel='gallery']").length > 0 )
	{
		j("a[rel='gallery']").colorbox({
			maxWidth:'80%', maxHeight:'80%', current:'{current} / {total}', initialWidth:100,initialHeight:100
		});
	}

	if( jQuery().autofill )
	{
		j('#pesquisa').autofill({
			value: 'filmes, DVDs, notícias',
			defaultTextColor: '#666',
			activeTextColor: '#000'
		});

		j('#newsletter').autofill({
			value: 'O seu e-mail',
			defaultTextColor: '#fff',
			activeTextColor: '#fff'
		});


	}
	
	j('#searchform .submit').click(function(e){
		
		e.preventDefault(); 
		
		// console.log( j('#searchform #pesquisa') );
		
		j('#searchform #pesquisa').trigger('focus');
		
		// console.log( j('#searchform #pesquisa').val() );
		
		if( j('#searchform #pesquisa').val().length > 0 && j('#searchform #pesquisa').val() != 'filmes, DVDs, notícias' )
		{
			j('#searchform').submit();
		}

	   
				
	});
	
	j('.cinema_menu').mouseenter(function(){
		
		show_cinema_menu();
		
		
		hide_dvd_menu();			

	});
	
	j('.cinema_menu').mouseleave(function(){
		
	   
		if( j(this).hasClass('open') == false )
		{
			hide_cinema_menu();
		}

		
	});
	
	j('.dvd_menu').mouseenter(function(){
		
		if( j('.cinema_menu').hasClass('open') )
		{
			hide_cinema_menu();
		}
		
		
		j('.menu').animate({
			marginTop: 5
		}, 300 );
		
		j(this).animate({
			height: 58
		}, 300 );
		
		show_dvd_menu();
		
		


	});
	
	j('.dvd_menu').mouseleave(function(){ 
		

		if( j(this).hasClass('open') == false )
		{
			hide_dvd_menu();			
		}

	});
	
	j('.menu').mouseleave(function(){
		
		 if( j('.cinema_menu').hasClass('open') )
		{
			show_cinema_menu();
		}
		
		if( j('.dvd_menu').hasClass('open') == false )
		{
			// alert('s1');
			j(this).animate({
				marginTop: 34
			}, 300 ); 
			
			j('.dvd_menu').animate({
				height: 29
			}, 300 );
		}
		else
		{
			// alert('s2');

			show_dvd_menu();
		}

	});
	
	function show_cinema_menu()
	{
	   	var time_delay = 0; 
	
		// j('.cinema_menu a').stop();
	
		
		j('.cinema_menu a').each(function(){
			
		
		    var time_delay_iu = time_delay;
		
			time_delay += 100; 
		
		
			j(this).delay(time_delay).animate({
				marginRight: 0,
				opacity:'show'
			}, 100 );
			
			
		}); 
	} 
	
	function show_dvd_menu()
	{
	   	var time_delay = 0;
	    
		// j('.dvd_submenu a').hide();
		
		j('.dvd_submenu a').each(function(){
		
		    var time_delay_iu = time_delay;
		
			time_delay += 100;
			
			// console.log( time_delay_iu ); 
		
			j(this).delay(time_delay_iu).animate({
				marginRight: 0,
				opacity:'show'
			}, 100 );
			
		}); 
	}
	
	function hide_cinema_menu()
	{
		 var time_delay = 300;
		
		j('.cinema_menu a').each(function(){
		
		    var time_delay_iu = time_delay;
		
			time_delay -= 100; 
		
		
			j(this).delay(time_delay).animate({
				marginRight: -10,
				opacity:'hide'
			}, 100 );
			
			
		});
	} 
	
	function hide_dvd_menu()
	{
		 var time_delay = 300;
		
		j('.dvd_submenu a').each(function(){
		
		    var time_delay_iu = time_delay;
		
			time_delay -= 100; 
		
		
			j(this).delay(time_delay).animate({
				marginRight: -10,
				opacity:'hide'
			}, 100 );
			
			
		});
	}

	if( j('#comprar_form').length > 0 )
	{

		j('#comprar_submit').click(function(e){
			
			e.preventDefault();

			j("#comprar_form").submit();

			return false;

		});
		
		j("#comprar_form").validationEngine();
			
	}

	// if( j('#newsletter_form').length > 0 )
	// {
	// 	j("#newsletter_form").validationEngine();
	// } 
     
	// alert('loaded');
	
	// alert( document.referrer );

	if( global_animate === 'true' )
	{ 
		// alert('anim');
		
		j('#content .animatior').animate({
			left: 0,
			opacity:1,
			useTranslate3d: true,
			leaveTransforms: false
		}, 400); 
		
		
		    
		// j('.main_content').css('opacity', 0).show();  	
		// 
		// j('.main_content').animateWithCss(
		//   {
		// 	marginLeft: 0,
		// 	opacity:1
		//   }, // CSS properties to animate
		//   400,                       // Duration in ms
		//   "default",
		//   function(){
		//   			  // j('.main_content').show();  	
		// 	}// The timing function
		// );
	


			j('a').click(function(event){
                

				if( j(this).hasClass('anim_exception') == false )
				{
					event.preventDefault();

					
					if( j(this).hasClass('go_back') && j(this).hasClass('exception') == false && history.length > 1 )
					{						 
							j('#content .animatior').animate({
								left: -20,
								opacity:0,
								useTranslate3d: true,
								leaveTransforms: false
							}, 250, function(){ 
								j(window).unload();
								window.history.go(-1);
							} );   
							
							// j('.main_content').animateWithCss(
							//   {
							// 	marginLeft: -20,
							// 	opacity:0
							//   }, // CSS properties to animate
							//   250,                       // Duration in ms
							//   "default",             // The timing function
							//   function(){                 // Callback
							// 	window.history.go(-1);
							//   }
							// );
							

							// return false;

					}
					else
					{   
						
						var linkLocation = this.href;

						j('#content .animatior').animate({
							left: -20,
							opacity:0,
							leaveTransforms: false
						}, 250, function(){
							clickPropagation(linkLocation);
						} );

						return false;
					}

				}


			});	
		}
		else
		{
			j('#content').animate({
				marginTop: 0,
				opacity:'show'
			}, 0, function(){

				j('#content .destaque_normal').each(function(index){

					j(this).animate({
						marginTop:0,
						opacity:'show'
						}, 0 );
					});



				});


			}

			function clickPropagation(linkLocation)
			{
				window.location = linkLocation;
			}


			//DIV AUTO HEIGHT
			var left_height = j('.filme_detalhe .left').height();
			var right_height = j('.filme_detalhe .right').height();

			var left_el = j('.filme_detalhe .left .block');
			var right_el = j('.filme_detalhe .right .block');

			if( j('.filme_detalhe').hasClass('exception') == false )
			{
				if( left_height != null && right_height != null )
				{
					if( left_height > right_height )
					{
						var height_diff = left_height - right_height;
						right_el.last().height( right_el.last().height()+height_diff );
					}
					else
					{
						var height_diff = right_height - left_height;
						left_el.last().height( left_el.last().height()+height_diff );
					}
				}
			}



			//FILME_BLOCK AUTO HEIGHT
			var filme_block_max_height = 0;

			j('.filme_block').each(function(){

				// console.log(j(this).height());


				if( j(this).height() > filme_block_max_height )
				{
					filme_block_max_height = j(this).height();
				}

			});

			// console.log( filme_block_max_height );

			j('.filme_block').css('height', filme_block_max_height+'px');

			//DVD_BLOCK AUTO HEIGHT
			var dvd_block_max_height = 0;
			var last_first = 1;

			j('.dvd_block').not('.exception').each(function(index){

				index = index+1;

				if( index%6 == 0 )
				{
					last_first = index;
				}

				index_mod_5 = index%5;

				if( j(this).height() > dvd_block_max_height )
				{
					dvd_block_max_height = j(this).height();
				}


				if( index_mod_5 == 0 || index == j('.dvd_block').length )
				{
					j('.dvd_block').each(function(index_2){

						index_2 = index_2+1;

						// if( ( index_2 >= (index-4) ) && ( index_2 <= index ) )
						if( ( index_2 >= last_first ) && ( index_2 <= index ) )
						{
							j(this).css('height', dvd_block_max_height+'px');

							//ALIGN PRECO TO THE BOTTOM
							// var height_diff = dvd_block_max_height - j(this).find('.dvd_info_holder').height();
							// 											
							// 											var height_subtract = 20;
							// 
							// 											var height_target = height_diff-height_subtract;
							// 
							// 
							// 											if( height_diff >= height_subtract )
							// 											{
								// 												j(this).find('.dvd_info_holder').height(j(this).find('.dvd_info_holder').height()+height_target);
								// 											}

							}



						});



						dvd_block_max_height = 0;
					}


				});


			   

			});
