var counter = 1;
var counter2 = 2;
var bannerCount = 3;	
function changeBanner() {
	$('#banner'+counter2).fadeIn('slow');
	$('#banner'+counter).fadeOut('slow');
	counter++;
	counter2++;
	if (counter > bannerCount) {
		counter = 1;
	}
	if (counter2 > bannerCount) {
		counter2 = 1;
	}
	setTimeout("changeBanner()",3000);
}


function validateAndSubmitForm(theForm) {
	$('#' + theForm).validate({ignore: ":hidden"});
	if ($('#' + theForm).valid()) {
		return true;
		//$('#' + theForm).submit();
	} else {
		//alert(lbl_contact_fill_out_required);
		return false;
	}
}


function thisMovie(movieName) {
	if (navigator.appName.indexOf("Microsoft") != -1) {
		return window[movieName]
	}
	else {
		return document[movieName]
	}
}

function changeFlash(country,region,city,store,opening) {
	thisMovie('sotester').doFlashInfo(country,region,city,store,opening);
}

function doActiveCountry(str) {
	// set the choosen country selected
	$("#country option:selected").attr("selected","");
	$("#country option[value='" + str + "']").attr("selected","selected");
	// update form options and results
	activateCountry(str);
}

function doActiveRegion(str) {
	// set the choosen region selected
	$("#region option:selected").attr("selected","");
	$("#region option[value='" + str + "']").attr("selected","selected");
	// update form options and results
	var country = $("#country option:selected").val();
	var opening = $("#opening:checked").val();
	activateRegion(country,str,opening);
}

function doActiveStore(str,flash) {
	// leave the select boxes as they are
	activateStore(str,"N");
}

function doHideStore() {
	// rebuild the search results
	var country = $("#country option:selected").val();
	var region = $("#region option:selected").val();
	var city = $("#city option:selected").val();
	var opening = $("#opening:checked").val();
	if (country && !region && !city) {
		activateCountry(country,opening);
	} else if (country && region && !city) {
		activateRegion(country,region,opening);
	} else if (country && region && city) {
		activateCity(country,region,city,opening);
	}
}

function activateCountry(country,opening) {
	// show/hide region-,city- and opening-select
	if(country != "") {
		$("#regionSelection").show();
		$("#openingSelection").show();
		if (country == "BE") {
			$("#openingSelection label").html(lbl_store_opening_expl_be);
		} else if (country == "FR") {
			$("#openingSelection label").html(lbl_store_opening_expl_fr);
		} else if (country == "DE") {
			$("#openingSelection label").html(lbl_store_opening_expl_de);
		} else {
			$("#openingSelection").hide();
		}
	} else {
		$("#regionSelection").hide();
		$("#citySelection").hide();
		$("#openingSelection").hide();
	}	
	// update region options through ajax
	var language = $("#language").val();
	var dataString = "country=" + country + "&language=" + language + "&opening=" + opening + "&type=country";
	var regionHtml = $.ajax({ type: "POST", url: "/inc/ajax.dhtml", data: dataString, async: false }).responseText;
	$("#region").html(regionHtml);
	// clear city options
	var theForm = document.storeForm;
	while (theForm.city.options.length > 1) {
		theForm.city.options[(theForm.city.options.length - 1)] = null;
	}
	// update info block
	$("#storeResult").load("/inc/ajax_results.dhtml", {country: country, language: language, opening: opening, type: "country"} );
}

function activateRegion(country,region,opening) {
	// show/hide city- and opening-select
	if(region != "") {
		$("#citySelection").show();
	} else {
		$("#citySelection").hide();
	}
	// update city options through ajax
	var language = $("#language").val();
	var dataString = "country=" + country + "&region=" + region + "&language=" + language + "&opening=" + opening + "&type=region";
	var regionHtml = $.ajax({ type: "POST", url: "/inc/ajax.dhtml", data: dataString, async: false }).responseText;
	$("#city").html(regionHtml);
	// update info block
	$("#storeResult").load("/inc/ajax_results.dhtml", {country: country, region: region, language: language, opening: opening, type: "region"} );
	// remove the option to choose blank
	//$("#region option[value='']").remove();
}

