heap.api.ridm = {};
heap.api.setup({
  object: heap.api.ridm,
  api_class:'ridmapi',
  url: heap.urlmap.json,
  methods: ['subscribe','unsubscribe']
});

$(document).ready(function(){
	
	$('.js-read-more').bind('click',function(e){
    e.preventDefault();
    $('.expend').hide();
    $('.js-read-more').show();
    $(this).parent().find('.expend').fadeIn();
    $(this).hide();
	});
	
	$('.js-read-less').bind('click',function(e){
    e.preventDefault();
    $(this).parent().hide();
    $('.js-read-more').show();
	});
	
	
	/* Menu */
	$('.menu li.top').bind('mouseenter',function(){
	   if(!$(this).hasClass('on')){
	     $('.sousmenu.on').hide();
	   }
	}).bind('mouseleave',function(){
	   if(!$(this).hasClass('on')){
	     $('.sousmenu.on').show();
	   }
	});
	
	/*
	 * Click dans le champ de recherche du header
	 */
	var lbl_head_Fr = 'Rechercher dans tout le site';
	var lbl_head_En = 'Search';
	var lbl_head_now = '';
	
	$('.recherche .q').bind('focus', function() {
		if($(this).val() == lbl_head_Fr || $(this).val() == lbl_head_En){
			lbl_head_now = $(this).val();
			$(this).val('');
		} 
	}).bind('blur', function() {
		if($(this).val() == ''){
			$(this).val(lbl_head_now);
		} 
	});
	
	$('.error .la_recherche').bind('focus', function() {
		if($(this).val() == lbl_head_Fr || $(this).val() == lbl_head_En){
			lbl_head_now = $(this).val();
			$(this).val('');
		} 
	}).bind('blur', function() {
		if($(this).val() == ''){
			$(this).val(lbl_head_now);
		} 
	});

	
	/*
	 * hover sur link page grille
	 */
	/*$('#content.programmation.grille .la_grille .section_salle a.link').mouseover(function(){
		$('#content.programmation.grille .la_grille .section_salle').css('background-color','#3375ab');
		Cufon.refresh('.gotham_x_bold');
	});
	$('#content.programmation.grille .la_grille .section_salle a.link').mouseout(function(){
		$('#content.programmation.grille .la_grille .section_salle').css('background-color','#6696c0');
		Cufon.refresh('.gotham_x_bold');
	});*/
	
	/*
	 * Click dans le champ de l'abonnement a la newsletter
	 */
	var lbl_news_Fr = 'Votre courriel...';
	var lbl_news_En = 'Your email...';
	var lbl_news_now = '';
	
	$('.infolettre .q').bind('focus', function() {
		if($(this).val() == lbl_news_Fr || $(this).val() == lbl_news_En){
			lbl_news_now = $(this).val();
			$(this).val('');
		} 
	}).bind('blur', function() {
		if($(this).val() == ''){
			$(this).val(lbl_news_now);
		} 
	});
	$('.infolettre .submit').bind('mouseover',function(){
	   $('.infolettre').addClass('hover');
	}).bind('mouseleave',function(){
	   $('.infolettre').removeClass('hover');
	});
	/*
	 * Click dans le champ de l'abonnement a la newsletter
	 */
	var lbl_rechFilm_Fr = 'Titre ou Cinéaste';
	var lbl_rechFilm_En = 'Titre ou Cinéaste';
	var lbl_rechFilm_now = '';
	
	$('#content.programmation.listing .top form .text').bind('focus', function() {
		if($(this).val() == lbl_rechFilm_Fr || $(this).val() == lbl_rechFilm_En){
			lbl_rechFilm_now = $(this).val();
			$(this).val('');
		} 
	}).bind('blur', function() {
		if($(this).val() == ''){
			$(this).val(lbl_rechFilm_now);
		} 
	});
	

	/*
	 * hover sur le block image
	 */
	$('.block_image').mouseover(function(){
		var h = $(this).find('.text').height();
		h = h+20;
		$(this).find('.container').stop().animate({ 
			height: h
	    }, 250 );
	});
	$('.block_image').mouseout(function(){
		$(this).find('.container').stop().animate({ 
			height: '100px'
	    }, 250 );
	});
	
	
	/*
	 * carousel photo
	 */
	$('.thumb_img').click(function(){
		var img = $(this).attr('rel');
		$('#photo').attr('src',img);
		
		var cr = $(this).find('img').attr('alt');
		
			if(heap.config.current_language == "fr"){
				if(cr != '')
					$('.credit').text('Crédit photo: ' + cr);
				else
					$('.credit').text('');
			}else{
				if(cr != '')
					$('.credit').text('Credit: ' + cr);
				else
					$('.credit').text('');
			}
		return false;
	});

	/*
	 * block photo video
	 */
	$('.block_photo_video .head a').mouseover(function(){
		if($(this).hasClass('off')){
			$(this).css('color','#676767');
			Cufon.refresh('.gotham_bold');
		}
	});
	$('.block_photo_video .head a').mouseout(function(){
		if($(this).hasClass('off')){
			$(this).css('color','#fff');
			Cufon.refresh('.gotham_bold');
		}
	});
	$('.block_photo_video .head a').click(function(){
		if($(this).hasClass('off')){
			$('.block_photo_video .head a').addClass('off');
			$('.block_photo_video .head a').css('color','#fff');
			$(this).removeClass('off');
			$(this).css('color','#676767');
			Cufon.refresh('.gotham_bold');
		}else{
			return false;
		}
	});
	
	/**
	 * Generic tabs
	 */
  $('.js-tabs').each(function()
  {
    var tab_holder = $(this);
    tab_holder.find('.js-tab').hide().eq(0).show();
    tab_holder.find('.js-tabs-head a').bind('click', function()
    {
      var id = $(this).attr('rel');
      tab_holder.find('.js-tab').hide();
      tab_holder.find('#' + id).show();
      return false;
    });
	});
	
	
	
	
	
	
	/*
	 * Block info page grille
	 */
	var toClose = false;
	
	function getOffset( el ) {
      var _x = 0;
      var _y = 0;
        while( el && !isNaN( el.offsetLeft ) && !isNaN( el.offsetTop ) ) {
        _x += el.offsetLeft - el.scrollLeft;
        _y += el.offsetTop - el.scrollTop;
        el = el.parentNode;
      }
      return { top: _y, left: _x };
    };

	
	$('#content.grille .une_seance').click(function(e){
	
		$('#content.grille .une_seance .info').hide();
		$('#content.grille .une_seance').removeClass('on');
		
		
		var parent = $(this).parent();
		var offset_p = parent.offset();
		var offset_m = $(this).offset();
		var ref = $('.section_salle').offset();
		
		top_p = offset_p.top;
		left_m = $(this).position().left;
		left_ref = ref.left;
		
		refTotal = left_ref + 254 + 9; // soit la largeur + padding
			
		info_height = $(this).find('.info').height();
		info_width = $(this).find('.info').outerWidth(); // good
		
		espaceTop = top_p - 419;
		espaceBas = 663 - espaceTop;
		
		if(espaceBas < info_height){
			toMove = info_height - espaceBas + 8;
			$(this).find('.info').css('top','-'+toMove+'px');
		}else{
			$(this).find('.info').css('top','0px');
		}

// vérification de la largeure de la grille
		var ccc = 1568 ;
		if( (left_m + info_width) > ccc){
		
//			if( (left_m + info_width) > 1466){
			toMove = left_m + info_width - ccc +8;
			$(this).find('.info').css('left','-'+toMove+'px');
			//console.log('large');
		}else{
			if(left_m < refTotal){
				//console.log('proche');
				toMove = refTotal - left_m;
				$(this).find('.info').css('left',''+toMove+'px');
			}else{
				$(this).find('.info').css('left','0px');
			}
		}
			
			
			
			
			
			
		var doc_actu = heap.urlmap.docircuitmtl_actualite;
		if($(this).hasClass('doc')){
			window.location.href = doc_actu ;
		}else{
			$(this).addClass('on');
			$(this).find('.info').show();
		}
		
		
		
		
		if(!$(e.target).hasClass('title'))
		return false;
	});
	$('#content.grille .une_seance .quit').click(function(){
		$('#content.grille .une_seance .info').hide();
		$('#content.grille .une_seance').removeClass('on');
		toClose = true;
		return false;
	});
	$('#content.grille .une_seance .see_more').click(function(){
		var myLocation = $(this).attr('href');
		
		//console.log(myLocation);
		window.location.href = myLocation;
		return true;
	});
	
	/*
	 * Inscription 
	 */
	var hideWindow = function(who)
	{
		$('#doc_step_1').hide();
		$('#doc_step_2').hide();
		$('#doc_step_3').hide();
		
		if(who == 'btn_step1')
		{
			$('#doc_step_1').show();
		}
		else if(who == 'btn_step2')
		{
			$('#doc_step_2').show();
		}
		else if(who == 'btn_step3')
		{
			$('#doc_step_3').show();
		}
		
		window.scrollTo(0,0);
	}
	$('.btn_step1').click(function(){
		hideWindow('btn_step1');
	});
	$('.btn_step2').click(function(){
		hideWindow('btn_step2');
	});
	$('.btn_step3').click(function(){
		hideWindow('btn_step3');
	});

	$('#i_payment_mode_paypal').click(function(){
		$('#doc_step_3 .more').hide();
		$('#doc_step_3 .paypal').show();
	});
	$('#i_payment_mode_check').click(function(){
		$('#doc_step_3 .more').hide();
		$('#doc_step_3 .cheque').show();
	});
	
	/*
	 * Formulaire accreditation
	 */
	var d_static = heap.urlmap.static;
	
	$('#content.accreditation .submit').mouseover(function(){
		if($(this).hasClass('fr'))
		{
			$(this).attr('src',d_static+'/images/form/submit_on_fr.gif');
		}
		else
		{
			$(this).attr('src',d_static+'/images/form/submit_on_en.gif');
		}
	}).mouseout(function(){
		if($(this).hasClass('fr'))
		{
			$(this).attr('src',d_static+'/images/form/submit_fr.gif');
		}
		else
		{
			$(this).attr('src',d_static+'/images/form/submit_en.gif');
		}
	});
	
	/*
	 * Formulaire connexion medias
	 */
	var d_static = heap.urlmap.static;
	
	$('#content.telechargements .submit').mouseover(function(){
		if($(this).hasClass('fr'))
		{
			$(this).attr('src',d_static+'/images/form/connexion_on_fr.gif');
		}
		else
		{
			$(this).attr('src',d_static+'/images/form/connexion_on_en.gif');
		}
	}).mouseout(function(){
		if($(this).hasClass('fr'))
		{
			$(this).attr('src',d_static+'/images/form/connexion_fr.gif');
		}
		else
		{
			$(this).attr('src',d_static+'/images/form/connexion_en.gif');
		}
	});
	
	var timeGrille = '4'; 
	var changeTimeGrille = function(direction){
    if(timeGrille == '4'){
      if(direction == 'left'){
        $('#content.programmation.grille .la_grille .section_grille .container').stop().animate({ 
				marginLeft: "-587px"
		    }, 500 );
		    $('#content.programmation.grille .la_grille .section_salle a.link.next').removeClass('off');
		    timeGrille = '3';
      }
    }
    else if(timeGrille == '3'){
      if(direction == 'left'){
        $('#content.programmation.grille .la_grille .section_grille .container').stop().animate({ 
				marginLeft: "-391px"
		    }, 500 );
		    $('#content.programmation.grille .la_grille .section_salle a.link').removeClass('off');
		    timeGrille = '2';
      }else{
       $('#content.programmation.grille .la_grille .section_grille .container').stop().animate({ 
				marginLeft: "-783px"
		    }, 500 );
		    $('#content.programmation.grille .la_grille .section_salle a.link.next').addClass('off');
		    timeGrille = '4';
      }
    }
    else if(timeGrille == '2'){
      if(direction == 'left'){
        $('#content.programmation.grille .la_grille .section_grille .container').stop().animate({ 
				marginLeft: "-196px"
		    }, 500 );
		    $('#content.programmation.grille .la_grille .section_salle a.link').removeClass('off');
		    timeGrille = '1';
      }else{
        $('#content.programmation.grille .la_grille .section_grille .container').stop().animate({ 
				marginLeft: "-587px"
		    }, 500 );
		    $('#content.programmation.grille .la_grille .section_salle a.link').removeClass('off');
		    timeGrille = '3';
      }
    }
    else if(timeGrille == '1'){
      if(direction == 'left'){
        $('#content.programmation.grille .la_grille .section_grille .container').stop().animate({ 
				marginLeft: "0px"
		    }, 500 );
		    $('#content.programmation.grille .la_grille .section_salle a.link.prev').addClass('off');
		    timeGrille = '0';
      }else{
        $('#content.programmation.grille .la_grille .section_grille .container').stop().animate({ 
				marginLeft: "-391px"
		    }, 500 );
		    $('#content.programmation.grille .la_grille .section_salle a.link').removeClass('off');
		    timeGrille = '2';
      }
    }else if(timeGrille == '0'){
      if(direction == 'right'){
        $('#content.programmation.grille .la_grille .section_grille .container').stop().animate({ 
				marginLeft: "-196px"
		    }, 500 );
		    $('#content.programmation.grille .la_grille .section_salle a.link').removeClass('off');
		    timeGrille = '1';
      }
    }
	}
	
	$('#content.programmation.grille .les_infos a.select_time').click(function(){
		if($(this).hasClass('on')){
			return false;
		}
		$('#content.programmation.grille .la_grille .section_salle a.link').each(function(){
			if($(this).hasClass('off')){
				$(this).removeClass('off')
			}else{
				$(this).addClass('off')
			}
		});
		
		$('#content.programmation.grille .les_infos a.select_time').removeClass('on');
		$(this).addClass('on');
		changeTimeGrille();
		return false;
	});
	
	$('#content.programmation.grille .la_grille .section_salle a.link').click(function(){
		$('#content.programmation.grille .les_infos a.select_time').each(function(){
			if($(this).hasClass('on')){
				$(this).removeClass('on')
			}else{
				$(this).addClass('on')
			}
		});
		if($(this).hasClass('prev')){
		  changeTimeGrille('left');
		}else{
		  changeTimeGrille('right');
		}
		return false;
	});
	
	$('#carousel a').click(function(){
		var caption = $(this).attr('title');
		$('#captionPhoto').text(caption);
	});
	var lirePlusFr = "lire plus";
	var lirePlusEn = "view more";
	var lireMoinsFr = "réduire";
	var lireMoinsEn = "view less";
	$('#content.programmation.index .see_more_text').click(function(){
		if($(this).hasClass('open')){
			$(this).parent().find('.cache').fadeOut(300);
			$(this).removeClass('open');
			//console.log($(this).text());
			
			if($(this).text() == lireMoinsFr){
				//console.log('1')
				$(this).text('lire plus');
			}else if($(this).text() == lireMoinsEn){
				//console.log('2')
				$(this).text('view more');
			}
			return false;
		}
		
		$(this).addClass('open');
		if($(this).text() == lirePlusFr){
			$(this).text(lireMoinsFr);
		}else if($(this).text() == lirePlusEn){
			$(this).text(lireMoinsEn);
		}
		
		$(this).stop().parent().find('.cache').fadeIn(300);
		return false;
	});
	
	/*
	 * target blank sur a du sous-menu
	 */
	$('.menu a[href^="http://"]').attr('target', '_blank');
	$('.accueil .text-bloc a[href^="http://"]').attr('target', '_blank');
	
	
	/*
	 * diapo home
	 */
	$('#slider .inner a').click(function(){
		clearTimeout(t2);
		clearTimeout(starterSlider);
		var position = $(this).attr('href');
		$('#slider .diapo_inner').stop().animate( { left:"-"+position+"px" }, 1000, 'swing' );
		$('#slider .inner a').removeClass('selected');
		$(this).addClass('selected');
		
		if($(this).hasClass('fleche_1')){
			slideNow = "1";
			slideNext = "1"
		}else if($(this).hasClass('fleche_2')){
			slideNow = "2";
			slideNext = "2"
		}else if($(this).hasClass('fleche_3')){
			slideNow = "3";
			slideNext = "3"
		}else if($(this).hasClass('fleche_4')){
			slideNow = "4";
			slideNext = "4"
		}else if($(this).hasClass('fleche_5')){
			slideNow = "5";
			slideNext = "5"
		}
		startSlider();
		return false;
	});
	/*
	* popup newsletter
	*/
	$('#bloc_newsletter #successMadmimi p.success a.quit').click(function(){
		return false;
	});
	
	
	/*
	 * Menu
	 
	var checkWhere = function(where){
		var a = '' + where; 
		var r = false;
		$(a).mouseover(function(){
			r = true;
			console.log($("#sousmenu .m1"));
		});
		console.log(a);
		return r;
	}
	
	$('.menu li').mouseover(function(){
		if($(this).hasClass('on'))
			return false;
		for(i=1;i<=5;i++){
			if($(this).hasClass('m'+i)){
				b = 'm'+i;
			}
			//console.log('test');
		}
		h = '#sousmenu .' + b;
		$('#sousmenu .selected').css('display','none');
		$(h).css('display','block');
		//console.log(h);
	}).mouseout(function(){
		if(checkWhere(h) == false){
			if($(this).hasClass('on'))
				return false;
			$('#sousmenu .selected').css('display','block');
			$(h).css('display','none');
		}
	});*/
});

