/**************************************/
/******** Site Public Functions *******/
/**************************************/

this.zoomImage = function(){
	
	//alert('1');
	/*
	$('a.bwGal').zoomimage({
	border: 10,
	centered: true,
	hideSource: true
	});
	*/
	
	$('a.customGal').zoomimage({
	centered: true,
	controlsTrigger: 'mouseover',
	className: 'custom',
	shadow: 40,
	controls: false,
	opacity: .5,
	beforeZoomIn: function(boxID) {
		$('#' + boxID)
			.find('img')
			.css('opacity', 0)
			.animate(
				{'opacity':1},
				{ duration: 500, queue: false }
			);
	},
	beforeZoomOut: function(boxID) {
		$('#' + boxID)
			.find('img')
			.css('opacity', 1)
			.animate(
				{'opacity':0},
				{ duration: 500, queue: false }
			);
	}

	});
	
}

/*****************/

//var loadBackground = function(){
this.loadBackground = function(){
	
	var slide = 0;
	
	// count the number of image
	var total_image = $('#supersize li').length;
	//alert(total_image);
	
	// set the opacity of all images to 0
	$('#supersize li').css({opacity: 0.0});
	
	// add the class of active to the first image and display it (set it to full opacity)
	$('#supersize li:first').addClass('active').css({opacity: 1.0});
	
	$('#supersize a').click(function(){
		return false;
	});
	
	if (total_image > 1) 
	{
		slide = parseInt(1);
	}
	else
	{
		slide = parseInt(0);
	}
	
	//alert(slide);
	
	//alert(typeof slide);
	
	$.fn.supersized.options = {  
			startwidth: 1024,  
			startheight: 768,
			minsize: .50,
			slideshow: slide,
			slideinterval: 5000  
		};
	$('#supersize').supersized(); 
	
	//$('#popup-home').hide();
	//$('#footer-home').hide();
	$('#supersize').hide().fadeIn(1500, function(){
		$('#popup-home').fadeIn(1500, function(){
			$('#footer-home').fadeIn(1500);
		});
	});
	
}

/*****************/

this.scrollbar = function(){	
$('#pane1').jScrollPane();
$('.scroll-pane').bind('scroll',function(event){
	console.log(event.target);
	});
};

this.scrollbarImg_content= function(){
$('#content-shop, .content-offers').jScrollPane({
	showArrows:false, 
	scrollbarWidth: 10,
	reinitialiseOnImageLoad: true
	});
};

this.scrollbarImg_option = function(){
$('#content-shop').jScrollPane({
	showArrows:false, 
	scrollbarWidth: 10,
	reinitialiseOnImageLoad: true
	});
};

/*****************/

this.attachToggler = function(){
	$('.toggler').toggle(
		function (){
			
			// change the text to a plus signs
			$(this).text('+').css({fontSize:'12px'});
			
			// get the parent which is <ul>
			var parent = $(this).parent().parent();
			
			$('#offer, #shop, #menu-main-shadow').fadeOut(500);
			
			// find the parent's offset position
			var position = parent.offset();
			//alert(position.top);
			
			// change the css
			parent.css({
				position:'fixed',
				top:position.top,
				left:position.left
			});

			// animate the parent to the new position
			parent.animate({ 
				top:'10px',
				left:'10px'
			 }, 1500 );
			
		},
		function (){
			$('#offer, #shop, #menu-main-shadow').fadeIn(500);
			
			// change the text to a minus signs
			$(this).text('-').css({fontSize:'14px'});
			
			// get the parent which is <ul>
			var parent = $(this).parent().parent();
			
			parent.css({
				position:'static',
				top:'0px',
				left:'0px'
			});
	});
	
};

/*****************/

this.getContent = function(){
	$('.get-content').click(function(){
		var target_body = $('#content-shop');
		var path = $(this).attr("href");
		target_body.html('<div><p><img src="'+http_root+rp_image_global+img_loader+'"/> loading</p></div>');
		target_body.load( path, function(){
			target_body.hide().fadeIn();
			scrollbarImg_content();
			zoomImage();
		});
		return false;
	});
}

