var oldclass = "";
function menuover(sname) {
    oldclass = document.getElementById('temoin-' + sname).className;
    document.getElementById('temoin-' + sname).className = 'on';
}
function menuout(sname) { document.getElementById('temoin-'+sname).className=oldclass; }


$(function () {

    $('a[rel="external"]').attr('target', '_blank');


    $('a').focus(function () { this.blur(); });
    SI.Files.stylizeAll();
    slider.init();


    $('input.text-default').each(function () {
        $(this).attr('default', $(this).val());
    }).focus(function () {
        if ($(this).val() == $(this).attr('default'))
            $(this).val('');
    }).blur(function () {
        if ($(this).val() == '')
            $(this).val($(this).attr('default'));
    });

    $('input.text,textarea.text').focus(function () {  $(this).addClass('textfocus'); }).blur(function () { $(this).removeClass('textfocus'); });


    /*
    $('p.images img').click(function () {
    var newbg = $(this).attr('src').split('bg/bg')[1].split('-thumb')[0];
    $(document.body).css('backgroundImage', 'url(' + _siteRoot + 'images/bg/bg' + newbg + '.jpg)');

    $(this).parent().find('img').removeClass('on');
    $(this).addClass('on');
    return false;
    });
    */


    $(window).load(function () {

       var css_ims = ['bottombar.png', 'colours.png', 'topbar.png'];
        $.each(css_ims, function () { (new Image()).src = _siteRoot + 'images/' + this; });

        var css_cims = ['line-bottom.png', 'line-top.png', 'silde-nav.png', 'temoins.png'];
        $.each(css_cims, function () {
            var css_im = this;
            $.each(['bleu', 'violet', 'rose', 'rouge', 'gris', 'vert', 'jaune', 'orange'], function () { (new Image()).src = _siteRoot + 'css/' + this + '/' + css_im; });
        });

        var colour;

        if (get_cookie("colour") != null) { colour = get_cookie("colour"); }
        else { colour = "bleu"; }
        
        var sheet = document.createElement('link');
        sheet.setAttribute('rel', 'stylesheet');
        sheet.setAttribute('type', 'text/css');
        sheet.setAttribute('media', 'all');
        sheet.setAttribute('href', _siteRoot + 'css/' + colour + '/colour.css');
        if ($.browser.safari) { $('head').append(sheet); }
        else { $(document.body).append(sheet); }
        document.getElementById(colour).className = 'on';
    });

    $('p.colours a').click(function () {
        var sheet = document.createElement('link');
        sheet.setAttribute('rel', 'stylesheet');
        sheet.setAttribute('type', 'text/css');
        sheet.setAttribute('media', 'all');
        sheet.setAttribute('href', _siteRoot + 'css/' + this.id + '/colour.css');
        toolbox_save(this.id);
        $('head').append(sheet);
        $(document.body).append(sheet);

        $(this).parent().find('a').removeClass('on');
        $(this).addClass('on');

        return false;
    });




}); 


// Other functions -->  
function ScrollHost_init() {
	$('#'+hosting.bloc_spec).scrollable({ 
		size: 1 
	});
}
 
