// JavaScript Document

$(window).load(function (){

	$window = $(window);	
	var browserVer=getInternetExplorerVersion(); 
	if (browserVer < 8.0 && browserVer > 0){
		//$("#old_browser").text(browserVer);
		//$("#old_browser").show('fade',500);
	} else {
		
		if(isMobile){
			$("#menu_container").css('position','absolute');
			//$("#icon_container").css('bottom','');
			//$("#icon_container").css('top','400px');
			$("#icon_container").css('position','relative');
			$("#emailForm input.ajax_input").css({
				margin:'0 0 10px 10px'
			});
			$("#email_form_div").css('width','150px');
			$("#email_surname, #email_firstName").css('width','130px');
			//$("#email_btn div.email_bar").css('float','none');
		}
		
		//$("body").ready(function() {
			$("#whole_page").show('fade',300, function(){
				//alert("haha");
				setTimeout(function() {
					//$('#bgLines').css({'background-image':'url(img/bgLines2.png)', 'opacity':'0.4'});
					$('#vineyard').css('background-image', 'url(img/vineyard2.jpg)');
					if(isMobile){
						$("#vineyard").css({ backgroundPosition: '0 100px'});
					}
				}, 500 );
			});
		//});
		
		var event_scroll_top = ($(".event_shadow").height()-$("#event_scroll_item").height())/2;
		//alert(event_scroll_top);
		if(event_scroll_top>0){
			$("#event_scroll").css({top:event_scroll_top-20});
		} else $("#event_scroll").css({top:0});
		
		$(".event_detail").live('click',function(){
			//alert("click");
			var event_id = $(this).attr('name').split("_"),
				target = '';
			var data = 'eid='+event_id[0];
			$.ajax({
				type: "GET",
				data : data,
				url: 'lib/event_detail.php',
				success: function(msg){
					//alert(msg);
					if(event_id[1] == 'more'){
						target = 'res_overlay';
						
					} else target = 'event_overlay';
					document.getElementById(target).innerHTML = msg;
				}
			});
		});
		var titleShow = 'VIP Rooms';
		var scrollState = "stopped";
		var root = $("#eventHostPage .scrollable").scrollable({
			circular: true,
			easing:'easeInOutQuint',
			onBeforeSeek:function(event,i){
				/*switch(i)
				{
					case 0:
					  titleShow = "VIP Rooms";
					  break;
					case 1:
					  titleShow = "Chef's Table";
					  break;
					case 2:
					  titleShow = "Main Dining Room";
					  break;
					default:
					  //titleShow = "sadler";
				}*/
				titleShow = $("#eventHostScroll_item").find("div").eq(i+1).attr("rel");
				$("#img_title").text(titleShow);
				//event.preventDefault();
				//return false;
			}
		}).autoscroll({
			autoplay: false,
			interval: 4000
		 });
		 
		window.api = root.data("scrollable");
		
		api.onBeforeSeek(function(event,i){
			if(scrollState == "stopped"){
				//alert("haha");
				api.stop();
			}
		});
		
		$("#eventHostPage .roomList span").click(function(){
			var selected_class = $(this).attr("class"),
				index = selected_class.split("_")[1];
			//$("#event a").click();
			
			var targetOffset = $("#event_div").offset().top;
			$(window.opera?'html':'html, body').stop(true, false).animate({scrollTop: targetOffset}, 1000);
				
			api.stop();
			scrollState = "stopped";
			$("#eventHostScroll a").removeClass("disabled");
			$("#scrollActBtn").css('background-position','left');
			api.seekTo(index);
			//api.getConf().interval = 3000;
		});
		
		$("#scrollActBtn").click(function(){
			//alert("stop");
			if(scrollState == "play"){
				api.stop();
				scrollState = "stopped";
				$("#eventHostScroll a").removeClass("disabled");
				$(this).css('background-position','left');
			} else {
				api.play();
				scrollState = "play";
				$("#eventHostScroll a").addClass("disabled");
				$(this).css('background-position','right');
			}
		});
		
		$("#enq_btn").overlay({
			top: '10px',
			mask: {
				color: '#000',
				loadSpeed: 200,
				opacity: 0.6
			},
			onBeforeLoad:function(){
				//alert("1");
				if($('#enquireForm_div').is(':hidden')){
					//alert("2");
					/*$("#result_msg").hide('fade',10,function(){
						$("#enquireForm_div").show('fade',10);
					})	*/
					$("#result_msg").css('display','none');
					$("#enquireForm_div .enquire_input").each(function(){
						$(this).attr('value','');
					});
					$("#enquireForm_div").show('fade',10);
				}
			},
		});
		
		$(".floorPlan img").overlay({
			top: '10px',
			mask: {
				color: '#000',
				loadSpeed: 200,
				opacity: 0.6
			},
			onBeforeLoad:function(){
				var img_src = this.getTrigger().attr("src").replace("/tb/","/reg/");
					//overlay_h = this.().height();
				$("#floor_plan_img").attr("src",img_src);
				$("#floor_plan_div").height(0);
				this.getOverlay().height(0);
			},
			onLoad:function(){
				//var overlay_h = this.getOverlay().height(),
				//floor_plan_h = $("#floor_plan_div").height(),
				img_h = $("#floor_plan_div img").height();
					
				//alert(overlay_h+' - '+floor_plan_h + ' - ' + img_h);
				/*if(img_h < overlay_h){
					//alert(floor_plan_h);
					this.getOverlay().height(img_h+5);
				} else $("#floor_plan_div").height(overlay_h);*/
				
				$("#floor_plan_div").height(img_h);
				this.getOverlay().height(img_h);
				
			},
			onClose:function(){
				$("#floor_plan_div").css('height','100%');
				//$("#mies2").height(0);
				//this.().css('height','80%');
			}
		});
		
		/*$(".scrollable").scrollable({
			easing:'easeInOutQuint',
			speed:1000,
			onBeforeSeek: function(){
				var leftEnd = '4px 0 4px rgba(0, 0, 0, 0.6)';
				var rightEnd = '-4px 0 4px rgba(0, 0, 0, 0.6)';
				var openDuration = 500;
				$("#press_shadow_left").stop().animate({'box-shadow':leftEnd}, openDuration);
				$("#press_shadow_right").stop().animate({'box-shadow':rightEnd}, openDuration);
			},
			onSeek: function(){
				var leftStart = '1px 0px 1px rgba(0, 0, 0, 0.9)';
				var rightStart = '-1px 0 1px rgba(0, 0, 0, 0.9)';
				var closeDuration = 200;
				$("#press_shadow_left").stop().animate({'box-shadow':leftStart}, closeDuration);
				$("#press_shadow_right").stop().animate({'box-shadow':rightStart}, closeDuration);
			}
		}).navigator();*/
		
		$contentHeight = $("#page_container .section .div_content").height();
		var margin_bottom = $contentHeight >= $window.height() ? $contentHeight + $window.height() * 0.5 : $window.height() * 0.5;
		//var margin_bottom = $window.height() * 0.5;
		$("#page_container .section:not(:last)").css('margin-bottom',margin_bottom+'px');
		
		var div_content_size = $("#news .div_content").size();
		$("#news .div_content").each(function(){
			$(this).css('z-index',div_content_size+10);
			div_content_size -= 1;
		});
		
		window.lang = new jquery_lang_js();
		window.lang.run();
		
		resizeMenu();
		
		$("#press img[title]").tooltip({
			position: "bottom right",
			offset: [-100, -80]
		});
		
		$("#icon_container div[title]").tooltip({
			position: "bottom right",
			offset: [-20, -80]
		});
		
		/*$("#email_btn").mouseenter(function(event){
			event.preventDefault();
			$(this).stop().animate({width:350},300);
		}).mouseleave(function(event){
			event.preventDefault();
			$(this).stop().animate({width:40},300);;	
		});*/
		$("#email_click").click(function(){
			//alert($(this).width());
			if(Math.floor($("#email_btn").width()) <= 44){
				if(isMobile){
					//$("#email_btn").stop().animate({width:200,height:200},300);
					$("#email_btn").stop().animate({width:200},150,function(){
						$("#email_btn").animate({height:150},150);
					});
				} else $("#email_btn").stop().animate({width:450},300);
				
				$(this).css({'background-position':'0px -44px'});
			} else {
				if(isMobile){
					$("#email_btn").stop().animate({width:44,height:44},300);
				} else $("#email_btn").stop().animate({width:44},300);
				$(this).css({'background-position':'0px 0px'});
			}
		});
		/*$("#email_btn").hover(function(event){
			event.preventDefault();
			$(this).animate({width:200},500);
		})*/
		/*$("#email_subButton").click(function(){
			var query = '';
			$("#email_btn .ajax_input").each(function(){
				//alert($(this).attr('name'));
				query += $(this).attr('name') + '=' + $(this).attr('value') + '&';
			});
			//alert(query);
			query += "dummy=dummy";
			$.ajax({
				type: "GET",
				url: 'lib/save_email.php',
				data : query,
				success: function(msg){
					alert(msg);
				},
				error : function(xhr) {
					alert("Failed");
				}
			});
		});*/
		if ($("#event_navi").length>0) {
			$("#event_navi").width($("#event_navi a").length * 61);
		}
		/*var currentTime = new Date()
		var month = currentTime.getMonth() + 1
		var day = currentTime.getDate()
		var year = currentTime.getFullYear()
		//$( "#datepicker" ).datepicker( "setDate" , event_date[0] );
		$( "#datepicker" ).val(year+"-"+month+"-"+day);*/
		
		/*$("#bg02").css('height',$(document).height());*/
		//$("#bgLines").css('height',$(document).height());
		
		$('#changeButton').live('click',function(){
			$("#show_submit").hide('fade',200,function(){
				$("#res_form").show('fade',200);
			})
		});
		
		$("#newResButton").live('click',function(){
			$("#show_result").hide('fade',200,function(){
				
				$('.ajax_input').each(function(){
					$(this).attr('value','');
				});
				$("#event_title").text('');
				document.getElementById('show_timeSlot_content').innerHTML = all_time_slot;
				$("#specialRequest").val($("#specialRequest").attr('placeholder'));
				$("#event_id").val(0);
				$("#check_box").attr('checked','checked');
				$("#res_form").show('fade',200);
			})
		});
		
		$('#cancelButton').live('click',function(){
			$("#show_submit").hide('fade',200,function(){
				
				$('.ajax_input').each(function(){
					$(this).attr('value','');
				});
				$("#event_title").text('');
				document.getElementById('show_timeSlot_content').innerHTML = all_time_slot;
				$("#specialRequest").val($("#specialRequest").attr('placeholder'));
				$("#event_id").val(0);
				$("#check_box").attr('checked','checked');
				$("#res_form").show('fade',200);
			})
		});
		
		$('#confirmButton').live('click',function(){
			$("#res_load_gif").show('fade', 200);
			var query = '',email_query='';
			var check_value = 0;
			var show_mc_msg,show_res_msg = '';
			$("#reservations_div .ajax_input").each(function(){
				if($(this).hasClass('check_box')){
					//alert($(this).attr('checked'));
					
					if($(this).attr('checked') =='checked')
						check_value = 1;
					query += $(this).attr('name') + '=' + check_value;
									
				} else {
					//alert($(this).attr('value'));
					var get_value = $(this).attr('value');
					if(get_value == $(this).attr('placeholder')){
						get_value = 'no_request';
					}
					if($(this).attr('name') == "event_title"){
						get_value = $(this).text();
						if(get_value == ""){
							get_value=0;
						}
					}
					query += $(this).attr('name') + '=' + get_value + '&';
				}
			});
			
			query += '&clientEmail=sadler';
			//query += '&clientEmail=maggieng@urbanmonk.hk';
			//alert(query);
			$.ajax({
				type: "GET",
				url: 'http://reservations.houyea.com/make_reservation.php',
				data : query,
				success: function(msg){
					//alert(msg);
					if(msg=='success'){
						show_res_msg = "<br />Thank you for your reservation request. An email has been sent to our service staff and we will follow up with you shortly to confirm your reservation.<br /><br />If you have any questions or concern, please call us at +86-(0)10-6559 1399 or email us at <a href='mailto:info.sadler@sadler-beijing.com'>info.sadler@sadler-beijing.com</a>.<br />";
					} else {
						show_res_msg = "We're sorry, it appears that there was a problem with your submission. Please try again later. If the problem persists, please email us at <a href='mailto:houyea.support@houyea.com?subject=Reservation%20System%20Error'>houyea.support@houyea.com</a>. <br />";
					}
					/*$('#show_submit').hide('fade', 200,function(){
						$("#show_result").show('fade',200);
					});*/
				},
				error : function(jqXHR, textStatus, errorThrown) {
					//alert("Failed");
					show_res_msg = "We're sorry, it appears that there was a problem with your submission. Please try again later. If the problem persists, please email us at <a href='mailto:houyea.support@houyea.com?subject=Reservation%20System%20Error'>houyea.support@houyea.com</a>. <br />";
					/*alert("readyState: "+jqXHR.readyState+"\nstatus: "+jqXHR.status);
    				alert("responseText: "+textStatus);
					alert(errorThrown);*/

					/*$('#show_submit').hide('fade', 200,function(){
						$("#show_result").show('fade',200);
					});*/
				},
				
				complete: function(){
					if(check_value == 1){
						$("#reservations_div .email_input").each(function(){
							email_query += $(this).attr('name') + '=' + $(this).attr('value')+'&';
						})
						email_query += 'dummy=dummy';
						//alert(email_query);
						$.ajax({
							type: "GET",
							url: 'lib/mcapi_listSubscribe.php',
							data : email_query,
							success: function(msg){
								/*if(msg=="success"){
									//alert('Subscribed - look for the confirmation email!\n');
									document.getElementById('show_result').innerHTML += 'Subscribed - look for the confirmation email!\n';
								} else document.getElementById('show_result').innerHTML += msg;
								$("#mc_error_msg").show('fade',200);*/
								var mc_msg = msg;
								if(msg=="success"){
									$.ajax({
										type: "GET",
										url: 'lib/save_email.php',
										data : email_query,
										success: function(msg){
											
										},
										error : function(xhr) {
											
										}
									});
									show_mc_msg = 'Also, we have subscribed you to our email newsletter. Please look for the confirmation email.';
								} else 
								show_mc_msg = mc_msg;
								
								document.getElementById('show_result_info').innerHTML = show_res_msg + "<br />" + show_mc_msg;
							}
						})
					} else document.getElementById('show_result_info').innerHTML = show_res_msg;
					$('#show_submit').hide('fade', 200,function(){
						$("#show_result").show('fade',200);
						$("#res_load_gif").hide('fade', 200);
					});
				},
			});
		});
		
		var clicked = 0;
		
		$( "#datepicker" ).datepicker({
			dateFormat: 'yy-mm-dd',
			minDate: 0,
			dayNamesMin: ['S', 'M', 'T', 'W', 'T', 'F', 'S'],
			maxDate: "+1M",
			showAnim: 'slideDown',
			beforeShow: function(event, ui){
				/*alert('haha');
				$('#ui-datepicker-div').append('<div>Test</div>');
				alert($('#ui-datepicker-div').html());*/
				$( "#datepicker" ).val('');
				if($("#show_event_content").is(':hidden')){
					$.ajax({
						type: "GET",
						url: 'lib/show_event.php',
						success: function(msg){
							//alert(msg);
							document.getElementById('show_event_content').innerHTML = msg;
							$("#show_event_content").show('blind', 400, function(){
								$("#close_event").show();
							});
							
							$("a[rel]").overlay({
								top: '3%',
								mask: {
									color: '#000',
									loadSpeed: 200,
									opacity: 0.6
								},
							});
						}
					});
				}
			},
			onClose: function(dateText, inst){
				//$("#show_event_content").hide('blind', 500);
				//setTimeout(function() {
				//$("#show_event_content").delay(200).hide('blind', 200);
				//}, 100 );
				$( "#datepicker" ).blur();
				if(dateText != ''){
					$("#close_event").click();
					
				}
			}
		});
		
		var time_slot = ['11:30','12:00','12:30','13:00','13:30','18:00','18:30','19:00','19:30','20:00','20:30','21:00','21:30','22:00'];
		var all_time_slot = '';
		for(i=0;i<=13;i++){
			all_time_slot += '<div class="timeSlot">'+time_slot[i]+'</div>';
		}
		$(".each_event").live('click',function(e){
			//alert($(this).attr('name'));
			var event_date = $(this).attr('name').split("_");
			var event_title = $(this).attr('id').split("_");
			$( "#datepicker" ).datepicker( "setDate" , event_date[0] );
			$( "#reserveTime" ).val(event_date[1]);
			//$( "#reservedMin" ).val(event_date[2]);
			
			
			/*var startTime = event_date[1],
				endTime = event_date[2],
				timeSlot = '',
				startIndex = 0;
				endIndex = 0;
			
			startIndex = time_slot.indexOf(startTime);
			endIndex = time_slot.indexOf(endTime);
			
			if(startIndex < 0)startIndex=0;
			if(endIndex < 0)endIndex=13;
			for(i=startIndex;i<=endIndex;i++){
				timeSlot += '<div class="timeSlot">'+time_slot[i]+'</div>';
			}*/
			var time = event_date[3].split(","),
				timeSlot = '';
			for(var i=0; i<time.length; i++){
				timeSlot += '<div class="timeSlot">'+time[i]+'</div>';
			}
			
			document.getElementById('show_timeSlot_content').innerHTML = timeSlot;
			
			$("#event_title").html(event_title[1]);
			$("#event_id").val(event_title[0]);
			$("#show_event_content").hide('blind', 200);
			$("#close_event").hide();
			//e.stopPropagation();
		});
		
		$("#close_event").live('click',function(){
			$("#show_event_content").hide('blind', 200);
			$("#close_event").hide();
		});
		
		$('#dialog-overlay').click(function () {
        	$('#dialog-overlay, #show_timeSlot_content').hide();      
			return false;
		});

		$('#reserveTime').click(function() {
			if ($('#show_timeSlot_content').is(':hidden'))
            popup();
		});
		
		$('.timeSlot').live('click',function() {
			//alert("click");
			$('#reserveTime').val($(this).text());
			if (!$('#show_timeSlot_content').is(':hidden')){
				$('#show_timeSlot_content').hide('blind', 400);
				$('#dialog-overlay').hide();      
			}
		});
		
		$(".res_btn").click(function () {
			//alert($(this).parent().parent().prev().text());
			
			if ($('#res_form').is(':hidden')){
				if($('#show_result').is(':visible')){
					$('#show_result').hide('fade',200,function(){
						$('#res_form').show('fade',200);
					});
				} else if($('#show_submit').is(':visible')){
					$('#show_submit').hide('fade',200,function(){
						$('#res_form').show('fade',200);
					});
				}
			}
			
			var event_data = $(this).attr('id').split(";;;");
			//var event_title = $($(this).attr('id')).text();
			//var event_id = $($(this).attr('id')).attr('id').split("_");
			$("#datepicker").datepicker( "setDate" , event_data[2] );
			$("#event_title").text(event_data[1]);
			$("#event_id").val(event_data[0].substr(1));
			var time = $(this).attr('name').split(","),
				time_slot = '';
			$("#reserveTime").val(time[0]);
			for(var i=0; i<time.length; i++){
				time_slot += '<div class="timeSlot">'+time[i]+'</div>';
			}
			document.getElementById('show_timeSlot_content').innerHTML = time_slot;
			$("#reservations a").click();
        })
		
		$(".calBtn").change(function () {
			//alert("change");
			$("#event_title").text('');
			document.getElementById('show_timeSlot_content').innerHTML = all_time_slot;
			$("#event_id").val(0);
        })
		
		/*$("#reservation").click(function(){
			var query = '';
			$("#reservations_div .ajax_input").each(function(){
				if($(this).hasClass('check_box')){
					//alert($(this).attr('checked'));
					var check_value = 0;
					if($(this).attr('checked') =='checked')
						check_value = 1;
					query += $(this).attr('name') + '=' + check_value;
									
				} else {
					//alert($(this).attr('value'));
					query += $(this).attr('name') + '=' + $(this).attr('value') + '&';
				}
			});
			//alert(query);
			$.ajax({
				type: "GET",
				url: 'lib/make_reservation.php',
				data : query,
				success: function(msg){
					alert(msg);
				}
			});
		});*/
		


		$('[placeholder]').focus(function() {
		  var input = $(this);
		  if (input.val() == input.attr('placeholder')) {
			input.val('');
			input.removeClass('placeholder');
		  }
		}).blur(function() {
		  var input = $(this);
		  if (input.val() == '' || input.val() == input.attr('placeholder')) {
			input.addClass('placeholder');
			input.val(input.attr('placeholder'));
		  }
		}).blur();

		// binds form submission and fields to the validation engine
		jQuery("#reservationForm").validationEngine();
		jQuery("#emailForm").validationEngine();
		jQuery("#enquireForm").validationEngine();
		
		$(".have_sub").click(function (event){
			var sub_menu_item = $(this).next("div.sub_menu_container");
			event.preventDefault();
			if(sub_menu_item.is(':hidden')){
				sub_menu_item.animate(
				{
					height: 'toggle',
					opacity: 'toggle'
				}, 200)
			}
			/*if(sub_menu_item.is(':hidden')){
				sub_menu_item.show("blind", 500);
			} else if (sub_menu_item.is(':visible')){
				sub_menu_item.hide("blind", 500);
			}*/
		});
		
		
		
		$("#event_scroll").scrollable({
			easing:'easeInOutQuint',
			speed:1000,
			onBeforeSeek: function(){
				var leftEnd = '4px 0 4px rgba(0, 0, 0, 0.6)';
				var rightEnd = '-4px 0 4px rgba(0, 0, 0, 0.6)';
				var openDuration = 500;
				$("#event_shadow_left").stop().animate({'box-shadow':leftEnd}, openDuration);
				$("#event_shadow_right").stop().animate({'box-shadow':rightEnd}, openDuration);
			},
			onSeek: function(){
				var leftStart = '1px 0px 1px rgba(0, 0, 0, 0.9)';
				var rightStart = '-1px 0 1px rgba(0, 0, 0, 0.9)';
				var closeDuration = 200;
				$("#event_shadow_left").stop().animate({'box-shadow':leftStart}, closeDuration);
				$("#event_shadow_right").stop().animate({'box-shadow':rightStart}, closeDuration);
			}
		}).navigator({
			navi:"#event_navi"	
		});
		
		/*$("#event_div .navi a").click(function(){
			var $self = $(this);
			var width_move = -680 * $self.attr('href');
			//alert(width_move);
			$("#event_div .navi a").attr('class','');
			$self.attr('class','active');
			
			/*var myAnimate = function() {
				$('.event_shadow').show().queue(function() { $(this).dequeue(); } );
				$("#event_scroll_item").stop().delay(500).animate({'left':width_move},500).queue(function() { $(this).dequeue(); } );
				$('.event_shadow').hide().queue(function() { alert('close'); } );
			};
			myAnimate();
			var leftStart = '1px 0px 1px rgba(0, 0, 0, 0.9)';
			var leftEnd = '4px 0 4px rgba(0, 0, 0, 0.6)';
			var rightStart = '-1px 0 1px rgba(0, 0, 0, 0.9)';
			var rightEnd = '-4px 0 4px rgba(0, 0, 0, 0.6)';
			var openDuration = 500;
			var slideDuration = 1000;
			var closeDuration = 200;
			var easeType = "easeInOutQuint";

			$(".event_shadow").stop().animate({'opacity':1}, 0);
			$("#event_div #event_shadow_left").stop().animate({'box-shadow':leftEnd}, openDuration);
			$("#event_div #event_shadow_right").stop().animate({'box-shadow':rightEnd}, openDuration);
			$("#event_scroll_item").stop().delay(openDuration/4).animate({'left':width_move},slideDuration,easeType,function(){
				//$(".event_shadow").stop().delay(200).animate({'opacity':0}, 500);
				$("#event_div #event_shadow_left").stop().delay(0).animate({'box-shadow':leftStart}, closeDuration);
				$("#event_div #event_shadow_right").stop().delay(0).animate({'box-shadow':rightStart}, closeDuration);
			});
			return false;
		});*/
		
		
		$(".expand_part").mouseover(function (){
			var content = $(this).prev('div');
			if(content.is(':hidden')){
				$(this).css({'background-position':'0px -40px'});
			}else{
				$(this).css({'background-position':'0px -120px'});
			}
		});
		$(".expand_part").mouseout(function (){
			var content = $(this).prev('div');
			if(content.is(':hidden')){
				$(this).css({'background-position':'0px 0px'});
			}else{
				$(this).css({'background-position':'0px -80px'});
			}
		});
		$(".expand_part").click(function (){
			var content = $(this).prev('div');
			var obj = $(this);
			
			
			if(content.is(':visible')){
				var target_herf = "#"+$(this).closest(".section").attr('id')
				$('.menu_item a[href='+target_herf+']').click();
			}
			content.toggle('blind', 500, function(){ 
				if(content.is(':hidden')){
					//alert('closed');
					obj.css({'background-position':'0px 0px'});
				}else{
					//alert('opened');
					obj.css({'background-position':'0px -80px'});
				}
			});
			
			/*if(content.is(':hidden')){
				var height = content.find('div').height();
				content.show('blind', 500, function(){ obj.css({'background-position':'0px -80px'});
				});
			} else {
				content.hide('blind', 500, function(){ obj.css({'background-position':'0px 0px'});
				});
			}*/
		});
		
		
		$(".wine_expand_part").click(function (){
			var content = $(this).prev('div');
			var part = $(this);
			
			if(content.height() <= 200){
				content.stop().animate({'height':content.find('img').height()},200);
				//$(this).find('img').attr('src','img/contractBtn.png');
				$(this).css({'background-position':'0px -80px'});
			} else {
				content.stop().animate({'height':'200'},200,function(){
					part.css({'background-position':'0px 0px'});
				});
				//$(this).find('img').attr('src','img/expandBtn.png');
			}
		});
		
		/*$(".wine_expand_part").mouseover(function (){
			var content = $(this).prev('div');
			if(content.height() <= 200){
				$(this).css({'background-position':'0px -40px'});
			}else{
				$(this).css({'background-position':'0px -120px'});
			}
		});
		$(".wine_expand_part").mouseout(function (){
			var content = $(this).prev('div');
			if(content.height() > 200){
				$(this).css({'background-position':'0px -80px'});
			}else{
				$(this).css({'background-position':'0px 0px'});
			}
		});*/
		$(".wine_expand_part").mouseenter(function() {
			var content = $(this).prev('div');
			if(content.height() <= 200){
				$(this).css({'background-position':'0px -40px'});
			}else{
				$(this).css({'background-position':'0px -120px'});
			}
		}).mouseleave(function() {
			var content = $(this).prev('div');
			if(content.height() > 200){
				$(this).css({'background-position':'0px -80px'});
			}else{
				$(this).css({'background-position':'0px 0px'});
			}
		});

		
		
		$("#mc_error_msg").click(function(){
			$(this).hide('fade',200);
		});
		
		/*$(".note_expand").click(function(){
			var note_content = $(this).prev('div');
			var content_height = note_content.find('.content').height();
			
			if(note_content.height() <= 80){
				note_content.stop().animate({'height':content_height},200);
				//$(this).parent("div.news_note").find(".content").stop().animate({'height':'150px'},300);
				$(this).find('img').attr('src','img/contractBtn.png');
			} else {
				note_content.stop().animate({'height':'80px'},200);
				//$(this).parent("div.news_note").find(".content").stop().animate({'height':'58px'},300);
				$(this).find('img').attr('src','img/expandBtn.png');
			}
		});*/
		
		//$.address.history(false).strict(false).wrap(true).init(function(event) {
			
			//$('.menu_item a[href*=#]').address(); 
		$('.menu_item a[href*=#]').address(function() {
			$hash = $(this).attr('href').replace("#",'');
			return $hash.replace("_div",'');
		});
		var current_id = 'concept_div';
		$.address.change(function(event){
			var target_page = event.value;
			//alert(event.value);
			if(target_page == '/'){
				target_page = '#concept_div';
			}
			target_page = target_page.substring(1)+"_div";
			//alert(target_page);
			//var target_id = $(this).parent().attr("id");
			var target_id = target_page;
			$('.sub_menu_item a[href*=#]').attr('class','not_clicked');
			
			//alert('current: '+current_id);
			//alert('target: '+target_id);
			//console.log('current: '+current_id);
			//console.log('target: '+target_id);
			//if(target_id != current_id){
				$("#"+current_id.replace("_div","")).find('a').css({color:''}); //previous
				$("#"+target_id.replace("_div","")).find('a').css({color:'#fff4e6'}); //selected
			//}
			current_id = target_id;
			//if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname){
			var $target = $('#'+target_id);
			//$target = $target.length && $target || $('[name=' + this.hash.slice(1) +']');
			if ($target.length) {
				//alert("scroll");
				//$.address.value('#'+current_id);
				//$.address.hash(current_id);
				var targetOffset = $target.offset().top;
				//alert(targetOffset);
				$(window.opera?'html':'html, body').stop(true, false).animate({scrollTop: targetOffset}, 1000);
				return false;
			}
			//}
		});
		
		/*$('.menu_item a[href*=#]').live('click',function() {
			
			var target_id = $(this).parent().attr("id");
			$('.sub_menu_item a[href*=#]').attr('class','not_clicked');
			
			if(target_id != current_id){
				$("#"+target_id).find('a').css({color:'#fff4e6'}); //selected
				$("#"+current_id).find('a').css({color:''}); //previous
			}
			
			$("#"+target_id).find('a').css({opacity:.6})
			$("#"+target_id).find('a').css('textShadow','#7F7F7F 1px 1px');
			$("#"+current_id).find('a').css({opacity:''})
			$("#"+current_id).find('a').css('textShadow','');
			
			if($("#"+current_id).hasClass('have_sub')){
				var sub_menu_item = $("#"+current_id).next("div.sub_menu_container");
				if(current_id != target_id){
					if(sub_menu_item.is(':visible')){
						sub_menu_item.animate(
						{
							height: 'toggle',
							opacity: 'toggle'
						}, 100)
					}
				}
			}
			if($("#"+target_id).hasClass('have_sub')){
				var sub_menu_item = $("#"+target_id).next("div.sub_menu_container");
				sub_menu_item.find(".sub_menu_item a:first").attr('class','clicked');
			}
		
			current_id = target_id;
			if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'')
			&& location.hostname == this.hostname) {
				var $target = $(this.hash);
				$target = $target.length && $target || $('[name=' + this.hash.slice(1) +']');
				if ($target.length) {
					//$.address.value('#'+current_id);
					//$.address.hash(current_id);
					var targetOffset = $target.offset().top; 
					$(window.opera?'html':'html, body').animate({scrollTop: targetOffset}, 1000,function(){
						//location.hash = '#'+current_id;
					});
					return false;
				}
			}
		});*/
		
		/*$(".menu_item").click(function (){
			var clicked = $(this);
			var target_id = clicked.attr('id');
			if(target_id != current_id){
				$('.sub_menu_item a[href*=#]').attr('class','not_clicked');
				
				if($("#"+current_id).hasClass('have_sub')){
					var sub_menu_item = $("#"+current_id).next("div.sub_menu_container");
					if(sub_menu_item.is(':visible')){
						sub_menu_item.animate(
						{
							height: 'toggle',
							opacity: 'toggle'
						}, 100)
					}
				}
				if($("#"+target_id).hasClass('have_sub')){
					var sub_menu_item = $("#"+target_id).next("div.sub_menu_container");
					sub_menu_item.find(".sub_menu_item a:first").attr('class','clicked');
				}
				
				$("#"+target_id).css({opacity:.6})
				$("#"+target_id).css('textShadow','#7F7F7F 1px 1px');
				$("#"+current_id).css({opacity:''})
				$("#"+current_id).css('textShadow','');
				
				if(clicked.hasClass('ajax_item')){
					$("#fade_part").fadeOut(200, function (){
						$.ajax({
							type: "GET",
							url: target_id+".php",
							success: function(msg){
								//alert( "Data Saved: " + msg );
								document.getElementById("fade_part").innerHTML=msg;
								
								$("#fade_part").fadeIn(400, function(){
									$("#bg01").css('height',$(document).height()+80);
									$("#bg02").css('position','fixed');
									$("#bg02").css('height',$(document).height()+80);
									$("#bg02").css('position','absolute');
								});
								$window.scrollTop(0);
								if(target_id == "buzz"){
									news_note();
								} else if(target_id == "menus"){
									$("input[rel]").overlay();
								}
							}
						});
					});
				}
				current_id = target_id;
				
			}
		});*/
		
		$('.sub_menu_item a[href*=#]').live('click',function() {
			
			$('.sub_menu_item a[href*=#]').attr('class','not_clicked');
			$(this).attr('class','clicked');
			
			if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'')
			&& location.hostname == this.hostname) {
				var $target = $(this.hash);
				$target = $target.length && $target || $('[name=' + this.hash.slice(1) +']');
				if ($target.length) {
					var targetOffset = $target.offset().top; 
					$(window.opera?'html':'html, body').animate({scrollTop: targetOffset}, 500);
					return false;
				}
			}
		});
		
		
		/*$(window).bind('scroll',function(e){
			parallaxScroll();
		});
		 
		function parallaxScroll(){
			var scrolled = $(window).scrollTop();
			var $window =  $(window);
			$('#bg01').css('top', -(scrolled*0.1)+'px');
			$('#bg02').css('top', -(scrolled*0.3)+'px');
			$('#bg03').css('top', -(scrolled*0.3)+'px');
			$('#dandelion').css('top', -(scrolled*0.2)+'px');
			//$('#page_container').css('top', -(scrolled*0.9)+'px');
			//$('#page_container').stop().animate({ top : 0 - scrolled}, 0);

		}*/
		
		$(window).scroll(function() {
		// Scroll the background at var speed
		// the yPos is a negative value because we're scrolling it UP!								
		/*var yPos = -Math.round($(window).scrollTop()*0.1); 
		var coords = '20%'+ yPos + 'px';
		$("#bgLines").css({ backgroundPosition: coords });
		*/
		var bottom_height = $(document).height() - $(window).height();
		var yPos2 = ($(window).scrollTop()/bottom_height)*300;
		//$("#vineyard").css({ backgroundPosition: '0 '+ yPos2 + 'px'});
		
		if(!(isMobile)){
			$("#vineyard").css({ backgroundPosition: '0 '+ yPos2 + 'px'});
		} else $("#vineyard").css({ backgroundPosition: '0 100px'});
		
		$('.formError').fadeTo("fast", 0.3, function() {
			$(this).remove();
		});
		
		if($("#news").length > 0){
			var $news = $("#news"),
				offsetCoords = $news.offset(),
				topOffset = offsetCoords.top;
			
			if (($window.scrollTop() + $window.height()) > (topOffset) && ( (topOffset + $news.height()) > $window.scrollTop())){
				$(".news_note").each(function(){
					var $self = $(this),
					offsetCoords = $self.offset(),
					topOffset = offsetCoords.top;
					
					var percentage = ((topOffset-$window.scrollTop()) / $window.height())*100;
					
					if(percentage < 110){
						if(percentage < 30)
							percentage = 60-percentage;
						if(percentage < 100)
							$self.css('left', percentage+'%');
					}
					
					//$self.css('left', '50%');
					
				});
			}
		}
		
		
		if($("#press").length > 0){
			var $news = $("#press"),
				offsetCoords = $news.offset(),
				topOffset = offsetCoords.top;
			
			if (($window.scrollTop() + $window.height()) > (topOffset) && ( (topOffset + $news.height()) > $window.scrollTop())){
				$(".note").each(function(){
					var $self = $(this),
					offsetCoords = $self.offset(),
					topOffset = offsetCoords.top;
					
					var percentage = ((topOffset-$window.scrollTop()) / $window.height())*100;
					
					if(percentage < 100 && percentage > 0){
						if($self.hasClass('col1')){							
							//$self.text(percentage);
							$self.css('left', '0%');
							/*if(percentage >= 0){
								//$self.css('left',(Math.round(((100-percentage)*5)/10))+'%');
							} else 
								$self.css('left', '0%');*/
						} else if($self.hasClass('col3')){
							//$self.css('left', Math.min(-percentage, 50)+'%');
							//$self.text(percentage);
							$self.css('left', '50%');
							/*if(percentage >= 0){
								//$self.css('left',Math.round(percentage)+'%');	
								//$self.css('left',(Math.round(((percentage-10)*5)/10))+'%');
							} else 
								$self.css('left','0%');*/
						}
					}
				});
			}
		}
		
	});
	
	var input_hash = location.hash;
	//alert(input_hash);
	if(input_hash != ''){
		//alert(input_hash.split('#')[2]);
		//var input_div = input_hash.split('#')[2];
		//alert(input_hash);
		//var $target = $(input_hash.replace('/','')+"_div");
		var target_id = input_hash.split('?')[0].replace('/','')+"_div";
		var $target = $(target_id);
		//alert($target.html());
		if ($target.length){
			var targetOffset = $target.offset().top;
			//alert(targetOffset);
			$(window.opera?'html':'html, body').animate({scrollTop: targetOffset}, 1000);
		}
	}
	
	}
	
	
});

