function preloadImages() {
	var d=document;
	if(d.images){
		if(!d.img_arr) d.img_arr=new Array();
  	var i, j=d.img_arr.length, a=preloadImages.arguments;
		for(i=0; i<a.length; i++)
    	if (a[i].indexOf("#")!=0){ d.img_arr[j]=new Image; d.img_arr[j++].src=a[i];}
	}
}


jQuery.easing.easeInOutCirc = function (x, t, b, c, d) {
	if ((t/=d/2) < 1) return -c/2 * (Math.sqrt(1 - t*t) - 1) + b;
	return c/2 * (Math.sqrt(1 - (t-=2)*t) + 1) + b;
}


function getFileExtension(fileName) 
{ 
	if( fileName.length == 0 ) return "";
	var dot = fileName.lastIndexOf(".");
	if( dot == -1 ) return "";
	var extension = fileName.substr(dot,fileName.length);
	return extension;
}


jQuery(document).ready(function($) {

	$("span.add").each(function() {
		var txt = $(this).text();
		switch(txt) {
			case "info (at) weddingsinstyle (dot) com":
				$(this).html(createAddress(info));
				break;
			case "amanda (at) weddingsinstyle (dot) com":
				$(this).html(createAddress(amanda));
				break;
			case "jill (at) weddingsinstyle (dot) com":
				$(this).html(createAddress(jill));
				break;
			case "gisele (at) weddingsinstyle (dot) com":
				$(this).html(createAddress(gisele));
				break;
			case "info (at) balanceinstyle (dot) com":
				$(this).html(createAddress(info_balance));
				break;
			case "amanda (at) balanceinstyle (dot) com":
				$(this).html(createAddress(amanda_balance));
				break;
			case "jill (at) balanceinstyle (dot) com":
				$(this).html(createAddress(jill_balance));
				break;
			case "gisele (at) balanceinstyle (dot) com":
				$(this).html(createAddress(gisele_balance));
				break;
			default:
				$(this).html(createAddress(info, txt));
		}
	});
	
	
	//set the starting bigestHeight variable
	var biggestHeight = 0;
	//check each of them
	$('.tab').each(function(){
		// if the height of the current element is
		// bigger then the current biggestHeight value
		if($(this).height() > biggestHeight){
			// update the biggestHeight with the
			// height of the current elements
			biggestHeight = $(this).height();
		}
	});
	// when checking for biggestHeight is done set that
	// height to all the elements
	
	$('#tabs').tabs({ fx: { opacity: 'toggle', duration:200 } });
	$('.tab').height( biggestHeight );
	
	$('.lightbox_photo').lightBox();
	$('#photos-box .photo').bind("mouseenter focus", function(){
		$(this).css("background-position","0 -160px");
	}).bind("mouseleave blur", function(){
		$(this).css("background-position","0 0");
	});
	
	


	if($("#contact-form").length && $("#this-form").length) {
	
		// add the contact-form anchor to the form action
		var form_action = $("#contact-form form").attr("action")
		form_action += "#contact-form";
		$("#contact-form form").attr("action",form_action);
		
		// open #contact-form on #this-form click
		$("#this-form").replaceWith('<a href="#contact-form" tabindex="0" id="this-form">' + $("#this-form").text() + '</a>')
		.attr("title","Click to open the Contact Us Form")
		.live("click", function(){
			$("#contact-form").slideFadeToggle("normal",function(){
				if($(this).is(":hidden")) {
					$("#this-form").attr("title","Click to open the Contact Us form");
					return false;
				} else {
					$("#this-form").attr("title","Click to hide the Contact Us form");
					$.localScroll.hash({reset:false,duration:500});
				}
			});
		}).live("mouseenter", function () {
			$(this).css({"text-decoration":"none","color":"#146481"});
    }).live("mouseleave", function () {
			$(this).css({"text-decoration":"underline","color":"#3c3533"});
    });

		var red_count = 0; // red_count represents error on the form
		$("#si_contact_form div").each(function(){if($(this).css("color")=="red"){red_count+=1}});
		
		var hash = window.location.hash;
		
		if(red_count || hash == "#contact-form"){ 	// if there were errors,
			$("#contact-form").show();								// show the contact form
			$("#this-form").attr("title","Click to hide the Contact Us form");
			$.localScroll.hash({duration:100}) 				// scroll to the contact form
		}
	}
	
	
	
	$(".expander").css("cursor","pointer").prepend('<img src="'+imgdir+'/expand.gif" class="expand" />').hover(
      function () {
        $(this).children(".expand").attr("src",imgdir+"/expand_over.gif");
				$(this).children(".collapse").attr("src",imgdir+"/collapse_over.gif");
      }, 
      function () {
        $(this).children(".expand").attr("src",imgdir+"/expand.gif");
				$(this).children(".collapse").attr("src",imgdir+"/collapse.gif");
      }
    ).click(function() {
		$(this).toggleClass("open").next("div.collapser").slideFadeToggle("fast");
		if($(this).hasClass("open")) {
			$(this).children(".expand").addClass("collapse").removeClass("expand");
			$(this).children(".collapse").attr("src",imgdir+"/collapse_over.gif");
		} else {
			$(this).children(".collapse").addClass("expand").removeClass("collapse");
			$(this).children(".expand").attr("src",imgdir+"/expand_over.gif");
		}
	});
	
	$(".collapser").css("cursor","pointer").click(function() {
		$(this).prev(".expander").click().children(".expand").attr("src",imgdir+"/expand.gif");
	});
	
	
	
	$('#scroller').prepend('<img class="prev mouseover" src="'+imgdir+'/prev_arrow.gif" alt="prev" />').append('<img class="next mouseover" src="'+imgdir+'/next_arrow.gif" alt="next" />');

	$('#scroller').serialScroll({
		target:'#sections',
		items:'li', // Selector to the items ( relative to the matched elements, '#sections' in this case )
		prev:'img.prev',// Selector to the 'prev' button (absolute!, meaning it's relative to the document)
		next:'img.next',// Selector to the 'next' button (absolute too)
		axis:'xy',// The default is 'y' scroll on both ways
		navigation:'#navigation li a',
		duration:500,// Length of the animation (if you scroll 2 axes and use queue, then each axis take half this time)
		force:true, // Force a scroll to the element specified by 'start' (some browsers don't reset on refreshes)
		
		//queue:false,// We scroll on both axes, scroll both at the same time.
		//event:'click',// On which event to react (click is the default, you probably won't need to specify it)
		//stop:false,// Each click will stop any previous animations of the target. (false by default)
		//lock:true, // Ignore events if already animating (true by default)		
		//start: 0, // On which element (index) to begin ( 0 is the default, redundant in this case )		
		//cycle:true,// Cycle endlessly ( constant velocity, true is the default )
		//step:1, // How many items to scroll each time ( 1 is the default, no need to specify )
		//jump:false, // If true, items become clickable (or w/e 'event' is, and when activated, the pane scrolls to them)
		//lazy:false,// (default) if true, the plugin looks for the items on each event(allows AJAX or JS content, or reordering)
		//interval:1000, // It's the number of milliseconds to automatically go to the next
		constant:false, // constant speed
		
		onBefore:function( e, elem, $pane, $items, pos ){
			/**
			 * 'this' is the triggered element 
			 * e is the event object
			 * elem is the element we'll be scrolling to
			 * $pane is the element being scrolled
			 * $items is the items collection at this moment
			 * pos is the position of elem in the collection
			 * if it returns false, the event will be ignored
			 */
			 //those arguments with a $ are jqueryfied, elem isn't.
			e.preventDefault();
			if( this.blur )
				this.blur();
		},
		onAfter:function( elem ){
			//'this' is the element being scrolled ($pane) not jqueryfied
		}
	});
	
	
	
	preloadImages(imgdir+'/nav-services_over.jpg', imgdir+'/nav-personal_over.jpg', imgdir+'/nav-corporate_over.jpg', imgdir+'/nav-relocation_over.jpg', imgdir+'/nav-pricing_over.jpg', imgdir+'/nav-about_over.jpg', imgdir+'/nav-faq_over.jpg', imgdir+'/btn-weddings_over.jpg', imgdir+'/btn-events_over.jpg')
	
	$('.mouseover').bind("mouseenter focus", function(){
		
		if($(this).children("img").length!=0){
			var ext = getFileExtension($(this).children("img").attr("src"));
			if($(this).children("img").attr("src").indexOf('_over'+ext)==-1){
				$(this).children("img").attr("src", $(this).children("img").attr("src").substring(0, $(this).children("img").attr("src").indexOf(ext))+"_over"+ext);
			}
		}
		else if ($(this).is("img")){
			var ext = getFileExtension($(this).attr("src"));
			if($(this).attr("src").indexOf('_over'+ext)==-1){
				$(this).attr("src", $(this).attr("src").substring(0, $(this).attr("src").indexOf(ext))+"_over"+ext);
			}
		}
	}).bind("mouseleave blur", function(){
		if($(this).children("img").length!=0){
			var ext = getFileExtension($(this).children("img").attr("src"));
			if($(this).children("img").attr("src").indexOf('_over'+ext)!=-1) {
				$(this).children("img").attr("src", $(this).children("img").attr("src").substring(0, $(this).children("img").attr("src").indexOf('_over'+ext))+ext);
			}
		}
		else if ($(this).is("img")){
			var ext = getFileExtension($(this).attr("src"));
			if($(this).attr("src").indexOf('_over'+ext)!=-1) {
				$(this).attr("src", $(this).attr("src").substring(0, $(this).attr("src").indexOf('_over'+ext))+ext);
			}
		}
	})
	
	
	
});

