var resizeTimer = null; 
var win_height = null;
var playerpath = '/layout/flash/main.swf',
favoured = 'flash'; //'flash','html5'
$(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)
	}*/
	
	$('.embed_video').each(function(){
		media_embed($(this));
	})

	
	//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

	hideCanvas();
	
	//if( navigator.mimeTypes ["application/x-shockwave-flash"] == undefined ){
		var bgimg = $('#flashcontent img');
		if($('#flashcontent img').length<0) bgimg = $('#flashcontenth img');
		
		if( $('#flashcontent img').length > 0 || $('#flashcontenth img').length > 0 ){
		
		bgimg.addClass('jsenabled');
		var options = {startwidth: bgimg.width(), startheight: bgimg.height(), minsize: .5}
		//bgimg.bgfullsize(options);

		bgimg.load(function(){ 
		options = {startwidth: bgimg.width(), startheight: bgimg.height(), minsize: .5}
		bgimg.bgfullsize(options);
		$(window).bind("resize", function(){
			bgimg.bgfullsize(options); 
		});		
		
		})

		
		}
	//}
	
});
function hideCanvas(){
	var open = true;
	$('body').append('<a class="clearfix" id="close_btn" href="#"><span>Hide banner</span><img width="21" height="21" alt="hide panel" src="/layout/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:#e2ded3");
			$('a#close_btn').attr("style","background-color:#e2ded3");
		},function(){
			$('a#close_btn span').hide();
			$('a#close_btn img').attr("style","background-color:#ffffff");
			$('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 banner")
			open = false;
		}else{
			if($('#canvash').text() != null && $('#canvash').text() != ""){
				$('#canvash').show();
			}else{
				$('#canvas').show();
			}
			$('a#close_btn span').text("Hide banner")
			open = true;
		}
		return;
	});
}
function loadHpVideo(){
	var so = new SWFObject("/layout/flash/home.swf", 'home', '100%', '100%', '9', '#e2ded3');
	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");
}
function hideHome(){
	$('div#canvas').css("visibility","hidden");
}
function showHome(){
	$('div#canvas').css("visibility","visible");
}

function resizeBgImg() {

	if(console) console.log('resize1');
	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;
	}
}

/* background image resizer */
$.fn.bgfullsize = function(options){
	var minsize = options.minsize,
		imagewidth = $(this).width(),
		imageheight = $(this).height(),
		minwidth = minsize*(options.startwidth),
		minheight = minsize*(options.startheight),
		ratio = options.startheight/options.startwidth,
		browserwidth = $(window).width(),
		browserheight = $(window).height();
	

	//minimum dimensions
	if ((browserheight < minheight) && (browserwidth < minwidth)){
		$(this).height(minheight);
		$(this).width(minwidth);
	}else{	
		// Browsers higher
		if (browserheight > browserwidth){
			imageheight = browserheight;
			$(this).height(browserheight);
			imagewidth = browserheight/ratio;
			$(this).width(imagewidth);
			
			if (browserwidth > imagewidth){
				imagewidth = browserwidth;
				$(this).width(browserwidth);
				imageheight = browserwidth * ratio;
				$(this).height(imageheight);
			}
		}
		
		//Browsers wider
		if (browserwidth >= browserheight){
			imagewidth = browserwidth;
			$(this).width(browserwidth);
			imageheight = browserwidth * ratio;
			$(this).height(imageheight);
			
			if (browserheight > imageheight){
				imageheight = browserheight;
				$(this).height(browserheight);
				imagewidth = browserheight/ratio;
				$(this).width(imagewidth);
			}
		}
	}

}

function media_embed(vidObj){

	var replacementId = vidObj.parent().attr('id'),
		video_src = vidObj.attr('href'),
		v_epart = video_src.split('/').pop(),
		format = v_epart.split('.')[1],
		still = vidObj.parent().find('img').attr('src'),
		relprts = vidObj.attr('rel').split(','),
		width = relprts[0],
		height = relprts[1],
		disallowed_formats = ['flv'],
		block = false,
		hasFlash = false;
		
		
		if($.browser.mozilla){
			disallowed_formats = disallowed_formats.concat(['m4v','mov','mp4','mpg','mpeg','3gp']);
		}
		if($.browser.webkit){
			disallowed_formats = disallowed_formats.concat(['ogg']);
		}
		if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPad/i))){
			var iDevice = true;
			disallowed_formats = disallowed_formats.concat(['mov','mp4']);
		}
		
		for(i=0;i<disallowed_formats.length;i++){
			if(disallowed_formats[i]==format){
				block=true;
				break;
			}
		}

			
	function _chkFlash(){
	
		try {
			var fo = new ActiveXObject('ShockwaveFlash.ShockwaveFlash');
			if(fo) hasFlash = true;
		}catch(e){
			if(navigator.mimeTypes ["application/x-shockwave-flash"] != undefined) hasFlash = true;
		}
		
	}
	
	function _embedflash(){

	  /*var params = { allowfullscreen: "true", allowscriptaccess: "always" };
	  var flashvars = { file: video_src, image: still };
	  swfobject.embedSWF(playerpath, replacementId, width, height, "9.0.0", "expressInstall.swf", flashvars, params);
	  */
	  
	  // old SWFObject
		var video = new SWFObject(playerpath, 'main', '529', height, '9', '#e2ded3');
		video.addParam("wmode", "transparent");
		video.addParam("scale", "noscale");
		video.addParam("salign", "tl");
		video.addVariable("videourl", video_src);
		//video.addVariable("w", "529");
		video.addVariable("h", height);
		video.write(replacementId);
		
		
	  
	}
	
	function _embedhtml5(){

		var myVideo = '<video style="width:'+width+'px;height:'+height+'px"';
		if(still != '') myVideo += ' poster="'+still+'"';
		myVideo += ' controls="controls"><source  src="'+ video_src +'" /></video>';
		$('#'+replacementId).html('');
		$('#'+replacementId).append(myVideo);	
	}

	_chkFlash();
	var vid = document.createElement("video");
	
	if(favoured=='flash'){
		if( hasFlash ){
			_embedflash();
		}else if ( vid.play && block === false ){
			// html5 video
			_embedhtml5();
		}
	}else{
		
		if ( vid.play && block === false ) {
			_embedhtml5();
		}else if( hasFlash ){
			_embedflash();
		}
	}
	
	if(block && iDevice){
		$('#'+replacementId).append('<span>Video format is not supported on this device ('+format+')</span>');
	}
}