$(window).resize(function() {
  	//$("#page_container").css({width:$(document).width()-300});
	$window = $(window);
	$contentHeight = $("#page_container .section .div_content").height();
	var margin_bottom = $contentHeight >= $window.height() ? $contentHeight + $window.height() * 0.5 : $window.height() * 0.5;
	//var margin_bottom = $window.height() * 0.5;
	$("#page_container .section:not(:last)").css('margin-bottom',margin_bottom+'px');
	resizeMenu();
	
	/*var menu_height = $("#menu_logo").height()+$("#menu_item_container").height()+$("#icon_container").height()+30;
	$("#concept").text(menu_height+'_'+$(window).height()+'_'+$('#menu_item_container').css('font-size'));
	var originalFontSize = parseInt($('#menu_item_container').css('font-size').replace("px", ""));
	if(menu_height > $(window).height() && originalFontSize>8){
		//alert("haha");
		$('#menu_item_container').css('font-size', originalFontSize-1+'px');
	} else if(menu_height+30 < $(window).height()){
		if(originalFontSize < 14)
			$('#menu_item_container').css('font-size', originalFontSize+1+'px');
	}*/
	/*if($(".first_note").length > 0)
		$(".first_note").css("margin-top",$(window).height()/3);
	if($(".last_note").length > 0)
	$(".last_note").css("margin-bottom",$(window).height()/2);*/
});