//slider auto
var slideNow = "1";
var slideNext = "1"
var t2 = "";
var starterSlider = "";

var loop_slider = function(){
	changeSlide();
}
var changeSlide = function(){
	//bouge a coup de 630 px
	slideNow++;
	slideNext++;
	
	if(slideNext > nbSlide){
		slideNext = slideNow = 1;
	}
	
	var position = slideNow * 630 - 630;
	
	
	$('#slider .diapo_inner').stop().animate( { left:"-"+position+"px" }, 1000, 'swing' );
	$('#slider .inner a').removeClass('selected');
	
	var flecheNow = '#slider .inner a.fleche_'+slideNext;
	$('#slider .inner a.fleche_').removeClass('selected');
	$(flecheNow).addClass('selected');
	
	
	//console.log(nbSlide);
	t2=setTimeout("testTimeOut()",7000);
	
}
var testTimeOut = function (){
	//console.log('from t2');
	loop_slider();
}
var testTimeOut2 = function (){
	//console.log('from starterSlider');
	loop_slider();
}

var startSlider = function (){
	//console.log('startSlider')
	starterSlider = setTimeout("testTimeOut2()",8000);
}


/*
* popup newsletter
*/
var callClosePopupNews = function (){
	cl=setTimeout("closePopupNews(1500)",5000);
}
var openPopup = function(text){
	$('.messagebox .js-text').html(text);
	$('.messagebox').show();
	$('.lightbox-wrapper').fadeIn();
	$('.messagebox .close').bind('click',closePopup);
}
var closePopup = function(){
	$('.lightbox-wrapper').fadeOut();
	$('.messagebox').hide();
}