/*****************/

this.getShop = function(){
	$('.get-shop').click(function(){
		
		var timer;
		
		var supersize = $('#supersize');
		var shop = $('#shop');
		var music = $('#music');
		var offer = $('#offer');
		
		var target_body = $('#content-shop');
		var path = $(this).attr("href");
		//alert(path);
		
		var pg_url = path.split('/').slice(-1);
		var content_option = path.split('/').slice(-2, -1);
		//alert(path.split('/').slice(-2, -1));
		
		if(content_option == 'flash')
		{
			var backbond_supersize = 'supersize_360.php';
		}
		else
		{
			var backbond_supersize = 'supersize.php';
		}
		
		supersize.fadeOut('fast',function(){
			//supersize.empty();
			//$('li').removeClass('active last-active');
			
			shop.fadeOut('fast',function(){
				offer.fadeOut('fast',function(){
					music.fadeOut('fast',function(){
				
						//supersize.fadeIn('fast').html('<div><p><img src="'+http_root+rp_image_global+img_loader+'" class="loader"/> loading</p></div>');
						supersize.fadeIn('fast').html('<p class="processing">loading</p>');
						timer = setInterval('animateProcessing()',500);
						
						supersize.load( http_root+rp_layout+backbond_supersize+'?load=1&pg_url='+pg_url, function(){
							
							clearInterval(timer);
							
							//$("#supersize").resizenow();
							//$(window).unbind("resize.supersized");
							//supersize.unbind("resize.supersized");
							if(content_option == 'flash')
							{
								supersize.fadeIn();
							}
							else
							{
								supersize.stopSupersized();
								loadBackground();
							}
							
							shop.load(http_root+rp_layout+'shop.php?load=1&pg_url='+pg_url, function(){
								shop.fadeIn();
								scrollbarImg_content();
								getContent();
								
								offer.load(http_root+rp_layout+'offer.php?load=1&pg_url='+pg_url, function(){
									offer.fadeIn(function(){
										offer.css({opacity:.8})
									});
									
									toggleTerms();
									
									music.load(http_root+rp_layout+'music.php?load=1&pg_url='+pg_url, function(){
										music.fadeIn();
									});
								})
							});
						
						});
					
					});
				})
			})
		});
		
		return false;
	});
}

function animateProcessing() {
	//Fade out the current content
	$('.processing,.loading').fadeOut('fast',function(){
		//Display the content
		$('.processing,.loading').fadeIn('fast');									   
	});
}

/*****************/

this.toggleTerms = function(){
	$('.terms-and-conditions').toggle(
		function (){
			
			// get the parent which is <h2>
			var parent = $(this).parent();
			
			parent.next().slideDown();
			
		},
		function (){
			// get the parent which is <h2>
			var parent = $(this).parent();
			
			parent.next().slideUp();	
	});
	
};

/*****************/

