//updateImages = function (){
function updateImages() {
	lastFooterImages++;
	var el = document.getElementById('footerimages');
	if(el.children.length - 1 < lastFooterImages) {
		lastFooterImages = 0;
	}
	var i = 0;
	while(i < el.children.length) {
		if(i == lastFooterImages) {
			el.children[i].style.display = 'block';
		} else {
			el.children[i].style.display = 'none';
		}
		i++;
	}
	if(lastFooterImages + 1 == el.children.length) {
		el.style.paddingTop = '0px';
	} else {
		el.style.paddingTop = '35px';
	}
	setTimeout("updateImages();",2337);
}
//changeApp = function(a) {
function changeApp(e) {
	var i = 0;
	var r = '';
	while(i < address[a]['style'].length) {
		r = r + '<option value="' + address[a]['style'][i] + '">' + address[a]['style'][i] + '</option>';
		i++;
	}
	$("selectStylist").innerHTML = '<option value="No Preference">No Preference</option>' + r.toString();
}
//changeAddress = function () {
function changeAddress() {
	lastAddress++;
	i = lastAddress;
	if(i >= 0 && i < address.length) {
	} else {
		i = 0;
		lastAddress = 0;
	}
	try {
		document.getElementById('contact').innerHTML = address[i]['name'].toUpperCase() + ' ' + address[i]['number'];
		setTimeout("changeAddress();",3000);
	} catch (e) {
	}
}

//init = function () {
function init() {
	setTimeout('changeAddress();',1337);
	try {
		document.getElementById('view_on_map').addEventListener("click",function () {showMap(false);}, false);
	} catch (e) {
	}
}

//changeVo = function (i) {
function changeVo(i) {

	try {
		document.getElementById('option' + lastmapid).style.fontWeight = "normal";
		document.getElementById('option' + lastmapid).style.color = "#777";
	} catch (e) {
	}	
	lastmapid = i;
	document.getElementById('option' + i).style.fontWeight = "bold";
	document.getElementById('option' + i).style.color = "#2D2156";
}

//changeMap = function (i) {
function changeMap(i) {
	map = false;
	try {
		document.getElementById('option' + lastmapid).style.fontWeight = "normal";
		document.getElementById('option' + lastmapid).style.color = "#777";
	} catch (e) {
	}
	lastmapid = i;
	try {
		document.getElementById('option' + i).style.fontWeight = "bold";
		document.getElementById('option' + i).style.color = "#2D2156";
		document.getElementById('salonimage').src = address[i]['image'];
		document.getElementById('mapname').innerHTML = address[i]['name'].toUpperCase();
		document.getElementById('mapaddress').innerHTML = address[i]['address'][0] + '<br />' + address[i]['address'][1] + '<br />' + address[i]['address'][2];
		document.getElementById('mapnumber').innerHTML = address[i]['number'];
		document.getElementById('mapnumber').href = "tel://" + address[i]['number'];
	} catch (e) {
	}
	lastMap = i;
	return false;
}

//createMap = function () {
function createMap() {
	var myOptions = {
		zoom: 9,
		center: new google.maps.LatLng(address[0]['googlemaps']['lat'],address[0]['googlemaps']['lng']),
		mapTypeId: google.maps.MapTypeId.ROADMAP
	};
	map = new google.maps.Map(document.getElementById("map"), myOptions);
	var i = lastMap;
		var ll = new google.maps.LatLng(address[i]['googlemaps']['lat'],address[i]['googlemaps']['lng']);
		var marker = new google.maps.Marker({
			position: ll,
			map: map,
			title: address[i]['name']
		});
}
//showMap = function (i) {
function showMap(i) {
	$('popmapheader').style.display = 'block';
	$('popmapwrapper').style.borderColor = '#2C2056';
	if(i === false) {
		i = lastMap;
	}
	document.getElementById('fade').style.display = 'block';
	document.getElementById('fade').style.height = window.innerHeight.toString() + "px";
	document.getElementById('popmapwrapper').style.display = 'block';
	document.getElementById('popmaptitle').innerHTML = address[i]['name'];
	if(map === false) {
		createMap();
	}
	google.maps.event.trigger(map, 'resize'); 
	var center = new google.maps.LatLng(address[i]['googlemaps']['lat'],address[i]['googlemaps']['lng']);
	map.setCenter(center);
}

