/**
 * @author Jean-Philippe Veilleux
 */

$(document).ready(function(){
    
    //#####################################################################################################
    //##  Foncitons générales  ############################################################################
    
    $('#profile-table-content td table tr:even').each(function(){
       if(!$(this).is('.not-colored')){
           $(this).addClass('even-tr');
       } 
    });
    
    function display_date(){
        mydate= new Date()
        theyear=mydate.getFullYear()
        themonth=mydate.getMonth()+1
        thetoday=mydate.getDate()
        
        $('#date-string').html(theyear);
    }
    display_date();
	
	//#####################################################################################################
	//##  Foncitons de liens  #############################################################################
	
	$('a[href*=".pdf"], a[href*=".doc"], a[href*=".docx"], a[href*=".xsl"], a[href*=".xslx"], a[href*=".ppt"], a[href*=".pptx"], a[href*=".txt"]').each(function(){
		var broken_href = $(this).attr('href').split('.');
		var extension = broken_href[broken_href.length-1];
		//.log(extension);
		if($(this).find('img').length < 1){ // Si il ne s'agit pas d'un liens sur imageUntitled Document
			switch(extension){
				
				case 'pdf':
					$(this).prepend('<img class="inline-icone" src="'+real_path+'img/document-pdf.png" alt="">').css('line-height','1.2em');
					break
				case 'doc':
				case 'docx':
					$(this).prepend('<img class="inline-icone" src="'+real_path+'img/document-word.png" alt="">').css('line-height','1.2em');
					break
				case 'xsl':
				case 'xslx':
					$(this).prepend('<img class="inline-icone" src="'+real_path+'img/document-excel.png" alt="">').css('line-height','1.2em');
					break
				case 'ppt':
				case 'pptx':
					$(this).prepend('<img class="inline-icone" src="'+real_path+'img/document-powerpoint.png" alt="">').css('line-height','1.2em');
					break
				case 'txt':
					$(this).prepend('<img class="inline-icone" src="'+real_path+'img/document-text.png" alt="">').css('line-height','1.2em');
					break
			}
		}
		$(this).attr('target','_blank');
	});
	
	$('a.doc-grille').each(function(){
	    $(this).prepend('<img class="inline-icone" src="'+real_path+'img/icone-grille.png" alt="">').css('line-height','1.2em');
	});
	
	$('#main-content a[href*="http://"]').not('a[href*="'+window.location.host+'"]').not('a[href*="youtube.com"]').each(function(){
		$(this).append('<img class="inline-icone-end" src="'+real_path+'img/document-externe.png" alt="">').css('line-height','1.2em').attr('target','_blank');
	});
	
	$('#main-content a[href*="youtube.com"]').each(function(){
		if($(this).next('img').length < 1){
			$(this).addClass('youtube-text-link').prepend('<img class="inline-icone" src="'+real_path+'img/document-youtube.jpg" alt="">').css('line-height','1.2em');
		}
		//var str = "http://www.youtube.com?v=salut123&test";
		var myregexp = /v=([^&]+)/;
		var cur_href = $(this).attr('href');
		var cur_vid_hash = cur_href.match(myregexp);
		var cur_vid_id = cur_vid_hash[1];
		
		$(this).attr('data-target_div', '#video-div').attr('data-target_subject',cur_vid_id).attr('href','#').addClass('vid-link');
		//$(this).attr('data-target_video', cur_vid_id);
		//.log($(this).dataset());
	});
	
	$('.vid-link').hover(function(){
		$(this).stop(true, true).animate({opacity: 1}, 500);
	}, function(){
		$(this).stop(true, true).animate({opacity: 0.7}, 500);
	});
	
	$('a[href="#"]').live('click', function(e){	e.preventDefault();	});
	
	$('#actualite-content-full img[align!=""]').each(function(){
		if($(this).attr('align') === 'left'){
			$(this).css({marginRight: '15px'});
		}else if($(this).attr('align') === 'right'){
			$(this).css({marginLeft: '15px'});
		}
	});
	
	$('a.btn-print').live('click', function(e){
		e.preventDefault();
	    window.print();
	});
	
	$('.lc-news-tidbit:last').css({borderBottom: 'none'});
	
	//#####################################################################################################
	//##  Overlay "Mission" sur l'accueil  ################################################################
	
	if($("#mission-overlay").length > 0){
		$("#btn-mission").hover(function(){
			$('#mission-overlay').stop(true, true).fadeIn(300);
			window.clearTimeout(closeoverlay_timeout);
		}, function(){
			closeoverlay_timeout = window.setTimeout(close_overlay, 1000);
		});
		
		$("#mission-overlay").hover(function(){
			window.clearTimeout(closeoverlay_timeout);
		}, function(){
			close_overlay();
		});
		
		function close_overlay(){
			$('#mission-overlay').stop(true, true).fadeOut(200);
		}
	}
	
	//#####################################################################################################
	//##  Ajustement de la colonne de droite selon celle de gauche  #######################################
	
	var $left_column = $('#main-content #left-column');
	var $right_column = $('#main-content #right-column');
	
	if($right_column.height() < $left_column.height()){
		$right_column.css('min-height',$left_column.height()-40);
	}
	
	//#####################################################################################################
    //##  Tabs system - Activites  ########################################################################
    
    $('#activites-tabs-menu li').click(function(){
        
        var target_tab = $(this).dataset('target_tab');
        $('#activites-tabs-menu li.active').removeClass('active');
        $(this).addClass('active');
        $('.activites-tab:visible').hide();
        $('#'+target_tab).show();
    });
    
    // Activites Tables
    
    $("#activite-single-dateslieux-table tr:even").addClass('even-tr');
	
	//#####################################################################################################
	//##  Gallery de publicites accueil  ##################################################################

    var $ss_container = $('#banner-wrapper');

	var $counter_z = 20;
	var $counter = 0;
	var $pics = $ss_container.children("div.slide");
	var $nbr_pics = $pics.length;
	var $slideshow_timeout;

	$ss_container.data("cur_pic_nbr",'1');

	$ss_container.children("div.slide").each(function(){
		$counter_z--;
		$counter++;
		$(this).attr('id','image_'+$counter).css("z-index",$counter_z);
        /*if($nbr_pics > 1){
            $ss_nav.append('<li id="image_link_'+$counter+'" target_img="'+$counter+'">'+$counter+'</li>');
        }*/
	});

	$("#image_link_1").addClass("current_image_link");

    var $target_pic = '';

	function switch_img_auto(){
		var $cur_pic_nbr = $ss_container.data("cur_pic_nbr");

		var $cur_pic = $("#image_"+$cur_pic_nbr);

		if($cur_pic_nbr == $nbr_pics){
			$target_pic_nbr = 1;
			$target_pic = $("#image_"+$target_pic_nbr);
		}else{
			$target_pic_nbr = parseInt($cur_pic_nbr) + 1;
			$target_pic = $("#image_"+$target_pic_nbr);
		}

		$target_pic.addClass("cur_image").css("z-index",$cur_pic.css("z-index")-1).show();

		//$ss_nav.find('li.current_image_link').removeClass("current_image_link");
		$("#image_link_"+$target_pic_nbr).addClass("current_image_link");

		$cur_pic.removeClass("cur_image").fadeOut(700, function(){
			$target_pic.css("z-index","20");
			$(this).css("z-index",$target_pic.css("z-index")-1)
			$ss_container.data("cur_pic_nbr",$target_pic_nbr).end().find("div[class!=cur_image]").hide();
			$slideshow_timeout = window.setTimeout(switch_img_auto, 7000);
		});
	}

    if($nbr_pics > 1){
        $slideshow_timeout = window.setTimeout(switch_img_auto, 7000);
    }
    
    
    //#####################################################################################################
    //##  Lightbox WT  ####################################################################################

    $.wt_lightbox = function(type, target, lb_width, lb_height, special)
    {
        
        var b_black = $("#back-overlay");

        var b_loading = $("#b_loading");

        var w_height = $(window).height();
        var w_width = $(window).width();

        var d_height = $(document).height();
        var d_width = $(document).width();

        var pos_x = 0;
        var pos_y = 0;

        var box_width = 0;
        var box_height = 0;

        b_black.css({
            "width": d_width,
            "height": d_height
        });

        pos_x = w_width / 2;
        pos_y = (w_height / 2) + $(window).scrollTop();

        // Si on veux afficher une image ##############################################

        if(type == "image"){
            $('body').append('<div class="b_ctr_image b_ctr"></div>')

            var b_ctr_image = $(".b_ctr_image");

            b_ctr_image.html('<img id="curr_zoom_image" src="'+target+'" />');

            b_ctr_image.append('<a href="#" class="b_close" title="Fermer la fenêtre">Fermer la fenêtre</a>');

            $("#curr_zoom_image").one("load",function(){

                box_width = b_ctr_image.width();
                box_height = b_ctr_image.height() - 2;
                //console.log(box_height);

                // Setup du container de la photo
                b_ctr_image.css({
                    'opacity': '0',
                    "height": box_height,
                    "width": box_width,
                    "top": pos_y - (box_height/2),
                    "left": pos_x - (box_width/2),
                    'display': 'block'
                })

                // Setup de la toile de fond
                b_black.stop(true,true).fadeTo(400, 0.75, function(){
                    var l_pos_x =  w_width / 2;
                    var l_pos_y = (w_height / 2) + $(window).scrollTop();
                    var l_height = b_loading.outerHeight() / 2;
                    var l_width = b_loading.outerWidth() / 2;
                    b_loading.css({'left' : l_pos_x - l_width, 'right' : l_pos_y - l_height}).fadeIn(300);
                });

                b_ctr_image.animate({opacity: 1}, 500, function(){
                    b_loading.hide();
                });
            })
            .each(function(){
                if(this.complete) $(this).trigger("load"); 
            });

        // Si on veux afficher du content  ############################################

        }else if(type == "content" || type == "iframe-content" || type == "video"){
			//console.log('Special = '+special);
            $('body').append('<div class="b_ctr_content b_ctr"></div>');
            b_ctr_content = $('.b_ctr_content');
            
            if(target instanceof Object){
                if(type == 'video'){
                    $(target.target_div).find('iframe').attr('src','http://www.youtube.com/embed/'+target.target_subject+'?rel=0&wmode=transparent');
                    $(target.target_div).clone().appendTo(b_ctr_content);
                    target_div = target.target_div;
                }
            }else{
                if(type == 'iframe-content'){
                    $(b_ctr_content).append('<strong id="popup-title">'+special+'</strong><iframe id="popup-iframe" src="'+target+'"></iframe>');
                    target_div = '#popup-iframe';
                }else{
                    target_div = target;
                    $(target).clone().appendTo(b_ctr_content);
                }
            }

            box_height = lb_height;
            box_width = lb_width;

            if(lb_width != '' && lb_height != ''){
                b_ctr_content.find(target_div).css({
                    "min-height": box_height,
                    "min-width": box_width,
                    'display': 'block'
                });
            }
            
            b_ctr_content.css({
                'opacity': '0',
                "min-height": box_height,
                "min-width": box_width,
                padding: '15px',
                backgroundColor: '#fff',
                'display': 'block'
            })
            
            if(type == 'iframe-content'){
                b_ctr_content.css({
                    paddingTop: '50px'
                })
            }

            b_ctr_content.css({
                "top": pos_y - (b_ctr_content.outerHeight()/2),
                "left": pos_x - (b_ctr_content.outerWidth()/2)
            })
            
            b_ctr_content.prepend('<a href="#" class="b_close" title="Fermer la fenêtre">Fermer la fenêtre</a>');
            b_black.stop(true,true).fadeTo(400, 0.75, function(){
                var l_pos_x =  w_width / 2;
                var l_pos_y = (w_height / 2) + $(window).scrollTop();
                var l_height = b_loading.outerHeight() / 2;
                var l_width = b_loading.outerWidth() / 2;
                b_loading.css({'left' : l_pos_x - l_width, 'right' : l_pos_y - l_height}).fadeIn(300);
            });
            
            b_ctr_content.animate({opacity: 1}, 500, function(){
                //.log('hellwhoe');
                //$.uniform.update('select');
                b_loading.hide();
            });

            
        }
        // ############################################################################

        $(".b_close").live('click',function(e){
            e.preventDefault();
            var popup_element;
            if (type == "image"){popup_element = b_ctr_image}else{popup_element = b_ctr_content}
            popup_element.stop(true,false).animate({opacity:0}, 300, function(){

                popup_element.find(".b_close").remove();
                popup_element.remove();

                b_black.fadeOut(200);
            });
        });
        
        
    }
    $('a.youtube-text-link, a.vid-link').click(function(){
        $.wt_lightbox('video', $(this).dataset(), 640, 390);
    });
    $('a.popup-iframe').click(function(e){
        e.preventDefault();
        $.wt_lightbox('iframe-content', $(this).attr('href'), 640, 420, $(this).attr('title'));
    });
});





