/*

GEMA

CSS + WORDPRESS by Leo Favre
http://leofavre.com

DESIGN by ARTERIAL
http://arterial.tv

*/





$(window).resize(function(){
	postInfoDefaultPosition();
	correctLayout();
});


$(window).smartresize(function(){  
	sliderVerticalHeight( "sl1", 2 );
	sliderVerticalHeight( "sl2", 4 );
	sliderVerticalHeight( "sl3", 4 );
	sliderCorrectResize( "sl1" );
	sliderCorrectResize( "sl2" );
	sliderCorrectResize( "sl3" );
});


$(window).load(function(){
	show_thumbnails();
	correctLayout();

	$.getScript('http://widgets.twimg.com/j/2/widget.js', function(){
		twitter_widget();
	});
});


$(document).ready(function(){
	correctLayout();
	auto_external_links();
	sliderVerticalHeight( "sl1", 2 );
	sliderVerticalHeight( "sl2", 4 );
	sliderVerticalHeight( "sl3", 4 );
	startSlider( "sl1" );
	startSlider( "sl2" );
	startSlider( "sl3" );


	/* HOME SLIDERS */
	
	$("body.blog.not-apple-device .w2, body.blog.not-apple-device .w1").mouseenter(function(){
		$(this).find(".top-bar").addClass("hover");
	});

	$("body.blog.not-apple-device .w2, body.blog.not-apple-device .w1").mouseleave(function(){
		$(this).find(".top-bar").removeClass("hover");
	});

	$("body.blog .previouspostslink.not-apple-device, body.blog.not-apple-device .nextpostslink").mouseenter(function(){
		$(".post-info").css("display", "none");
	});

	$("body.blog .previouspostslink.not-apple-device, body.blog.not-apple-device .nextpostslink").mouseleave(function(){
		$(".post-info").css("display", "block");
	});
	
	
	/* FLASH BANNER */
	
	swfobject.embedSWF("http://www.gemagema.tv/2010/wp-content/uploads/anuncios/fb_rmc2011_728-90.swf", "top-full-ad-inner", "728", "90", "9.0.0", "", "", {scale:"noscale", wmode:"transparent"});
	swfobject.embedSWF("http://www.gemagema.tv/2010/wp-content/uploads/anuncios/banner_ipanema_150X430.swf", "footer-ad-inner", "150", "430", "9.0.0", "", "", {scale:"noscale", wmode:"transparent", salign:"tl"});
	swfobject.embedSWF("http://www.gemagema.tv/2010/wp-content/uploads/anuncios/banner_ipanema_150X430.swf", "blogao-sidebar-left-ad-inner", "150", "430", "9.0.0", "", "", {scale:"noscale", wmode:"transparent", salign:"tl"});


	/* PAGE NAVIGATION */

	if( $.browser.msie ){
		$(".page-navigation.slider-menu .page, .page-navigation.slider-menu .current").html("&#9660;");
	} else {
		$(".page-navigation.slider-menu .page, .page-navigation.slider-menu .current").html("&#9670;");
	}
	
	
	/* POST INFO */
	
	$(".thumb img").attr("title", "");
	
	$(".post-info").each(function(){
		var up = ($(this).outerHeight()*-1)-4+"px";
		$(this).animate( {bottom: up}, 100, function(){
			$(this).css("display", "block");
		});
	});
	
	$(".post-info").click(function(){
		location.href = $(this).find("a").attr("href");
	});

	$("body.not-apple-device .thumb").mouseenter(function(){
		$(this).find(".post-info").animate({bottom: "0"}, 100);
	});

	$("body.not-apple-device .thumb").mouseleave(function(){
		var up = ($(this).find(".post-info").outerHeight()*-1)-4+"px";
		$(this).find(".post-info").animate({bottom: up}, 100);
	});
	
	$("body.category-blogao .post-title, body.category-eventos .post-title").click(function(){
		location.href = $(this).find("a").attr("href");
	});
	
	
	/* EVENT TITLE INFO */
	
	$("#agenda .event-title").click(function(){
		location.href = $(this).find("a").attr("href");
	});
	
	
	/* COMMENTS */
	
	$("#commentform").slideToggle(0);
	
	$("#respond>h4").click(function(){
		$("#commentform").slideToggle();
	});
	
	$(".tocomments").click(function(){
		$("#commentform").slideDown();
	});
	
	
	/* COLORBOX */

	$(document).bind('cbox_complete', function(){
		$("#cboxSlideshow, #cboxPrevious, #cboxNext, #cboxClose").css("visibility", "visible");
	});
	
	$(document).bind('cbox_closed', function(){
		$("#cboxSlideshow, #cboxPrevious, #cboxNext, #cboxClose").css("visibility", "hidden");
	});

	$(".storycontent a, .storycontent img").attr("title", "");
	$(".storycontent a[href$='.jpeg'] img, .storycontent a[href$='.jpg'] img, .storycontent a[href$='.png'] img, .storycontent a[href$='.gif'] img").parent().colorbox({transition: "none", width: "95%", height: "92%", title: function(){ return $(this).closest("div.wp-caption").find(".wp-caption-text").text();}});
	$(".storycontent .gallery a[href$='.jpeg'] img, .storycontent .gallery a[href$='.jpg'] img, .storycontent .gallery a[href$='.png'] img, .storycontent .gallery a[href$='.gif'] img").parent().colorbox({transition: "none", width: "95%", height: "92%", title: function(){ return $(this).parent().next(".gallery-caption").text();}});
	$(".attachment-thumbnail").closest("a").attr("rel", function(){ return $(this).closest(".gallery").attr("id") });
	colorbox_gemashots();
	
	
	/* SUBCAT */
	
	$('#subcat-title').click(function(evt){
		$('#subcat-list').slideToggle();
		$('#subcat').toggleClass('open');
		evt.preventDefault();
	});
	
	$('#subcat').mouseleave(function(evt){
		if( $('#subcat-list').css('display') != 'none' ){
			$('#subcat-list').slideUp();
			$('#subcat').removeClass('open');
			evt.preventDefault();
		}
	});
	
	
	/* CREDITS */
	
	$("body.single #main[class!=blogao] p>object, body.single #main[class!=blogao] iframe[class!=facebook-like]").unwrap();
	$("body.single #main[class!=blogao] object, body.single #main[class!=blogao] iframe[class!=facebook-like]").after('<div id="credits-bar" class="pink-bar"></div>');
	if( $("body.single .post ul:first").parent().hasClass("post") ){ 
		$("#credits-bar").html("<h4>Créditos</h4>");
	} else {
		$("#credits-bar").addClass("add-height");
	};
	
	
	/* SEARCH FORM */
	
	$("#searchform input[type=text]").attr("value", "Busca");
	
	$("#searchform input[type=text]").focus(function(){
		$(this).attr("value", "");
	});
	
	$("#searchform input[type=text]").blur(function(){
		if( $(this).attr("value")=="" ){
			$(this).attr("value", "Busca");
		}
	});
	
	
	/* SCROLL */
	
	$("#sidebar .w2 .inner-scroll").each(function(){
		if( $(this).height()>390 ){
			// $(this).addClass("flexcroll");
		}
	});
	
	fleXenv.initByClass("flexcroll");
	
});


