// current active image, enables us to keep track in page and slideshow
var activeimage = '';
// set the default page for the site
var currentpage = '';
// gallery image
var galleryimage = 0;
// set the homepage
var homepage = '';
// slideshow image
var slideshowimage = 0;
// time for the slideshow images to change;
var slideshowtimer = 6000;
// time for the sub slideshow images to change;
var subslideshowtimer = 6000;
// slideshow timer image
var timercount = 0;
// start the sub images
var subimagecount = 1;
// number of images in sub level
var subimageslength = 1;

function createMusicPlayer() {
	// creates music player and loads music
	$("#mplayer").jPlayer({
		ready: function () {
			$(this).setFile('/assets/site-music.mp3').play()
		}
	});
	
	// run when file finishes playing, we want auto-repeat
	$("#mplayer").onSoundComplete( function() {
		$(this).play();
	});
}

// gallery image clicked, open up large version
function enlargeImage($src) {

	// check if there is a gallery image available, and remove if so
	$('img#galleryimage').remove();

	var img = new Image(); 
	// init image loading for main version
	img.src = $src;
	// id should be this so that we can remove the gallery image when navigating away
	img.id = 'galleryimage';

	$(img).load(function() {
		$("#imageholder").append(img);
		$(img).css('display','block');
		$('img#'+activeimage).css('display','none');
	});
}

// display default website for user
function displayHome() {
	// load gallery now, and NEVER remove the images. Loading in background
	// remove all from image gallery container
	$('div#txt_gallery').html('');
	loadGallery();
	$('#altnavigation').slideToggle('normal');
	
	// slide in the sitedetail when loaded use call back to load rest of the site
	$('#sitedetail').slideToggle('normal',function() {
		// display and add scroller if needed
		$('div#txt_'+currentpage).jScrollPane();
		$('div#txt_'+currentpage).fadeIn(2500);
		// display image corresponding with default page
		$('img#'+activeimage).fadeIn(2500);
		// call function to begin slideshow
		startSlideshow();
		// load the flash into the flashcontent id
		var params={};
		var attributes={};
		var flashvars = {};
		params.wmode = 'transparent';						
		swfobject.embedSWF("/assets/ribbon.swf", "flashcontent", "380", "38", "10.0.0", "",flashvars,params,attributes);
	});
}

