var utils = {
    baseHref : '',
    ieVersion : -1,
    init : function() {

	this.getIEVersion();
	this.getBaseHref();

    },
    getIEVersion : function () {
	var rv = -1;
	if (navigator.appName === 'Microsoft Internet Explorer') {
	    var ua = navigator.userAgent;
	    var re  = new RegExp("MSIE ([0-9]{1,}[\\.0-9]{0,})");
	    if (re.exec(ua) !== null) {
		rv = parseFloat( RegExp.$1 );
	    }
	}
	this.ieVersion = rv;
    },
    popupWindow : function (url, name, attributes) {
	// Append popup to the URL if it does not already exist
	var queryStringExists = false;
	if(url.match(/\?/)) {
	    queryStringExists = true;
	}

	if(queryStringExists) {
	    // Only append if we haven't already got the parameter popup defined
	    if(!url.match(/(\?|&)popup=/)) {
		url = utils.baseHref + url + '&popup=true';
	    }
	} else {
	    url =  utils.baseHref + url + '?popup=true';
	}

	var newWindow = window.open(url, name.replace(/[^A-z]/ig, ''), attributes);
	newWindow.focus();
    },
    getBaseHref : function () {
	var baseTags = document.getElementsByTagName('base');
	if(baseTags !== undefined && baseTags.length > 0) {
	    this.baseHref =  baseTags[0].href;
	}
    }
};

ahLanding = {
    keyAnimateOpts : {
	duration: 200,
	easing: 'swing',
	queue: false
    },
    init: function () {
	$logo = $('#logo a');
	$fg = $('#fg-elements');
	$ivyTop = $('#ivy-top');
	$ivyMid = $('#ivy-mid');
	$ivyBot = $('#ivy-bottom');
	$linksWrapper = $('#go');
	$discoverLink = $('#discover');
	$shopLink = $('#shop-online');
	$keys = $('#keys');
	$discKey = $('#disc-key');
	$shopKey = $('#shop-key');
	if(utils.ieVersion == -1) {
	    $('#logo a, #fg-elements, #ivy-top, #ivy-mid, #ivy-bottom, #go, #disc-key, #shop-key').css({opacity: 0});
	    $('#container').css({visibility: 'visible'});
	    $keys.css({top: -800});
	    setTimeout(function () {
		ahLanding.showLogo();
	    }, 1500);
	} else {
	    $('#disc-key, #shop-key').css({visibility: 'hidden'});
	    $('#container').css({visibility: 'visible'});
	    ahLanding.enableLinksIE();
	}
    },
    showLogo : function () {
	$logo.animate({opacity: 1}, {
	    duration: 1000,
	    easing: 'swing',
	    queue: false,
	    complete: function () {
		ahLanding.showElements();
	    }
	});
    },
    showElements : function() {
	$keys.animate({top: 0}, {
	    duration: 1000,
	    easing: 'easeOutExpo',
	    queue: false
	});
	setTimeout(function() {
	    $ivyTop.animate({opacity : 1}, {
		duration: 1000,
		easing: 'swing',
		queue: false
	    });
	}, 500);
	setTimeout(function() {
	    $ivyMid.animate({opacity : 1}, {
		duration: 1000,
		easing: 'swing',
		queue: false
	    });
	}, 1000);
	setTimeout(function() {
	    $ivyBot.animate({opacity : 1}, {
		duration: 1000,
		easing: 'swing',
		queue: false
	    });
	}, 1500);
	setTimeout(function() {
	    $linksWrapper.animate({opacity : 1}, {
		duration: 1000,
		easing: 'swing',
		queue: false,
		complete : function () {
		    ahLanding.enableLinks();
		}
	    });
	}, 2000);
	setTimeout(function() {
	    $fg.animate({opacity : 1}, {
		duration: 2000,
		easing: 'swing',
		queue: false,
		complete : function () {
		    ahLanding.enableLinks();
		}
	    });
	}, 2000);
    },
    enableLinks : function () {
	$('#go a').removeAttr('onclick');
	$shopLink.mouseenter(function() {
	   $shopKey.stop();
	   $shopKey.animate({opacity : 1}, ahLanding.keyAnimateOpts);
	}).mouseout(function() {
	    $shopKey.stop();
	    $shopKey.animate({opacity : 0}, ahLanding.keyAnimateOpts);
	});
	$discoverLink.mouseenter(function() {
	   $discKey.stop();
	   $discKey.animate({opacity : 1}, ahLanding.keyAnimateOpts);
	}).mouseout(function() {
	    $discKey.stop();
	    $discKey.animate({opacity : 0}, ahLanding.keyAnimateOpts);
	});
    },
    enableLinksIE : function () {
	$('#go a').removeAttr('onclick');
	$shopLink.mouseenter(function() {
	   $shopKey.css({visibility: 'visible'});
	}).mouseout(function() {
	   $shopKey.css({visibility: 'hidden'});
	});
	$discoverLink.mouseenter(function() {
	   $discKey.css({visibility: 'visible'});
	}).mouseout(function() {
	    $discKey.css({visibility: 'hidden'});
	});
    }

}

