//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();

j(document).ready(function(){	

	j('.go_back').click(function(){

		if( history.length > 1 )
		{
			history.go(-1);

			return false;
		}

	});

	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: 'Pesquisar',
			defaultTextColor: '#fff',
			activeTextColor: '#fff'
		});

		j('#newsletter').autofill({
			value: 'O seu e-mail',
			defaultTextColor: '#fff',
			activeTextColor: '#fff'
		});

		// j('.comprar_form #form_name').autofill({
			// 			value: 'António José Ferrão',
			// 			defaultTextColor: '#585555',
			// 			activeTextColor: '#585555'
			// 		});
			// 
			// 		j('.comprar_form #form_morada').autofill({
				// 			value: 'Rua da Alegria, Nº 78, 1500-000\nLisboa\nPortugal',
				// 			defaultTextColor: '#585555',
				// 			activeTextColor: '#585555'
				// 		});
				// 
				// 		j('.comprar_form #form_telefone').autofill({
					// 			value: '92354428',
					// 			defaultTextColor: '#585555',
					// 			activeTextColor: '#585555'
					// 		});
					// 
					// 		j('.comprar_form #form_email').autofill({
						// 			value: 'antonio.ferrao@gmail.com',
						// 			defaultTextColor: '#585555',
						// 			activeTextColor: '#585555'
						// 		});

					}

					if( j('#comprar_form').length > 0 )
					{
						j("#comprar_form").validationEngine();

						j('#comprar_submit').click(function(){

							j("#comprar_form").submit();

							return false;

						});	
					}

					if( j('#newsletter_form').length > 0 )
					{
						j("#newsletter_form").validationEngine();
					}


					if( global_animate === 'true' )
					{
						j('#content').animate({
							marginTop: 0,
							opacity:'show'
						}, 500, function(){

							j('#content .destaque_normal').each(function(index){
								j(this).delay(index*100).animate({
									marginTop:0,
									opacity:'show'
									}, 500 );
								});

								j('#content .em_exibicao_cell').each(function(index){
									j(this).delay(index*100).fadeIn(500);
								});
							} );

							j('a').click(function(event){

								if( j(this).hasClass('menu_expander') == false && j(this).hasClass('anim_exception') == false )
								{
									event.preventDefault();
									var linkLocation = this.href;

									j('#content').animate({
										marginTop: -10,
										opacity:0
									}, 500, 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 );
								});

								j('#content .em_exibicao_cell').each(function(index){
									j(this).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;
								}
	

							});
							
							
							//EM EXIBICAO CELL AUTO HEIGHT
							var em_exibicao_cell_max_height = 0;
							var em_exibicao_cell_max_corpo_height = 0;
							var last_first = 1;
							
							j('.em_exibicao_cell').not('.exception').each(function(index){
								
								index = index+1;
								
								if( index%2 != 0 )
								{
									last_first = index;
								}
								
								index_mod_2 = index%2;

								if( j(this).height() > em_exibicao_cell_max_height )
								{
									em_exibicao_cell_max_height = j(this).height();
									em_exibicao_cell_max_corpo_height = j(this).find('.corpo').height();
								}
								
								
								if( index_mod_2 == 0 || index == j('.em_exibicao_cell').length )
								{
									j('.em_exibicao_cell').each(function(index_2){
										
										index_2 = index_2+1;
										
										
										if( ( index_2 >= last_first ) && ( index_2 <= index ) )
										{
											
											// console.log('app');
											j(this).css('height', em_exibicao_cell_max_height+'px');
											// j(this).find('.corpo').css('height', em_exibicao_cell_max_corpo_height+'px' );
											
										}
										
										
										
									});
									
									em_exibicao_cell_max_height = 0;
									em_exibicao_cell_max_corpo_height = 0;
								}
	

							});
							
							

							j('.menu_expander').click(function(){
								var current_selection = j(this);
								j('.menu_expander').each(function(){

									if( current_selection.attr('id') != j(this).attr('id') )
									{
										j(this).next('.submenu').slideUp('fast');
										j(this).removeClass('current-cat-expanded');
									}

								})
								j(this).toggleClass('current-cat-expanded');
								j(this).next('.submenu').slideToggle('fast');
								return false;
							});

						});