// form submission 
function formSubmission() {
	
	var n = $("input#Name13_138").val();
	var a = $("input#Address13_261").val();
	var t = $("input#Telephone13_328").val();
	var e = $("input#Email13_432").val();
	var na = $("input#Nationality13_517").val();	
	var cap = $("input#captcha").val();	


	if(hex_md5(cap.toUpperCase()).toUpperCase() == captchares.toUpperCase()){
		if (n != '' && a != '' && t != '' && e != '' & na != '') {
			
			var urlpost=jsdomain+'/index.cfm';
			
			var urlstr = $('#register_form').serialize();
			urlstr += '&go=formProcess';
			
			urlstr+='&ajaxform=true';
			urlstr+='&_customform=true';
			
			// ajax post to our custom event in mach-ii
			$.ajax({  
				type: "POST",  
				url: urlpost,  
				data: urlstr,  
				success: function() { 
					$('p#submitform span').remove();

					if($("input#registerlang").val() == "eng"){
						$('p#submitform').append('<span class="successsubmit">Details submitted</span>');
					}else if($("input#registerlang").val() == "ar"){
						$('p#submitform').append('<span class="successsubmit">&#1588;&#1603;&#1585;&#1575; &#1604;&#1603;. &#1578;&#1605; &#1573;&#1585;&#1587;&#1575;&#1604; &#1575;&#1604;&#1606;&#1605;&#1608;&#1584;&#1580; &#1576;&#1606;&#1580;&#1575;&#1581;. </span>');
					}else if($("input#registerlang").val() == "ch"){
						$('p#submitform').append('<span class="successsubmit">&#35874;&#35874;&#12290;&#34920;&#26684;&#25552;&#20132;&#25104;&#21151;&#12290;</span>');
					}else if($("input#registerlang").val() == "ru"){
						$('p#submitform').append('<span class="successsubmit">&#1041;&#1083;&#1072;&#1075;&#1086;&#1076;&#1072;&#1088;&#1080;&#1084; &#1042;&#1072;&#1089;. &#1060;&#1086;&#1088;&#1084;&#1072; &#1073;&#1099;&#1083;&#1072; &#1087;&#1086;&#1076;&#1072;&#1085;&#1072; &#1091;&#1089;&#1087;&#1077;&#1096;&#1085;&#1086;.</span>');
					}		

					$("#thankyouformresultname").html(n);
					$("#thankyouformlink").trigger('click');
					$("#navigation a:first").trigger('click');
					
					//window.open( "http://www.thepalmbeach.co.uk/thankyou.html" );
				}
			});
		} else {
			$('p#submitform span').remove();
			
			if($("input#registerlang").val() == "eng"){
				$('p#submitform').append('<span>All fields are compulsory</span>');
			}else if($("input#registerlang").val() == "ar"){
				$('p#submitform').append('<span>&#1580;&#1605;&#1610;&#1593; &#1575;&#1604;&#1581;&#1602;&#1608;&#1604; &#1590;&#1585;&#1608;&#1585;&#1610;&#1577;</span>');
			}else if($("input#registerlang").val() == "ch"){
				$('p#submitform').append('<span>&#25152;&#26377;&#23383;&#27573;&#22343;&#20026;&#24517;&#22635;&#39033;</span>');
			}else if($("input#registerlang").val() == "ru"){
				$('p#submitform').append('<span>&#1047;&#1072;&#1087;&#1086;&#1083;&#1085;&#1077;&#1085;&#1080;&#1077; &#1074;&#1089;&#1077;&#1093; &#1087;&#1086;&#1083;&#1077;&#1081; &#1086;&#1073;&#1103;&#1079;&#1072;&#1090;&#1077;&#1083;&#1100;&#1085;&#1086;</span>');
			}				
			
		}
	}else {
		$('p#submitform span').remove();
		
		if($("input#registerlang").val() == "eng"){
			$('p#submitform').append('<span>Captcha text is incorrect</span>');
		}else if($("input#registerlang").val() == "ar"){
			$('p#submitform').append('<span>&#1606;&#1589; &#1575;&#1604;&#1603;&#1575;&#1576;&#1578;&#1588;&#1575; &#1594;&#1610;&#1585; &#1589;&#1581;&#1610;&#1581;</span>');
		}else if($("input#registerlang").val() == "ch"){ 
			$('p#submitform').append('<span>&#39564;&#35777;&#30721;&#25991;&#26412;&#19981;&#27491;&#30830;</span>');
		}else if($("input#registerlang").val() == "ru"){
			$('p#submitform').append('<span>&#1058;&#1077;&#1082;&#1089;&#1090; &#1082;&#1086;&#1085;&#1090;&#1088;&#1086;&#1083;&#1100;&#1085;&#1086;&#1075;&#1086; &#1080;&#1079;&#1086;&#1073;&#1088;&#1072;&#1078;&#1077;&#1085;&#1080;&#1103; (CAPTCHA) &#1085;&#1077;&#1087;&#1088;&#1072;&#1074;&#1080;&#1083;&#1077;&#1085;</span>');
		}			
		
		
	}		
	
	

}

