/* Created by Martin Hintzmann 2008 martin [a] hintzmann.dk
 * MIT (http://www.opensource.org/licenses/mit-license.php) licensed.
 *
 * Version: 0.2
 * Requires: jQuery 1.2+
 * http://plugins.jquery.com/project/textshadow
 *
 */
(function($) {
	$.fn.textShadow = function(option) {
		if (!$.browser.msie) return;
		var IE6 = $.browser.version < 7;
		return this.each(function() {
			var el = $(this);
			var shadow = el.textShadowParse(this.currentStyle["text-shadow"]);
			shadow = $.extend(shadow, option);

			el.textShadowRemove();

			if (shadow.x == 0 && shadow.y == 0 && shadow.radius == 0) return;

			if (el.css("position")=="static") {
				el.css({position:"relative"});
			}
			el.css({zIndex:"0"});
			if (IE6) {
				el.css({zoom:"1"});
			}
			
			var span=document.createElement("span");
			$(span).addClass("jQueryTextShadow");
			$(span).html(el.html());
			$(span).css({
				padding:		this.currentStyle["padding"],	
				width:		el.width(),
				position:	"absolute",
				zIndex:		"-1",
				color:		shadow.color!=null?shadow.color:el.css("color"),
				left:			(-parseInt(shadow.radius)+parseInt(shadow.x))+"px",
				top:			(-parseInt(shadow.radius)+parseInt(shadow.y))+"px"
			});
			
			if (shadow.radius != 0) {
				if (shadow.opacity != null) {
					$(span).css("filter", "progid:DXImageTransform.Microsoft.Blur(pixelradius="+parseInt(shadow.radius)+", enabled='true', makeShadow='true', ShadowOpacity="+shadow.opacity+")");
				} else {
					$(span).css("filter", "progid:DXImageTransform.Microsoft.Blur(pixelradius="+parseInt(shadow.radius)+", enabled='true')");
				}
			}	
			el.append(span);
		
	  });
	};
	
	$.fn.textShadowParse = function(value) 
	{
		value = String(value)
			.replace(/^\s+|\s+$/gi, '')
			.replace(/\s*!\s*important/i, '')
			.replace(/\(\s*([^,\)]+)\s*,\s*([^,\)]+)\s*,\s*([^,\)]+)\s*,\s*([^\)]+)\s*\)/g, '($1/$2/$3/$4)')
			.replace(/\(\s*([^,\)]+)\s*,\s*([^,\)]+)\s*,\s*([^\)]+)\s*\)/g, '($1/$2/$3)')
	
		var shadow = {
			x      : 0,
			y      : 0,
			radius : 0,
			color  : null
		};

		if (value.length > 1 || value[0].toLowerCase() != 'none') {
			value = value.replace(/\//g, ',');
			var color;
			if ( value.match(/(\#[0-9a-f]{6}|\#[0-9a-f]{3}|(rgb|hsb)a?\([^\)]*\)|\b[a-z]+\b)/i) && (color = RegExp.$1) ) {
				shadow.color = color.replace(/^\s+/, '');
				value = value.replace(shadow.color, '');
			}

			value = value
				.replace(/^\s+|\s+$/g, '')
				.split(/\s+/)
				.map(function(item) {
						return (item || '').replace(/^0[a-z]*$/, '') ? item : 0 ;
					});

			switch (value.length)
			{
				case 1:
					shadow.x = shadow.y = value[0];
					break;
				case 2:
					shadow.x = value[0];
					shadow.y = value[1];
					break;
				case 3:
					shadow.x = value[0];
					shadow.y = value[1];
					shadow.radius = value[2];
					break;
			}
			if ((!shadow.x && !shadow.y && !shadow.radius) || shadow.color == 'transparent') {
				shadow.x = shadow.y = shadow.radius = 0;
				shadow.color = null;
			}
		}

		return shadow;
	};

	$.fn.textShadowRemove = function() {
		if (!$.browser.msie) return;
		return this.each(function() {
			$(this).children("span.jQueryTextShadow").remove();
		});
	};
})(jQuery);




// Indicates if prority animation is running
var busy = false;
	
function switchBgImages() {
	if (busy) {
		// Next try after 1600 ms
		setTimeout(switchBgImages, 4000);
		return;
	}

	//$('.bgImage').hide();
	var curr = $('.bgImage:last');
	var next = $('.bgImage:first');
	next.insertAfter(curr);
	
	next.fadeIn(1000, function(){ curr.hide(); });
	
	setTimeout(switchBgImages, 6000);		
}

