//----------------------------------------------------------------------------------------

// Authors : Nadia HADJI & Guillaume Quimper, Influenza marketing http://www.influenza.ca
// Version 1.0
// Created: August 2011

//----------------------------------------------------------------------------------------

//------------------------------------------------------------------------
// Main Nav Functions
//------------------------------------------------------------------------

$(function(){
	
	// Move & Hide Submenus
	$("#navLevel1 li ul").css('top', '24px');
	$("#navLevel2 li ul").css('top', '15px');
	$("#mainNav li ul").hide();
	
	
	//Open submenu
	$("#mainNav li:not('#mainNav li ul li')").hover(
    	function () {
      		$('ul', this).fadeIn(300);
    	}, 
    	function () {
      	$('ul', this).fadeOut(150);
    	}
  	);
  	
  	//Add class to active parent link
	$("#mainNav ul li ul").hover(
		function(){
			$(this).prev("a").addClass("active");
		},
		function() {
			$(this).prev("a").removeClass("active");
		}
	); 
});

//------------------------------------------------------------------------
// Expos & Calendar Listings Functions
//------------------------------------------------------------------------

function getQueryParams(qs) {
    qs = qs.split("+").join(" ");
    var params = {},
        tokens,
        re = /[?&]?([^=]+)=([^&]*)/g;

    while (tokens = re.exec(qs)) {
        params[decodeURIComponent(tokens[1])]
            = decodeURIComponent(tokens[2]);
    }

    return params;
}

$(function(){
	$('#exposListing, #eventsListing').after('<div class="clear"></div>');
	$('#exposListing li div, #eventsListing li div').append('<div class="clear"></div>');
	
	// Hack Some Styles for the Crappy Custom Font
	if ($.client.os == "Mac") {
        $('#viewCalendar').css('line-height', '60px');
        $('#featuredExpos').css('margin-top', '-10px');
        $('#featuredExpos h1, #featuredEvents h1, #featuredExpos h2, #featuredEvents h2').css('margin-bottom', '-7px');
        $('#sideContent.home').css('padding-top', '47px');
    }
 
	
	// Trigger jQueryUI
	var $_GET = getQueryParams(document.location.search);
  	if($_GET.jour != undefined){
  		var defaultDate = $_GET.jour;
  	} else {
  		var defaultDate = null;
  	}
	$( "#calendarSelector" ).datepicker({
		dateFormat: 'dd.mm.yy',
		defaultDate : defaultDate,
		showOtherMonths: true,
		selectOtherMonths: true,
		onSelect: function(dateText, inst) {
  			var curDay = dateText;
  			window.location.replace('?jour=' + curDay); 
  			  			
		}
	});
	
});

//------------------------------------------------------------------------
// General Content Functions
//------------------------------------------------------------------------

$(function(){
	$('#mainContent .colonneDroite').after('<div class="clear"></div>');
	$('#linkHub').append('<div class="topMask"></div>');
	$('#linkHub ul li').append('<span class="bottomLine"></span>');
});


//------------------------------------------------------------------------
// Hub Listings Functions
//------------------------------------------------------------------------

$(function(){
	$('#resultsListing .item').append('<div class="clear"></div>');
	$('#resultsListing .item:odd').addClass('even');
	$('#resultsListing.location .item:last-child').addClass('last');
	$('#resultsListing.location .thumb a').append ('<span class="zoom"></span>');
	$('#archives li:last-child').addClass('last');
});

//------------------------------------------------------------------------
// Exposition's Page Functions
//------------------------------------------------------------------------

