/*
This file contains other functions and logic to perform
*/

// exclude old browsers
/*if (!document.getElementById){
	top.location.replace('http://www.iht.com/info/browser.html');
}*/

// disallow the page to be framed
/*if (window.self != window.top){ 
	top.location.replace(window.location.pathname); 
}*/
var activeFieldValue;
var modifiedFields = "";

function MM_openBrWindow(theURL,winName,features) { //v2.0
  new_window = window.open(theURL,winName,features);
  new_window.focus();
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_showHideLayers() { //v6.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
    obj.visibility=v; }
}

function win_pop(url, display_win_name, width, height ){
    var pop_up = window.open(url, display_win_name,'width=' + width + ',height=' + height + ',resizable=yes,scrollbars=yes,menubar=no,status=no' );
    pop_up.focus();
}
popupWins = new Array();
function win_popup(url, name, width, height ) {
	if ( typeof( popupWins[name] ) != "object" ){		
		popupWins[name] = window.open(url,name,'width=' + width + ',height=' + height + ',resizable=yes,scrollbars=yes,menubar=no,status=no');
	} else {
		if (!popupWins[name].closed){	
		} else {
			popupWins[name] = window.open(url, name,'width=' + width + ',height=' + height + ',resizable=yes,scrollbars=yes,menubar=no,status=no');
		}
	}
	popupWins[name].focus();
}
function out_of_frame(){
	if (parent.frames.length < 1){
		document.location.href = '../index.php';
	}
}

// generic popup window
function pop_me_up(pURL,name,features, nofocus){
	
	new_window = window.open(pURL,name,features);
	if(!nofocus)
		new_window.focus();
	else if(nofocus==0)
		new_window.focus();
}

function popupfiche(){
	var nb_chassis = document.my_form.by_nb_chassis.value ;
	var url = "forms/fiche_lamborghini_miura.php?by_nb_chassis="+nb_chassis  ;
	win_popup(url,"Miura",1020,600 );
}


function envoi(typ,val){     
	
	if (typ == "add_to_cart") alert("Ce produit a été ajouté à votre panier");
	if (typ == "confirm_cart" && val =="unlogged") {
		
		document.my_form.submitv.value="frm_inscription.php";
		slide_deballe();
	} else { 
		
		if (typ.indexOf("__") > 0){
			var arr_typ = typ.split("__");
			var typ2 = arr_typ[0];
		} else {
			typ2 = typ;
		}
		switch(typ2){
			case "acces_pro" :
				if(val == "unlogged") {
					document.my_form.submitv.value=(arr_typ[1]);
					slide_deballe();				
				} else {
					document.location.href = arr_typ[1];
				}
				break;
			case "carriere" :
			
				break;
			default :
			   
			   
			    document.my_form.submitt.value=typ;
			    document.my_form.submitv.value=val;
			   	document.my_form.submit();
	   	}
   	}
}
function connect(){
	envoi("connexion",document.my_form.submitv.value); //???
}

function hide_layer(lay){
	if (document.all) {
		if (lay!="") {document.all[lay].style.visibility="hidden";}
	}
	if (document.layers) {
	  	if (lay!="") {document.layers[lay].visibility="hide";}
	}
	if (document.getElementById){
	    document.getElementById(lay).style.visibility="hidden";
	}	
}

function show_layer(lay){
    if (document.all) {
		if (lay!="") {document.all[lay].style.visibility="visible";}
	}
	if (document.layers) {
		if (lay!="") {document.layers[lay].visibility="show";}
	}
	if (document.getElementById){
	    document.getElementById(lay).style.visibility="visible";
	}	
}

//functions from lib_mat
function orig_val(obj){
    activeField_value=obj.value;
}

function new_val(obj){
	if ( obj.value != activeFieldValue ) {
	    modifiedFields += obj.name+",";
	   // alert(modifiedFields);
	}
}

function new_check(obj){
	modifiedFields += obj.name+",";
	//alert(modifiedFields);
}

function clear_field(obj){
	obj.value = "";
}
function clear_password(obj){
	obj.value = "";
	obj.type = "password";
}

function update_multiple_checks(obj_id, new_val, typeAction) { 
    var value_string = ""; 
    var current_values = "";
    var obj = document.getElementById(obj_id);  	
    if (typeof(obj) == "undefined" || obj.value.indexOf("undefined") > 0) {
    	current_values = "";
    	alert("Indefini");
    } else {   
    	current_values = obj.value; 
    }
    if (typeAction == "add"){
    	if (current_values !== ""){
	    	value_string = current_values + "," + new_val; 	
	    	value_string = value_string.replace(",,",",");	    	
    	} else {
    		value_string = new_val; 
    	}	
    	//TODO if journaliste show div media
    } else {  //typeAction = "remove"
    	if (current_values != "" ){
	    	current_values = current_values.replace(",,",",");
	    	if (current_values.indexOf(",") > 0){
		        var arr_val = eval("Array(" + current_values + ");");
		    	var nb_vals = arr_val.length;	    	
		        for (lop=0; lop < nb_vals; lop++) { 
		        	if (arr_val[lop]!= new_val && arr_val[lop]!= "") {		        	 	
		            	value_string = value_string + arr_val[lop] + ",";
		            } 
		        }		        
		        if(value_string.length > 1) value_string = value_string.slice(0,-1); else value_string = "";
	        } else {
	        	value_string = "";
	        }
	    }	       
    } 	
    obj.value = value_string; 
} 

function verif_pass(obj){
	var origPasswName = obj.name.substr(3);
	var origPasswValue = eval("document.my_form." + origPasswName + ".value");
	if (obj.value != origPasswValue){
		 alert("Mots de passe non identiques");
	}	
}