var initRadioSubmit=function()
{
  $('.radio').bind('click',function(e){
    if($(this).hasClass('active')){
      return false;
    }else{
      var name = $(this).find('input').attr('name');
      $('input[name="'+name+'"]').parent().removeClass('active');
      $('input[name="'+name+'"]').attr('checked','');
      $(this).addClass('active');
      $(this).find('.radio').attr('checked','checked');
    }
    
  });
}

var initCheckboxSubmit=function()
{
  $('.checkbox').bind('click',function(e){
    if($(this).hasClass('active')){
      $(this).removeClass('active');
      $(this).find('.checkbox').attr('checked','');
    }else{
      $(this).addClass('active');
      $(this).find('.checkbox').attr('checked','checked');
    }
  });
}

var interval = null;

var initCarousel = function(){

  $('.js-carousel .js-arrow').bind('click',arrowClick);
  $('.js-carousel .js-arrow').bind('mouseenter',arrowHover);
  $('.js-carousel .js-arrow').bind('mouseleave',arrowLeave);
  
  interval = setInterval(nextCarousel,6000);
  
}

var arrowHover = function(){
  if(!$(this).hasClass('active')){
    $(this).find('img').attr('src', heap.urlmap.static+'/images/2011/home/fle_hover.png')
  }
}