// form submission 
function formSubmissionMenu() {
	
	var n = $("input#Name14_161").val();
	var a = $("input#Date14_286").val();
	var t = $("input#Time14_327").val();
	var e = $("input#Number14_425").val();
	var na = $("input#Email14_575").val();	
	var tel = $("input#Telephone14_687").val();	
	var capsecond = $("input#captchasecond").val();	

	if(hex_md5(capsecond.toUpperCase()).toUpperCase() == captcharessecond.toUpperCase()){
		if (n != '' && a != '' && t != '' && e != '' & na != '' & tel != '') {
			
			var urlpost=jsdomain+'/index.cfm';
			var urlstr = $('#menu-booking').serialize();
			urlstr += '&go=formProcessMenu';
			urlstr+='&ajaxform=true';
			urlstr+='&_customform=true';
			
			// ajax post to our custom event in mach-ii
			$.ajax({  
				type: "POST",  
				url: urlpost,  
				data: urlstr,  
				success: function() { 
					$('p#submitformmenu span').remove();
					
					if($("input#registerlang").val() == "eng"){
						$('p#submitformmenu').append('<span class="successsubmit">Details submitted</span>');
					}else if($("input#registerlang").val() == "ar"){
						$('p#submitformmenu').append('<span class="successsubmit">&#1588;&#1603;&#1585;&#1575; &#1604;&#1603;. &#1578;&#1605; &#1573;&#1585;&#1587;&#1575;&#1604; &#1575;&#1604;&#1606;&#1605;&#1608;&#1584;&#1580; &#1576;&#1606;&#1580;&#1575;&#1581;. </span>');
					}else if($("input#registerlang").val() == "ch"){
						$('p#submitformmenu').append('<span class="successsubmit">&#35874;&#35874;&#12290;&#34920;&#26684;&#25552;&#20132;&#25104;&#21151;&#12290;</span>');
					}else if($("input#registerlang").val() == "ru"){
						$('p#submitformmenu').append('<span class="successsubmit">&#1041;&#1083;&#1072;&#1075;&#1086;&#1076;&#1072;&#1088;&#1080;&#1084; &#1042;&#1072;&#1089;. &#1060;&#1086;&#1088;&#1084;&#1072; &#1073;&#1099;&#1083;&#1072; &#1087;&#1086;&#1076;&#1072;&#1085;&#1072; &#1091;&#1089;&#1087;&#1077;&#1096;&#1085;&#1086;.</span>');
					}					
					
					$("#thankyouformresultname").html(n);
					$("#thankyouformlink").trigger('click');
					$("#navigation a:first").trigger('click');
				}
			});
		} else {
			$('p#submitformmenu span').remove();
			
			if($("input#registerlang").val() == "eng"){
				$('p#submitformmenu').append('<span>All fields are compulsory</span>');
			}else if($("input#registerlang").val() == "ar"){
				$('p#submitformmenu').append('<span>&#1580;&#1605;&#1610;&#1593; &#1575;&#1604;&#1581;&#1602;&#1608;&#1604; &#1590;&#1585;&#1608;&#1585;&#1610;&#1577;</span>');
			}else if($("input#registerlang").val() == "ch"){
				$('p#submitformmenu').append('<span>&#25152;&#26377;&#23383;&#27573;&#22343;&#20026;&#24517;&#22635;&#39033;</span>');
			}else if($("input#registerlang").val() == "ru"){
				$('p#submitformmenu').append('<span>&#1047;&#1072;&#1087;&#1086;&#1083;&#1085;&#1077;&#1085;&#1080;&#1077; &#1074;&#1089;&#1077;&#1093; &#1087;&#1086;&#1083;&#1077;&#1081; &#1086;&#1073;&#1103;&#1079;&#1072;&#1090;&#1077;&#1083;&#1100;&#1085;&#1086;</span>');
			}				
							
			
			
			
		}
	}else {
		$('p#submitformmenu span').remove();
		
		if($("input#registerlang").val() == "eng"){
			$('p#submitformmenu').append('<span>Captcha text is incorrect</span>');
		}else if($("input#registerlang").val() == "ar"){
			$('p#submitformmenu').append('<span>&#1606;&#1589; &#1575;&#1604;&#1603;&#1575;&#1576;&#1578;&#1588;&#1575; &#1594;&#1610;&#1585; &#1589;&#1581;&#1610;&#1581;</span>');
		}else if($("input#registerlang").val() == "ch"){ 
			$('p#submitformmenu').append('<span>&#39564;&#35777;&#30721;&#25991;&#26412;&#19981;&#27491;&#30830;</span>');
		}else if($("input#registerlang").val() == "ru"){
			$('p#submitformmenu').append('<span>&#1058;&#1077;&#1082;&#1089;&#1090; &#1082;&#1086;&#1085;&#1090;&#1088;&#1086;&#1083;&#1100;&#1085;&#1086;&#1075;&#1086; &#1080;&#1079;&#1086;&#1073;&#1088;&#1072;&#1078;&#1077;&#1085;&#1080;&#1103; (CAPTCHA) &#1085;&#1077;&#1087;&#1088;&#1072;&#1074;&#1080;&#1083;&#1077;&#1085;</span>');
		}	
	}		
	

}


// gets the instance of flash to send data to
function getFlashMovie(movieName) {
	var isIE = navigator.appName.indexOf("Microsoft") != -1;
	return (isIE) ? window[movieName] : document[movieName];
}