//needs mootools.js
window.addEvent('domready', function(){
	var Tips2 = new Tips($$('.Tips2'), {
		initialize:function(){
			this.fx = new Fx.Style(this.toolTip, 'opacity', {duration: 500, wait: false}).set(0);
		},
		onShow: function(toolTip) {
			this.fx.start(1);
		},
		onHide: function(toolTip) {
			this.fx.start(0);
		}
	});
	
	var arrCommande = new Array();
    $$('div.commander').each(function(div){
    	var linkId = div.get('id');
    	arrCommande[linkId]= false;
        div.addEvent('click', function(e){
            e.stop();
            if(arrCommande[linkId]== false){            
	            arrProductId = linkId.split("_");
	        	strHtml = '<iframe src="../admin_miura/ifr_commande.frm.php?submitt=nl&prod_id='+arrProductId[1]+'" width="480" height="490" style="margin-left:0px;border-width:0px;" ></iframe>';
	            var myVerticalSlide = new Fx.Slide('frmcmde_'+arrProductId[1],{
	            	duration : 750, 
	            	fps: 15
	            });
	            myVerticalSlide.hide();                        
	            $('frmcmde_'+arrProductId[1]).set('html', strHtml);
	            myVerticalSlide.slideIn();
	            div.tween('color', "#bbb");
	            div.setStyle('cursor', 'default');
	            $('annuler_'+arrProductId[1]).tween('color', "#333");
	            $('annuler_'+arrProductId[1]).setStyle('cursor', 'pointer');
	            arrCommande[linkId]= true;
	            $('annuler_'+arrProductId[1]).addEvent('click', function(e){
	                e.stop();
	                //myVerticalSlide.toggle();
	                myVerticalSlide.slideOut();
	                div.tween('color', "#333");
	                div.setStyle('cursor', 'pointer');
	                $('annuler_'+arrProductId[1]).tween('color', "#bbb");
	                $('annuler_'+arrProductId[1]).setStyle('cursor', 'default');     
	                arrCommande[linkId]= false;
	            });
	            
            }

        });                
    });

	var arrEchange = new Array();
    $$('div.echanger').each(function(div){
    	var linkId = div.get('id');
    	arrEchange[linkId]= false;
        div.addEvent('click', function(e){
            e.stop();
            if(arrEchange[linkId]== false){            
	            arrProductId = linkId.split("_");
	        	strHtml = '<iframe src="../admin_miura/ifr_echange.frm.php?submitt=nl&prod_id='+arrProductId[1]+'" width="480" height="490" style="margin-left:0px;border-width:0px;" ></iframe>';
	            var myVerticalSlide = new Fx.Slide('frmcmde_'+arrProductId[1],{
	            	duration : 750, 
	            	fps: 15
	            });
	            myVerticalSlide.hide();                        
	            $('frmcmde_'+arrProductId[1]).set('html', strHtml);
	            myVerticalSlide.slideIn();
	            div.tween('color', "#bbb");
	            div.setStyle('cursor', 'default');
	            $('annuler_'+arrProductId[1]).tween('color', "#333");
	            $('annuler_'+arrProductId[1]).setStyle('cursor', 'pointer');
	            arrEchange[linkId]= true;
	            $('annuler_'+arrProductId[1]).addEvent('click', function(e){
	                e.stop();
	                //myVerticalSlide.toggle();
	                myVerticalSlide.slideOut();
	                div.tween('color', "#333");
	                div.setStyle('cursor', 'pointer');
	                $('annuler_'+arrProductId[1]).tween('color', "#bbb");
	                $('annuler_'+arrProductId[1]).setStyle('cursor', 'default');     
	                arrEchange[linkId]= false;
	            });
	            
            }

        });                
    });	
	
/*	if($defined($('menu_gauche'))){		
		
		new multipleOpenAccordion($$('.menu_text'),$$('.menu_description'), {
		   	display: openedMenu,
		   	alwaysHide: false,
		   	fps:25,
		   	duration:350,
		   	opacity: true,
			onActive: function(toggler, element){
				toggler.setStyle('background-color', '#eee');
				//toggler.setStyle ('cursor','pointer');
			},
			onBackground: function(toggler, element){
				toggler.setStyle('background-color', '#ccc');
				//toggler.setStyle ('cursor','pointer');
			}		   	
		}); 	
	}*/	


});


var multipleOpenAccordion = new Class({
		Extends: Accordion,
		options: {
			allowMultipleOpen: true
		},
		initialize: function(togglers,togglees,options){
			//this.parent(togglers,togglees,this.options);
			this.parent(togglers,togglees,options);
			//console.log(this.options.toSource());
		},
		display: function(index){
			index = ($type(index) == 'element') ? this.elements.indexOf(index) : index;
			if ((this.timer && this.options.wait) || (index === this.previous && !this.options.alwaysHide)) return this;
			
			var obj = {};
			if(this.options.allowMultipleOpen){
				var el = this.elements[index];
				if($defined(el)){
				// console.log($type(el));
					obj[index] = {};
					var hide = (el.offsetHeight > 0);
					this.fireEvent(hide ? 'onBackground' : 'onActive', [this.togglers[index], el]);
					for (var fx in this.effects) obj[index][fx] = hide ? 0 : el[this.effects[fx]];
				}
			}else{
				this.previous = index;
				this.elements.each(function(el, i){
					obj[i] = {};
					var hide = (i != index) || (this.options.alwaysHide && (el.offsetHeight > 0));
					this.fireEvent(hide ? 'onBackground' : 'onActive', [this.togglers[i], el]);
					for (var fx in this.effects) obj[i][fx] = hide ? 0 : el[this.effects[fx]];
				}, this);
				
			}
			return this.start(obj);
		}
	}); 
	
	



