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

$(document).ready(function(){
    
    //#####################################################################################################
    //##  Foncitons générales  ############################################################################
    
    
	
	var highestCol = Math.max($('#colone-left').height(),$('#colone-center').height(),$('#colone-right').height()) - "30";
	$("#colone-left .colone-middle").eq(0).css('height', highestCol + 'px');
	$("#colone-center .colone-middle").eq(0).css('height', highestCol + 'px');
	$("#colone-right .colone-middle").eq(0).css('height', highestCol + 'px');
	
	
	var heightcontent = $('#colone-left-content').height() - 125;
	if(heightcontent > $("#colone-right-middle").height()){
		$("#colone-right-middle").eq(0).css('height', heightcontent + 'px');
	}else{
		$("#colone-right-middle").eq(0).css('height', ($("#colone-right-middle").height()+20) + 'px');
		var heightcontent = $('#colone-right-middle').height() + 75;
		$("#colone-left-middle").eq(0).css('height', heightcontent + 'px');
	}
	
	
	$(".right-menu li.active").prepend('<span class="arrow"></span>') 
	
	
	// Link functions  #####################################################################
    $("body").delegate("a[href='#']", 'click', function(e){e.preventDefault();});
    $('a[href^="http://"]').not('a[href*="'+window.location.host+'"]').attr('target', '_blank');
	$('a[href$=".pdf"]').attr('target', '_blank');
	
	var i = 1;
	$('#nav-main ul li').each(function (){
		$(this).addClass("link"+i);
		$(this).find("a").html($(this).find("a").html().replace(/ et/g,"<br />et"));
		$(this).find("a").html($(this).find("a").html().replace(/ and/g,"<br />and"));
		i++;
	});
	
	
	//#####################################################################################################
	//##  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');
	});
	
	
	$('#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();
	});
	
	
	/*##################################################################*/
	/*##  Carousel  ###############################*/
	
	$('#rc-pub-slider').wtCarrousel({
        name: 'c2',
        ss_container: '#rc-pub-slider',
        ss_nav: true,
        fadeSpeed: 702,
        delay: 5000,
        slidesPrefix: 'slide-b'
    });
	
	//#####################################################################################
    //###  Set du lien actif de section  ##################################################
/*
    var current_url = document.location.href;
	if(current_url.indexOf("?") > 0){current_url = current_url.substr(0, current_url.indexOf("?"))}
    var cur_url_split = current_url.split("/");

    function trim(arr2){
        var arr1=new Array();
        for(j=0;j<arr2.length;j++)
        if(arr2[j]!="")
        arr1[arr1.length]=arr2[j];
        arr2.length=arr1.length;
        for(j=0;j<arr1.length;j++)
        arr2[j]=arr1[j];
        return arr2;
    }

    new_array = trim(cur_url_split);

    var last_param = new_array.length - 1;
    var needle = new_array[last_param];

    $("#nav-main a").each(function(){
        var cur_href = $(this).attr('href');
        var cur_href_split = cur_href.split("/");
        var new_href_array = trim(cur_href_split);
        var last_href_param = new_href_array.length - 1;
        var needle_href = new_href_array[last_href_param];
		
		if(needle_href === needle){
            $(this).parent().addClass('active');
        }
    });
*/
	
	
	$('.map-canvas').each(function(index, e){
		var value = $(this).html().split("|");
		$(this).html("");
					
	
		var latlng = new google.maps.LatLng(value[1], value[2]);
		var myOptions = {
			zoom: 14,
			center: latlng,
			scrollwheel: false,
			mapTypeId: google.maps.MapTypeId.ROADMAP
		};
		var map = new google.maps.Map(e, myOptions);
		var marker = new google.maps.Marker({
			position: latlng,
			title: value[3]
		});
		marker.setMap(map);
		//$(this).after("<div class='map-direction clearfix'><a href='http://maps.google.ca/maps?saddr=&daddr=8750,+boulevard+de+la+Rive-Sud,+L%C3%A9vis+%28Qu%C3%A9bec%29++G6V+6N6&ie=UTF8' target='_blank' class='btn-bleu-google'>Directions</a><div style='float:left'>pour ce rendre chez Pharmalab</div></div>");
	});
	
    
    //#####################################################################################################
    //##  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'));
    });
});





