$(document).ready(function() {
mybox.init();
mypop.init();
    $('.pngfix').pngFix();
    mainNav();
    
    //diaporama on home page
    if($('#homeBanner').length) 
        $('#homeBanner').s3Slider({
          timeOut: 5000
        });
        
    //news slider on home page
    if($('#homeSlider').length) 
        $('#homeSlider').s3Slider({
          timeOut: 3000
        });
        
    if($('#topBanner').length) 
        $('#topBanner').s3Slider({
          timeOut: 7000
        });  
   
     //news slider on home page
    if($('#colBanner').length) 
        $('#colBanner').s3Slider({
          timeOut: 3000
        });   
     
      
            
    toggleMapLegend();
    
     //rounded corners:
    //rounded corners:
    var toRound = $('.programs-list-page .list-filter');
    if(toRound.length)
        toRound.corner("8px"); 
    if($('.contact-form .round').length)
        $('.contact-form .round').corner("keep");
        
        
        
    
    //switching images:
    imgHover();
    
    //simulateur
    $('.two-columns .simulateur .left-side').css({"height":$('.two-columns #main-col').height()});
    
    tabsHover();
    
    //toggle actus:
    toggleActu();
    
     //adjust width:
    adjustOverlay();
});

function updateprog(url,urlgif,node) {
ajaxload("#prog_result",urlgif);
$("#prog_result").load(url,"node="+node);
}

function ajaxload(div,image){
$(div).html('<p><img src='+image+' /></p>');
}
	
function mainNav() {
    $('.main-nav ul > li').mouseover(function() {
            $(this).addClass('over');
        })
        .mouseleave(function() {
            $(this).removeClass('over');      
    });
}


function toggleMapLegend() {
    $('a.open-legend').click(function() {
        $(this).hide();
        $('#map-legend').slideDown('fast');
        return false;
    });
    
    $('#map-legend img.close').click(function(){
        $('#map-legend').slideUp('fast');
        $('a.open-legend').show();
    });
}


function imgHover() {
    var largeImg = $('.large-photo img');
    $('.photos-container .thumbnails img')
        .mouseover(function(){
        $(this).closest('.photos-container').find('.large-photo img').attr('src', $(this).attr('alt'));
		$(this).closest('.photos-container').find('.large-photo a').attr('href', $(this).attr('value'));
        })
        .click(function() {
            return false;
    });
}

function tabsHover() {
    if($('.program-tabs').length) {
        var initActive = $('.program-page .program-tabs .tabs-nav li a.active');
        var initActiveHref = initActive.attr('href');
        $('.tab-body').hide().filter(initActiveHref.substr(initActiveHref.indexOf('#'))).show();
        var clicked = false;
        $('.program-page .program-tabs .tabs-nav li a').hover(
            function() {
                $('.program-page .program-tabs .tabs-nav li a.active').removeClass('active');
                $(this).addClass('active');    
            },
            function() {
                if(!clicked) {
                    $(this).removeClass('active');
                    initActive.addClass('active');
                }
                else
                    clicked = false;
            }
        ).click(function(){
            initActive = $(this);
            clicked = true;
            var activeDivHref = $(this).attr('href');
            $('.tab-body').hide().filter(activeDivHref.substr(activeDivHref.indexOf('#'))).show();
            return false;
        });
    }
}/*
function tabsHover() {

    if($('.program-tabs').length) {
       var initActive = $('.program-page .program-tabs .tabs-nav li a');
		var initActiveHref = initActive.attr('href');
		
        $('.tab-body').hide().filter(initActiveHref.substr(initActiveHref.indexOf('#'))).show();
        var clicked = false;
        $('.program-page .program-tabs .tabs-nav li a').hover(
            function() {
                $('.program-page .program-tabs .tabs-nav li a.active').removeClass('active');
                $(this).addClass('active');    
            },
            function() {
                if(!clicked) {
                    $(this).removeClass('active');
                    initActive.addClass('active');
                }
                else
                    clicked = false;
            }
        ).click(function(){
            initActive = $(this);
            clicked = true;
            var activeDivHref = $(this).attr('href');
            $('.tab-body').hide().filter(activeDivHref.substr(activeDivHref.indexOf('#'))).show();
            return false;
        });
    }
}*/

function toggleActu() {
    $('.actu-list li').click(function() {
        $(this).toggleClass('expanded');
    })
    .children().click(function(event){
        event.stopPropagation(); 
        return true;
    });
    
    /*$('.actu-toolbar span a.plus').click(function(){
        $('.actu-list li').addClass('expanded');  
        return false; 
    });
    
    $('.actu-toolbar span a.minus').click(function(){
        $('.actu-list li').removeClass('expanded');   
        return false; 
    });*/
}

function  adjustOverlay() {
    var toAdjust = $('.geo-search .search-result .result-image span');
    toAdjust.width(toAdjust.prev('img').width());
}