this.loadPopOut = function(){
	
	$('.load-popup-public').click(function (){
		
		$('.popup-public').remove();
		$(this).parent().siblings().find('a').removeClass("active");
		//$('.load-popup-public:not('+$(this)+')').removeClass("active");
		
		/**
		if the browser is IE, remove .ie-shadow/ .ie-shadow-1/ .ie-shadow-2/ .ie-shadow-3
		**/
		if ($.browser.msie && parseInt($.browser.version) < 9) 
		{
			var shadow = $('.ie-shadow-1');
			shadow.remove();
		}
		
		if($(this).hasClass("active") == '')
		{
			var timer;
			
			$(this).addClass("active");
			
			$(document.body).prepend("<div class=\"popup-public\"></div>");
			$(document.body).prepend("<div class=\"loading\">loading</div>");
			targetMenu = $(this);
			var target = $('.popup-public');
			var loading = $('.loading');
			var path = $(this).attr("href");
			
			/**
			set the variable and get the value for the pop-out
			**/
			var dimension = $(this).attr("class").split(' ').slice(1,2);
			var width = dimension[0].split('x').slice(0,1);
			var height = dimension[0].split('x').slice(1,2);
			//alert(height);
			
			var scrollTop = $(window).scrollTop();
			var scrollLeft = $(window).scrollLeft();
			
			var marginLeft = "-"+((scrollLeft + width)/2);
			var marginTop = ((scrollLeft + height)/2);
			marginTop = "-"+(marginTop + 25);
			//alert(marginTop);
			
			/**
			set the variable and get the value for the loading
			**/
			var width_loading = loading.width();
			var height_loading = loading.height();
			
			var marginLeft_loading = "-"+((scrollLeft + width_loading)/2);
			var marginTop_loading = ((scrollLeft + height_loading)/2);
			marginTop_loading = "-"+(marginTop_loading + 25);
			//alert(loading.width());	
			
			/**
			set the css for pop-out
			**/
			target.css({
					width: width + "px",
					height: height + "px",
					marginLeft:marginLeft + "px",
					marginTop:marginTop + "px",
					color:"#ffffff",
					background:"#3958a8",					
					zIndex:"10",
					opacity: 0.8, // for IE, you must not use something like this '0.8'
					display: 'none'
					});
			
			/**
			set the css for loading
			**/
			loading.css({
					width: width_loading + "px",
					height: height_loading + "px",
					marginLeft:marginLeft_loading + "px",
					marginTop:marginTop_loading + "px",
					color:"#ffffff",			
					zIndex:"10",
					display: 'none'
					});
					
			
			/**
			fade in the loading and animate it
			**/
			loading.fadeIn('fast', function(){
				timer = setInterval('animateProcessing()',500);
			});
			
			/**
			load the requested page and remove loading
			*/
			target.load( path, {}, function(){
				
				clearInterval(timer);
				
				loading.remove();
				target.fadeIn('fast', function(){
					ieShadowClass = 'ie-shadow-1';
					ieShadowzIndex = 9;
					ieBoxShadow(target);
				});
				scrollbarImg_content();
				$('form *[title]').inputHint();
				closePopup_doubleTargets(target);
				postForm_public();
				
			});
			
		}
		else
		{
			var target = $('.popup-public');
			
			/**
			if it is IE, run this code
			**/
			if ($.browser.msie && parseInt($.browser.version) < 9) 
			{
				$('.ie-shadow-1').remove();
				$('.ie-shadow-2').remove();
			}
			
			/**
			fade out #pop-out (*it does not fade out for some reason)
			**/
			target.fadeOut('slow', function(){
				target.remove();
			});
			
			/**
			remove .active
			**/
			$(this).removeClass("active");
		}
		
		return false;
		
	});
	
};

this.closePopup_doubleTargets = function(target){
	
	$('.close',target).click(function (){
		
		/**
		if it is IE, run this code
		**/
		if ($.browser.msie && parseInt($.browser.version) < 9) 
		{
			$('.ie-shadow-1').remove();
			$('.ie-shadow-2').remove();
		}
		
		/**
		fade out #pop-out
		**/
		target.fadeOut('slow', function(){
			target.remove();
			targetMenu.removeClass("active");
		});
		
		return false;
		
	});
	
}

this.closePopup_public = function(target){

	$(".close",target).click(function(){
		
		/**
		if it is IE, run this code
		**/
		if ($.browser.msie && parseInt($.browser.version) < 9) 
		{
			$('.ie-shadow-2').remove();
		}
		
		target.fadeOut('fast', function(){
			target.remove();
		});
		
	return false;
	
	});
	
}

