$(function(){
	$('.big-img').parent().css({'position':'relative', 'width':'369px', 'height':'216px', 'margin':'0 15px 25px 0'}).append('<span class="big-frame" />');
	$('.small-img').parent().css({'position':'relative', 'width':'166px', 'height':'164px', 'margin':'0 15px 25px 0'}).append('<span class="small-frame" />');
	$('a.video').append('<span class="thumb-video-overlay"></span>');
	
	$('a>img').parent().addClass('noback');
	
	$('.comment-text').expander({
	  slicePoint: 250, 
	  widow: 10,
	  expandText: ' Llegir mes...', 
	  userCollapseText: ' Col.lapsar'
	});
	
	$('.comment-button').click(function() {
		var form = $(this).attr('href');
		$('#comment-type-selector').togle();
		$(form).togle();
		
		if (form == '#type-foto'){
			var src_value = $("#IMGID_foto").attr("src");
			if (src_value == ""){
				$("#IMGID_foto").attr("src","http://www.tv3.cat/su/shared/image.jsp?imgSubId=2&imgId=341790");
			}
		}

		if (form == '#type-video'){
			var src_value = $("#IMGID_video").attr("src");
                        if (src_value == ""){
                        	$("#IMGID_video").attr("src","http://www.tv3.cat/su/shared/image.jsp?imgSubId=3&imgId=341790");
			}
                }

		if (form == '#type-text'){
			var src_value = $("#IMGID_text").attr("src");
                        if (src_value == ""){
	                        $("#IMGID_text").attr("src","http://www.tv3.cat/su/shared/image.jsp?imgSubId=1&imgId=341790");
			}
                }

		$('#comenta').scrollTo(500);
		return false;
	});
	
	$('.select-type-button').click(function() {
		$(this).parents('.type-form:first').togle();
		$('#comment-type-selector').togle();
		return false;
	});
	
	$('.text-input').fillvalue().siblings('span').hide();
	
	$('.foto-file').filestyle({ 
		image: "/espaiterra/img/choose-foto.gif",
		imageheight : 23,
		imagewidth : 130,
		width : 120
 	});

	$('.video-file').filestyle({ 
		image: "/espaiterra/img/choose-video.gif",
		imageheight : 23,
		imagewidth : 130,
		width : 120
 	});

	$('#reportatge .media-charger').click(function() {
		var $parent = $(this).parent();
		var selected_media = $parent.parent().siblings('.hidden').html();
		var selected_info = $parent.parents('.media-info').clone();
		
		$('.selected').fadeTo(200, 1).removeClass('selected');
		$(this).parents('.media-list-item').addClass('selected').fadeTo(200, .6);
		$('#video-container').html(selected_media);
		$('#main').scrollTo(500);
		$('#media-info').html(selected_info);
		$('#media-info h3').html($('#media-info h3 a').text());
		$('#media-info .date').show();
		$('#media-info .video-image').hide();
		return false;
	});
	
	$('#media-list').equalHeights(true);
	
	$('.media-list-item').click(function() {
		if($('#reportatge').length) {
			$(this).find('.media-charger').click();
		} else {
			window.location = $(this).find('a').attr('href');
		}
	});

	$('#reportatge .media-charger:first').click();

	$('.show-podcast-info').click(function() {
		$(this).parent().siblings('#podcast-info').slideToggle();
		return false;
	});

	$('.show-podcast-resources').click(function() {
		$(this).parent().siblings('#podcast-resources').slideToggle();
		return false;
	});

	$('a[href*=#comenta]').click(function() {
		var $target = $(this.hash);
		$target.scrollTo(500);
	});
	
	$('a.external').click(function() {
		this.target = "_blank";
	});
	
	$('.foto-contrasenya, .text-contrasenya, .video-contrasenya').focus(function(){
		$(this).parent().html('<input type="password" id="'+$(this).attr('id')+'" name="'+$(this).attr('name')+'" value="" class="text-input" />');
		$('#'+$(this).attr('id')).focus();
	});
	
	$('.image-list li:last').css('border', 'none');
	var activeText = $('ul.tabs .active').text();
$('ul.tabs .active').html('<span>' + activeText + '</span>');
});

jQuery.fn.extend({
	scrollTo: function(speed, easing) {
    	return this.each(function() {
      		var targetOffset = jQuery(this).offset().top;
      		jQuery('html, body').animate({scrollTop: targetOffset}, speed, easing);
    	});
  	},
	togle: function() {
		if(jQuery.browser.msie && jQuery.browser.version == 6.0) {
			jQuery(this).toggle();
		} else {
			jQuery(this).slideToggle();
		}
		return this;
	},
	light: function() {
		var content = this.siblings('.hidden');
		return this.attr('href', '#' + content.attr('id'));
	},
	fillvalue: function() {
		this.each(function(){
			var value = $(this).parent().find('span').html();
			jQuery(this).attr('value', value);
		});
		return this;
	},
	equalHeights: function(px) {
		jQuery(this).each(function(){
			var currentTallest = 0;
			jQuery(this).children('li').each(function(i) {
				if (jQuery(this).height() > currentTallest) { 
					currentTallest = jQuery(this).height(); 
				}
			});

			if (jQuery.browser.msie && jQuery.browser.version == 6.0) { 
				jQuery(this).children('li').not('.clear').css({'height': currentTallest}); 
			}
			jQuery(this).children('li').not('.clear').css({'min-height': currentTallest}); 
		});
		return this;
	}
});

function permalink(idvideo){
	//permalink
	document.getElementById("PERMALINK").value = "http://www.tv3.cat/videos/"+jQuery(".desc_fl > span", idvideo).text();
}

function mostraLlegendaYouTube(){					
	$("#llegenda_ytube").toggle("fast");
}

