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

$(document).ready(function(){
    
    //#####################################################################################################
    //##  Foncitons générales  ############################################################################
    
	$("#nav-main ul li").find("ul li:first").addClass("first");
	$("#nav-main ul li").find("ul li:last").addClass("last");
	
	
	
	$(".textfield").focusin(function(){
		if($(this).val() == $(this).attr('title'))
		{
			$(this).val('');
		}
	});
	
	$(".textfield").focusout(function(){
		if($(this).val() == '')
		{
			$(this).val($(this).attr('title'));
		}
	});
	
	
	$('.map-canvas').each(function(index, e){
		var value = $(this).html().split("|");
					
	
		var latlng = new google.maps.LatLng(value[1], value[2]);
		var myOptions = {
			zoom: 15,
			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);
	});
	
	$(".left-box-wrapper h3").prepend('<span class="pointe"></span>');
	
	
	//#####################################################################################################
	//##  Foncitons de liens  #############################################################################

	$("a[href^='#']").each(function(){
		if($(this).attr("href") != "#"){
			$(this).attr("href", document.location.href+$(this).attr("href"));
		}
	});
		
		
	$("a[href='#']").click(function(){return false;});
	
	
	$('.editor-wrapper a[href*=".pdf"], .editor-wrapper a[href*=".doc"], .editor-wrapper a[href*=".docx"], .editor-wrapper a[href*=".xsl"], .editor-wrapper a[href*=".xslx"], .editor-wrapper a[href*=".ppt"], .editor-wrapper a[href*=".pptx"], .editor-wrapper 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').css("display","inline-block");
	});
	
	
	$('#section-wrapper a[href*="mailto:"]').each(function(){
		$(this).append('<img class="inline-icone-end" src="'+real_path+'img/document-mail.png" alt="">').css('line-height','1.2em').attr('target','_blank');
	});
	
	$('.editor-wrapper a[href*="http://"], .editor-wrapper a[href*="https://"]').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-gris-1.png" alt="">').css('line-height','1.2em').attr('target','_blank');
		$(this).attr('target','_blank');
	});

	
	$('.editor-wrapper 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());
	});
	
	
	$('a[href="#"]').live('click', function(e){	e.preventDefault();	});
	
	
	$('a.btn-print').live('click', function(e){
		e.preventDefault();
	    window.print();
	});
	
	//#####################################################################################################
	//##  Affichage des tableau #########################################
	
	$('.editor-wrapper table tbody').find("tr:odd td, tr:odd td a .inline-icone-end").css('background','#efefef');
	
	
	
	
	
	
	/*##################################################################*/
	/*##  Carousel  ###############################*/
	
	if($('#home-banner-slider .slide').size() > 1){
		$('#home-banner-slider .slide[href="#"]').css("cursor","default")
		$('#home-banner-slider').wtCarrousel({
			nav: true,
			delay: 5000,
			restartDelay: 30000,
			fadeSpeed: 700,
			zindex: 20		
		});
	}
	


	
	
	
	
    
    //#####################################################################################################
    //##  Lightbox WT  ####################################################################################

    $.wt_lightbox = function(type, target, lb_width, lb_height, special)
    {
        
		if($("#back_overlay").size() == 0) $('body').append('<div id="back_overlay"></div>')
        
		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
        });
		b_black.fadeIn(200);

        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">×</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'){
					console.log(target.target_div);
                    $(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">×</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'));
    });
	
	$('a.lightbox-image').click(function(e){
        e.preventDefault();
        $.wt_lightbox('image', $(this).attr('href'));
    });
});