$(function(){
	
	// Get Document Language
    var htmlLang = $('html').attr('lang');
    var stripLang = htmlLang.split('-');

	$('.slide a').append ('<span class="zoom"></span>');
	$('#expoPagers, .sponsors, .room').append ('<div class="clear"></div>');
	$('.photoPager').append ('<ul></ul>');
	
	if ($.client.os == "Mac") {
		$('#sectionHeading .expoDates').css('padding', '15px 10px 5px 10px');
	}
	// Slideshow Params
	$('#expoSlideshow, #contentSlideshow').cycle({ 
    	slideExpr: 'div.slide',
    	pause: 1,
    	timeout: 4000,
    	speed: 300,
    	fx: 'scrollHorz',
		pager: '.photoPager ul',
        pagerAnchorBuilder: function(idx, slide) {
        	var s = idx > 8 ? ' style="display:none"' : '';
  			return '<li'+s+'><a href="#"><span class="number">' + (idx+1) + '</span> <span class="title">' + slide.title + '</span></a></li>';
		}
	});
	
	
	function pagerFactory(idx, slide) {
        var s = idx > 8 ? ' style="display:none"' : '';
        return '<li'+s+'><a href="#"><span class="number">'+(idx+1)+'</spans> <span class="title">'+ slide.title +'</span></a></li>';
    };
    
    // Set button labels
    if (stripLang[0] == 'fr') {
    	var startLabel = 'Lancer le diaporama';
    	var stopLabel = 'Arrêter le diaporama';
    } else {
    	var startLabel = 'Start Slideshow';
    	var stopLabel = 'Stop Slideshow';
    }
    
    $('.photoPager').prepend('<a href="#" id="startSlides">'+ startLabel +'</a><a href="#" id="stopSlides">'+ stopLabel +'</a>');
    
    $('#stopSlides').click(function() { 
    	$('#expoSlideshow, #contentSlideshow').cycle('pause');
    	$(this).hide();
    	$('#startSlides').fadeIn('fast');
    	return false;
	});
	
	 $('#startSlides').click(function() { 
    	$('#expoSlideshow, #contentSlideshow').cycle('resume');
    	$(this).hide();
    	$('#stopSlides').fadeIn('fast');
    	return false;
	});
	
	$('#stopSlides').click();
    
    
    /*/ Set Variables for PrettyPhoto
    $('#expoSlideshow .slide a').click(function() {
    	var picDesc = $(this).next().html();
    	$(this).attr('title', picDesc);
  	});*/
  	
  	// Set button labels
    if (stripLang[0] == 'fr') {
    	var closeLabel = 'Fermer';
    	var expandLabel = 'Agrandir';
    	var nextLabel = 'Suivant';
    	var prevLabel = 'Précédent';
    } else {
    	var closeLabel = 'Close';
    	var expandLabel = 'Expand';
    	var nextLabel = 'Next';
    	var prevLabel = 'Previous';
    }
	
	// Open PrettyPhoto
	
    $(".slide a").prettyPhoto({
		opacity: 0.70,
		slideshow: false,
		theme: 'dark_square',
		horizontal_padding: 36,
		deeplinking: false,
		social_tools: false,
		markup: '<div class="pp_pic_holder"> \
						<div class="pp_top"> \
							<div class="pp_left"></div> \
							<div class="pp_middle"></div> \
							<div class="pp_right"><a class="pp_close" href="#">'+ closeLabel +'</a></div> \
						</div> \
						<div class="pp_content_container"> \
							<div class="pp_left"> \
							<div class="pp_right"> \
								<div class="pp_content"> \
									<div class="pp_loaderIcon"></div> \
									<div class="pp_fade"> \
										<a href="#" class="pp_expand" title="'+ expandLabel +'">'+ expandLabel +'</a> \
										<div class="pp_hoverContainer"> \
											<a class="pp_next" href="#">'+ nextLabel +'</a> \
											<a class="pp_previous" href="#">'+ prevLabel +'</a> \
										</div> \
										<div id="pp_full_res"></div> \
										<div class="pp_details"> \
											<h3 class="ppt"></h3> \
											<p class="pp_description"></p> \
										</div> \
									</div> \
								</div> \
							</div> \
							</div> \
						</div> \
						<div class="pp_bottom"> \
							<div class="pp_left"></div> \
							<div class="pp_middle"></div> \
							<div class="pp_right"></div> \
						</div> \
					</div> \
					<div class="pp_overlay"></div>'
	});
	
	$("a[rel^='zoom']").prettyPhoto({
		opacity: 0.70,
		slideshow: false,
		theme: 'dark_square',
		horizontal_padding: 36,
		deeplinking: false,
		social_tools: false,
		markup: '<div class="pp_pic_holder"> \
						<div class="pp_top"> \
							<div class="pp_left"></div> \
							<div class="pp_middle"></div> \
							<div class="pp_right"><a class="pp_close" href="#">'+ closeLabel +'</a></div> \
						</div> \
						<div class="pp_content_container"> \
							<div class="pp_left"> \
							<div class="pp_right"> \
								<div class="pp_content"> \
									<div class="pp_loaderIcon"></div> \
									<div class="pp_fade"> \
										<a href="#" class="pp_expand" title="'+ expandLabel +'">'+ expandLabel +'</a> \
										<div class="pp_hoverContainer"> \
											<a class="pp_next" href="#">'+ nextLabel +'</a> \
											<a class="pp_previous" href="#">'+ prevLabel +'</a> \
										</div> \
										<div id="pp_full_res"></div> \
										<div class="pp_details"> \
											<h3 class="ppt"></h3> \
											<p class="pp_description"></p> \
										</div> \
									</div> \
								</div> \
							</div> \
							</div> \
						</div> \
						<div class="pp_bottom"> \
							<div class="pp_left"></div> \
							<div class="pp_middle"></div> \
							<div class="pp_right"></div> \
						</div> \
					</div> \
					<div class="pp_overlay"></div>'
	});


	//Video Links Numbering
	$("#expoPagers .video a.clip").each(function (i) {
		i = i+1;
		$(this).wrapInner('<span class="title" />');
		$(this).prepend('<span class="number">'+i+'</span> ');
	});
	
	//Audio Links Numbering
	$("#expoPagers .audio a.capsule").each(function (i) {
		i = i+1;
		$(this).wrapInner('<span class="title" />');
		$(this).prepend('<span class="number">'+i+'</span> ');
	});
	
	//Sponsors Logos Equal Heigth
	var tallestLogo = 0;
	$('.sponsors li img').each(function(){
		if($(this).height() > tallestLogo){
			tallestLogo = $(this).height();
		}
	});
	$('.sponsors li, .sponsors li a').height(tallestLogo);
});