mypop = {
	
	init : function(){			
			mypop.opacite = 0.5;
			mypop.largeur = 700;
			mypop.hauteur = 350;
			$("a[rel='mypop']").click(function(){
				mypop.lien = $(this).attr("href");
				if ($(this).attr("name") == "contact")
				{
					mypop.largeur = 1000;
					mypop.hauteur = 650;
				}
				else
				{
					mypop.largeur = 700;
					mypop.hauteur = 350;
				}
				mypop.open(mypop.lien);
				return false;
			}
		);
		
		$(window).resize(mypop.redim);
	},
	
	open : function(lien){
		mypop.lien = lien;
		$("body").append('<div id ="mybox"> <div id="mybox_aplat"></div> <div id="mybox_loader"></div> <div id="mybox_conteneur"> <div id="mybox_relative"> <div id="mybox_close"></div> <div id="mybox_contenu"> </div> </div> </div> </div>');
		$("#mybox_conteneur").hide();
		$("#mybox_loader").hide().fadeIn();
		$("#mybox_aplat").css("opacity",0).fadeTo(500,mypop.opacite);

		mypop.timer = window.setInterval(mypop.load,100);
		
		$('#mybox_close').click(mypop.close);
		$('#mybox_aplat').click(mypop.close);
	},
	
	load : function(){
			window.clearInterval(mypop.timer);
			mypop.anim();
	},
	
	anim : function(){
		$("#mybox_conteneur").show();
		
		$("#mybox_contenu").css("width",mypop.largeur+"px").css("height",mypop.hauteur+"px");
		
		mypop.redim();

		$("#mybox_contenu").append("<iframe src="+mypop.lien+" width='100%' height='100%' frameborder='0' scrolling='no'></iframe>");
		$("#mybox_contenu img").hide();
		$("#mybox_contenu").animate( {width:mypop.largeur}, 500).animate({height:mypop.hauteur},500,'linear',function(){
			$("#mybox_contenu img").fadeIn();
			$("#mybox_loader").fadeOut();
		});
		
	},
		
	redim : function(){
		$('#mybox_conteneur').css("left",(mypop.windowW() - mypop.largeur)/2+"px");
		$('#mybox_conteneur').css("top",(mypop.scrollY()+(mypop.windowH() - mypop.hauteur)/2)+"px");
	},
	
	close : function(){
		$('#mybox').fadeOut(500,function(){
			$('#mybox').remove();
		});
	},
	
	windowH : function(){
		if (window.innerHeight) return window.innerHeight;
		else{return $(window).height();}
	},
	
	windowW : function(){
		if (window.innerWidth) return window.innerWidth;
		else{return $(window).width();}
	},
	
	scrollY : function(){
		scOlY = 0;
		if ( typeof(window.pageYOffset) == 'number'){
			scrOlY = window.pageYOffset;
		} else if(document.body && (document.body.scrollTop)){
			scrOlY = document.body.scrollTop;
		}else if(document.documentElement && (document.documentElement.scrollTop)){
			scrOlY = document.documentElement.scrollTop;
		}
		return scrOlY;
		
	}
}

mybox = {
	
	init : function(){
			mybox.opacite = 0.7;
			mybox.largeur = 400;
			mybox.hauteur = 200;
			$("a[rel='mybox']").click(function(){
				mybox.lien = $(this).attr("href");
				mybox.open(mybox.lien);
				return false;
			}
		);
		
		$(window).resize(mybox.redim);
	},
	
	open : function(lien){
		mybox.lien = lien;
		$("body").append('<div id ="mybox"> <div id="mybox_aplat"></div> <div id="mybox_loader"></div> <div id="mybox_conteneur"> <div id="mybox_relative"> <div id="mybox_close"></div> <div id="mybox_contenu"> </div> </div> </div> </div>');
		$("#mybox_conteneur").hide();
		$("#mybox_loader").hide().fadeIn();
		$("#mybox_aplat").css("opacity",0).fadeTo(500,mybox.opacite);

		mybox.img = new Image();
		mybox.img.src = lien;
		mybox.timer = window.setInterval(mybox.load,100);
		
		$('#mybox_close').click(mybox.close);
		$('#mybox_aplat').click(mybox.close);
	},
	
	load : function(){
		if(mybox.img.complete){
			window.clearInterval(mybox.timer);
			mybox.anim();
		}
	},
	
	anim : function(){
		$("#mybox_conteneur").show();
		
		$("#mybox_contenu").css("width",mybox.largeur+"px").css("height",mybox.hauteur+"px");
		
		mybox.largeur = mybox.img.width;
		mybox.hauteur = mybox.img.height;
		
		mybox.redim();

		$("#mybox_contenu").append('<img src="'+mybox.lien+'"/>');
		$("#mybox_contenu img").hide();
		$("#mybox_contenu").animate( {width:mybox.largeur}, 500).animate({height:mybox.hauteur},500,'linear',function(){
			$("#mybox_contenu img").fadeIn();
			$("#mybox_loader").fadeOut();
		});
		
	},
	
	redim : function(){
		$('#mybox_conteneur').css("left",(mybox.windowW() - mybox.largeur)/2+"px");
		$('#mybox_conteneur').css("top",(mybox.scrollY()+(mybox.windowH() - mybox.hauteur)/2)+"px");
	},
	
	close : function(){
		$('#mybox').fadeOut(500,function(){
			$('#mybox').remove();
		});
	},
	
	windowH : function(){
		if (window.innerHeight) return window.innerHeight;
		else{return $(window).height();}
	},
	
	windowW : function(){
		if (window.innerWidth) return window.innerWidth;
		else{return $(window).width();}
	},
	
	scrollY : function(){
		scOlY = 0;
		if ( typeof(window.pageYOffset) == 'number'){
			scrOlY = window.pageYOffset;
		} else if(document.body && (document.body.scrollTop)){
			scrOlY = document.body.scrollTop;
		}else if(document.documentElement && (document.documentElement.scrollTop)){
			scrOlY = document.documentElement.scrollTop;
		}
		return scrOlY;
		
	}
}