/* FUNCTIONS */

function correctLayout(){
	$("#top-full-ad").css( "width", ($(".main-inner").width()-5)+"px" );
}

function postInfoDefaultPosition(){
	$(".post-info").each(function(){
		var up = ($(this).innerHeight()*-1)-4+"px";
		$(this).css("bottom", up);
	});
}

function auto_external_links(){
	$('a').click(function(){
		var href = $(this).attr("href");
		if( href.substr(0, 19)!="http://www.gemagema" && href.substr(0, 15)!="http://gemagema" && href.substr(0, 1)!="#" ){
			$(this).attr("target", "_blank");
		}
	});
}


/* THUMBNAIL FUNCTIONS */

function show_thumbnails(){
	$(".media-thumb, .facebook-thumb").each(function(){
		var thumb_id_or_url = $(this).find("span").attr("title");

		if( thumb_id_or_url ){
			if( thumb_id_or_url.substr(0, 4) != "http" ){
				// vimeo thumb
				var url = 'http://vimeo.com/api/v2/video/' + thumb_id_or_url + '.json?callback=get_vimeo_thumbnail';
				var js = document.createElement('script');
				js.setAttribute('type', 'text/javascript');
				js.setAttribute('src', url);
				document.getElementsByTagName('head').item(0).appendChild(js);
				
			} else {
				// image thumb
				var target = $(this).find("span");
				var url = thumb_id_or_url;
				get_image_thumbnail( target, url );
			}
		}
	});
}