// init site
function init($homepage) {
	// initiate music loading
	createMusicPlayer();
	// set current page as home
	currentpage = $homepage;
	homepage = $homepage;
	// set the active image, to begin with will be home page
	// added new subimagecount to the id, as sections have sub images
	activeimage = currentpage+'_'+subimagecount;
	// update the number of images loaded
	updateImagePreloader();
	// init the preloader for the slideshow images	
	preloadSlideshow();
}

// load the dynamic image gallery
function loadGallery() {
	
	if (galleryimage < jsGallery.length) {
	
	//	alert('loading ' + galleryimage);
	
		// create new image dom
		var img = new Image(); 

		// init image loading for the gallery
		img.src = g+jsGallery[galleryimage]; 
		img.id = jsGallery[galleryimage].split('.')[0];
		
		// image has loadd, add into the image holder div
		$(img).load(function() {
			// create the a href, but remove the 'thumb' from source, ready to load large version
 			var a = $('<a/>').attr({
 				href:'#',
 				onClick:"enlargeImage('"+img.src.replace('_thumb','') +"'); return false;",
 				title:img.id.split('-')[1].split('_').join(' ')
 			});
			$("div#txt_gallery").append(img);
			$(this).wrap(a);
			// increase gallery image by one
			galleryimage++;
			// ensure image value is not this, set when new page is loaded in attempt to quit loading process
		//	if (galleryimage != 1000000) {	
				// init jscrollpane
				//$('div#txt_gallery').jScrollPane();
				// start preloader again based on new image
				loadGallery();
			//}
		}); 
	} else {
		// reset the gallery images
		galleryimage = 0;
		// add the rollover effect to all the image gallery
		/*$('div#txt_'+currentpage+' a').tooltip({
			track: true,
			delay: 0,
			showURL: false,
			showBody: " - "
		});*/
	}
}

// navigation clicked : hide and show content
function navigation($title,$scroll,$logo) {
	// check that $scroll is true for the jscroller
	// if we have clicked navigation need to stop the image slideshow
	$('#imageholder').stopTime('imagechange');
	// also ensure that the sub images timer has been stopped
	$('#imageholder').stopTime('subimagechange');	
	// if logo is true, then logo has been clicked. Start the slideshow
	if ($logo != null) startSlideshow();
	// problem with ie and image gallery. IE too slow to remove current nodes, so added this to make sure it does it first
	//if (currentpage == 'gallery') $('div#txt_'+currentpage).html('');
		 
	// increase image gallery to something high, images try to load when swithcing page
	//if (galleryimage > 0) galleryimage = 1000000;
	
	// need to remove the scrollpane first on all content divs which in turn allows us to hide the current content and fade image out		
	$('div.content').jScrollPaneRemove();
	$('div.content').css('display','none');	
	$('div.alt_content').css('display','none');

	// fade in image are fade out complete
	$('img#'+activeimage).fadeOut('slow',function() {

		// hdie and rmeoce jscroll again as it sometimes leaves the scroller when gallery loading 
		$('div.content').jScrollPaneRemove();
		$('div.content').css('display','none');				
		$('div.alt_content').css('display','none');
		
		// with new sub images, need to reset the image subcount to use for main image
		subimagecount = 1;
		
		// display and add scrollers to new content block and fade image in	
		if ($scroll) $('div#txt_'+$title).jScrollPane();
		
		if ($('div#txt_'+$title).parent().attr('id') != "alt_navigation" ){
				$('div#txt_'+$title).fadeIn('fast');
				// img id no longer is just the title -> uses the subimagecount too
				$('img#'+$title+'_'+subimagecount).fadeIn(500);
		}else{
			$('img#'+$title+'_'+subimagecount).fadeIn(500, function() {
		    	$('div#txt_'+$title).fadeIn(166);
			});

		}
		//$('div#txt_'+$title).css('display','block');
		// update the current page var with new page title
		currentpage = $title;
		// set the new active image
		// add the image subcount to this too
		activeimage = currentpage+'_'+subimagecount;
		
			if ($logo == null) {
			// as we have clicked a page, need to init the sub carousel for the page, but ensure we dont use this on the home page
			startSubSlideshow();
		}
		
		//// REMOVED GALLERY	
		// if its the gallery then load in images	
		//if ($title == 'gallery') {
			// clear contents of gallry content again, to be sure. IE problem. :-(
		//	$('div#txt_'+currentpage).html('');			
			// init the gallery
		//	loadGallery();
		//}
	});
} 