var arrowLeave = function(){
  if(!$(this).hasClass('active')){
    $(this).find('img').attr('src', heap.urlmap.static+'/images/2011/home/fle.png')
  }
}

var nextCarousel = function(){
  
  var _nextI = $('.js-media .active').next();
  var _nextT = $('.js-info .active').next();
  var _nextF = $('.arrow .active').next();
  
  if($(_nextI).length < 1){
    _nextI = $('.js-media li:eq(0)');
    _nextT = $('.js-info li:eq(0)');
    _nextF = $('.arrow a:eq(0)');  
  }
  
  //fleche
  $('.js-arrow.active').find('img').attr('src', heap.urlmap.static+'/images/2011/home/fle.png');
  $('.js-arrow.active').removeClass('active');
  
  $(_nextF).addClass('active');
  $(_nextF).find('img').attr('src', heap.urlmap.static+'/images/2011/home/fle_hover.png');
  //media
  var elm = $('.js-media .active');
  elm.hide();
  $('.js-media .active').removeClass();
  $(_nextI).fadeIn('800').addClass('active');
  //info
  var info = $('.js-info .active');
  info.hide();
  $('.js-info .active').removeClass();
  $(_nextT).fadeIn('800').addClass('active');
  
}

var arrowClick = function(e){
  e.preventDefault();
  
  // check si deja sur image
  if($(this).hasClass('active'))
    return false;
  
  clearInterval(interval)
  
  // changer le bouton actif
  $('.js-arrow.active').find('img').attr('src', heap.urlmap.static+'/images/2011/home/fle.png');
  $('.js-arrow.active').removeClass('active');
  $(this).addClass('active');
  $(this).find('img').attr('src', heap.urlmap.static+'/images/2011/home/fle_hover.png')
  
  // changer l'image
  var elm = $('.js-media .active');
  elm.hide();
  $('.js-media .active').removeClass();
  //elm.next().fadeIn('800').addClass('active');
  $('.js-media li:eq('+$(this).attr('rel')+')').fadeIn('800').addClass('active');
  
  // change linfo
  var info = $('.js-info .active');
  info.hide();
  $('.js-info .active').removeClass();
  $('.js-info li:eq('+$(this).attr('rel')+')').fadeIn('800').addClass('active');
  
  interval = setInterval(nextCarousel,6000);
}