function get_vimeo_thumbnail(videos){
	var id = videos[0].id;
	if( id ){
		var target = $(".media-thumb, .facebook-thumb").find("span[title="+id+"]");
		var size = target.attr("class");
		var url = (size=="medium") ? videos[0].thumbnail_medium : videos[0].thumbnail_large;
		var only_cache_url = (size=="medium") ? videos[0].thumbnail_large : videos[0].thumbnail_medium;
		url = template_dir +"/show-and-cache.php?url="+ url +"&vimeo_id="+ id + "&size=" + size;
		get_image_thumbnail( target, url );
	}
}

function get_image_thumbnail( target, url ){
	var img = '<img src="'+url+'" alt="" />';
	target.html( img );
	resize_thumbnail( target );
}

function resize_thumbnail( target ){
	target.find("img").each(function(){
		$(this).css("visibility", "hidden");
		$(this).load(function(){
			correct_image_size( $(this) );
		});
	});
	target.removeAttr("title");
}

function correct_image_size( imagem ){
	var original_imagem_height = imagem.height();
	var original_imagem_width = imagem.width();
	var original_container_height = imagem.closest(".media-thumb").innerHeight();

	if(imagem.width() > 0 && imagem.height() > 0 && !imagem.hasClass("resized") ){
		var ratio = original_container_height / original_imagem_height;
		var css_ratio = 100*ratio;
		var valign = -0.5*((original_imagem_width*ratio)-(original_imagem_width));

		if( css_ratio > 100 ){
			imagem.css("width", css_ratio+"%");
			imagem.css("height", "auto");
			imagem.css("margin-left", valign+"px");
			imagem.addClass("resized");
		}
	}
	
	imagem.css("visibility", "visible");
}


/* TWITTER WIDGET */

function twitter_widget(){
	twitter_widget = new TWTR.Widget({
		version: 2,
		type: 'profile',
		rpp: 5,
		interval: 6000,
		width: '100%',
		height: 390,
		id: 'twitter-div',
		theme: {
			shell: {
				background: '#333333',
				color: '#ffffff'
			},
			tweets: {
				background: '#C0C0B4',
				color: '#000000',
				links: '#EC008C'
			}
		},
		features: {
			scrollbar: false,
			loop: false,
			live: false,
			hashtags: true,
			timestamp: true,
			avatars: true,
			behavior: 'all'
		}
	});
	
	twitter_widget.render().setUser('gematv').start();
	
}


/* GEMA SHOTS COLORBOX */

function colorbox_gemashots(){
	$(".attachment .post-info").unbind("click");
	$(".attachment .media-thumb").parent().wrap('<a rel="gallery" href="#" />');

	$("a>.thumb").parent().each(function(){
		$(this).attr( "href", $(this).find(".media-thumb a").attr("href") );
	});
	
	$(".attachment .media-thumb h3").each(function(){
		var text = $(this).text();
		$(this).html( text );
	});

	$("a>.thumb").parent().colorbox({transition: "none", width: "95%", height: "92%", title: function(){ return $(this).find("h3").text();}});
}


/* SLIDER */

