
/* popup center */
function popwin(url,name,winW,winH,opts){
	if(winW=='') winW=771;
	if(winH=='') winH=650;
	winW = parseInt(winW);
	winH = parseInt(winH);
	var l= parseInt((screen.width)?(screen.width-winW)/2:100);
	var t= parseInt((screen.height)?(screen.height-winH)/2:100);
	var conf = "width="+winW+",height="+winH+", top="+t+",left="+l;
	
	if(opts!="") {
		conf += ","+opts;
	}

	window.open(url,name,conf);
}

/* open_flash (home pages) */
function open_flash(lg,href){
	popwin("/"+lg+"/popupflash.sn?"+href,'flash', 750, 650, 'scrollbars=1');
}

/* show_popup image-lien-popup box*/
show_popup=function(sel){
	$(document).ready(function(){
		$(sel).dialog({
			modal: true,
			autoOpen: true,
			resizable: false,
			width: 'auto',
			height: 'auto',
			dialogClass:'my_popup'
		}).dialog('open');
	});
}

/* photo galery */
var gallery_current=1,gallery_new=2,gallery_n,gallery_stop=0,gallery_out;

function anim2(){
	$(".picture_gallery2 #p_"+gallery_new).hide();
	$(".picture_gallery2 #p_"+gallery_new).css({'zIndex':3});
	$(".picture_gallery2 #p_"+gallery_new).animate(
	{opacity:'show'},
	1500,
	function(){
		$(this).css({'zIndex':2});
		$(".picture_gallery2 #p_"+gallery_current).css({'zIndex':1});
		gallery_current=gallery_new;
		if(gallery_new<gallery_n){
			gallery_new++;
		}
		else{
			gallery_new=1;
		}
		if(gallery_stop!=1){
			gallery_out=setTimeout("anim2(); $('.gallery_photo_idx').removeClass('gallery_photo_idx_on'); $('#gallery_photo_idx_'+gallery_new).addClass('gallery_photo_idx_on'); $('.page').hide(0); $('#page_'+Math.ceil(gallery_new/10)).css('display','inline');",1000);
		}
		}
	)
}

function gallery_prev(){
	gallery_stop=1;
	clearTimeout(gallery_out);
	$(".picture_gallery2 img").stop(true,true);
		gallery_new=gallery_current-1;
	if(gallery_new==0){
		gallery_new=gallery_n;
	}
	$('.gallery_photo_idx').removeClass('gallery_photo_idx_on');
	$('#gallery_photo_idx_'+gallery_new).addClass('gallery_photo_idx_on');
	$(".page").hide(0);
	$("#page_"+Math.ceil(gallery_new/10)).css('display','inline');
	gallery_stop=0;
	anim2();
}

function gallery_next(){
	gallery_stop=1;
	clearTimeout(gallery_out);
	$(".picture_gallery2 img").stop(true,true);
	if(gallery_new>gallery_n){
		gallery_new=1;
	}
	$('.gallery_photo_idx').removeClass('gallery_photo_idx_on');
	$('#gallery_photo_idx_'+gallery_new).addClass('gallery_photo_idx_on');
	$(".page").hide(0);
	$("#page_"+Math.ceil(gallery_new/10)).css('display','inline');
	gallery_stop=0;
	anim2();
}
/* breve-texte box */
function breve_toggle(ob){
	var n=$(ob).prev().filter(".breve_texte");
	if(n.length){
		n.toggle();
		$(ob).find('span').toggle();
	}
}

/* verif set appointement form */
function verif_set_appointment(){
	var MsgErreurs = new Array();
	var i = 0;
	var TexteErreurs = "";
	var reTrim=/^\s+|\s+$/g;
	
	/* Insurance Company */
	if($(".make_appointment .pay:checked").length==0) {
		if(document.f.insurance_company.selectedIndex==0){
			MsgErreurs[i++]="- Insurance Company";
		}
	}
	/* First Name */
	document.f.firstname.value=document.f.firstname.value.replace(reTrim,'');
	if (document.f.firstname.value.length==0) {
		MsgErreurs[i++]="- First Name";
	}
	/* Last Name */
	document.f.lastname.value=document.f.lastname.value.replace(reTrim,'');
	if (document.f.lastname.value.length==0) {
		MsgErreurs[i++]="- Last Name";
	}
	/* Email */
	document.f.email.value=document.f.email.value.replace(reTrim,'');
	if (document.f.email.value.length==0 || !/^(.+)@(.+)\.(.+)$/.test(document.f.email.value)) {
		MsgErreurs[i++]="- Email";
	}
	/* Phone */
	document.f.phone1.value=document.f.phone1.value.replace(reTrim,'');
	if (document.f.phone1.value.length<10) {
		MsgErreurs[i++]="- Primary Phone Number";
	}
	/* Phone */
	document.f.phone2.value=document.f.phone2.value.replace(reTrim,'');
	if (document.f.phone2.value.length>0 && document.f.phone2.value.length<10) {
		MsgErreurs[i++]="- Secondary Phone Number";
	}
	/* year */
	if ($("select[name='vehicule_year']").val()=="") {
		MsgErreurs[i++]="- Year";
	}
	/* date */
	if ($("select[name='app_date']").val()=="") {
		MsgErreurs[i++]="- Day";
	}
	/* time */
	if ($("select[name='app_time']").val()=="") {
		MsgErreurs[i++]="- Time";
	}
	
	/* Errors */
	if (MsgErreurs.length > 0){
		TexteErreurs += "Mandatory fields : \n\n";
		for (i=0; i < MsgErreurs.length; i++) {
			TexteErreurs += "  " + MsgErreurs[i] + "\n";
		}
		alert(TexteErreurs);
		return false;
	}
	else{
		return true;
	}
}