var lb = {};

var lightbox = function(params){
	lb.type = params.type;
	lb.itemclass = params.item;
	lb.parent = $(lb.itemclass).parent();
	
	if(lb.type == 'photo'){
		var winWidth = $(window).width();
		var winHeight = $(window).height();
		
		$('.js-lightbox.js-photo').addClass('active');
		//$('.js-lightbox img').css('max-width',winWidth-50+'px');
		//$('.js-lightbox img').css('max-height',winHeight-200+'px');
		
		$(lb.itemclass).bind('click',openLightbox);
		$('.js-lightbox .js-close').bind('click',closeLightbox);
		$('.js-lightbox .js-next').bind('click',nextPhoto);
		$('.js-lightbox .js-prev').bind('click',prevPhoto);
	}
	
	if(lb.type == 'video'){
		lb.player = params.player;
		$('.js-lightbox.js-video').addClass('active');
		
		$(lb.itemclass).bind('click',openVideoLightbox);
		$('.js-lightbox .js-close').bind('click',closeLightbox);
	}
}

var openLightbox = function(e){
	e.preventDefault();
	lb.currentItem = $(this);
	changeLightboxContent(lb.currentItem);
	$('.lightbox-wrapper').fadeIn('500',centerLightbox);
}

var closeLightbox = function(e){
	e.preventDefault();
	$('.lightbox-wrapper').fadeOut();
	$('.js-lightbox #youtube').html('');
}
var nextPhoto = function(e){
	e.preventDefault();
	var next = lb.currentItem.next();
	if(next.length != 0){
		lb.currentItem = next;
	}else{
		lb.currentItem = lb.parent.find('a:eq(0)');
	}
	
	changeLightboxContent(lb.currentItem);
}