$(document).ready(function() {
    utils.init();
    ahLanding.init();
});


/*

#logo a { display: block; margin: 0px auto; width: 971px; height: 305px;  background-image: url(../images/ah-logo-1.png); background-position: 0px 0px; background-repeat: no-repeat; text-indent: -9999px; z-index: 4; position: relative; }
#fg-elements { display: block; width: 273px; height: 198px;  background-image: url(../images/fg-elements.png); background-position: 0px 0px; background-repeat: no-repeat; text-indent: -9999px; position: absolute; top: 64px; left: 283px; z-index: 10;  }
#ivy-top { display: block; width: 120px; height: 157px;  background-image: url(../images/top-ivy.png); background-position: 0px 0px; background-repeat: no-repeat; text-indent: -9999px; position: absolute; top: 0px; left: 342px; z-index: 3;  }
#ivy-mid { display: block; width: 662px; height: 325px;  background-image: url(../images/bg-elements.png); background-position: 0px 0px; background-repeat: no-repeat; text-indent: -9999px; position: absolute; top: 33px; left: 119px; z-index: 1;  }
#ivy-bottom { display: block; width: 288px; height: 201px;  background-image: url(../images/bot-flowers.png); background-position: 0px 0px; background-repeat: no-repeat; text-indent: -9999px; position: absolute; bottom: 0px; left: 333px; z-index: 1;  }
#discover { display: block; width: 88px; height: 57px;  background-image: url(../images/secret-garden.png); background-position: 0px 0px; background-repeat: no-repeat; text-indent: -9999px; position: absolute; top: 0px; left: 0px; z-index: 1;  }
#shop-online { display: block; width: 81px; height: 57px;  background-image: url(../images/shop-online.png); background-position: 0px 0px; background-repeat: no-repeat; text-indent: -9999px; position: absolute; top: 0px; right: 0px; z-index: 1;  }
#keys 

 * */


jQuery.easing.jswing = jQuery.easing.swing;

jQuery.extend( jQuery.easing,
{
	def: 'easeOutQuad',
	swing: function (x, t, b, c, d) {
		//alert(jQuery.easing.default);
		return jQuery.easing[jQuery.easing.def](x, t, b, c, d);
	},
	easeInQuad: function (x, t, b, c, d) {
		return c*(t/=d)*t + b;
	},
	easeOutQuad: function (x, t, b, c, d) {
		return -c *(t/=d)*(t-2) + b;
	},
	easeInOutQuad: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) {
		    return c/2*t*t + b;
		} else {
		    return -c/2 * ((--t)*(t-2) - 1) + b;
		}
	},
	easeInQuart: function (x, t, b, c, d) {
		return c*(t/=d)*t*t*t + b;
	},
	easeOutQuart: function (x, t, b, c, d) {
		return -c * ((t=t/d-1)*t*t*t - 1) + b;
	},
	easeInOutQuart: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) {
		    return c/2*t*t*t*t + b;
		} else {
		    return -c/2 * ((t-=2)*t*t*t - 2) + b;
		}
	},
	easeInExpo: function (x, t, b, c, d) {
		return (t===0) ? b : c * Math.pow(2, 10 * (t/d - 1)) + b;
	},
	easeOutExpo: function (x, t, b, c, d) {
		return (t===d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;
	},
	easeInOutExpo: function (x, t, b, c, d) {
		if (t===0) { return b; } else
		if (t===d) { return b+c; } else
		if ((t/=d/2) < 1) { return c/2 * Math.pow(2, 10 * (t - 1)) + b; } else {
		return c/2 * (-Math.pow(2, -10 * --t) + 2) + b; }
	}
});