//------------------------------------------------------------------------
// Custom Modal for Youtube
//------------------------------------------------------------------------

$(function(){
 $('.multimediaPager .video li a').click(function(e) {
       //Cancel the link behavior  
       e.preventDefault();
	   
	   // Append Modal Markups
	   $('body').append('<div id="mask"></div><div id="youtubeContent" class="modalWindow"><div id="modalContainer"><div id="modalZoom"><div class="closeBox"><a href="#" class="close">Fermer</a></div><div id="youtubeData"></div></div></div></div>');
     
       var embedVideo = $(this).parent().html();
	  
	   $('#youtubeData').empty();
	   $('#youtubeData').html(embedVideo);
	   
	   varMovieTitle = $('#youtubeData span.title').text();
	   varMovieTranscript = $('#youtubeData p.download').html();
	   //console.log(varMovieTranscript);
	   
	   movieID = $(this).attr('href');
	   movieID = movieID.split('http://youtu.be/');
	   movieID = movieID[1];
	   
	   movieEmbed = '<iframe src ="http://www.youtube.com/embed/'+ movieID +'" width="560" height="315" frameborder="no" allowfullscreen></iframe>';
	   
	   $('#youtubeData a.clip').hide();
	   $('#youtubeData').prepend('<h3>'+varMovieTitle+'</h3>');
	   $('#youtubeData').prepend(movieEmbed);
      
 
       //Get the screen height and width  
       var maskHeight = $(document).height();
       var maskWidth = $(window).width();

       //Set height and width to mask to fill up the whole screen  
       $('#mask').css({ 'width': maskWidth, 'height': maskHeight });

       //transition effect          
       $('#mask').fadeTo(100, 0.7);

       //Get the window height and width  
       var winH = $(window).height();
       var winW = $(window).width();

       //Set the popup window to center
	   var boxHeight = $('#youtubeContent').height();
       var boxWidth = $('#youtubeContent').width();
       		
       $('#youtubeContent').css({'height': boxHeight, 'width': boxWidth });
       $('#youtubeContent').css('top', (winH / 4 - $('#youtubeContent').height() / 4) + 20) ;
       $('#youtubeContent').css('left', winW / 2 - $('#youtubeContent').width() / 2);

       //transition effect
       $('#youtubeContent').show();
       
       var startLeft = (boxWidth / 2 - 36);
       var startTop = (boxHeight / 2 - 36);
       
       $('#modalZoom').append('<div id="loader"></div>');
       $('#modalZoom').css({'width': 72, 'height': 72, 'position': 'absolute', 'left': startLeft, 'top': startTop, 'display' : 'block' });
       $('#youtubeData').css('display','none');
       $('#loader').delay(300).fadeOut(150);
       $('#modalZoom').delay(700).animate({'width': boxWidth, 'height': boxHeight, 'left': 0, 'top': 0 }, 200, function() {
  	   		$('#youtubeData').fadeIn(300);
	   });
       
       
       //if close button is clicked  
       $('.modalWindow .close').click(function() {   		
       		$('#mask').fadeOut('fast');
			$('.modalWindow').fadeOut('fast', function() {
				$('#mask').remove();
				$('#youtubeContent').remove();
  			});
       		return false;
       });

    	//if mask is clicked  
    	$('#mask').click(function() {
        	$(this).fadeOut('fast');
        	$('.modalWindow').fadeOut('fast', function() {
				$('#mask').remove();
				$('#youtubeContent').remove();
  			});
		});
		
		// If Window is resized while modal is opened
		$(window).resize(function() {
  			var winH2 = $(window).height();
       		var winW2 = $(window).width();
			
			//Get the screen height and width  
       		var maskHeight2 = $(document).height();
       		var maskWidth2 = $(window).width();

       		//Set height and width to mask to fill up the whole screen  
       		$('#mask').css({ 'width': maskWidth2, 'height': maskHeight2 });
			
			//Set the popup window to center  
       		$('#youtubeContent').css('top', (winH2 / 4 - $('#youtubeContent').height() / 4) + 20);
       		$('#youtubeContent').css('left', winW2 / 2 - $('#youtubeContent').width() / 2);
		});
    }); 
});