var prevPhoto = function(e){
	e.preventDefault();
	var prev = lb.currentItem.prev();
	
	if(prev.length != 0){
		lb.currentItem = prev;
	}else{
		var total = lb.parent.children().length;
		total = total-1;
		lb.currentItem = lb.parent.find('a:eq('+total+')');
	}
	changeLightboxContent(lb.currentItem);
}

var changeLightboxContent = function(item){
	var img = item.attr('href');
	var title = item.attr('rel');
	var credit = item.attr('alt');
	$('.js-lightbox .js-title').text(title);
	$('.js-lightbox .js-credit-text').text(credit);
	$('.js-lightbox .js-image').attr('src', img);
	centerLightbox();
}

var openVideoLightbox = function(e){
	e.preventDefault();
	lb.currentItem = $(this);
	var title = $(this).attr('alt');
	
	$('.js-lightbox .js-title').text(title);
	$('.js-lightbox .js-credit').hide();
	
	if($(this).hasClass('youtube')){
	 var html = '<iframe width="630" height="350" src="http://www.youtube.com/embed/'+ $(this).attr('href')+'" frameborder="0" allowfullscreen></iframe>';
	 $('#player').hide();
	 $('.js-lightbox #youtube').html(html).show();
	}else{
		$('#player').show();
    $('.js-lightbox #youtube').hide();
	   embedVideo(lb.currentItem);
	}
		centerLightbox();
	
	$('.lightbox-wrapper').fadeIn();
}