//popup = function (url,width,height) {
function popup(url,width,height) {
	newwindow=window.open(url,'name','height=' + height.toString() + ',width=' + width.toString());
	if (window.focus) {
		newwindow.focus();
	}
	return false;
}

//showGallery = function (image) {
function showGallery(image) {
	if(image < 0) {
		image = images.length - 1;
	}else if(image < images.length) {
	} else {
		image = 0;
	}
	document.getElementById('fade').style.display = 'block';
	document.getElementById('popmapwrapper').style.display = 'block';
	document.getElementById('map').style.backgroundImage = "url('"+images[image]['location']+"')";
	document.getElementById('map').innerHTML = '<div style="right: -4px;" class="arrow"><img src="/images/rightarrow.png" alt="next" onclick="showGallery(lastImage + 1);"></div><div style="left: -4px;" class="arrow"><img src="/images/leftarrow.png" alt="next" onclick="showGallery(lastImage - 1);"></div>';

	document.getElementById('popmaptitle').innerHTML = images[image]['title'];
	lastImage = image;
}



//\///////////////////////////////////////////////////////////////////////////////////////////////\
//\///////////////////////////////////////////////////////////////////////////////////////////////\
//\////////////////////////////////// NOTHING TO DO WITH ME///////////////////////////////////////\
//\///////////////////////////////////////////////////////////////////////////////////////////////\
//\///////////////////////////////////////////////////////////////////////////////////////////////\


// AjaxForms

	// All forms with class 'ui-ajaxform' are submitted via AJAX and
	// the AJAX response is placed in the element with the ID that
	// is specified in the target attribute of the form.

	var AjaxForms = new Class({
		initialize: function(){
			// Initialize Events
			$$("form.ui-ajaxform").removeEvents('submit');
			$$("form.ui-ajaxform").addEvent('submit',this.submitform.bind(this));
		},
		submitform: function(event){

			var form = event.target;

			// Stop the form form submitting...
			event.preventDefault();
			event.stop();

			// If a user hits return in a text field, the text field is the target!
			while (form.nodeName != "FORM") { form = $(form).parentNode; }

			// Check if validation is required
			if (form.validate) {
				response = form.validate();
				if (response != true) {
					if (response != false) alert(response);
					return false;
				}
			}

			// Check if confirmation is required
			confirmation = form.getProperty('confirmation');
			if (confirmation && confirmation != "") {
				if (!confirm(confirmation)) return false;
			}

			// Figure out where the response is going (if anywhere)...
			var target_id = form.getProperty('target');
			if (target_id != "") {
				var target = $$("#"+target_id);
				target.addClass("ui-loading");
				target.fireEvent("ui-loading");
			}

			// GRAB THE FORM URL..
			url = form.get('action');

			// CONVERT THE FORM TO A QUERY...
			var querystring = form.toQueryString();

			// THIS SHOULDNT BE REQUIRED BUT IT IS...
			querystring = querystring.replace(/%5B%5D/g,"");

			// TURN THE QUERY STRING IN TO AN OBJECT..
			query = querystring.parseQueryString();

			// Fire off ajax request...
			if (form.req) form.req.cancel();

			form.req = new Request.HTML({
				'url':url,
				'method':'post',
				'data':query,
				'evalScripts':true,
				'onComplete':function(){
					if (target_id != "") {
						// Put content in destination text...
						target.set('html',this.response.text);
						target.removeClass("ui-loading");
						target.fireEvent("ui-complete-pre-interface"); // Use this event to put things in place before the interface loads
						UIloadInterface();
						target.fireEvent("ui-complete"); // Use this event to do things after the interface loads
					}
				},
				'onFailure': function(){
					if (target_id != "") {
						// Put content in destination text...
						target.innerHTML = "<p class=\"ui-error\">Ajax Request Failed</p>";
						target.removeClass("ui-loading");
					}
				}

			});

			form.req.send();

			return false;

		}
	});