/* sort vehicule make */
function sortlist(id) {
	var lb = document.getElementById(id);
	if (lb){
		var arrTexts = new Array();
	
		for(i=1; i<lb.length; i++) {
			arrTexts[i-1] = lb.options[i].text+':'+lb.options[i].value;
		}
		arrTexts.sort();
		
		for(i=0; i<arrTexts.length; i++) {
			el = arrTexts[i].split(':');
			lb.options[i+1].text = el[0];
			lb.options[i+1].value = el[1];
		}
	}
}

/* zebra testimonials table*/
function zebra_testimonials(){
	$(".table_testimonials tr:visible").each(function(i){
		if(i%2==1){
			$(this).addClass("tr_1");
		}
		else {
			$(this).removeClass("tr_1");
		}
	});
}

var td_make=new Array();


/* DOCUMENT READY */
$(function(){
	/* close popup if click on overlay */
	$('.menu_top .m1_item').mouseenter(function(){
		$(".m1_a",this).addClass("m1_a_on");
		$(".m2",this).show(0);
	}).mouseleave(function(){
		$(".m1_a",this).removeClass("m1_a_on");
		$(".m2",this).hide(0);
	})
	
	/* close popup if click on overlay */
	$('.ui-widget-overlay').live('click',function(event){
		if(this==event.target){
			$('.popup_box,#map_dialog').dialog('close');
		}
	})
	
	var my_input_default_value=$('.form_login .my_input').val();
	
	/* login form input */
	$('.form_login .my_input').focus(function(){
		if($(this).val()==my_input_default_value){
			$(this).val("");
		}
	})
	
	/* login form input */
	$('.form_login .my_input').blur(function(){
		if($.trim($(this).val())==""){
			$(this).val(my_input_default_value);
		}
	})
	
	
	$('.gallery_photo_idx').click(function(){
		gallery_stop=1;
		clearTimeout(gallery_out);
		$(".picture_gallery2 img").stop(true,true);
		gallery_new=parseInt($(this).text());
		$('.gallery_photo_idx').removeClass('gallery_photo_idx_on');
		$('#gallery_photo_idx_'+gallery_new).addClass('gallery_photo_idx_on');
		$(".page").hide(0);
		$("#page_"+Math.ceil(gallery_new/10)).css('display','inline');
		gallery_stop=0;
		anim2();
		return false;
	});
	
	/* gallery */
	gallery_n=$(".picture_gallery2 img").length;
	setTimeout("$('.gallery_photo_idx').removeClass('gallery_photo_idx_on'); $('#gallery_photo_idx_'+gallery_new).addClass('gallery_photo_idx_on');",1500);
	gallery_out=setTimeout('anim2()',1000);
	/* google map */
	initialize();
	
	/* calendar 
	$(".make_appointment input[name='date']").datepicker({minDate: 0, maxDate: "+7D"});*/
	
	/* change date */
	$("select[name='app_date']").change(function(){
	
		$("select[name='app_time']").append(op);
		op=$("select[name='app_time'] option").not("."+$(this).val()).detach();
		/*$("select[name='app_time']")[0].selectedIndex=0;*/
	});
	
	/* change time - detach */
	var op=$("select[name='app_time'] option").not("."+$("select[name='app_date']").val()).detach();
	
	/* sort vehicule make */
	sortlist('vehicule_make');
	sortlist('make');
	sortlist("make_testimonials");
	
	zebra_testimonials();
	
	/* create array from make column */
	$(".table_testimonials .td_5").not(":first").each(function(){
		if(jQuery.inArray($(this).text(),td_make)==-1){
			td_make.push($(this).text());
		}
	});
	
	/* remove make */
	$("#make_testimonials option").not(":first").each(function(){
		if(jQuery.inArray($(this).text(),td_make)==-1){
			$(this).remove();
		}
	});
	
	/* remove make */
	$("#make_testimonials").change(function(){
		var vtext=$("#make_testimonials option:selected").text();
		$(".table_testimonials tr").not(":first").each(function(){
			if($(".td_5",this).text()==vtext || vtext=="All"){
				$(this).removeClass("tr_hide");
			}
			else {
				$(this).addClass("tr_hide");
			}
		});
		zebra_testimonials();
	});
	
	/* toggle insurance company */
	$(".make_appointment .pay").click(function(){
		if($(".make_appointment .pay:checked").length>0) {
			$(".make_appointment .tr_insurance").addClass("off");
		}
		else {
			$(".make_appointment .tr_insurance").removeClass("off");
		}
	});
});