function startSlider( context, allowLoop ){
	allowLoop = typeof(allowLoop) != "undefined" ? allowLoop : true;
	
	if( $("#"+context).length != 0 ){
		if( $("#"+context+" .group").length > 1 ){
			var currentSlide = $("#"+context+" .thumb.selected").closest(".group").attr("id");
			currentSlide = ( !currentSlide ) ? context+"-0" : currentSlide;
			sliderScrollTo( context, "#"+currentSlide, true );
			disableButtons( context, allowLoop );
		
			$("#"+context+"-menu .nextpostslink").click(function(evt){
				var target = findNext( context, allowLoop );
				sliderScrollTo( context, target );
				disableButtons( context, allowLoop );
				evt.preventDefault();
			});

			$("#"+context+"-menu .previouspostslink").click(function(evt){
				var target = findPrev( context, allowLoop );
				sliderScrollTo( context, target );
				disableButtons( context, allowLoop );
				evt.preventDefault();
			});
		
			$("#"+context+"-menu .page").click(function(evt){
				var target = $(this).attr("href");
				sliderScrollTo( context, target );
				disableButtons( context, allowLoop );
				evt.preventDefault();
			});

		} else {
			$("#"+context+"-menu .nextpostslink, #"+context+"-menu .previouspostslink").css("visibility", "hidden");
		}
	}
	
}

function sliderCorrectResize( context ){
	if( $("#"+context).length != 0 ){
		if( $("#"+context+"-menu a.selected").length != 0 ){
			sliderScrollTo( context, $("#"+context+"-menu a.selected").attr("href") );
		}
	}
}

function sliderVerticalHeight( context, factor ){
	$("#"+context+".vertical").each(function(){
		$(this).css( "height", (1.5*(factor/2)*$(this).width())+"px" );
	});
}

function sliderScrollTo( context, target, nospeed ){
	var spd = (nospeed) ? 0 : 400;
	$("#"+context).scrollTo( target, spd );
	markSelectedGroup( context, target );
}

function markSelectedGroup( context, target ){
	$("#"+context+" .group").removeClass("selected");
	$(target).addClass("selected");
	markSliderMenu( context, target );
}

function markSliderMenu( context, target ){
	$("#"+context+"-menu a").removeClass("selected");
	$("#"+context+"-menu a[href="+target+"]").addClass("selected");
}

function findSelectedGroup( context ){
	if( !$("#"+context+" .group").hasClass("selected") ){
		$("#"+context+" .slider .group:first-child").addClass("selected");
	}
	return( $("#"+context+" .group.selected").attr("id") );
}

function findNext( context, allowLoop ){
	var total = $("#"+context+" .group").length;
	var selected_id = findSelectedGroup( context );
	var selected_index = parseFloat( selected_id.split('-').pop() );
	if( allowLoop ){
		var target = ( selected_index == total-1 ) ? "#"+context+"-0" : "#"+context+"-"+(selected_index+1);
	} else {
		var target = ( selected_index == total-1 ) ? "#"+context+"-"+selected_index : "#"+context+"-"+(selected_index+1);
	}
	return target;
}

function findPrev( context, allowLoop ){
	var total = $("#"+context+" .group").length;
	var selected_id = findSelectedGroup( context);
	var selected_index = parseFloat( selected_id.split('-').pop() );
	if( allowLoop ){
		var target = ( selected_index == 0 ) ? "#"+context+"-"+(total-1) : "#"+context+"-"+(selected_index-1);
	} else {
		var target = ( selected_index == 0 ) ? "#"+context+"-"+selected_index : "#"+context+"-"+(selected_index-1);
	}
	return target;
}

function disableButtons( context, allowLoop ){
	var total = $("#"+context+" .group").length;
	var selected_id = findSelectedGroup( context);
	var selected_index = parseFloat( selected_id.split('-').pop() );
	if( !allowLoop ){
		if( selected_index == 0 ){
			$("#"+context+"-menu .previouspostslink").addClass("disabled");
			$("#"+context+"-menu .nextpostslink").removeClass("disabled");
		} else if( selected_index == total-1 ){
			$("#"+context+"-menu .previouspostslink").removeClass("disabled");
			$("#"+context+"-menu .nextpostslink").addClass("disabled");
		} else {
			$("#"+context+"-menu .previouspostslink").removeClass("disabled");
			$("#"+context+"-menu .nextpostslink").removeClass("disabled");
		}
	}
}