function getInternetExplorerVersion()
// Returns the version of Windows Internet Explorer or a -1
// (indicating the use of another browser).
{
   var rv = -1; // Return value assumes failure.
   if (navigator.appName == 'Microsoft Internet Explorer')
   {
      var ua = navigator.userAgent;
      var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
      if (re.exec(ua) != null)
         rv = parseFloat( RegExp.$1 );
   }
   return rv;
}

function resizeMenu(){
	
	var menu_height = $("#menu_logo").height()+$("#menu_item_container").height()+$("#icon_container").height()+30;
	//var originalFontSize = parseInt($('#menu_item_container').css('font-size').replace("px", ""));
	var originalFontSize = 14;
	var height_arr = new Array(), i=0;
	//$("#concept").text(menu_height+'_'+$(window).height()+'_'+$('#menu_item_container').css('font-size'));
	while (originalFontSize >= 8){
		$('#menu_item_container').css('font-size', originalFontSize+'px');
		height_arr[i] = $("#menu_item_container").height();
		i += 1;
		originalFontSize -= 1;
	}
	
	
	var need_height = $(window).height()-$("#menu_logo").height()-$("#icon_container").height()-30-150;
	height_arr.push(need_height);
	height_arr.sort( function (a,b) { return a-b });
	$('#menu_item_container').css('font-size', 7+height_arr.indexOf(need_height)+'px');
	
}