//------------------------------------------------------------------------
// Custom Modal for MP3
//------------------------------------------------------------------------

$(function(){
	$('.multimediaPager .audio li a').click(function(e) {
		//Cancel the link behavior  
		e.preventDefault();
	   
		// Append Modal Markups
		$('body').append('<div id="mask"></div><div id="audioContent" class="modalWindow"><div id="modalContainer"><div id="modalZoom"><div class="closeBox"><a href="#" class="close">Fermer</a></div><div id="audioData"></div></div></div></div>');
		
		var embedAudio = $(this).parent().html();
		
		$('#audioData').empty();
	   	$('#audioData').html(embedAudio);
	   	
	   	var AudioTitle = $('#audioData span.title').text();
	   	var AudioSource = $('#audioData a.capsule').attr('href');
	   	var DownloadSource = AudioSource.replace('http://www.macm.org', '../../..');
	   	var AudioFile = DownloadSource.lastIndexOf("/");
	   	var filename = DownloadSource.substr(AudioFile);
	   	var mp3File = filename.substr(1);
	   	
	   	var sourceDomain = document.domain;
	   	if (sourceDomain == '192.168.1.200') {
	   		var sourceDomain = '192.168.1.200:8888/museedart';
	   	}
	   	
	   	$('#audioData a.capsule').hide();
	   	$('#audioData').prepend('<h3>'+AudioTitle+'</h3>');
	   	$('#audioData').prepend('<div id="mediaplayer"></div>');
	   	$('#audioData').prepend('<ul class="hiddenControl"><li><a href="#" onclick="jwplayer().play()">Lancer / arrêter le lecteur</a></li><li><a href="#" onclick="jwplayer().setMute()">Activer / désactiver sourdine</a></li></ul>');
	   	$('#audioData p.download.fr').prepend('Téléchargez le <a href="http://'+sourceDomain+'/wp-content/themes/macm/download.php?file='+DownloadSource+'&filename='+mp3File+'">fichier MP3</a>');
	   	$('#audioData p.download.en').prepend('Download the <a href="http://'+sourceDomain+'/wp-content/themes/macm/download.php?file='+DownloadSource+'&filename='+mp3File+'">MP3 file</a>');
	   	
	   	

	   	
	   	jwplayer('mediaplayer').setup({
    		'flashplayer': 'http://'+sourceDomain+'/wp-content/themes/macm/js/player.swf',
    		'id': 'playerID',
    		'width': '560',
    		'height': '24',
    		'file': ''+AudioSource+'',
    		'controlbar': 'bottom',
    		'modes': [
        		{type: 'html5'},
        		{type: 'flash', src: 'http://'+sourceDomain+'/wp-content/themes/macm/js/player.swf'},
        		{type: 'download'}
    		]
  		});
	   	
	   	
		
		//Get the screen height and width  
       var maskHeight = $(document).height();
       var maskWidth = $(window).width();

       //Set height and width to mask to fill up the whole screen  
       $('#mask').css({ 'width': maskWidth, 'height': maskHeight });

       //transition effect          
       $('#mask').fadeTo(100, 0.7);

       //Get the window height and width  
       var winH = $(window).height();
       var winW = $(window).width();
       
       
       //transition effect          
       $('#mask').fadeTo(100, 0.7);

       //Get the window height and width  
       var winH = $(window).height();
       var winW = $(window).width();

       //Set the popup window to center
	   var boxHeight = $('#audioContent').height();
       var boxWidth = $('#audioContent').width();
       		
       $('#audioContent').css({'height': boxHeight, 'width': boxWidth });
       $('#audioContent').css('top', (winH / 4 - $('#audioContent').height() / 4) + 20) ;
       $('#audioContent').css('left', winW / 2 - $('#audioContent').width() / 2);
       		
       $('#audioContent').css({'height': boxHeight, 'width': boxWidth });
       $('#audioContentt').css('top', (winH / 4 - $('#audioContent').height() / 4) + 20) ;
       $('#audioContent').css('left', winW / 2 - $('#audioContent').width() / 2);

       //transition effect
       $('#audioContent').show();
       
       var startLeft = (boxWidth / 2 - 36);
       var startTop = (boxHeight / 2 - 36);
       
       $('#modalZoom').append('<div id="loader"></div>');
       $('#modalZoom').css({'width': 72, 'height': 72, 'position': 'absolute', 'left': startLeft, 'top': startTop, 'display' : 'block' });
       $('#audioData').css('display','none');
       $('#loader').delay(300).fadeOut(150);
       $('#modalZoom').delay(700).animate({'width': boxWidth, 'height': boxHeight, 'left': 0, 'top': 0 }, 200, function() {
  	   		$('#audioData').fadeIn(300);
	   });
	   
	   //if close button is clicked  
       $('.modalWindow .close').click(function() {   		
       		$('#mask').fadeOut('fast');
			$('.modalWindow').fadeOut('fast', function() {
				$('#mask').remove();
				$('#audioContent').remove();
  			});
       		return false;
       });

    	//if mask is clicked  
    	$('#mask').click(function() {
        	$(this).fadeOut('fast');
        	$('.modalWindow').fadeOut('fast', function() {
				$('#mask').remove();
				$('#audioContent').remove();
  			});
		});
		
		// If Window is resized while modal is opened
		$(window).resize(function() {
  			var winH2 = $(window).height();
       		var winW2 = $(window).width();
			
			//Get the screen height and width  
       		var maskHeight2 = $(document).height();
       		var maskWidth2 = $(window).width();

       		//Set height and width to mask to fill up the whole screen  
       		$('#mask').css({ 'width': maskWidth2, 'height': maskHeight2 });
			
			//Set the popup window to center  
       		$('#audioContent').css('top', (winH2 / 4 - $('#audioContent').height() / 4) + 20);
       		$('#audioContent').css('left', winW2 / 2 - $('#audioContent').width() / 2);
		});
		
    }); 
});