// AjaxLinks
// All links with class 'ui-ajaxlink' are submitted via AJAX and
// the AJAX response is placed in the element with the ID that
// is specified in the target attribute of the link.
var AjaxLinks = new Class({
	initialize: function(){
		// Initialize Events
		$$(".ui-ajaxlink").removeEvents('click');
		$$(".ui-ajaxlink").addEvent('click',this.submitlink.bind(this));
	},
	submitlink: function(event){
		var link = $(event.target);
		message = link.rel;
		if (link.tagName != "A") {
			while (!link.hasClass('ui-ajaxlink') && link.tagName != 'HTML') {
				link = $(link).getParent();
			}
		}
		url = $(link).getProperty("href");
		url = url.match(/^[^?]*/);
		url = url.toString();
		querystring = $(link).getProperty("href");
		querystring = querystring.match(/[^?]*$/);
		querystring = querystring.toString();
		query = querystring;
		// Stop the link from clicking...
		event.preventDefault();
		event.stop();
		// Check if confirmation is required
		confirmation = link.getProperty('confirmation');
		if (confirmation && confirmation != "") {
			if (!confirm(confirmation)) return false;
		}
		// Figure out where the response is going (if anywhere)...
		var target_id = link.getProperty('target');
		if (target_id != "") {
			var target = $$("#"+target_id);
			target.addClass("ui-loading");
			target.fireEvent("ui-loading");
		}
		// Fire off ajax request...
		var req = new Request.HTML({
			'url':url,
			'method':'post',
			'data':query,
			'evalScripts':true,
			'onComplete':function(){
				$('popmapheader').style.display = 'none';
				$('popmapwrapper').style.borderColor = '#5F6062';
				if (target_id != "") {
					document.getElementById('map').style.overflowX = 'visible';
					document.getElementById('map').style.overflowY = 'visible';
					document.getElementById('popmapwrapper').style.display = 'block';
					document.getElementById('fade').style.display = 'block';

					target.set('html',this.response.text);
					target.removeClass("ui-loading");
					UIloadInterface();
					document.getElementById('popmaptitle').innerHTML = message;
				}
			},
			'onFailure': function(){
				if (target_id != "") {
					document.getElementById('popmapwrapper').style.display = 'block';
					document.getElementById('fade').style.display = 'block';
					// Put content in destination text...
					target.innerHTML = "<p class=\"ui-error\">Ajax Request Failed</p>";
					target.removeClass("ui-loading");
				}
			}
		});
		req.send();
		// Don't let the link click!
		return false;
	}
});

window.onload = init;


window.addEvent('domready', function(){
                UIloadInterface();
        });

        function UIloadInterface() {
                // FUNCTION TO LOAD THE INTERFACE COMPONENTS
                ajaxlinks = new AjaxLinks();
        }

	function runSlideShow(backid, imageid, imagearray,d) {

		if (!d) var d = 0;

		nextPic = d + 1; if (nextPic >= imagearray.length) nextPic = 0; 

		fadelength = 2000;
		steps = 100;

		// Set Background Image To Match Foreground
		document.getElementById(backid).style.background = "url('"+document.getElementById(imageid).src+"')";

		// Set Foreground Opacity to Transparent
		setTimeout('setOpacity("'+imageid+'",0)',50);

		// Set Foreground to the next Picture
		setTimeout('document.getElementById(\''+imageid+'\').src = \''+imagearray[nextPic]+'\'',100);

		// Loop through changing the Opacity
		for(i = 0; i <= steps; i++) {
			opacity = i/steps * 100;
			setTimeout('setOpacity("'+imageid+'",'+opacity+')',((i*(fadelength/steps))+150));
		}

		d++;
		
		if (d >= imagearray.length) d=0;

		setTimeout( function () { runSlideShow(backid,imageid,imagearray,d) } ,7000);

	}

	function arrayToString(arr) {
		return str = 'new Array("' + arr.join('", "') + '")';
	}

	function setOpacity(id,value) {
		document.getElementById(id).style.opacity = (value / 100);
		document.getElementById(id).style.MozOpacity = (value / 100);
		document.getElementById(id).style.KhtmlOpacity = (value / 100);
		document.getElementById(id).style.filter = "alpha(opacity=" + value + ")";
	}


