var resizeTimer = null; 
var win_height = null;
$(document).ready(function(){
	
	// do scrolling on tour page
	if ($('tr.highlighted').length > 0) {
		destination = $('tr.highlighted').eq(0).offset().top;
		$('html,body').animate({ scrollTop: destination}, 1100 );
	}
	
	

	//check and replace title image

	var line1 = $('#title h1').text();
	var line2 = $('#title h2').text();
	if(line1 != null && line1 != ""){
		$('#title').before('<div class="print"><h1>'+line1+'</h1><h2>'+line2+'</h2></div>');
		loadText(line1,line2)
	}

	//check and replace smalltitle
	var counter = 0;
	$('div.smalltitle').each(function(e){
		if ($(this).children().is("h1")) {
			var headline = $(this).children().text();
			$(this).before('<div class="print"><h1>'+headline+'</h1></div>');
			loadTextSingle(headline,"blue",99);
		}else if ($(this).children().is("h2")) {
			var headline = $(this).children().text();	
			$(this).before('<div class="print"><h2>'+headline+'</h2></div>');
			loadTextSingle(headline,"black",counter-1);
		}
		counter++;
	});


	//check and replace video
	var video = $('div#video span.video').text();
	var height = $('div#video span.height').text();
	if(video != null && video != ""){
		loadVideo(video,height)
	}
	
	
	//check and replace home video
	if($('div#flashcontenth span.video').text() != null && $('div#flashcontenth span.video').text() != ""){
		loadHpVideo();
		//hideNews();
		//console.log($('div#flashcontenth span.video').text());
	}
	
	
	//
	if(!($.browser.safari && parseInt($.browser.version) < 500)){
		if($('div.gallery').length != 0){
			Shadowbox.init();
			iStripInit();
		}
	}
	win_height = $(document).height();
	//check and replace background image
	if(!($.browser.mozilla && $.browser.version.substr(0,3)=="1.8")) {
		var bg_img = $('#flashcontent span').text();

		if(bg_img != null && bg_img != ""){
			resizeBgImg();
			$(window).bind('resize', function() { 
				if (resizeTimer) clearTimeout(resizeTimer); 
					resizeTimer = setTimeout(resizeBgImg, 100); 
		
			}); 

		}
	}	
	
	 hideCanvas();
	
	
});
function hideCanvas(){
	var open = true;
	$('body').append('<a class="clearfix" id="close_btn" href="#"><span>Hide text</span><img width="21" height="21" alt="Hide text" src="img/close_btn.gif"/></a>');
	$('a#close_btn span').hide();
	
	
	$('#close_btn').hover(function(){
			$('a#close_btn span').show();
			$('a#close_btn img').attr("style","background-color:#f6f6f4");
			$('a#close_btn').attr("style","background-color:#f6f6f4");
		},function(){
			$('a#close_btn span').hide();
			$('a#close_btn img').attr("style","background-color:#f6f6f4");
			$('a#close_btn').attr("style","background-color:transparent");
	});
	
	$('#close_btn').click(function(me){
		me.preventDefault();
		if(open){
			if($('#canvash').text() != null && $('#canvash').text() != ""){
				$('#canvash').hide();
			}else{
				$('#canvas').hide();
			}
			$('a#close_btn span').text("Show text")
			open = false;
		}else{
			if($('#canvash').text() != null && $('#canvash').text() != ""){
				$('#canvash').show();
			}else{
				$('#canvas').show();
			}
			$('a#close_btn span').text("Hide text")
			open = true;
		}
		return;
	});
}
function loadHpVideo(){
	
	/* var so = new SWFObject("home_coming_soon.swf", 'home', '100%', '100%', '9', '#e2ded3'); */
	var so = new SWFObject("home_def.swf", 'home', '100%', '100%', '9', '#e2ded3');
	so.addParam("allowFullScreen", "true");
	var flv = $('div#flashcontenth span.video').text();
	var w = $('div#flashcontenth span.height').text();
	var h = $('div#flashcontenth span.width').text();
	var n = $('div#flashcontenth span.name').text();
	var u = $('div#flashcontenth span.url').text();
	so.addParam("quality", "high");
	so.addParam("wmode", "transparent");
	so.addParam("scale", "noscale");
	so.addParam("salign", "tl");
	so.addVariable("videourl",flv);
	so.addVariable("w",w);
	so.addVariable("h",h);
	so.addVariable("vname",n);
	so.addVariable("link",u);
	so.write("flashcontenth");
	$('div#flashcontent').css("visibility","visible");
	

}
function hideHome(){
	$('div#canvas').css("visibility","hidden");
}
function showHome(){
	$('div#canvas').css("visibility","visible");
	
}