// load images and show loader
function preloadSlideshow() {
	
	if (slideshowimage < jsSlideshow.length) {
		// create new image dom
		var img = new Image(); 
		
		// init image loading for the slideshow
		img.src = s+jsSlideshow[slideshowimage]; 
		img.id = jsSlideshow[slideshowimage].split('.')[0];
		
		// image has loaded, add into the image holder div
		$(img).load(function() {
			$("#imageholder").append(img);
			// increase slideshow image by one
			slideshowimage++;
			// update the image preloader
			updateImagePreloader();
			// start preloader again based on new image
			preloadSlideshow();			
		
		}); 
	} else {
		// remove the preloader
		$("#preloader").remove();
		// image have all loaded, create all children and load in interface elements
		displayHome();
	}
}

// function to change the slideshow image
function slideshowImageChange() {
	// check the current page is not being animated
	if ($('img#'+ activeimage+':not(:animated)')) {
		$('img#'+ activeimage).fadeOut('slow',function() {
			// Janusz : removed this section as would display all images inc the sub images;			
			// increase by one after animation
			timercount++;
			// check to timercount not greater than number of images, if so reset to sero
			if (timercount > jsMainPage.length-1) timercount = 0;
			// set the active image
			//activeimage = jsSlideshow[timercount].split('.')[0];
			activeimage = jsMainPage[timercount].split('.')[0]; 
			// fade out next image in count
			$('img#'+ activeimage).fadeIn(833);

		});		
	}
}

// function to change the sub slideshow image
function subSlideshowImageChange() {
	// using the value gained from starting slideshow function - change images
	$('img#'+currentpage+'_'+ subimagecount).fadeOut('slow',function() {
		// increase by one after animation
		subimagecount++;
		// check to timercount not greater than number of images, if so reset to sero
		if (subimagecount > subimageslength-1) subimagecount = 1;
		// set the active image so that we can hide the image if needed
		activeimage = currentpage+'_'+ subimagecount;
		// fade out next image in count
		$('img#'+currentpage+'_'+subimagecount).fadeIn(833);		
	});
}

// function to start the slideshow, needed when site loads and when logo is clicked
function startSlideshow() {
	// init the timerCount
	timercount = 0;
	
	// init the timer for the image slideshow
	$('#imageholder').everyTime(slideshowtimer, 'imagechange',slideshowImageChange);
}

// function to start the sub slideshow
function startSubSlideshow() {
	subimageslength = 1;
	// count the number of images availabe in sub images so we can use for the rotation
	for (var i = 1; i < jsSlideshow.length; i++) {
		if (jsSlideshow[i].split('_')[0] == currentpage) subimageslength ++;
	}
	if(currentpage == homepage && subimageslength != 1)subimageslength++;
	// only init the timer for the image slideshow if we have more than one image
	if (subimageslength > 2) $('#imageholder').everyTime(subslideshowtimer, 'subimagechange',subSlideshowImageChange);	
}


function updateImagePreloader() {
		$('span#loadimages').html(slideshowimage + ' / ' + jsSlideshow.length + ' ');
}

// volume control for music player
function volumeControl() {
	// as i cant find way to get current volume, decide on min or max by link value
	var musicstr = $('#playercontrol').text();
		
	if (musicstr == 'Music Off'){	
		$('#mplayer').volume(0);
		$('#playercontrol').text('Music On');
	} else if (musicstr == 'Music On') {
		$('#mplayer').volume(100);
		$('#playercontrol').text('Music Off');	
	}
}

// handler the css centering issue with scrollers
function resize() {
	
	if($(window).height() < $('#holder').height()){
		$('#container').css('top', '0px');
		$('#holder').css('top', '0px');
	} else {
		$('#container').css('top', '50%');
		$('#holder').css('top', '-'+($('#holder').height()/2)+'px');
	}
	
	if($(window).width() < $('#holder').width()){
		$('#holder').css('left', ($('#holder').width()/2)+'px');
	} else {
		$('#holder').css('left', '50%');
	}
}

//Bind events on resize
$(document).ready(function(){ resize(); })
$(window).bind("resize", function(){ resize(); });