// slideFadeToggle function
(function($) {
	$.fn.slideFadeToggle = function(speed, easing, callback) {
		return this.animate({opacity: 'toggle', height: 'toggle'}, speed, easing, callback);
	};
})(jQuery);

var mt=["x6d","x61","x69","108","000116","x6f","00058"];
var amanda=["x61","109","x61","x6e","100","97","64","98","x61","108","x61","x6e","x63","101","x69","110","x73","116","x79","x6c","x65","x2e","99","111","x6d"];
var jill=["x6a","x69","x6c","108","64","98","x61","108","97","110","99","x65","105","110","115","x74","121","x6c","101","x2e","x63","111","109"];
var gisele=["x67","x69","115","101","108","x65","64","x62","x61","108","97","x6e","x63","x65","105","110","x73","116","x79","x6c","101","46","x63","111","x6d"];
var info=["x69","110","102","x6f","x40","119","101","x64","100","x69","110","103","x73","105","110","x73","x74","x79","x6c","x65","x2e","99","x61"];

var amanda_balance=["x61","109","x61","x6e","100","97","64","98","x61","108","x61","x6e","x63","101","x69","110","x73","116","x79","x6c","x65","x2e","99","111","x6d"];
var jill_balance=["x6a","x69","x6c","108","64","98","x61","108","97","110","99","x65","105","110","115","x74","121","x6c","101","x2e","x63","111","109"];
var gisele_balance=["x67","x69","115","101","108","x65","64","x62","x61","108","97","x6e","x63","x65","105","110","x73","116","x79","x6c","101","46","x63","111","x6d"];
var info_balance=["x69","x6e","102","x6f","x40","98","97","x6c","x61","x6e","99","x65","x69","110","x73","116","x79","108","101","x2e","x63","111","109"];


function obscureArr(arr){
	obscureStr = "";
	for (i=0; i<arr.length; i++) obscureStr += '&#'+arr[i]+';';
	return obscureStr;
}

function createAddress(arr, txt){
	var vmt = obscureArr(mt);
	var ea = obscureArr(arr);
	if(typeof(txt) == "undefined"){ txt = ea; } 
	var el = '<a href="' + vmt + ea + '" tabindex="0">' + txt + '</a>';
	return el;
}
