var slug;
var alertTimerId;
var rotationTimerId;
var scrollWidth = 0;
var currentSlogan;
var halfWidth;
var fullWidth;
var featureNum = 0;
var rotationSpeed = 0;
var img;
var totalFeatureNum;
var currentTableID;
var treatShow;
var treatLast;
var finish = false;

$(document).ready( function() {
	$('#press-for-gift').click( function() {
		$('#press-popup').fadeIn();
	});
	
	$('#press-popup').click( function() {
		$('#press-popup').fadeOut();
	});

	$('#beauty table thead tr td a').click( function() {
		var id = $(this).attr('name').replace(/\D*/, '');
		var tableId;
		var sloganId;

		if(currentSlogan != id) {
			$('#slogan .dax').fadeOut('normal', function() { $(this).css({'left': -10000}); } );
		}

		$('#loader').animate({ width: 0 }, 100, "linear", function() {
			$('#loader .service').hide();
			$('#loader .section').hide();
			$('#loader #section-' + id).show();
			$('#loader').animate({ width: 439 }, 300, "linear", function() {

				sloganId = id;

				if(id == '5') {
					id = '4';
					tableId = '4';
				}
		
				$('#table-' + id + ' tbody, #table-' + id + ' tfoot').slideDown();
				$('#table-' + id + ' tbody, #table-' + id + ' tfoot').hide(1);
				$('#table-' + id + ' tbody, #table-' + id + ' tfoot').fadeIn(1);
				
				if(currentSlogan != sloganId) {
					$('#slogan-' + sloganId).css({'left': 65});
					$('#slogan-' + sloganId).fadeIn('normal');

					currentSlogan = sloganId;
				} else {
					if($('#slogan-' + sloganId).size() > 0 ) {
						if($('#slogan-' + sloganId).get(0).style.display != 'block') {
							$('#slogan #subslogan .dax').fadeOut('normal', function() { $(this).css({'left': -10000}); } );

							$('#slogan-' + sloganId).css({'left': 65});
							$('#slogan-' + sloganId).fadeIn('normal');
						}
					} else {
						$('#slogan #subslogan .dax').fadeOut('normal', function() { $(this).css({'left': -10000}); } );
					}
				}

				$('#beauty table').each( function() {
					tableId = $(this).attr('id').replace(/\D*/, '');

					if(tableId != id) {
						$('#table-' + tableId + ' tbody, #table-' + tableId + ' tfoot').slideUp();
						//$('#table-' + tableId + ' tbody').animate({ height: 0 }, 300);
						$('#table-' + tableId + ' .short-desc').show();
						$('#table-' + tableId + ' .long-desc').hide();
					}
				});
		
				$('#table-' + id + ' .short-desc').hide();
				$('#table-' + id + ' .long-desc').show();

				$('#beauty table tbody tr td a').css({'font-weight': 'normal'});

				currentTableID = parseFloat(id);
			} );
		} );
	});

	$('#beauty table tbody tr td a').click( function() {
		var currentTime = new Date();
		treatShow = false;

		$('#beauty table tbody tr td a').css({'font-weight': 'normal'});
		$(this).css({'font-weight': 'bold'});
		$('#loader .service').css({'left': 0});

		var id = $(this).attr('name').replace(/\D*/, '');
		var idHTML = $(this).html();
		var sect = $(this).parents('table').get(0);
		var sectId = parseFloat(sect.id.replace(/\D*/, ''));
		var tableId;
		var tableWidth = 0;
		var sloganId;

		if(currentSlogan != sectId) {
			$('#slogan .dax').fadeOut('normal', function() { $(this).css({'left': -10000}); } );
		} else {
		
		}

		if(currentTableID == sectId) {
			tableWidth = 439;
		}

		$('#loader').animate({ width: tableWidth }, 100, "linear", function() {
  			if(tableWidth > 0) {
				$('#loader .service').fadeOut( function() { 
					$('#treat-'+treatLast).hide();
				});
			} else {
				$('#loader .service').hide();
			} 


			$('#loader #section-' + sectId).show();
			
			$('#loader').animate({ width: 439 }, 300, "linear", function() {
				$('#loader #service-' + id + ' .base').show();
				$('#loader #service-' + id).fadeIn();


				sloganId = sectId;

				if(sectId == '5') {
					sectId = '4';
					tableId = '4';
				}
		
				if(currentSlogan != sloganId) {
					if($('#subslogan-'+id).size() > 0) {
						$('#subslogan-' + id).css({'left': 65});
						$('#subslogan-' + id).fadeIn('normal');
					} else {
						$('#slogan-' + sloganId).css({'left': 65});
						$('#slogan-' + sloganId).fadeIn('normal');
					}

					currentSlogan = sloganId;
				} else {
					if($('#subslogan-'+id).size() > 0) {
						$('#slogan .dax').fadeOut('normal', function() { $(this).css({'left': -10000}); } );

						$('#subslogan-' + id).css({'left': 65});
						$('#subslogan-' + id).fadeIn('normal');
					} else {
						$('#slogan #subslogan .dax').fadeOut('normal', function() { $(this).css({'left': -10000}); } );
					}
				}
				
 				$('#loader #service-' + id + ' .treatments ul li a').each( function() {
					if($(this).html().trim() == idHTML.trim()) {
						var tN = $(this).attr('name');

						if((id == 13 && tN == 'tr-191') || (id == 22 && tN == 'tr-45'))  {
							$(this).parents('div:first .right').scrollTo( $(this), 100, { easing:'easeOutQuart' } );
							$(this).css({'background': '#514131', 'color': '#ffffff'});
						} else if((id == 15 && tN == 'tr-152') || (id == 23 && tN == 'tr-54'))  {
							$(this).parents('div:first .left').scrollTo( $(this), 100, { easing:'easeOutQuart' } );
							$(this).css({'background': '#514131', 'color': '#ffffff'});
						} else {
							if(tN != 'tr-153' && tN != 'tr-190' && tN != 'tr-45' && tN != 'tr-48' && tN != 'tr-51' && tN != 'tr-54') {
								$(this).parents('div:first').scrollTo( $(this), 100, { easing:'easeOutQuart' } );
								$(this).css({'background': '#514131', 'color': '#ffffff'});
							}
						}
					}
				});

				if(currentTableID != sectId) {
					$('#beauty table').each( function() {
						tableId = $(this).attr('id').replace(/\D*/, '');

						if(tableId != sectId) {
							$('#table-' + tableId + ' tbody, #table-' + tableId + ' tfoot').slideUp();
							$('#table-' + tableId + ' .short-desc').show();
							$('#table-' + tableId + ' .long-desc').hide();
						}
					});

					$('#table-' + sectId + ' .short-desc').hide();
					$('#table-' + sectId + ' .long-desc').show();
				}

				currentTableID = sectId;

 				var endTime = new Date();

				var startTime = currentTime.getTime();
				var endTime = endTime.getTime();

				//alert(endTime - startTime); 
			});

		});

	});

	$('.treatments ul').hover( function() {
	}, function() {
		$(this).children('li').children('a:first').css({'background': '#d8cabb', 'color': '#514131'});
		$(this).parents('div:first').animate({ height: 18 }, 200 );
	});

	$('.treatments ul li a').click( function() {
		var id = $(this).attr('name').replace(/\D*/, '');
		
		if(id == 0 || parseFloat($(this).parents('div:first').height()) == 18) {
			$(this).parents('div:first').animate({ height: 300 }, 50 );
			$(this).parents('div:first').scrollTo( 'a:eq(0)', 100, { easing:'easeOutQuart' } );
			return false;
		}

		var serv = $(this).parents('.service:first').get(0);

		if(treatShow == false) {
			$('#' + serv.id + ' .base').fadeOut( 'normal', function() {
				$('#treat-'+id).fadeIn( 'normal' );
			});
		} else {
			$('#treat-'+treatLast).fadeOut( 'normal', function() {
				$('#treat-'+id).fadeIn( 'normal' );
			});
		}

		treatShow = true;
		treatLast = id;

		var a = $(this);

		$('.treatments ul li a').css({'background': '#d8cabb', 'color': '#514131'});

		$(this).parents('div:first').animate({ height: 18 }, 300, "linear", function() {
			$(this).scrollTo( a, 300, { easing:'easeOutQuart' } );
			a.css({'background': '#514131', 'color': '#ffffff'});
		});
	});

	
	$('.treatments ul.left li a:first, .treatments ul.right li a:first').click( function() {
		$(this).parents('div').children('ul').children('li').children('a').css({'background': '#d8cabb', 'color': '#514131'});
		$(this).parents('div').children('ul').children('li').children('a:first').css({'background': '#514131', 'color': '#ffffff'});
		
		$(this).parents('div:first').animate({ height: 300 }, 50 );
		}, function() {
	});

	if(!$.browser.safari)
		Variables.last = parseFloat($('#slideshow li').size()) - 1;

	$('#slideshow').serialScroll({
		items: 'li',
		prev: ((slug == 'gallery') ? 'a#arrow-prev' : 'a#arrow-next'),
		next: ((slug == 'gallery') ? 'a#arrow-next' : 'a#arrow-prev'),
		offset: 0,
		start: ((slug == 'gallery') ? 0 : Variables.last), //(Variables.last - 1)
		step: ((slug == 'gallery') ? 5 : 1),
		duration: 800,
		force: true,
		stop: true,
		lock: false,
		cycle: true,
		easing: 'easeOutQuart',
		jump: true,
		exclude: ((slug == 'gallery') ? 4 : 0)
	});

	if($.browser.safari)
		Variables.last = parseFloat($('#slideshow li').size()) - 1;
	
	$('a#arrow-up').click( function() {
		if(Variables.scrollPosition > 0)
			Variables.scrollPosition = Variables.scrollPosition - 1;
		$('#titlelist').scrollTo( 'div:eq(' + Variables.scrollPosition + ')', 400, { easing:'easeOutQuart' } );

		return false;
	});
	
	$('a#arrow-down').click( function() {
		if(Variables.scrollPosition < parseFloat($('#titlelist div').size()))
			Variables.scrollPosition = Variables.scrollPosition + 1;
		$('#titlelist').scrollTo( 'div:eq(' + Variables.scrollPosition + ')', 400, { easing:'easeOutQuart' } );

		return false;
	});
	
	if(slug == 'press') {
		$('#slideshow img').click( function() {
			$('#loader').css({ width: 874, left: 0, top: 0, opacity: 0 });

			var img = new Image();
			src = $(this).attr('longdesc');
			src = '/images/phpThumb.php/q=90;800x530;' + src;

			$('#press-popup-image div').html('');

			$(img)
				.load(function () {
					$('#press-popup-image div').append(this);
					$('#press-popup-image')
						.css({ 
							width: (parseFloat(this.width) + 20),
							left: ((844 - parseFloat(this.width)) / 2),
							top: ((600 - parseFloat(this.height)) / 2)
						})
						.fadeIn();
				})
				.error(function () {
				})
				.attr('src', src);

			$('#press-popup-image a').unbind( 'click' );

			$('#press-popup-image a').click( function() {
				$('#loader').css({ width: 0 });

				$('#press-popup-image').fadeOut();

				return false;
			});
		});

		$('.navigator .hidden:eq(' + Variables.last + ')').show();
		$('.thumbs a:eq(0)').addClass('current');
		$('.thumbs').scrollTo( 'a:eq(0)', { easing:'easeOutQuart' } );

		$('.year').html($('.thumbs a:eq(0)').children('span').html());

		$('.navigator .hidden').hide();
		$('.navigator .hidden:eq(' + Variables.last + ')').show();

		if($.browser.safari) {
			setTimeout("$('#slideshow').trigger( 'goto', Variables.last );", 1000);
		}
		
		Variables.current = Variables.last;
	}

	$('.thumbs a').hover( function() {
		$('.navigator .hidden').hide();
		$('.navigator .hidden:eq(' + $(this).attr('name').replace(/\D*/, '') + ')').show();

		$('.year').html($(this).children('span').html());
	}, function() {
	});
	
	$('.thumbs').hover( function() {
	}, function() {
		$('.navigator .hidden').hide();
		$('.navigator .hidden:eq(' + Variables.current + ')').show();

		$('.year').html($('.thumbs a:eq(' + (Variables.last - Variables.current) + ')').children('span').html());
	});

	$('.thumbs a').click( function() {
		$('.navigator .hidden').hide();
		$('.navigator .hidden:eq(' + $(this).attr('name').replace(/\D*/, '') + ')').show();

		$('#slideshow').trigger( 'goto', parseFloat($(this).attr('name').replace(/\D*/, '')) );
	});

	if($('.slug-index').size() > 0) {
		totalFeatureNum = $(".rotation img").size();

		rotationSpeed = 0;
		rotationTimerId = setTimeout("showImage($('.rotation img:eq("+featureNum+")').attr('id'))", rotationSpeed);
	}
	
	if(slug == 'gallery') {
		totalFeatureNum = $(".rotation img").size();

		rotationSpeed = 0;
		rotationTimerId = setTimeout("showImage($('.rotation img:eq("+featureNum+")').attr('id'))", rotationSpeed);
	}

	$('#return-to-gallery').click( function() {
		$('#loader').animate({ width: 439 }, 1000, "easeOutQuart", function() {
			//$('#right-image img').remove();
			$('#left-image').css({'width': 439});
			$('#return-to-gallery').hide();
		});

		return false;
	});

	$('#gallery #slideshow a').click( function() {
		
		$( '#slider' ).remove();
		
		clearTimeout(rotationTimerId);

		if(!finish) {
			$('.rotation img').fadeOut(2000);

			finish = true;
		}

		$('#right .gallery-slogan h2.one').css({'left': -10000});
		$('#right .gallery-slogan h2.two').css({'left': 110});
		$('#right .gallery-slogan .gallery-mini-slogan').hide();

		var img = new Image();
		src = $(this).attr('name');

		$('#right-image').css({'left': 'auto'});
		$('#right-image').css({'right': 0});
		$('#right-image img').css({'position': 'absolute', 'right': 0});
		$('#right-image').animate({width: 0, opacity: 0}, 1000, "easeOutQuart");
		$('#left-image').animate({width: 0, opacity: 0}, 1000, "easeOutQuart", function() {

 			$(img).load(function () {
/* 					$('#loader #left-image img, #loader #right-image img').remove();
    
					$('#left-image').append(this);
					$(this).clone().appendTo('#right-image');

 					halfWidth = parseFloat(($(this).width() + 1) / 2);
					fullWidth = parseFloat($(this).width() + 1);

					if(halfWidth == 437.5) {
						halfWidth = 439;
					}

					$('#loader').css({'width': fullWidth - 1});

					$('#right-image img').css({'margin-left': halfWidth - fullWidth});
					$('#right-image').css({'right': 0});

					$('#loader #left-image').animate({width: halfWidth}, 300);
					$('#loader #right-image').animate({width: halfWidth}, 300);

					//clearTimeout(alertTimerId);
					//alertTimerId = setTimeout("$('#loader').animate({ width: 439 }, 300 )", 5000); */
					$('#loader #left-image img, #loader #right-image img').remove();
    
					$('#right-image').append(this);

					halfWidth = 439;
					fullWidth = parseFloat($(this).width() + 1);
					
					$('#loader').css({'width': fullWidth - 1});

 					if(fullWidth > 500) {
						$('#left-image').append(this);
						//$(this).clone().appendTo('#right-image');

						//$('#right-image img').css({'margin-left': halfWidth - fullWidth});

						$('#left-image').css({'opacity': 0});
						//$('#right-image').css({'right': 0, 'opacity': 0});

						$('#left-image').animate({width: fullWidth, opacity: 1}, 1000, "easeOutQuart", function() {
							rotationTimerId = setTimeout("$('#loader').animate({ width: 439 }, 1000, 'easeOutQuart', function() {$('#left-image').css({'width': 439}); $('#return-to-gallery').hide();})", 5000);
							$('#return-to-gallery').fadeIn('slow');
						});
						//$('#right-image').animate({width: halfWidth, opacity: 1}, 1000, "easeOutQuart");
					} else {
						$('#right-image').css({'left': 0, 'opacity': 0});
						$('#right-image').append(this);

						$('#right-image').animate({width: fullWidth, opacity: 1}, 1000, "easeOutQuart");
					}
				})
				.error(function () {
				})
				.attr('src', src).addClass('image');
			}
		);

		//featureNum = (parseFloat($('#gallery #slideshow a').index(this)));

/* 		rotationSpeed = 5000;
		if(featureNum < totalFeatureNum - 1) {
			featureNum++;
		} else {
			featureNum = 0;
		} */

		return false;
	});

	$('#subscribe').click( function() {
		$('#subscribe-form .text').each( function() {
			$(this).val('');
		});

		$('#subscribe-form .result').hide();
		$('#subscribe-form .form').show();
		$('#subscribe-form').fadeIn();

		return false;
	});

	$('#subscribe-form .close, #subscribe-form .close-one').click( function() {
		$('#subscribe-form').fadeOut();
	});
	
	$('#subscribe-form').submit( function() {
		var error = 0;
		var regEmail = /^[A-Za-z0-9\-_]+(\.[A-Za-z0-9\-_]+)*@([A-Za-z0-9\-_\.]+\.)+[A-Za-z]{2,8}$/i;

		$('#subscribe-form .validate').each( function() {
			if($(this).val().trim() == '') {
				alert('Please complete all required fields');

				$(this).select();
				error = 1;

				return false;
			}
		});

		if(error > 0)
			return false;

		if ($('#email').get(0).value.search(regEmail) == -1) {
			alert('Please enter correct email address');

			$('#email').select();
			return false;
		}

		$.post( document.location.toString() , { 
				action: 'subscribe', 
				name: $('#name').val(),
				email: $('#email').val(),
				phone: $('#phone').val(),
				mobile: $('#mobile').val() }, function(data) {

				if(data == 'ok') {
					$('#subscribe-form .result').show();
					$('#subscribe-form .form').hide();
				} else {
					alert('Please complete all required fields and enter correct data');
				}
		});

		return false;
	});

	var autoplay = 'true';
	var is_autoplay = readCookie('music');
	if(is_autoplay == 0)
		autoplay = "false";
	
	$("#sounddl").jmp3({
		showfilename: "false",
		backcolor: "000000",
		forecolor: "333333",
		width: 80,
		showdownload: "false",
		repeat: "true",
		volume: "50",
		autoplay: autoplay
	});

	if($('.blink').size() > 0) {
		setTimeout("$.blink('blink')", 3000);
	}
	



	if($.browser.msie) {
		$('#menu ul li a').hover( function() {
			if($(this).parent('li').attr('class').match(/current/i))
				return false;
			$(this).css({'background-image': 'url(/share/images/site/menu/'+$(this).attr('class')+'-ie.png)'});
		}, function() {
			if($(this).parent('li').attr('class').match(/current/i))
				return false;
			$(this).css({'background-image': 'url(/share/images/site/menu/'+$(this).attr('class')+'.png)'});
		});
	}
});