var embedVideo = function(item){
	var $slug = '';
	var height = '340';
	var width = '630';

	var params = 	{	allowScriptAccess: 'always',
									allowFullscreen: 'true',
									wmode: 'opaque',
									bgcolor: '#000'
								};
	var flashvars =	{	previewImageURL: item.attr('rel'),
										videoURL: item.attr('href'),
										scaleMode:'16:9',
										buttonsColor: 'blue'
									};
	var urlPlayer = lb.player;
	swfobject.embedSWF(urlPlayer, 'player', width, height, "9.0.0", null, flashvars, params, {id: 'player', name: 'player'});
	//$(this).unbind('click');
}

var centerLightbox = function(){
	var winHeight = $(window).height();
	var lightboxHeight = $('.js-lightbox').innerHeight();
	if (lb.type == 'video'){
  	$('.js-lightbox').css('margin-top', (winHeight-lightboxHeight)/4);
  }else{
  	$('.js-lightbox').css('margin-top', (winHeight-lightboxHeight)/2);
  }
  
}

var selectInput = function(mclass){
  $(mclass + ' select').change(function(){
    var str = "";
    $(mclass + " select option:selected").each(function () {
      str += $(this).text() + " ";
    });
    $(mclass + ' span').text(str);
  });
}

var reloadForm = function(id){
  $(id).submit();
}