//------------------------------------------------------------------------
// Generate Social Buttons
//------------------------------------------------------------------------

$(function(){
	
	var shareURL = $(location).attr('href');
	var shareTITLE = $('title').text();
	
	// Facebook
	$('.share li.facebook').empty();
	$('.share li.facebook').append('<fb:like href="'+ shareURL +'" send="false" layout="button_count" width="110" show_faces="false" font="arial"></fb:like>');
	
	// Twitter
	$('.share li.twitter').empty();
	$('.share li.twitter').append('<a href="http://twitter.com/share" class="twitter-share-button" data-url="'+ shareURL +'" data-count="horizontal" data-via="macmtl">Tweet</a>');
	
	// Other Services
	$('.share ul').append('<li class="google"><g:plusone size="medium" href="'+ shareURL +'"></g:plusone></li>');
	$('.share ul').append('<li class="add this"><div class="addthis_toolbox addthis_default_style "><a class="addthis_counter addthis_pill_style" addthis:url="'+ shareURL +'" addthis:title="'+shareTITLE+'"></a></div></li>');
});


//------------------------------------------------------------------------
// Menu actif
//------------------------------------------------------------------------

$(function(){
	$('.expos #navLevel1 li.expositions a').addClass('current-menu-parent');
	$('.act #navLevel1 li.activites').addClass('current-menu-parent');
	$('.pub #navLevel2 li.publications').addClass('current-menu-parent');
	$('.com #navLevel2 li.communiques').addClass('current-menu-parent');
});

//------------------------------------------------------------------------
// Sidebar Functions
//------------------------------------------------------------------------
$(function(){
	$(".autoPromo.linked").click(function(){
	  window.location=$(this).find("a").attr("href"); 
	  return false;
	});
});
