﻿//standard code for cms?
info.sortHeight = function () {
	var content = $("#container");
	if (content.length) {
		content.height("auto");
		if (content.height() < info.height) {
			content.height(info.height);
		}
	}
};


// uitzonderingen: 
function isiPad(){
    return (navigator.platform.indexOf("iPad") != -1);
}

function isAndroid(){
	var ua = navigator.userAgent.toLowerCase();
	return	ua.indexOf("android") > -1;
}



$(document).ready(function() {
	
	$('#MainMenu img').click(function(){
		window.location='start.aspx';
	})
	
	if($('#ship_container').length > 0 && isiPad() == true){
		$('#header').prepend('<div id="hotspot"></div>');
		$('#hotspot').click(function(){
			window.location='start.aspx'
		})
		
	}
	
	
	//fade in the content of new screen
	//$('#content').animate({opacity: '0'}, 1);
	$('#content').animate({opacity: '1'}, 1500);
	
	// fadeout the content 'before' continue to new page
	$('a').click(function(event)
	{
		//event.preventDefault(false);
		
		if(!(($(this).hasClass('iframe_box')) || ($(this).attr('id') == 'fancybox-close') || ($(this).attr('href') == '') || ($(this).attr('href') === undefined))){
			if($(this).hasClass('fleet_box' == false) && $(this).hasClass('FleetThumbLink') == false){
				var link = $(this).attr('href');
			
				$('#content').animate({opacity:0},1000,function(){
					window.location = link;
				});
				return false;
			}
		}
		
	}
	);
	
	 //test js bestand van Luc en Martijn toevoegen
	$('head').append('<script type="text/javascript" src="/js/jquery.colorbox-min.js"/>');
	$('head').append('<script type="text/javascript" src="/js/mobile.js"/>');
	$('head').append('<script type="text/javascript" src="/js/luc.js" /></script>');
	$('head').append('<script type="text/javascript" src="/js/martijn.js" /></script>');

	
	
	
	
	// all html5 video's on website use mediaelements.js 
	$('video').mediaelementplayer(
	{
		// shows debug errors on screen
		enablePluginDebug: false,
		// path to Flash and Silverlight plugins
		pluginPath: '/js/',
		// name of flash file
		flashName: 'flashmediaelement.swf',
		// name of silverlight file
		silverlightName: 'silverlightmediaelement.xap',
		// larger number is less accurate, but less strain on plugin->JavaScript bridge
		timerRate: 250,
		//disable fullscreen
		features: ['playpause','progress','duration','volume'],
		// method that fires when the Flash or Silverlight object is ready
		success: function (vid, domObject) { 
			// pause the video to prevent autoplay
			vid.pause();
			if(isiPad() == true || isAndroid() == true){
				$('video').css('margin-top', '12px');
			}
			
			
		},
		// fires when a problem is detected
		error: function () { 
		
		}
	}
	);
	
	//set lightbox listner
	//$("a.iframe_box").livequery( function()
	//{
		
		$('a.iframe_box').colorbox(
		{
			iframe:true, width:1024, height: 748, opacity: '.5', scrolling: false
		}
		);
		
		$('a.fleet_box').colorbox({
			iframe:true, width:1024, height:800, opacity: '.5', scrolling: false
		})
	//}
	//); 
	
	
    
	if ($('div').hasClass('intro_video'))
	{
		// this inserts the introvideo, this can also be done by using a different template...
		$('.intro_video').html('<div id="video_box"><div id="video_container"><div id="video_player"><video tabindex="0" controls="controls" autobuffer preload="auto" id="videoID" height="344" width="762" src="/Userdata/videos/intro.mp4"></video></div></div></div>');
		
		//because the video is inserted the mediaelements plugin is started again on this video
		$('video').mediaelementplayer(
		{
			// shows debug errors on screen
			enablePluginDebug: false,
			// path to Flash and Silverlight plugins
			pluginPath: '/js/',
			// name of flash file
			flashName: 'flashmediaelement.swf',
			// name of silverlight file
			silverlightName: 'silverlightmediaelement.xap',
			// larger number is less accurate, but less strain on plugin->JavaScript bridge
			timerRate: 250,
			//disable fullscreen
			features: ['playpause','progress','duration','volume'],
			// method that fires when the Flash or Silverlight object is ready
			success: function (vid, domObject) 
			{ 
				//call the play method
				//vid.play();	
				
				//attempt to get video to autostart with flashplayer
				//setTimeout(function(){vid.play();},2500);
				
				
				
				vid.addEventListener('canplay', function(e) 
				{
					vid.play();
				}
				);
				
				
				//when video is finished sent it through to the menu page
				vid.addEventListener('ended', function(e) {
					
					$('#content').fadeOut(500);
					document.location.href = 'start.aspx';
					
				}, false);
			},
			// fires when a problem is detected
			error: function () { 
				alert('There is a problem loading the video.');
			}
		}
		);
		
		
	}
	//End intro movie animation//
	
	
	//build menu blocks//	
	if ($('div').hasClass('HSN_ShipsPanel'))
	{
		//reset the cookie so everypage will start with the top of the accordion open...
		$.cookie("contact", "gone");
		$.cookie("news", "gone");
		$.cookie("press", "gone");
		$.cookie("jobs", "gone");
		
		$(".HSN_ShipsPanel table tbody").delay(1000).diagonalFade(
		{
			time: 200,
			fadeDirection_x: 'left-right', // "left-right" || "right-left"
			fadeDirection_y: 'top-bottom', // "top-bottom" || "bottom-top"
			fade: 'in',                   // "in" || "out"
			complete: null                 // callback function
		}
		);
		
		
		//set edit link to normal size on edit menu page
		$('a[title="Wijzig"]').css({'height' : 'auto', 'zIndex' : '9999', 'position' : 'absolute'});	
		
	}
	//End build menu blocks//
	
	
	//ShipGallery stuff //
	
	$('.Header').click(function()
	{
		
		if($(this).find('.more').length > 0){
			$('.Header .more').css({'display' : 'block'});
			$('.less').css({'display' : 'none'});
			
			$(this).find('.more').css({'display' : 'none'});
			$(this).parent().find('.less').css({'display' : 'block'});
		
			if($(this).hasClass('ui-state-active'))
			{
				$("#ship_container").accordion('activate', 0);
		
				$(this).find('.more').css({'display' : 'block'});
				$(this).parent().find('.less').css({'display' : 'none'});
			}
		}else{
			$('.more').css({'display':'block'});
			$('.less').css({'display':'none'});
		}
	});
	
	$('.less').click(function()
	{
		$("#ship_container").accordion('activate', 0);
		$('.Header .more').css({'display' : 'block'});
		$('.less').css({'display' : 'none'});
	}
	);
	
	$("#ship_container").accordion(
	{
		header: '.Header',
		autoHeight: false,
		clearStyle: false,
		fillSpace: false
	}
	);

	
		
	if ($('div').hasClass('GalleryDetail') || $('div').hasClass('FleetDetail'))
	{	
		
		
		if($('div').hasClass('GalleryDetail') || $('div').hasClass('FleetDetail')){
			//center image if too small for viewport
			function center_image()
			{
				var width_big_img = $('.GalleryDetail img').width();
				if(width_big_img <= 969)
				{
					var margin_big_img = ((975-width_big_img)/2);
				}
				
				$('.GalleryDetail img').css({'margin-left' : margin_big_img});
				
				$('.galleryPDF').addpowerzoom(
				{
					defaultpower: 1.5,
					powerrange: [1.5,5.5],
					largeimage: null,
					magnifiersize: [250,250] //<--no comma following last option!
				}
				);
			}
			
			function image_loaded()
			{
				if ($('div').hasClass('FleetBigPhoto'))
				{
					var image_loaded = $('.FleetBigPhoto img').width();
				}
				
				else
				{
					var image_loaded = $('.GalleryDetail img').width();
				}
				
		
				if(image_loaded > 600)
				{
					center_image();
					
					if ($('div').hasClass('FleetBigPhoto'))
					{
						var first_image = $('.FleetBigPhoto img').attr('src');
					}
					
					else
					{
						var first_image = $('.GalleryDetail img').attr('src');
					}
					
					//search for position of thumbnail tag in filename
					var tn_pos = (first_image.lastIndexOf("/"))+1;
					
					//get total length of link
					var first_image_string_length = first_image.length;
		
					//set the filename by adding the tn_ tag from the filename
					var first_image_name = "tn_" + first_image.substr(tn_pos, first_image_string_length);
					
					//set the dirname without the filename
					var first_image_dir = first_image.substr(0, tn_pos);
				
					//set the complete name for the thumbnail to search for
					var first_image_thumb_name = first_image_dir + first_image_name;
					
					//count the number of instances before the current image (-3 to get center position)
					var count_images = ($('img[src="'+ first_image_thumb_name + '"]').parent().prevAll().length)-2;
					
					//set the strip position
					var strip_pos = -120*count_images;
				
					if ($('div').hasClass('FleetBigPhoto'))
					{
						// no strip move because this function is not operational yet...
					}
					
					else
					{
						//scroll the strip to right position
						$('#strip_move').stop().animate({marginLeft:strip_pos}, 400);
					}
				
					
				
				}
				else
				{
					//repeat the call after 500 msec.
					setTimeout(function(){image_loaded_repeat();},1000);
				}
			}
		
			function image_loaded_repeat()
			{
				image_loaded();
			}
		
			//start center image, first check if the image is loaded for getting the width
			image_loaded();
		}//end if GalleryDetail;
		
		
		//scroll elements
		$('.GalleryStrip').append('<div class="fadeLeft"></div><div class="fadeRight"></div>')
		var galleryScrolling = false;
		$('#strip_move').css('margin-left', '0px');

		// HOVER FUNCTIONS:
	/*	if(isIE() == true){
			$('#strip_left').hover(function(){
				galleryScrolling = true;
				doGalleryScroll('left');
			}).mouseup(function(){
				galleryScrolling = false;
			}).mouseleave(function(){
				galleryScrolling = false;
			})

			$('#strip_right').hover(function(){
				galleryScrolling = true;
				doGalleryScroll('right');
			}).mouseup(function(){
				galleryScrolling = false;
			}).mouseleave(function(){
				galleryScrolling = false;
			})
		}
*/
		$('#strip_left').click(function(){
			var positions = $('#strip_move').css('margin-left');
			positions = parseInt(positions.replace('px', ''));
			
			if(positions < 0){
				doGalleryScroll('left');
			};
		})

		
		$('#strip_right').click(function(){
			var amountImg = 0;
			$('#strip_move').find('img').each(function(){
				amountImg++;
			})
			
			var totalWidth = (amountImg * 130)-820;
			var positions = $('#strip_move').css('margin-left');
			positions = parseInt(positions.replace('px', ''));
			positions = positions * -1;
			
			if(positions < totalWidth){
				doGalleryScroll('right');
			}
		})
		
		
		
		function doGalleryScroll(direction){
			if(direction == 'left'){
				var amount = '+=120';
			}else{
				amount = '-=120';
			}
			
			$('#strip_move').stop().animate({marginLeft:amount}, 400, function(){
				if(galleryScrolling == true){					
					doGalleryScroll(direction);
				}
			});
		}
		

		// click on thumb to load new big image
		$('.ShipGalleryThumb').click(function()
		{
			//get image link
			var clicked_image = $(this).attr('src');
			//get total length of link
			var dir_length = clicked_image.length;
			//search for position of thumbnail tag in filename
			var dir_pos = clicked_image.search(/tn_/i);			
			//set the position of the filename
			var image_name_pos = dir_pos+3;
			
			//set the filename by extracting the tn_ tag from the filename
			var image_name = clicked_image.substr(image_name_pos, dir_length);
			//set the filename by extracting the tn_ tag from the filename and changing .jpg to .mp4
			var video_name_temp = clicked_image.substr(image_name_pos, dir_length);
			var video_name_length = video_name_temp.length;
			var video_name = video_name_temp.substr(0, video_name_length-3);
			video_name = video_name+'mp4';
			//create the dir
			var dir = clicked_image.substr(0, dir_pos);
			//make the filename by adding the dir and filename (minus the tn_ tag) together
			var new_image = dir+image_name;
			//make the filename by adding the dir and filename (minus the tn_ tag) together
			var new_video = dir+video_name;
			//determine what type of file it is
			var file_type = image_name.substr(0, 3);
			
			if(file_type=='img')
			{
				$('.GalleryDetail img').animate({opacity: '0'}, 500, function()
				{
					$('#gallery_loading').delay(250).css({'display' : 'block'});
					$('.GalleryDetail img').attr('src', new_image);
					$('.GalleryDetail img').load(function()
					{
						$('#gallery_loading').css({'display' : 'none'});
						$('.GalleryDetail img').animate({opacity: '1'},500); 
					}
					);
				}
				);
			}
			
			if(file_type=='vid')
			{
				
				$('.GalleryDetail').animate({opacity: '1'}, 500, function()
				{
					$('.GalleryDetail').empty();
					$('.GalleryDetail').append('<video tabindex="0" id="inserted_video" controls="controls" autobuffer preload="auto" src="'+new_video+'"></video>');
					
					var vid = document.getElementById('inserted_video');
					var video_width = vid.videoWidth;
					var video_height = vid.videoHeight;
					
					//reload video elementsplayer for new video
					$('video').mediaelementplayer(
					{
						// shows debug errors on screen
						enablePluginDebug: false,
						// path to Flash and Silverlight plugins
						pluginPath: '/js/',
						// name of flash file
						flashName: 'flashmediaelement.swf',
						// name of silverlight file
						silverlightName: 'silverlightmediaelement.xap',
						// larger number is less accurate, but less strain on plugin->JavaScript bridge
						timerRate: 250,
						// if set, overrides <video width>
						videoWidth: video_width,
						// if set, overrides <video height>
						videoHeight: video_height,
						//disable fullscreen
						features: ['playpause','progress','duration','volume'],
						// method that fires when the Flash or Silverlight object is ready
						success: function (vid, domObject)
						{ 
							// pause the video to prevent autoplay
							vid.pause();
						},
						// fires when a problem is detected
						error: function () { 	
						}
					}
					);
				}
				);
				
				$('.mejs-container').css({'margin' : '0px auto'});
			}
			
			if(file_type=='pdf')
			{
				$('.GalleryDetail img').animate({opacity: '0'}, 500, function()
				{
					$('#gallery_loading').delay(250).css({'display' : 'block'});
					$('.GalleryDetail img').attr('src', new_image);
					$('.GalleryDetail img').load(function()
					{
						$('#gallery_loading').css({'display' : 'none'});
						var width_pdf = $('.GalleryDetail img').width();
						if(width_pdf <= 969)
						{
							var margin_pdf = ((975-width_pdf)/2);
						}
						
						$('.galleryPDF').addpowerzoom(
						{
							defaultpower: 1.5,
							powerrange: [1.5,5.5],
							largeimage: null,
							magnifiersize: [250,250] //<--no comma following last option!
						}
						);
						
						$('.GalleryDetail img').css({'margin-left' : margin_pdf});
						$('.GalleryDetail img').animate({opacity: '1'},500); 
					}
					);
				}
				);
			}
		}
		);
		
		//load mediaelements video player on video's in gallerypage (for the first video that is loaded)//
		$('video').mediaelementplayer(
		{
			// shows debug errors on screen
			enablePluginDebug: false,
			// path to Flash and Silverlight plugins
			pluginPath: '/js/',
			// name of flash file
			flashName: 'flashmediaelement.swf',
			// name of silverlight file
			silverlightName: 'silverlightmediaelement.xap',
			// larger number is less accurate, but less strain on plugin->JavaScript bridge
			timerRate: 250,
			//disable fullscreen
			features: ['playpause','progress','duration','volume'],
			// method that fires when the Flash or Silverlight object is ready
			success: function (vid, domObject)
			{ 
				// pause the video to prevent autoplay
				vid.pause();
			},
			// fires when a problem is detected
			error: function () { 
			}
		}
		);
	}
	//END ShipGallery stuff //
	
	//open accordion to specific level based on the click and the set cookie
	if($.cookie("contact") == 'set')
	{
		$("#ship_container").accordion('activate', 3);
	}
	
	if($.cookie("news") == 'set')
	{
		$("#ship_container").accordion('activate', 1);
	}
	
	if($.cookie("press") == 'set')
	{
		$("#ship_container").accordion('activate', 2);
	}
	
	if($.cookie("jobs") == 'set')
	{
		$("#ship_container").accordion('activate', 2);
	}
	
	http://www.heesenyachts.nl/HEESENYACHTS2011/about.aspx
	
	//styling the search option
	if ($('div').hasClass('searchPortal'))
	{
		$('.searchPortal input[type=text]').attr('value', "I'm looking for...");
		var standard_text = $('.searchPortal input[type=text]').attr('value');
		
		$('.searchPortal input[type=text]').focus(function()
		{
			if($('.searchPortal input[type=text]').attr('value') == standard_text)
			{
				$('.searchPortal input[type=text]').attr('value', "");
			}
		});
		
		$('.searchPortal input[type=text]').blur(function()
		{
			if($('.searchPortal input[type=text]').attr('value') == '')
			{
				$('.searchPortal input[type=text]').attr('value', standard_text);
			}
		});
	}
	
	$(".shopProducts").accordion(
	{
		header: '.header',
		active: '0',
		collapsible: 'true'
	}
	);
	
	if ($('div').hasClass('press_login'))
	{
		
		$('.contactForm .header a, .signIn .header a').click(function(event)
		{
			event.preventDefault(false);	
		}
		);
		var reg = $.getUrlVar('reg');
		
		if(reg == 'true')
		{
			$(".contactForm").animate({opacity: '0.3'}, 500);
		}
		else
		{
			$(".signIn").animate({opacity: '0.3'}, 500);
		}
		
		$(".contactForm").mouseenter(function()
		{
			$(".contactForm").stop(true, true).animate({opacity: '1'}, 500);
			$(".signIn").stop(true, true).animate({opacity: '0.3'}, 500);
		}
		);
		
		$(".signIn").mouseenter(function()
		{
			$(".signIn").stop(true, true).animate({opacity: '1'}, 500);
			$(".contactForm").stop(true, true).animate({opacity: '0.3'}, 500);
		}
		);

	}
	

});

$.extend(
{  
	getUrlVars: function()
	{
		var vars = [], hash;
		var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
		
		for(var i = 0; i < hashes.length; i++)
		{
			hash = hashes[i].split('=');
			vars.push(hash[0]);
			vars[hash[0]] = hash[1];
		}
		return vars;
	},  
	getUrlVar: function(name)
	{
		return $.getUrlVars()[name];
	}
}
);