this.ieBoxShadow = function(target){
	
	/**
	check if it is IE first
	**/
	if ($.browser.msie && parseInt($.browser.version) < 9) 
	{
		/**
		check which version it is.
		Often you only care about the "major number," the whole number. 
		This can be accomplished with JavaScript's built-in parseInt() function.	
		**/
		if ($.browser.msie && parseInt($.browser.version) < 9) {
			//alert($.browser.version);
			//alert(parseInt($.browser.version));
		}
		
		var width = target.width();
		var height = target.height();
		//alert(height);

		var scrollTop = $(window).scrollTop();
		var scrollLeft = $(window).scrollLeft();

		var marginLeft_shadow = ((scrollLeft + width)/2);
		var marginTop_shadow = ((scrollLeft + height)/2);

		marginLeft_shadow = "-"+(marginLeft_shadow + 10);
		marginTop_shadow = "-"+(marginTop_shadow + 35);

		/**
		create a div for ie-shadow
		place the #pop-out after the .ie-shadow
		set the css of .ie-shadow
		**/
		target.after("<div class='"+ieShadowClass+"'></div>");
		$("."+ieShadowClass+"").width(target.width()+40).height(target.height()+40).css({
			marginLeft:marginLeft_shadow + "px",
			marginTop:marginTop_shadow + "px",					
			zIndex:ieShadowzIndex
		});
	}
}

this.loadPopOut_bk = function(){
	$('.late-offers').toggle(
		function (){
			
			$(document.body).prepend("<div class=\".popup-public\"></div>");
			var target = $('.popup-public');
			var path = $(this).attr("href");
			
			var dimension = $(this).attr("class").split(' ').slice(1,2);
			var width = dimension[0].split('x').slice(0,1);
			var height = dimension[0].split('x').slice(1,2);
			//alert(height);
			
			var scrollTop = $(window).scrollTop();
			var scrollLeft = $(window).scrollLeft();
			var width = 500;
			var top = 70;
			var marginLeft = "-"+((scrollLeft + width)/2);
			var marginTop = "-"+((scrollLeft + height)/2);
			
			target.css({
					position: 'absolute',
					top:"50%",
					left:"50%",
					width: width + "px",
					height: height + "px",
					marginLeft:marginLeft + "px",
					marginTop:marginTop + "px",
					color:"#ffffff",
					background:"#3958a8",					
					zIndex:"10",
					opacity: '0',
					display: 'none'
					});
					
			target.load( path, {}, function(){
				
				//target.fadeTo(1500, 0.5);
				target.fadeIn('fast');
				scrollbarImg_content();
				closePopup_public(target);
				
			});
			
		},
		function (){
			var target = $('.popup-public');
			target.fadeOut('fast');
			target.remove();
	});
	
};

/*****************/

this.postForm_public = function(){
	
	$("#form-contact, #form-message").submit(function(){
	
		var timer;
		
		$("#popup-result-public").remove();
		$('.ie-shadow-2').remove();
		
		var path = $(this).attr('action');
		var processing = $('.processing');
		//alert(path);
		
		/**
		set the css for processing
		**/
		processing.css({
			margin:"5px 0px 0px 340px",
			position:"absolute",
			visibility:"visible"
		});
			
		/**
		add text to the processing div and animate it
		**/
		processing.html('processing');
		processing.fadeIn('fast', function(){
			timer = setInterval('animateProcessing()',500);
		});
			
		/**
		load the requested page
		**/
		$.post(path, $("#form-contact,#form-message").serialize(),function(xml){
				
			clearInterval(timer);
			
			//var str = $("#form_data_message").serialize();
			//alert(str);
			$("label").removeClass('error');
			$("form img").css({visibility:'hidden'});
			processing.css({
				visibility:"hidden"
			});
			processForm_public(xml);
		});
		
		return false;
	});
}

