$(document).ready(function() {

	// Navigation stuff

	$("ul.main-menu li").mouseover(function() {
		$("ul.main-menu li a").removeClass("active");															// Remove all active classes, in effect resetting all states
		$("a:lt(1)", this).addClass("active");																		// Add the active class to the first a in the hovered tab (using lt(1) prevents children being set with the active tab as well)
		$("ul.main-menu li ul").hide();																						// Hide all children
		$("ul", this).show();																											// Show highlighted tabs children
	});
	$("ul.main-menu").mouseleave(function() {																		// When you leave the navigation, reset tabs children to the current tab
		$("ul.main-menu li a").removeClass("active");															// Remove active class from all tabs
		$("ul.main-menu li ul").hide();																						// Hide all children
		$("ul.main-menu li a.current").parent().children("ul").show();						// Show current tabs children
	});
	$("ul.main-menu li a.current").parent().children("ul").show();							// Show current tabs children when page first loads

	// End navigation stuff





		$("#retrieve-design img").click(function() {
			//$(this).hide();
			$(this).parent().children().eq(1).fadeIn("slow");

			$(this).parent().append('\
						<div id="retrieve" style="color:black; font-size:85%; padding:0 19px; position:absolute; top:60px; z-index:3;" class="ret-quote"> \
							<div class="text" style="padding-bottom: 5px;"> \
								Enter the reference number that was supplied to you when you saved your design.  You will have been emailed a copy of this number. \
							</div> \
							<input class="saved" name="saved" type="text" /> \
							<input class="get" name="get" type="button" value="Retrieve" /> \
						</div>').fadeIn("slow");
			/*$("div#retrieve").fadeIn("slow");*/
			$("#retrieve-design #retrieve input.get").click(function() {

				//retrieve the design and load it up
				var design = $("#retrieve-design #retrieve input.saved").val();
				alert(design);
				$.get("/configurator/process.php", { product: "getdesign", mydesign: design}, function(data) {
					if(data) {

						Configurator._goto = data;
						Configurator.fire(Configurator._goto, design);

					} else {
						alert("Could not find specified design");
					}
			  });
				return false;
			});

			//Configurator.goto = "galaxy";
			//$.modal.close();
			return false;
		});



//$('#slide-arrow').animate({left: '960px'}, 1500);
//$('#slide-arrow').animate({left: '415px'}, 1500);


var tabout;
var tabin;

if($.browser.msie && $.browser.version == '6.0'){
    tabout = '-50px';
    tabin = '-115px';
} else if($.browser.msie && $.browser.version != '6.0'){
    tabout = '-50px';
    tabin = '-110px';
} else {
    tabout = '-56px';
    tabin = '-120px';
}

$("#baskettab").mouseenter(function(){
    $(this).stop().animate({'top':tabout}, 600);
});

$("#baskettab").mouseleave(function(){
    $(this).stop().animate({'top':tabin}, 300);
});


/*$('p.detail-listing').next().hide();

$('.show-detail').click(function(){
	$(this).parent().next().toggle('normal');
	return false;
});

$('.hide-detail').click(function(){
	$(this).parent().toggle('normal');
	//alert($(this).prev(2).html());
	return false;
});*/


/*$(".stage").click(function(){
	var nextDiv = $(this).attr("rev");
	var prevDiv = $(this).attr("rel");
	$("#" + prevDiv).toggle('fast');
	changeColor($('#' + prevDiv).prev());
	$("#" + nextDiv).toggle('fast');
	changeColor($('#' + nextDiv).prev());
	return false;
});*/





$("#choose-range-button").click(function(){
    if($.browser.msie && $.browser.version < 7){
        return true;
    } else {
        $("html").css('overflow-y', 'scroll');
        if($.browser.msie){
            //$("#overlay").show();
            $("#overlay").css('filter', 'alpha(opacity=80)');
            $("#overlay").fadeIn(200);
            //$("#overlay-box").css({'height':'400px','display':'block'});
            $("#overlay-box").css({'display':'block'});
            $("#overlay-box").animate({
                height:'400px'
            }, 'slow');
        } else {
            $("#overlay").fadeIn('slow');
            $("#overlay-box").animate({
                height:'400px'
            }, 'slow');
        }
        $("#overlay-box").css({left:( $(window).width() - $("#overlay-box").width() )/2,top:200});
        $("body").css({'height':$(window).height(),'overflow':'hidden'});
        return false;
    }
});

$("#view-range-button").click(function(){
    if($.browser.msie && $.browser.version < 7){
        return true;
    } else {
        $("html").css('overflow-y', 'scroll');
        if($.browser.msie){
            //$("#head-overlay").show();
            $("#head-overlay").css('filter', 'alpha(opacity=80)');
            $("#head-overlay").fadeIn(200);
            //$("#head-overlay-box").css({'height':'460px','display':'block'});
            $("#head-overlay-box").css({'display':'block'});
            $("#head-overlay-box").animate({
                height:'460px'
            }, 'slow');
        } else {
            $("#head-overlay").fadeIn('slow');
            $("#head-overlay-box").animate({
                height:'460px'
            }, 'slow');
        }
        $("#head-overlay").css({'top':'143px'});
        $("#head-overlay-box").css({left:(($(window).width() - $("#head-overlay-box").width())/2)-1,top:144});
        if($.browser.mozilla){
            $("#head-overlay").css({'top':'142px'});/*148*/
            $("#head-overlay-box").css({'top':'143px'});/*150*/
        } else if($.browser.mozilla != true && $.browser.msie != true){
            $("#head-overlay").css({'top':'142px'});
            $("#head-overlay-box").css({'top':'144px'});
        }
        $("body").css({'height':$(window).height(),'overflow':'hidden'});
        return false;
    }
});

function closeOverlay(){
    if($.browser.msie){
        $("#overlay-box").css({'height':'0px','display':'none'});
        $("#overlay").hide();
        $("#head-overlay-box").css({'height':'0px','display':'none'});
        $("#head-overlay").hide();
    } else {
        $("#overlay-box").fadeOut(200);
        $("#overlay-box").css('height','0px');
        $("#overlay").fadeOut(200);
        $("#head-overlay-box").fadeOut(100);
        $("#head-overlay-box").css('height','0px');
        $("#head-overlay").fadeOut(200);
    }
    $("html").css('overflow-y', 'visible');
    $("body").css({'height':'auto','overflow':'auto'});
};

$("#overlay-box-content a").click(function(){
    $(closeOverlay);
    if($(this).attr("class") != ""){
        return false
    } else {
        return true;
    }
});

$("#head-overlay-box-content a").click(function(){
    $(closeOverlay);
    if($(this).attr("class") != ""){
        return false
    } else {
        return true;
    }
});

var pathname = window.location.pathname.split('/');
if(pathname[2] == 'orion' || pathname[2] == 'galaxy' || pathname[2] == 'titan' || pathname[2] == 'freespace' || pathname[2] == 'cyberspace' || pathname[2] == 'apollo' || pathname[2] == 'pluto' || pathname[2] == 'nebula' || pathname[2] == 'mercury' || pathname[1] == 'pocket-doors' || pathname[1] == 'bedroom-furniture' || pathname[1] == 'freestanding-wardrobes'){
    //$("body").append('<div id="conversion"><h3>Dimension Conversion Help</h3>Use this conversion calculator to work out Metric or Imperial lengths.  The Calculator can convert from Imperial to Metric or from Metric to Imperial.<br /><form name="frm"><div class="convert-box"><h4>Convert Imperial to Metric:</h4><input type="text" name="ft" value="0" size="4" class="formField" /> feet <input name="in" type="text" value="0" size="4" class="formField" /> inches <br /><input type="image" src="/images/convert-button.png" class="calc" size="6" /><br /><input type="text" name="mm" value="" size="6" readonly="readonly" class="formField" /> mm</div><div style="margin-right: 0px;" class="convert-box"><h4>Convert Metric to Imperial:</h4><input type="text" name="mm_1" value="0" size="4" class="formField" /> mm <br /><input type="image" src="/images/convert-button.png" class="calc2" size="6" /><br /><input name="ft_1" type="text" value="" size="4" readonly="readonly" class="formField" /> feet <input type="text" name="in_1" value="" size="6" class="formField"> inches</div></form></div>');
}

var windowWidth = $(window).width();
var containerWidth = 920;
var conversionPosition
if(containerWidth < windowWidth){
	conversionPosition = (((windowWidth - containerWidth) / 2) + 224) + "px";
} else {
	conversionPosition = "500px";
}
//$("#conversion").css('left',conversionPosition);
//$("#conversion").css('left',"28%");

//$("#conversion").css('left',windowWidth + '600
$("#conversion").mouseenter(function(){
	//$(this).stop().animate({'right':'300px'}, 600);
});

$("#conversion").mouseleave(function(){
	//$(this).stop().animate({'right':'-168px'}, 600);
});
$("#conversion-img").show();
$("#conversion-img").click(function(){
	if($("#conversion").css('width') !== '404px'){
		$("#conversion").css('display', 'block');
		$("#conversion").stop().animate({width:'404px'}, 500);
	} else {
		$("#conversion").stop().animate({width:'0px'}, 500, function(data){
			$("#conversion").css('display', 'none');
		});
	}
});
$("#conversion-img-fire").click(function(){
	$("#conversion-img").trigger('click');
});

$("input.calc").click(function(){
	var ftValue = 0;
	var inValue = 0;
	var mmValue = 0;
	inValue = eval(document.frm['in'].value);
	ftValue = eval(document.frm['ft'].value);
	//Check for NaN
	if(isNaN(inValue)) inValue = 0;
	if(isNaN(ftValue)) ftValue = 0;
	mmValue = ((ftValue * 12) + inValue) * 25.4;
	//round 2 dp
	mmValue=Math.round(mmValue*10)/10;
	//Check for NaN
	if(isNaN(mmValue)) mmValue = 0;
	document.frm.mm.value=mmValue;
	return false //matt return false stops the usual action from running
});

$("input.calc2").click(function(){
	var ftValue = 0;
	var inValue = 0;
	var mmValue = 0;
	mmValue = eval(document.frm['mm_1'].value);
	inValue = mmValue * 0.0394;
	ftValue=inValue/12;
	var tmpft = new String();
	tmpft = ftValue.toString();
	var temp = new Array();
	temp = tmpft.split('.');
	var tmpIn = new String();
	ftValue=temp[0];
	tmpIn="0." + temp[1];
	tmpIn = tmpIn * 1;
	inValue = 12 * tmpIn;
	//round 2 dp
	inValue=Math.round(inValue*10)/10;
	//Check for NaN
	if(isNaN(ftValue)) ftValue = 0;
	if(isNaN(inValue)) inValue = 0;
	document.frm.ft_1.value=ftValue;
	document.frm.in_1.value=inValue;
	return false //matt return false stops the usual action from running
});


$('.gallerybox').click(function(){
 	$.gallerybox('<iframe src="'+$(this).attr('href')+'" width="532" height="570" frameborder="0" scrolling="no"></iframe>');
	return false;
});




/*
function gallerybox(href){
	$.get(href, function(data) {
		$("#gallerybox").remove();
		$("#container").prepend('\
			<div id="gallerybox" style=""> \
				<div class="popup"> \
					<table> \
						<tbody> \
							<tr> \
								<td class="tl"/><td class="b"/><td class="tr"/> \
							</tr> \
							<tr> \
								<td class="b"/> \
								<td class="body"> \
									<div class="inner-content"> \
									</div> \
									<div class="footer"> \
										<a href="#" class="close"> \
											<img src="/images/gallerybox-close.png" title="close" class="close_image" /> \
										</a> \
									</div> \
								</td> \
								<td class="b"/> \
							</tr> \
							<tr> \
								<td class="bl"/><td class="b"/><td class="br"/> \
							</tr> \
						</tbody> \
					</table> \
				</div> \
			</div>');

			$('#gallerybox .inner-content').html(data);
			$('#gallerybox').css('left', (467 - ($('#gallerybox table').width()) / 2));
			$('#gallerybox').css('top', (298 - ($('#gallerybox table').height()) / 2));
			$("#gallerybox .close").unbind('click');
			$('#gallerybox .close').click(function() {
				$("#gallerybox").remove();
				return false;
			});
			$(document).bind('keydown.gallerybox', function(e) {
				if (e.keyCode == 27) $("#gallerybox").remove();
				return true
			});
		});
}	*/



//Configurator.install(".run-configurator-galaxy", "galaxy");
//Configurator.install(".run-configurator-minima", "minima");
//Configurator.install(".run-configurator-saturn", "saturn");
//Configurator.install(".run-configurator-titan", "titan");
//Configurator.install(".run-configurator-orion", "orion");
//Configurator.install(".run-configurator-landing", "landing");
//Configurator2.install(".run-configurator-dressing", "dressing");
//Configurator2.install(".run-configurator-drawerunits", "drawerunits");
//Configurator2.install(".run-configurator-lunar", "lunar");

transitions.construct();

});