function scroll(obj,offset){ 
 if(typeof obj.pathname=='undefined'){ 
  var $target=$(obj); 
 }else if(location.pathname.replace(/^\//,'')==obj.pathname.replace(/^\//,'') && location.hostname==obj.hostname){ 
  var $target=$(obj.hash); 
  $target=$target.length&&$target||$('[name='+obj.hash.slice(1)+']'); 
 } 
 if($target.length){ 
  var targetOffset=$target.offset().top+offset; 
  $('html,body').stop().animate({scrollTop:targetOffset},500,'swing'); 
  return false; 
 } 
}

/* ---- SLIDER D'IMAGE -------------------------------------------------------------------------- */
var slider={ 
    num:-1, 
    cur:0, 
    cr:[], 
    al:null, 
    at:10*1000, 
    ar:true, 
    init:function(){ 
        if(!slider.data || !slider.data.length) 
        return false; 
 
        var d=slider.data; 
        slider.num=d.length; 
        var pos=Math.floor(Math.random()*1); 
        for(var i=0;i<slider.num;i++){ 
            $('#'+d[i].id).css({left:((i-pos)*914)}); 
            if (slider.num>1) { $('#slide-nav').append('<a id="slide-link-'+i+'" href="#" onclick="slider.slide('+i+');return false;" onfocus="this.blur();">'+(i+1)+'</a>'); }
        } 
 
        $('img,div#slide-controls',$('div#slide-holder')).fadeIn(); 
        slider.text(d[pos]); 
        slider.on(pos); 
        slider.cur=pos; 
        window.setTimeout('slider.auto();',slider.at); 
    }, 
    auto:function(){ 
        if(!slider.ar) 
        return false; 
 
        var next=slider.cur+1; 
        if(next>=slider.num) next=0; 
        slider.slide(next); 
    }, 
    slide:function(pos){ 
        if(pos<0 || pos>=slider.num || pos==slider.cur) 
        return; 
 
        window.clearTimeout(slider.al); 
        slider.al=window.setTimeout('slider.auto();',slider.at); 
 
        var d=slider.data; 
        for(var i=0;i<slider.num;i++) 
        $('#'+d[i].id).stop().animate({left:((i-pos)*914)},914,'swing'); 
   
        slider.on(pos); 
        slider.text(d[pos]);
        slider.cur = pos;
    },
    on: function (pos) {
        $('#slide-nav a').removeClass('on');
        $('#slide-nav a#slide-link-' + pos).addClass('on');
    },
    text: function (di) {
        $('#slide-client span').fadeOut();
        $('#slide-desc').fadeOut();
        slider.cr['a'] = di.client;
        slider.cr['b'] = di.desc;
        slider.ticker('#slide-client span',di.client,0,'a'); 
        slider.ticker('#slide-desc',di.desc,0,'b'); 
    }, 
    ticker:function(el,text,pos,unique){ $(el).html(text).fadeIn(500);} 
};


/* ---- Google MAP -------------------------------------------------------------------------- */
var cmap = {
    is_open: false,
    gmap: false,
    form_id: 'contact-form',
    map_id: 'contact-map',
    time: 750,
    open: function () {
        if (cmap.is_open) return false;
        $('#' + cmap.form_id).fadeOut(cmap.time, function () {
            $('#' + cmap.map_id).fadeIn(cmap.time, function () {
                if (GBrowserIsCompatible() && !cmap.gmap) {
                    cmap.gmap = true;
                    var map = new GMap2(document.getElementById('gmap'));
                    var ll = new GLatLng(43.514695, 4.962218);
                    map.setCenter(ll, 13);
                    map.addOverlay(new GMarker(ll));
                    map.addControl(new GSmallMapControl());
                    map.addControl(new GMapTypeControl());
                    cmap.gdir = new GDirections(map, document.getElementById('cmap_dir'));
                    GEvent.addListener(cmap.gdir, 'error', cmap.handleErrors);
                }
            });
        });
        cmap.is_open = true;
    },
    close: function () {
        if (!cmap.is_open) return false;
        $('#' + cmap.map_id).fadeOut(cmap.time, function () { $('#' + cmap.form_id).fadeIn(cmap.time); });
        cmap.is_open = false;
    },
    directions: function (to) {
        cmap.gdir.load('from: ' + to + ' to: 43.5146955, 4.962218', { 'locale': 'fr_FR' });
    },
    handleErrors: function () {
        if (cmap.gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
            alert("Aucune situation g\351ographique correspondante n'a pu \352tre trouv\351e pour l'une des adresses indiqu\351es. Cela peut \352tre d\372 a une adresse relativement r\351cente ou incorrecte\n(Erreur # " + cmap.gdir.getStatus().code + ")");
        else if (cmap.gdir.getStatus().code == G_GEO_SERVER_ERROR)
            alert("Aucune situation g\351ographique correspondante n'a pu \352tre trouv\351e pour l'une des adresses indiqu\351es. Cela peut \352tre d\372 a une adresse relativement r\351cente ou incorrecte\n(Erreur # " + cmap.gdir.getStatus().code + ")");
        else if (cmap.gdir.getStatus().code == G_GEO_MISSING_QUERY)
            alert("Aucune situation g\351ographique correspondante n'a pu \352tre trouv\351e pour l'une des adresses indiqu\351es. Cela peut \352tre d\372 a une adresse relativement r\351cente ou incorrecte\n(Erreur # " + cmap.gdir.getStatus().code + ")");
        else if (cmap.gdir.getStatus().code == G_GEO_BAD_KEY)
            alert("Aucune situation g\351ographique correspondante n'a pu \352tre trouv\351e pour l'une des adresses indiqu\351es. Cela peut \352tre d\372 a une adresse relativement r\351cente ou incorrecte\n(Erreur # " + cmap.gdir.getStatus().code + ")");
        else if (cmap.gdir.getStatus().code == G_GEO_BAD_REQUEST)
            alert("Aucune situation g\351ographique correspondante n'a pu \352tre trouv\351e pour l'une des adresses indiqu\351es. Cela peut \352tre d\372 a une adresse relativement r\351cente ou incorrecte\n(Erreur # " + cmap.gdir.getStatus().code + ")");
        else alert("Aucune situation g\351ographique correspondante n'a pu \352tre trouv\351e pour l'une des adresses indiqu\351es. Cela peut \352tre d\372 a une adresse relativement r\351cente ou incorrecte\n");
    }
}; 
 
function toolbox_save(colour){ 
	var bg="";
	//set_cookie("colour", colour, 365, "/", "", "");
	set_cookie("colour", colour, 365, "/", ".perfea.fr", "");
	$.get(_siteRoot,{set_bg:bg,set_colour:colour},function(){}); 
} 
 
var client_root=_root+'client/'; 
function client_login(form){ 
 $.post(client_root,{user:form.client_user.value,pass:form.client_pass.value}, 
  function(data){ 
   if(data=='0') $('#popup-client p.error').fadeIn(); 
   else location.href=client_root; 
  } 
 ); 
} 
function cms_login(form){ 
 $.post(_siteRoot+'ajax-login.php',{user:form.cms_user.value,pass:form.cms_pass.value}, 
  function(data){ 
   if(data=='0') $('#popup-cms p.error').fadeIn(); 
   else location.href=_root+'wp-admin/'; 
  } 
 ); 
} 
 
Array.prototype.in_array=function(v){ 
 for(var i in this) 
  if(this[i]==v) 
   return true; 
 return false; 
} 
Array.prototype.remove=function(s){ 
 for(i=0;i<this.length;i++) 
  if(s==this[i]) 
   this.splice(i,1); 
} 

// STYLING FILE INPUTS 1.0 | Shaun Inman <http://www.shauninman.com/> | 2007-09-07 
if(!window.SI){var SI={};};
SI.Files = {
    htmlClass: 'SI-FILES-STYLIZED',
    fileClass: 'file',
    wrapClass: 'cabinet',

    fini: false,
    able: false,
    init: function () {
        this.fini = true;

        var ie = 0; 
        if (window.opera || (ie && ie < 5.5) || !document.getElementsByTagName) { return; }
        this.able = true;

        var html = document.getElementsByTagName('html')[0];
        html.className += (html.className != '' ? ' ' : '') + this.htmlClass;
    },
    stylize: function (elem) {
        if (!this.fini) { this.init(); };
        if (!this.able) { return; };

        elem.parentNode.file = elem;
        elem.parentNode.onmousemove = function (e) {
            if (typeof e == 'undefined') e = window.event;
            if (typeof e.pageY == 'undefined' && typeof e.clientX == 'number' && document.documentElement) {
                e.pageX = e.clientX + document.documentElement.scrollLeft;
                e.pageY = e.clientY + document.documentElement.scrollTop;
            };
            var ox = oy = 0;
            var elem = this;
            if (elem.offsetParent) {
                ox = elem.offsetLeft;
                oy = elem.offsetTop;
                while (elem = elem.offsetParent) {
                    ox += elem.offsetLeft;
                    oy += elem.offsetTop;
                };
            };
            var x = e.pageX - ox;
            var y = e.pageY - oy;
            var w = this.file.offsetWidth;
            var h = this.file.offsetHeight;
            this.file.style.top = y - (h / 2) + 'px';
            this.file.style.left = x - (w - 30) + 'px';
        };
    },
    stylizeById: function (id) {
        this.stylize(document.getElementById(id));
    },
    stylizeAll: function () {
        if (!this.fini) { this.init(); };
        if (!this.able) { return; };

        var inputs = document.getElementsByTagName('input');
        for (var i = 0; i < inputs.length; i++) {
            var input = inputs[i];
            if (input.type == 'file' && input.className.indexOf(this.fileClass) != -1 && input.parentNode.className.indexOf(this.wrapClass) != -1)
                this.stylize(input);
        };
    }
}; 

var hosting = { 
    is_open:false, 
    level1_id:'host_infra', 
    level2_id:'host_domaines', 
    level3_id:'host_mutu',
    level4_id:'host_special',
    level5_id:'host_virtuel',
    bloc_spec:'blocspec',
    time:750,
    level1: function () { $('#' + hosting.level1_id).fadeOut(hosting.time, function () { $('#' + hosting.level2_id).fadeOut(hosting.time, function () { $('#' + hosting.level3_id).fadeOut(hosting.time, function () { $('#' + hosting.level4_id).fadeOut(hosting.time, function () { $('#' + hosting.level1_id).fadeIn(hosting.time); }); }); }); }); },
    level2: function () { $('#' + hosting.level1_id).fadeOut(hosting.time, function () { $('#' + hosting.level3_id).fadeOut(hosting.time, function () { $('#' + hosting.level4_id).fadeOut(hosting.time, function () { $('#' + hosting.level5_id).fadeOut(hosting.time, function () { $('#' + hosting.level2_id).fadeIn(hosting.time); }); }); }); }); },
    level3:function() { $('#'+hosting.level1_id).fadeOut(hosting.time,function(){$('#'+hosting.level2_id).fadeOut(hosting.time,function(){$('#'+hosting.level4_id).fadeOut(hosting.time,function(){$('#'+hosting.level5_id).fadeOut(hosting.time,function(){$('#'+hosting.level3_id).fadeIn(hosting.time);});});});}); },
    level4:function() { $('#'+hosting.level1_id).fadeOut(hosting.time,function(){$('#'+hosting.level2_id).fadeOut(hosting.time,function(){$('#'+hosting.level3_id).fadeOut(hosting.time,function(){$('#'+hosting.level5_id).fadeOut(hosting.time,function(){$('#'+hosting.level4_id).fadeIn(hosting.time);});});});}); },
    level5:function() { $('#'+hosting.level1_id).fadeOut(hosting.time,function(){$('#'+hosting.level2_id).fadeOut(hosting.time,function(){$('#'+hosting.level3_id).fadeOut(hosting.time,function(){$('#'+hosting.level4_id).fadeOut(hosting.time,function(){$('#'+hosting.level5_id).fadeIn(hosting.time);});});});}); }
}; 



/* ---- Gestion des Cookies -------------------------------------------------------------------------- */
function set_cookie(name,value,expires,path,domain,secure){ 
	var today=new Date(); 
	today.setTime(today.getTime()); 
	if(expires) expires*=(1000*60*60*24); 
	var expires_date=new Date(today.getTime()+expires); 
	
	document.cookie=name+'='+escape(value)+ 
	((expires)?';expires='+expires_date.toGMTString():'')+ 
	((path)?';path='+path:'')+ 
	((domain)?';domain='+domain:'')+ 
	((secure)?';secure':''); 
} 
 
function get_cookie(check_name){ 
	var a_all_cookies=document.cookie.split(';'); 
	var a_temp_cookie=''; 
	var cookie_name=''; 
	var cookie_value=''; 
	var b_cookie_found=false; 
	
	for(i=0;i<a_all_cookies.length;i++){ 
		a_temp_cookie=a_all_cookies[i].split('='); 
		cookie_name=a_temp_cookie[0].replace(/^\s+|\s+$/g,''); 
 
		if(cookie_name==check_name){ 
			b_cookie_found=true; 
			if(a_temp_cookie.length>1) 
			cookie_value=unescape(a_temp_cookie[1].replace(/^\s+|\s+$/g,'')); 
			return cookie_value; 
			break; 
		} 
		a_temp_cookie=null; 
		cookie_name=''; 
	} 
	if(!b_cookie_found) return null; 
} 
 
function delete_cookie(name,path,domain){ 
	if(get_cookie(name)) {
		document.cookie=name+'='+((path)?';path='+path:'')+ 
		((domain)?';domain='+domain:'')+';expires=Thu,01-Jan-1970 00:00:01 GMT';
	}
}