function updateBgPos() {
	
	if ($('.bgImage').width() > $(window).width()) {
	
		var offset = ($(window).width() - $('.bgImage').width()) / 2;
		if (offset < -150) offset = -150;
		$('.bgImage').css("left", offset);
	} else {
		if (!$.browser.msie) {
			$('.bgImage').css("left", 0);
			$('.bgImage').css("width", "100%");
			$('.bgImage').css("height", "auto");
		} else {
			$('.bgImage').css("left", 0);
		}
	}
}

$(function(){
	function all_null(){
		$('#content article').css({left:'-1100px'});
		$('.close').css({right:'177px'})
		$('#menu').css({left:'-1000px'})
		$('#icon li a').css({height:'0px', width:'0px'});
		$('#icon li a img').css({top:'35px', left:'35px'});
	}
	function content_null(){
		$('#content article').css({left:'-1100px'});
		$('.close').css({right:'177px'})
	}
	
	all_null();
	var act;
	$('a').click(function(){
		page=$(this).attr('href');
		if (page.substr(page.indexOf('.'),5)=='.html') { return true}
		if (page.substr(page.indexOf('#'),6)=='#page_') {
			if (page=='#page_0') {
				$(act).find('.close').unbind('mouseenter mouseleave').stop().animate({right:'177'},400,'easeOutBack',function(){
					$('#icon li a').animate({height:'0px', width:'0px'},400,'easeInBack').find('img').animate({top:'35px', left:'35px'},400,'easeInBack');
					$('#menu').stop().animate({left:'-1000'}, 400,'easeOutCirc');
					$(act).stop().animate({left:'-1100'}, 400,'easeOutCirc',function(){
						$('.enter_button').stop().animate({right:'18%'}, 400,'easeOutCirc');
						act=undefined;
						closeBind();
						all_null();
						$('#content').animate({height:'0'}, 400,'easeOutCirc' , function(){$('#content article').css({height:'0px'});});
					});	
					});
			} else {
	
			// Tour iframe opt.
			
			if (page == '#page_4_1') {
				jQuery(page).find('iframe').hide().show();
			}
			
			if (act!=page) {
				menuBind();
				menuUlBind();
				$('#menu li').removeClass('active').find('a').mouseleave();
			if (act) {
				busy = true;	
				
				$(this).parent().addClass('active').find('a').mouseenter().unbind('mouseenter mouseleave').parent().find('ul').unbind('mouseenter mouseleave');
				$(act).find('.close').stop().animate({right:'177'},400,'easeOutBack',function(){
					$(act).stop().animate({left:'-1100'}, 400,'easeOutCirc',function(){
						content_null();
						$(page).stop().animate({left:'0'}, 400,'easeOutCirc',function(){
							$(page).find('.close').stop().animate({right:'92'}, 400,'easeOutBack');
							act=page;
							
							busy = false;	
						});
					});
				})
			} else {
				all_null();
					
				busy = true;
				$('#content').animate({height:'638'}, 400,'easeOutCirc',function(){ 	
					$('#content article').css({height:'638px'});
					$('.enter_button').stop().animate({right:'-300px'}, 400,'easeOutCirc');
					if (page=='#page_6') {} else {$('#menu > li').eq(0).addClass('active').find('a').mouseenter().unbind('mouseenter mouseleave');}
					$('#menu').stop().animate({left:'0'}, 400,'easeOutCirc');
					$(page).stop().animate({left:'0'}, 400,'easeOutCirc',function(){
						$(page).find('.close').stop().animate({right:'92'}, 400,'easeOutBack');
						$('#icon li a').animate({height:'70px', width:'70px'},400,'easeOutBack').find('img').animate({top:'0px', left:'0px'},400,'easeOutBack');
						act=page;
						busy = false;
						
					});
				})
			}
			}}
				return false;
			} else {return true}
	});
	
	jQuery(window).resize(updateBgPos);
	
	setTimeout(switchBgImages, 6000);
	
	$("a[rel^='prettyPhoto']").prettyPhoto();
	
	$("h1, h2").textShadow();
	
	setTimeout(updateBgPos, 5);
	
	
});
