function show_spoiler()
{	
	jQuery('.spoiler').wrap(function(){
		if(!$(this).hasClass('spoilerReady')) 
		{
			return '<div class="spoilerHidden"></div>';
		} else {
			return '';
		}
	});
	jQuery('.spoiler').before(function(){
		if(!$(this).hasClass('spoilerReady')) 
		{
			$(this).addClass('spoilerReady');
			return '<div class="message">Comentário contando partes do filme. <a href="#" class="showSpoiler">Mostrar comentário</a>.</div>';
		} else {
			return '';
		}
	});
	
	jQuery('a.showSpoiler').click(function(){
		jQuery(this).parent().parent().children('.spoiler').slideDown('slow');
		jQuery(this).parent().slideUp('slow');
		return false;
	});
}

function load_reviews(href, page)
{
	$.ajax({
		type: 'GET',
		url: href,
		dataType: 'json',
		global: false,
		beforeSend: function()
		{
			$('a#loadReviews').addClass('disabled');
			$('a#loadReviews').html('<img src="/public/images/ajax-loader.gif" />');
		},
		
		error: function()
		{
			$('a#loadReviews').html('doh! Ocorreu um erro estranho.');
			$('a#loadReviews').removeClass('disabled');
		},
		
		success: function(data){
			$('ol.reviewsList').append(data.html);
			show_spoiler();
			nextPage = parseInt(page) + 1;
			$('a#loadReviews').attr('href', '#reviews/'+nextPage);
			
			if(data.showing >= data.reviews_count)
			{
				$('a#loadReviews').html('Fim!');
			} else {
				$('a#loadReviews').html('Mais Opiniões <small>(mostrando '+data.showing+' de '+data.reviews_count+' opiniões)</small>');
				$('a#loadReviews').removeClass('disabled');
			}
		}
	});
}

function buscape_get_offers(movie_pk)
{
	$.ajax({
		type: 'GET',
		url: '/ajax/pegar-ofertas-do-buscape/'+movie_pk+'/',
		dataType: 'json',
		global: false,
		success: function(data)
		{
			jQuery('div#buscapeWraper').html(data.html);
		}
	});
}

jQuery(document).ready(function() {	
	jQuery(".movieDetails .movieMedia .photos a.photo").fancybox();
	
	moviesDetailsWrapHeight = jQuery("#detailsWrap").innerHeight();
	if(moviesDetailsWrapHeight > 200)
	{
		jQuery("#detailsWrap").addClass('minDetailsWrap');
		jQuery("#detailsWrap").prepend('<a href="#" class="toggleContent"><span class="left">Mais informações</span><span class="right">Mais informações</span></a>');
		toggleContentHeight = jQuery("#detailsWrap a.toggleContent").innerHeight();
		jQuery("#detailsWrap a.toggleContent").toggle(
			function(){
				jQuery("#detailsWrap").animate({
					"height":moviesDetailsWrapHeight+toggleContentHeight
				}, "slow");
				
				if(jQuery.browser.msie)
				{
					jQuery("#detailsWrap .toggleContent").animate({'top':moviesDetailsWrapHeight+46}, 'slow');
				}
				
				jQuery("#detailsWrap a.toggleContent span").text('Menos informações');
			},
			function(){
				jQuery("#detailsWrap").animate({
					"height":200
				}, "slow");
				
				if(jQuery.browser.msie)
				{
					jQuery("#detailsWrap .toggleContent").animate({'top':184}, 'slow');
				}
				
				jQuery("#detailsWrap a.toggleContent span").text('Mais informações');
			}
		);
	}
	
	jQuery('.adsBg a').each(function(){
		ad_id = jQuery(this).attr('id');
		ad_id = ad_id.split('-');
		ad_id = ad_id[1];
		
		jQuery(this).attr('href', '/ad/redirect/'+ad_id+'/');
	});
	
	show_spoiler();
	
	jQuery('a#loadReviews').click(function(){
		a = $(this);
		gethash = a.attr('href');
		hash = gethash.split('#')[1].split('/');
		page = parseInt(hash[1]);
		
		load_reviews('/ajax/filmes/opinioes/'+movie_id+'/'+page+'/', page);
	})
	
	getinithash = document.location.hash;
	if(getinithash.length > 0)
	{
		inithash = getinithash.split('#')[1].split('/');
		if(inithash[0] == 'reviews')
		{
			initpage = parseInt(inithash[1]);
			load_reviews('/ajax/filmes/opinioes/'+movie_id+'/2/'+(initpage-1)+'/', initpage);
		}
	}
	
	jQuery("#buscapeAd").live('mouseover', function(){
		jQuery(this).addClass('buscapeOpened');
		jQuery(this).removeClass('buscapeClosed');
	});

	jQuery("#buscapeAd").live('mouseout', function(){
		jQuery(this).removeClass('buscapeOpened');
		jQuery(this).addClass('buscapeClosed');
	});
});