/**
 * @author Alexander Farkas
 * v. 1.21
 */


(function($) {
	if(!document.defaultView || !document.defaultView.getComputedStyle){ // IE6-IE8
		var oldCurCSS = jQuery.curCSS;
		jQuery.curCSS = function(elem, name, force){
			if(name === 'background-position'){
				name = 'backgroundPosition';
			}
			if(name !== 'backgroundPosition' || !elem.currentStyle || elem.currentStyle[ name ]){
				return oldCurCSS.apply(this, arguments);
			}
			var style = elem.style;
			if ( !force && style && style[ name ] ){
				return style[ name ];
			}
			return oldCurCSS(elem, 'backgroundPositionX', force) +' '+ oldCurCSS(elem, 'backgroundPositionY', force);
		};
	}

	var oldAnim = $.fn.animate;
	$.fn.animate = function(prop){
		if('background-position' in prop){
			prop.backgroundPosition = prop['background-position'];
			delete prop['background-position'];
		}
		if('backgroundPosition' in prop){
			prop.backgroundPosition = '('+ prop.backgroundPosition;
		}
		return oldAnim.apply(this, arguments);
	};

	function toArray(strg){
		strg = strg.replace(/left|top/g,'0px');
		strg = strg.replace(/right|bottom/g,'100%');
		strg = strg.replace(/([0-9\.]+)(\s|\)|$)/g,"$1px$2");
		var res = strg.match(/(-?[0-9\.]+)(px|\%|em|pt)\s(-?[0-9\.]+)(px|\%|em|pt)/);
		return [parseFloat(res[1],10),res[2],parseFloat(res[3],10),res[4]];
	}

	$.fx.step. backgroundPosition = function(fx) {
		if (!fx.bgPosReady) {
			var start = $.curCSS(fx.elem,'backgroundPosition');

			if(!start){//FF2 no inline-style fallback
				start = '0px 0px';
			}

			start = toArray(start);

			fx.start = [start[0],start[2]];

			var end = toArray(fx.options.curAnim.backgroundPosition);
			fx.end = [end[0],end[2]];

			fx.unit = [end[1],end[3]];
			fx.bgPosReady = true;
		}
		//return;
		var nowPosX = [];
		nowPosX[0] = ((fx.end[0] - fx.start[0]) * fx.pos) + fx.start[0] + fx.unit[0];
		nowPosX[1] = ((fx.end[1] - fx.start[1]) * fx.pos) + fx.start[1] + fx.unit[1];
		fx.elem.style.backgroundPosition = nowPosX[0]+' '+nowPosX[1];

	};
})(jQuery);