this.processForm_public = function(xml){

	$(document.body).prepend("<div id='popup-result-public' class='popup-public'><div class='close'><a href='#' class='close'>x close</a></div><div class='result'></div><div class='form-confirm'></div></div>");
	
	/**
	set the target's value and set the target's width
	**/
	var target = $('#popup-result-public').width(400);
	
	$("error", xml).each(function(){
			
		/**
		set the local variable
		**/
		var elementid = $(this).attr('elementid');
		var message = $(this).attr('message');

		/**
		add an error class to each error element
		**/
		$("#"+elementid+"_label").addClass('error');
		
		/**
		append the message to the .result class
		**/
		$(".result").append("<img src='"+http_root+rp_image_global+"attention.png' /> " + message + "<br />");
		
	});
		
	$("confirm", xml).each(function(){
		
		/**
		set the local variable
		**/
		var message = $(this).attr('message');
		path = $(this).attr('path');
		//alert(path);
		
		/**
		append the message to the .result class
		**/
		$(".result").append("<img src='"+http_root+rp_image_global+"attention.png' /> " + message + "");
	});
		
		
	$("result", xml).each(function(){
		
		/**
		set the local variable
		**/
		var message = $(this).attr('message');
		//alert(message);
		
		/**
		append the message to the .result class
		**/
		$(".result").append("<img src='"+http_root+rp_image_global+"info.png' /> " + message + "");
		
	});
	
		
	if($(xml).find("error").length > 0)
	{
		//alert('error');
		ieShadowClass = 'ie-shadow-2';
		ieShadowzIndex = 29;
		showFormResult_public(target);
		$('form *[title]').inputHint();
		closePopup_public(target);	
	}
	else if($(xml).find("confirm").length > 0)
	{
		//alert('confirm');
		/**
		load the confirmation form and attach the postForm_confirm() function
		**/
		$(".form-confirm").load(http_root+rp_layout+"form_confirm.php?path_return="+path, {}, function(){
			
			//alert(target.height());
			ieShadowClass = 'ie-shadow-2';
			ieShadowzIndex = 29;
			postForm_confirm();
			showFormResult_public(target);
			$('form *[title]').inputHint();
			closePopup_public(target);	
		});
	}
	else
	{
		
		//salert('result');
		ieShadowClass = 'ie-shadow-2';
		ieShadowzIndex = 29;
		showFormResult_public(target);
		clearFormElements('#form-contact,#form-message');
		$('form *[title]').inputHint();
		closePopup_public(target);
	}
}

this.showFormResult_public = function(target){
	
	var width = target.width();
	var height = target.height();
	//alert(height);
	
	var scrollTop = $(window).scrollTop();
	var scrollLeft = $(window).scrollLeft();

	var marginLeft = "-"+((scrollLeft + width)/2);
	var marginTop = ((scrollLeft + height)/2);
	marginTop = "-"+(marginTop + 25);
			
	target.css({
		marginTop: marginTop + "px",
		marginLeft:marginLeft + "px",
		width:width + "px",
		color:"#ffffff",
		background:"#122a6a",
		border:"0px solid #fff",			
		zIndex:"30",
		opacity: 0.9, // for IE, you must not use something like this '0.8'
		display: 'none'
		});
	
	target.fadeIn('slow', function(){
		ieBoxShadow(target);
	});

}

this.postForm_confirm = function(){
	
	var target = $("#popup-result-public");
	
	$("input[name=yes]").click(function(){
	
		$("#form-confirm").submit(function(){
			
			var timer;
			
			var path_return = $(this).attr('action');
			var processing = $('.processing');
			//alert(path_return);
			
			/**
			set the css for processing
			**/
			processing.css({
				margin:"5px 0px 0px 10px"
			});
			
			/**
			add text to the processing div and animate it
			**/
			processing.html('processing');
			processing.fadeIn('fast', function(){
				timer = setInterval('animateProcessing()',500);
			});
			
			/*
			target.fadeOut('fast', function(){
				target.remove();
			});
			*/
			
			$.post(path_return, $("#form-contact,#form-message").serialize(),function(xml){
				
				clearInterval(timer);
				
				//var str = $("#form-message").serialize();
				//alert(str);
				$('.ie-shadow-2').remove();
				
				target.fadeOut('slow', function(){
					target.remove();
					processForm_public(xml);
				});
				
				
			});
			
			return false;
		});
	});	
	
	$("input[name=no]").click(function(){

		$("#form-confirm").submit(function(){
			target.fadeOut('fast', function(){
				
				$('.ie-shadow-2').remove();
				target.remove();
			});
			return false;
		})	
	});
	
}