function resizeBgImg() {

	
	var win_width = $(window).width();
	//var win_height = $('#content').height() + $('#header').height() + 20;
	
	var i_width = $('#flashcontent img').width();
	var i_height = $('#flashcontent img').height();
	var hor_ratio = win_width / i_width;
	var ver_ratio = win_height / i_height;
	var aspect_ratiowh = i_width / i_height;
	var aspect_ratiohw = i_height / i_width;
	var scale = win_height * aspect_ratiowh;
	
	if (scale >= win_width) {
		new_height = win_height;
		new_width = scale;
	} else {
		scale = win_width * aspect_ratiohw;
		new_height = scale;
		new_width = win_width;
	}
	
	var isIE6 = /msie|MSIE 6/.test(navigator.userAgent);
	if (isIE6){
		$('#flashcontent img').css({height: new_height+"px", width: new_width+"px"});
		$('#flashcontent').css({height: win_height+"px", width: win_width+"px"});
	} else {
		$('#flashcontent img').css({height: new_height, width: new_width});
		$('#flashcontent').css({height: win_height, width: win_width});
	}
	
	$('#flashcontent').css({visibility:"visible", display:"block", overflow:"hidden"});

}
function loadTextSingle(line1,color,c){
	if(color == "blue"){
		var txt = new SWFObject("/layout/flash/text_h1.swf", 'text_h1', '730', '27', '8', '#FFFFFF');
	}else{
		var txt = new SWFObject("/layout/flash/text_h2.swf", 'text_h2', '730', '27', '8', '#FFFFFF');
	}
	txt.addParam("quality", "high");
	txt.addParam("scale", "noscale");
	txt.addParam("salign", "tl");
	txt.addVariable("line1",line1);
	txt.write("s"+c);
}
function loadText(line1,line2){	
	var txt = new SWFObject("/layout/flash/text.swf", 'text', '730', '55', '8', '#FFFFFF');
	txt.addParam("quality", "high");
	txt.addParam("scale", "noscale");
	txt.addParam("salign", "tl");
	txt.addVariable("line1",line1);
	txt.addVariable("line2",line2);
	txt.write("title");
}
function loadVideo(flv,h){	
	var video = new SWFObject("/layout/flash/main.swf", 'main', '529', h, '9', '#e2ded3');
	video.addParam("wmode", "transparent");
	video.addParam("scale", "noscale");
	video.addParam("salign", "tl");
	video.addVariable("videourl", flv);
	//video.addVariable("w", "529");
	video.addVariable("h", h);
	video.write("video");
}
function iStripInit() {
	$('div.slider').each(function(){
		new iStrip(this);
	});
}
function iStrip(container) {
	this.me = $(container);
	this.wrap = $($('div.gallery'));
	this.arrows = $($('div.arrows'));
	this.slngth = this.wrap.width();
	//console.log(this.slngth);
	this.imgs = new Array();
	this.gap = null;
	this.lngth = 0;
	this.cpos = 0;
	this.max = null;
	this.maxp = null;
	this.btn = new Array();
	this.act = [true,true];
	this.init();
	this.easing = 'easeInOutExpo';
	this.time = 650;
	return this;
}
iStrip.prototype.init = function() {
	var sobj = this;
	this.me.find('a img').each(function(){
		sobj.addImg($($(this)));	
	});
	this.me.css('width',this.lngth);
	this.max = -((this.lngth-6) - this.slngth);
	this.maxp = 0;
	var l = this.imgs.length;
	for(this.maxp=0;this.maxp<l;this.maxp++) {
		if (this.max >= this.imgs[this.maxp][2]) {
			break;
		}
	}

	if (this.lngth > this.slngth) {
		$(this.arrows.append('<a class="prev" href="#">&nbsp;</a>'));
		$(this.arrows.append('<a class="next" href="#">&nbsp;</a>'));
		this.btn[0] = $(this.arrows.find('a.prev'));
		this.btn[1] = $(this.arrows.find('a.next'));
		this.btn[0].click(function(me){me.preventDefault();sobj.move(-1);return false;});
		this.btn[1].click(function(me){me.preventDefault();sobj.move(1);return false;});
		this.ckB();
	} 
}
iStrip.prototype.addImg = function(elm) {
	//console.log(elm);
	var w = 175;
	//console.log(w);
	var p = parseInt(elm.css('margin-right'));
	//console.log(p);
	w += p;
	//console.log(w);
	if (this.gap == null) {
		this.gap = parseInt(elm.css('margin-right'));
		//alert('adding element '+this.gap);
	}
	this.imgs.push([elm,-w,-this.lngth]);
	this.lngth +=  w ;//+ this.gap;
}
	
iStrip.prototype.getW = function() {
	return this.lngth+this.gap;
}
iStrip.prototype.move = function(dir) {
	//alert('move');
	if (dir == 1) {
		if (this.cpos < this.maxp && this.act[1]) {
			this.cpos++;
			var npos = this.imgs[this.cpos][2];
			if (-(npos) > (this.lngth-this.gap-this.slngth)) {
				npos = -(this.lngth-this.gap-this.slngth);	
			}
			this.me.animate({marginLeft:npos},this.time,this.easing);
		}
	} else {
		if (this.cpos > 0 && this.act[0]) {
			this.cpos--;
			var npos = this.imgs[this.cpos][2];
			this.me.animate({marginLeft:npos},this.time,this.easing);
		}
	}	
	this.ckB();
}
iStrip.prototype.ckB = function() {
	if (this.cpos == 0) {
		this.btn[0].addClass('notactive');
		this.act[0] = false;
		this.act[1] = true;
		this.btn[1].removeClass('notactive');
	} else if (this.cpos == this.maxp) {
		this.btn[1].addClass('notactive');
		this.act[1] = false;
		this.act[0] = true;
		this.btn[0].removeClass('notactive');
	} else {
		this.btn[0].removeClass('notactive');
		this.btn[1].removeClass('notactive');
		this.act[0] = true;
		this.act[1] = true;
	}
}