$(function(){
	// Production services header
	$('.cycleSlide').cycle({
		fx: 'fade',
		pager:  '#cycleNav'
	});
	
	// Click en thumbs (4 thumbs layout): zoom de la foto mediana
	$('.four-thumbs .thumbs-wrapper .grid_4 a').click(function(e) {
		e.preventDefault(); 		
		var total = $(this).parent().parent().find('.fullVideo').find('ul[id^=slideshow_] li.photo').length;
		var index = $(this).attr('data-index');
		var p = $(this).parent().parent().find('.fullVideo').find('div[id^=nav_] li:eq('+(Number(total)+Number(index))+') a');
		p.click(); 
	});
	
	sublimevideo.ready(function(){
		sublimevideo.onStart(function(sv){
			$(sv.element).closest('.video_wrap').find('.info').hide(); // playlist version
			$(sv.element).closest('.canvas').find('> .info').hide(); // normal version
		});
		sublimevideo.onEnd(function(sv){
			$(sv.element).closest('.video_wrap').find('.info').show();
			$(sv.element).closest('.canvas').find('> .info').show();
			sublimevideo.stop();
		});
	});
	
	/* Four thumbs Slider */
	$('#content .four-thumbs').each(function(){
		$(this).find('.slider').first().show();
	});
	$('#content .four-thumbs').each(function(){
		$(this).find('.sliders-wrapper .grid_4').first().find('a').addClass('active');
	});
	// Click en thumbs de sliders
	$('.four-thumbs .sliders-wrapper .grid_4 a').click(function(e) {
		e.preventDefault();
		
		var index = $(this).attr('data-index');
		$(this).closest('.four-thumbs').find('.sliders-wrapper .grid_4 a').removeClass('active');
		$(this).addClass('active');		
		
		// hide all
		$(this).closest('.four-thumbs').find('.slider').each(function(){
			if ($(this).attr('data-index')!=index) {
				$(this).hide();
			} else {
				$(this).show(); // selected slider
			}
		});
	});
	
	/* Send mail dialog */
	$('.social a.mail').click(function(e){
		e.preventDefault();
		
		var title = $(this).parent().next().find('a').attr('data-title');
		var desc = $(this).parent().next().find('a').attr('data-description');
		var thumb = $(this).parent().next().find('a').attr('data-thumb');
		var link = $(this).parent().next().find('a').attr('href');
		
		// pre-populate form
		$('#share-mail #ShareMailSubject').val(title + ' - garlic.tv');
		$('#share-mail #ShareMailBody').val(desc);
		$('#share-mail #ShareMailLink').val(link);
		
		$("#share-mail").modal({
			overlayClose:true
		});
		
		$('#share-mail form').live('submit', function(e){
			e.preventDefault();
			$(this).ajaxSubmit({
				target:'#share-mail .form'
			});
		});
	});
	
	/* Facebook share */
	$('.social a.facebook').click(function(e){
		e.preventDefault();
		
		/* article info */
		var title = $(this).attr('data-title');
		var desc = $(this).attr('data-description');
		var thumb = $(this).attr('data-thumb');
		var href = $(this).attr('href');
		
		FB.ui(
		  {
		   method: 'feed',
		   name: title,
		   caption: 'Garlic.tv',
		      description: (
				desc
		   ),
		   link: href,
		   picture: thumb,
		  },
		  function(response) {
		    if (response && response.post_id) {
		      alert('¡Gracias por compartirlo con tus amigos!');
		    } else {
		      //alert('Post was not published.');
		    }
		  }
		);
	});
	
	// Main menu
	$('#header .logo ul.main li').first().click(function(e){
		e.preventDefault();
		if ($('#header ul.directors').css('display') == 'none') {
			$('#header .logo ul.directors').fadeToggle();
		}
	});
	// Rebolucion submenu
	$('#header ul.directors li.rebo a').click(function(e){
		e.preventDefault();
		if ($('#header ul.rebolucion').css('display') == 'none') {
			$('#header .logo ul.rebolucion').fadeToggle();
		}		
	});
	
	// footer form
	$('#footer .newsletter form').live('submit', function(e){
		e.preventDefault();
		$(this).ajaxSubmit({
			target: '#footer .newsletter .form'
		});
	});
	
	/* two columns video roll-over */
	$('.slot .info > a').mouseover(function(){
		$(this).closest('.grid_16').find('.roll-over').css('display','block');
	});
	$('.slot .info > a').mouseout(function(){
		$(this).closest('.grid_16').find('.roll-over').css('display','none');
	});
	
	/* #singleVideo excepcion (roll-over) */
	$('#singleVideo .wrapper a').mouseover(function(){
		$(this).closest('#singleVideo').find('.roll-over').css('display','block');
	});
	$('#singleVideo .wrapper a').mouseout(function(){
		$(this).closest('#singleVideo').find('.roll-over').css('display','none');
	});
	
	// Rollover PLAY button
	$('.directors .slot .bg a').mouseover(function(){
		$(this).parent().parent().find('.roll-over').css('display','block');
	});
	$('.directors .slot .bg a').mouseout(function(){
		$(this).parent().parent().find('.roll-over').css('display','none');
	});
});