jQuery.blink = function () {
	for(var i = 0; i < arguments.length; i++) {

		$('.blink').fadeOut(3000, function() {
			$('.blink').fadeIn(3000);
		});

		setTimeout("$.blink('blink')", 6000);
	}
}
/* 
$(window).resize(function(){
	var bodyHeight = parseFloat($('body').height());
	var bodyWidth = parseFloat($('body').width());

	if(bodyHeight > 638) {
		$('#wrap').css({'top': ((bodyHeight / 2) - 319)});
	}
	if(bodyWidth > 1062) {
		$('#wrap').css({'left': ((bodyWidth / 2) - 531)});
	}
}); */

jQuery.easing.easeOutQuart = function (x, t, b, c, d) {
	return -c * ((t=t/d-1)*t*t*t - 1) + b;
};

String.prototype.trim = function () {
	return this.replace(/^\s*|\s*$/g,"");
};

function showImage(src) {
		/*var img = new Image();

 		$('#loader #right-image').animate({width: 0}, 200);
		$('#loader #left-image').animate({width: 0}, 200, "linear", function() {

 			$(img)
				.load(function () {
					$('#left-image img, #right-image img').remove();
    
					$('#right-image').append(this);

					halfWidth = 439;
					fullWidth = parseFloat($(this).width() + 1);
					
					$('#loader').css({'width': fullWidth - 1});

					if(fullWidth > 500) {
						$('#left-image').append(this);
						$(this).clone().appendTo('#right-image');

						$('#right-image img').css({'margin-left': halfWidth - fullWidth});
						$('#right-image').css({'right': 0});

						$('#loader #left-image').animate({width: halfWidth}, 300);
						$('#loader #right-image').animate({width: halfWidth}, 300);
					} else {
						$('#left-image').append(this);

						$('#left-image img').fadeIn();
					}
				})
				.error(function () {
				})
				.attr('src', src).addClass('image');
			}
		); */
		
		if(slug == 'gallery')
			rotationSpeed = 4000;
		else
			rotationSpeed = 3000;
		
		
		$('.rotation img').fadeOut(2000);

		$('#'+src).fadeIn(2000, function() {
			
			featureNum = ++featureNum % totalFeatureNum;
			
			if(slug == 'gallery') {
				rotationTimerId = setTimeout("showImage($('.rotation img:eq("+featureNum+")').attr('id'))", rotationSpeed);
			} else {
				rotationTimerId = setTimeout("showImage($('.rotation img:eq("+featureNum+")').attr('id'))", rotationSpeed);
			}
		});
		/*
		$(".rotation img").hide();
		
		$(".rotation img:eq("+(featureNum || 0)+")").show();
		(function() {
			
			featureNum = ++featureNum % totalFeatureNum;
			
			if(slug == 'gallery') {
				rotationTimerId = setTimeout("showImage($('.rotation img:eq("+featureNum+")'))", rotationSpeed);
			} else {
				rotationTimerId = setTimeout("showImage($('.rotation img:eq("+featureNum+")'))", rotationSpeed);
			}
		})();
		*/
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

Variables = {
	last: 0,
	current: 0,
	scrollPosition: 0
}