function activateCity(country,region,city,opening) {
	// update info block
	var language = $("#language").val();
	$("#storeResult").load("/inc/ajax_results.dhtml", {country: country, region: region, city: city, language: language, opening: opening, type: "city"} );
	// remove the option to choose blank
	//$("#city option[value='']").remove();
}

function activateStore(store,flash) {
	// update info block
	var language = $("#language").val();
	$("#storeResult").load("/inc/ajax_results.dhtml", {store: store, language: language, type: "store"} );
	if (flash == "Y") {
		// send info to flash
		var country = $("#country option:selected").val();
		var region = $("#region option:selected").val();
		var city = $("#city option:selected").val();
		var opening = $("#opening:checked").val();
		changeFlash(country,region,city,store,opening);
	}
}




$(document).ready(function() {

	// STORE LOCATOR
    $("#storeForm #country").change( function () { 
		// send info to flash
		var country = $("#country option:selected").val();
		//var region = $("#region option:selected").val();
		// var city = $("#city option:selected").val();
		var opening = $("#opening:checked").val();
		changeFlash(country,"","","",opening);
		// update form options and results
		activateCountry(country,opening);
    });
    $("#storeForm #region").change( function () { 
		// send info to flash
		var country = $("#country option:selected").val();
		var region = $("#region option:selected").val();
		if(region == "") { var city = ""; } else { var city = $("#city option:selected").val(); }
		var opening = $("#opening:checked").val();
		changeFlash(country,region,city,"",opening);
		// update form options and results
		activateRegion(country,region,opening);
    });
    $("#storeForm #city").change( function () { 
		// send info to flash
		var country = $("#country option:selected").val();
		var region = $("#region option:selected").val();
		var city = $("#city option:selected").val();
		var opening = $("#opening:checked").val();
		changeFlash(country,region,city,"",opening);
		// update info block
		activateCity(country,region,city,opening);
    });
    $("#storeForm #opening").change( function () { 
		// send info to flash
		var country = $("#country option:selected").val();
		var region = $("#region option:selected").val();
		var city = $("#city option:selected").val();
		var opening = $("#opening:checked").val();
		changeFlash(country,region,city,"",opening);
		// update info block
		if (country && !region && !city) {
			activateCountry(country,opening);
		} else if (country && region && !city) {
			activateRegion(country,region,opening);
		} else if (country && region && city) {
			activateCity(country,region,city,opening);
		}
    });



	// VALIDATOR
	//$.validator.messages.required = "*";
	$.validator.messages.email = "*";
	// Add validation for required fields
	$.validator.addMethod("req", function (value, element){ return jQuery.trim(value).length > 0;}, "*");

	// HOVER EFFECT ON BRAND LIST
	$('#brandsOverview .transparent').hover(
		function(){
			$('#brandsOverview img.transparent').show(); //mouseover
			$('#brandsOverview #' + this.id).hide();
		},
		function(){
			// mouseout, just keep the last one active
		}
	);
	
	// TRAINING
    $("#trainingForm select").change( function () { 
		$("#trainingForm").submit();
    });

	// COUNTRY CHANGE
	$("#changeCountry").hover(
		function(){
			$("#taalkeuze").show(); //mouseover
		},
		function(){
			//$("#taalkeuze").hide(); // mouseout
		}
	);
	$("#taalkeuze").hover(
		function(){
			//$("#taalkeuze").show(); //mouseover
		},
		function(){
			$("#taalkeuze").hide(); // mouseout
		}
	);
    //$("#changeCountry").click( function () { 
	//	$("#taalkeuze").toggle();
	//	return false;
    //});
    
 	// EMPTY SEARCHBOX
	$('#searchTerm').click(function() {
		$('#searchTerm').attr('value', '');
	});
	$('#emailInput').click(function() {
		$('#emailInput').attr('value', '');
	});

	// TOP BANNER FADE IN/OUT
	setTimeout("changeBanner()",3000);

});