//Popup dialog
function popup() {
         
    // get the screen height and width 
    var maskHeight = $(document).height(); 
    var maskWidth = $(window).width();
     
    // calculate the values for center alignment
    //var dialogTop =  (maskHeight/3) - ($('#dialog-box').height()); 
    //var dialogLeft = (maskWidth/2) - ($('#dialog-box').width()/2);
     
    // assign values to the overlay and dialog box
    $('#dialog-overlay').css({height:maskHeight, width:maskWidth}).show();
	$('#show_timeSlot_content').show('blind', 400);
    //$('#dialog-box').css({top:dialogTop, left:dialogLeft}).show();
     
    // display the message
   //$('#dialog-message').html(message);
             
}
/*
function news_note(){
	var $window = $(window);
	var init_height = $(".news_note").height();
	$(".first_note").css("margin-top",$window.height()/3);
	$(".last_note").css("margin-bottom",$window.height()/2);
	//alert(Math.ceil($window.scrollTop()/200));
	
	$(".news_note").each(function(){
		var $self = $(this),
		offsetCoords = $self.offset(),
		topOffset = offsetCoords.top;
		
		var percentage = ((topOffset-$window.scrollTop()) / $window.height())*100;
		
		if(percentage < 30)
			percentage = 60-percentage;
		$self.css('left',percentage+'%');
	});
	
	
	
	//$(".news_note").each(function(){
		//alert($(this).offset().top);
		//$window.scroll(function (event){
				/*$(".news_note").animate({'right':'0px'},300);
			} else $(".news_note").animate({'right':'-80%'},300);
			
		//});
	//});
}*/
//This prototype is provided by the Mozilla foundation and
//is distributed under the MIT license.
//http://www.ibiblio.org/pub/Linux/LICENSES/mit.license

if (!Array.prototype.indexOf)
{
  Array.prototype.indexOf = function(elt /*, from*/)
  {
    var len = this.length;

    var from = Number(arguments[1]) || 0;
    from = (from < 0)
         ? Math.ceil(from)
         : Math.floor(from);
    if (from < 0)
      from += len;

    for (; from < len; from++)
    {
      if (from in this &&
          this[from] === elt)
        return from;
    }
    return -1;
  };
}
