var ie = false;
var ie6 = false;
var ie7 = false;

// slideFadeToggle function
(function($) {
	$.fn.slideFadeToggle = function(speed, easing, callback) {
		return this.animate({opacity: 'toggle', height: 'toggle'}, speed, easing, callback);
	};
})(jQuery);

/* ################################################################## */

var preloadFlag = false;
function preloadImages() {
	ashton_about_us_full = newImage(udir+"/about-us-full.jpg");
	ashton_bridal_bg_1 = newImage(hdir+"/images/ashton_bridal_bg-1.jpg");
	ashton_bridal_bg_2 = newImage(hdir+"/images/ashton_bridal_bg-2.jpg");
	ashton_content_bg = newImage(hdir+"/images/ashton_content_bg.jpg");
	model_over_1 = newImage(hdir+"/images/model_over-1.png");
	model_over_2 = newImage(hdir+"/images/model_over-2.png");
	corner_flourish = newImage(hdir+"/images/corner_flourish.png");
	preloadFlag = true;
}

function newImage(arg) {
	if (document.images) {
		img = new Image();
		img.src = arg;
		return img;
	}
};

preloadImages();

jQuery(document).ready(function($) {

	if($('#page_bg.frontpage').length){
		setTimeout('nextbg()', 3000);
	}
	
	$('.bridal-img').lightBox();

	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;
	}

	$('.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);
			}
		}
	});
	
	$(".sub-menu").css("display","none").removeClass("js_trans");
	// menu keyboard support and fade effects
	$("#menu a + .sub-menu").prev("a").bind("mouseenter focus", function(e){
		var nextsub = $(this).next(".sub-menu");
		if ( nextsub.hasClass("sub-menu-1") ) {
			nextsub.stop(true,true).delay(1000).fadeIn("fast");
		} else {
			nextsub.stop(true,true).fadeIn("fast");
		}
	});
	$("#menu a + .sub-menu").parent("li").bind("mouseleave", function(e){
		$(this).children("ul").stop(true,true).fadeOut("slow");
	});
	
	accessories_div = $(".accessories_img");
	accessories_img = $(".accessories_img img");
	if(accessories_img.length > 0){
		// init accessories img swapper
		//accessories[0] = newImage(udir+"/accessories.jpg");
		//accessories[0] = newImage(udir+"/accessories-bags.jpg");
		accessories[0] = newImage(udir+"/accessories-earrings.jpg");
		accessories[1] = newImage(udir+"/accessories-neck1.jpg");
		accessories[2] = newImage(udir+"/accessories-neck2.jpg");
		accessories[3] = newImage(udir+"/accessories-neck3.jpg");
		accessories[4] = newImage(udir+"/accessories-shoes.jpg");
		setTimeout("runAccessoriesSwap();", 2000)
	}
	
	var searchText = $('#s');
	if(searchText.length > 0){
		
		searchText.bind("focus dblclick", function () {
			if ( $(this).val() == 'enter search text...' ) {
				$(this).val('').css({'color':'#333333'});
			} else {
				$(this).select();
			}
		}).bind("blur", function () {
			if ( $(this).val() == '' ) {
				$(this).val('enter search text...').css({'color':'#aaaaaa'});
			}
		});
		
		if ( searchText.val() == 'enter search text...' ) {
			searchText.css({'color':'#aaaaaa'});
		}
		
		$('#searchform').bind("submit", function () {
			if ( searchText.val() == 'enter search text...' || searchText.val() == '' ) {
				return false;
			}
		});
		
	}

});

/* ################################################################## */

var accessories_img;
var accessories_div;
var current_accessory = 0;
var accessories = new Array();
function runAccessoriesSwap() {
	if (current_accessory >= accessories.length) { current_accessory = 0; }
	accessories_div.css("background", "url(" + accessories[current_accessory].src + ") no-repeat top right");
	current_accessory++;
	if (current_accessory >= accessories.length) { current_accessory = 0; }
	accessories_img.fadeOut(0).attr("src", accessories[current_accessory].src).fadeIn(2000);
	setTimeout("runAccessoriesSwap();", 4000)
}

var bg=1;

function nextbg() {

	setbg_under();

	if (bg==1) {
		$("#bg_over").fadeOut(0);
		setbg_over(2);
		$("#bg_over").fadeIn(2000, function(){
			bg=2;
		});
	} else if (bg==2) {
		$("#bg_over").fadeOut(0);
		setbg_over(1);
		$("#bg_over").fadeIn(2000, function(){
			bg=1;
		});
	}
	
	setTimeout('nextbg()', 8000);

};

function setbg_over(num) {
	$("#background-over").css({"background-image":"url("+hdir+"/images/ashton_bridal_bg-"+num+".jpg)"});
	$("#model-over").css({"background-image":"url("+hdir+"/images/model_over-"+num+".png)"});
};

function setbg_under() {
	$("#background-under").css({"background-image":$("#background-over").css("background-image")});
	$("#model-under").css({"background-image":$("#model-over").css("background-image")});
};
