function resize_elements() {
	var hh = $('.page-header').height();
	var fh = $('.page-footer').height();
	
	$('.page-background').css('height',$(window).height()-fh-hh+'px');	
	$('.page-background').css('top',hh+'px');
	
	var w = $('.page-background').css('width');
	var h = $('.page-background').css('height');
	$('.image-preview').css('width',w).css('height',h);
	$('.image-preview div').css('width',w).css('height',h);
	$('.image-preview canvas').attr('width',parseInt(w)).attr('height',parseInt(h));
	$('.image-preview').css('top','0px');
	
	$('#preload').css('top','0px');
	$('#preload').height($(window).height());	
	
	$('.page-content').css('height',$(window).height()-fh-hh+'px');
	$('.page-content-box').css('height', $('.page-content').height() - parseInt( $('.page-content-box').css('margin-bottom') ) - parseInt( $('.page-content-box').css('margin-top') ) + 'px');
	$('.page-content-behind-box').css('height', $('.page-content').height() - parseInt( $('.page-content-behind-box').css('margin-bottom') ) - parseInt( $('.page-content-behind-box').css('margin-top') ) + 'px');
	$('.page-content-behind-wrapper').css('height', $('.page-content-behind-box').height() - parseInt( $('.page-content-behind-btn_up').height() ) - parseInt( $('.page-content-behind-btn_down').height() ) + 'px');
	$('.page-content-behind').css('height',$('.page-content').height()-parseInt($('.page-content-behind').css('top'))*2+'px');
	$('.page-content-behind').css('right',parseInt($('.page-content').css('width'))+'px');
	$('#page-content-map').css('height',$(window).height()-fh-hh+'px');
	//$('.page-content-map-box').css('height',$(window).height()-fh-hh+'px');
	$('.page-content-map-box').css('margin-left',parseInt(($('#page-content-map').width()-$('.page-content-map-box img').width())/2)+'px');
	$('.page-footer').css('top',$(window).height()-fh+'px');
	$('#facebox .body').css('height',parseInt($('.page-background').css('height')) + 'px' );
	$('#gmap').height( $('#facebox .body').height()-70 );
	
	if( $('.page-content-behind-data').height() <= $('.page-content-behind-wrapper').height() ) {
		$('.page-content-behind-btn_up div,.page-content-behind-btn_down div').hide();
	} else {
		$('.page-content-behind-btn_up div:eq(0),.page-content-behind-btn_down div:eq(0)').show();
	}	
	
	$('.vertical-middle').each(function(){
		$(this).css('margin-top',-1*parseInt($(this).height()/2)+'px');
	});
}

function prepare_intro() {
	var is_resort_map = $('#page-content-map').length;	
	
	if( is_resort_map ) {
		$('#page-content-map').hide();	
	}
	
	$('.submenu-box a').hide();
	
	$('.page-content-behind').hide();	
	$('.page-content-box').hide();
	$('.page-content').hide();
}

function intro() {
	desired_width = $('.page-content').css('width');
	desired_height = $('.page-content').css('height');
	behind_width = $('.page-content-behind').css('width');
	
	$('.page-content').css( { 
		width: '50px', 
		height: '50px', 
		top: (parseInt(desired_height)/2-25)+'px', 
		right: (parseInt(desired_width)/2-25)+'px' 
	} );
	
	$('.page-content').show();
	
	$('.page-content').animate( { 
			width: desired_width, 
			right: '0px' 
		}, 300, function() { $(this).animate( { 
				height: desired_height, 
				top: '0px' 
			}, 500, function() {
			$('.page-content-box').fadeIn('slow',function() {
				content_behind_handler();
				prepare_map();
			});
		} );
	} );
}

function content_behind_handler() {

	if( $.trim( $('.page-content-behind-data').html() ) != '' ) {
		$('.page-content-behind-data img').click(function() {
			if( $(this).hasClass('grayscale') ) {
				$(this).addClass('repeat_grayscale');
			}
			
			$(this).addClass("grayscale").addClass('clicked');
			
			$('.page-content-behind-data img').each(function() {
				if( $(this).hasClass("grayscale") && !$(this).hasClass('clicked') ) {
					$(this).addClass('del_grayscale');
				}
			});
			
			$(this).attr('src',$(this).attr('src2'));
			$(this).removeClass('clicked');
		});
		
		$('.page-content-behind-data img').each(function() {
			$(this).attr('src2',$(this).attr('src'));
			grayscale(this);
		});
		
		$('.page-content-behind-data img').mouseover(function(){
			if( !$(this).hasClass('grayscale') ) {
				$(this).attr('src',$(this).attr('src2'));
			}
		});
		
		$('.page-content-behind-data img').mouseout(function(){
			if( !$(this).hasClass('grayscale') ) {
				grayscale(this);
			}
		});
		
		$('#content-behind-btn_up2,#content-behind-btn_down2').each(function(){
			$(this).hide();
		});
		
		$('.page-content-behind-btn_up').mouseover(function(){
			$('#content-behind-btn_up2').show();
			$('#content-behind-btn_up1').hide();

			var scroll_up = function(){
				if($('.page-content-behind-data').attr('scroll') == 'up') {
					if( parseInt($('.page-content-behind-data').css('margin-top')) < 0 ) {
						$('.page-content-behind-data').animate({marginTop: '+=7px'},50,scroll_up);
					}
				}
			}
			
			$('.page-content-behind-data').attr('scroll','up');
			scroll_up();
		});
		
		$('.page-content-behind-btn_up').mouseout(function(){
			$('#content-behind-btn_up1').show();
			$('#content-behind-btn_up2').hide();
			
			$('.page-content-behind-data').attr('scroll','');
		});
		
		$('.page-content-behind-btn_down').mouseover(function(){
			$('#content-behind-btn_down2').show();
			$('#content-behind-btn_down1').hide();

			var scroll_down = function(){
				if($('.page-content-behind-data').attr('scroll') == 'down') {
					if( $('.page-content-behind-wrapper').height() - parseInt($('.page-content-behind-data').css('margin-top')) <= $('.page-content-behind-data').height() ) {
						$('.page-content-behind-data').animate({marginTop: '-=7px'},50,scroll_down);
					}
				}
			}
			
			$('.page-content-behind-data').attr('scroll','down');
			scroll_down();
		});
		
		$('.page-content-behind-btn_down').mouseout(function(){
			$('#content-behind-btn_down1').show();
			$('#content-behind-btn_down2').hide();
			
			$('.page-content-behind-data').attr('scroll','');
		});
		
		$('.page-content-behind-link').click(function() {
			var d = $('.page-content-behind-data').width();
			var facebox_delay = 0;
			
			$('.page-content-behind-box').hide();
			
			if( $('#facebox:visible').length > 0 ) {
				facebox_delay = 500; 
				if( $('#facebox').width() == parseInt($('.page-background').width() - parseInt($('.page-content-behind').css('right')) - parseInt($('.page-content-behind-link').css('width'))) ) {
					$('#facebox').animate( {
						//left: parseInt($('.page-background').width() - parseInt($('.page-content-behind').css('right')) - parseInt($('.page-content-behind-link').css('width')) - d - $('#facebox').width())/2 + 'px',
						width: parseInt($('.page-background').width() - parseInt($('.page-content-behind').css('right')) - $('.page-content-behind-link').width() - d) + 'px'
					}, facebox_delay );
				} else {
					$('#facebox').animate( {
						left: parseInt($('.page-background').width() - parseInt($('.page-content-behind').css('right')) - parseInt($('.page-content-behind-link').css('width')) - d - $('#facebox').width())/2 + 'px'
						//width: parseInt($('.page-background').width() - parseInt($('.page-content-behind').css('right')) - $('.page-content-behind-link').width() - d) + 'px'
					}, facebox_delay );
				}
			}

			if( $('.page-content-behind-link2 .link-right:visible').length > 0 ) {				
				$('.page-content-behind-box').fadeOut();
				
				$('.page-content-behind').animate( { width: '-='+d }, 300 ); 
		
				$('.page-content-behind-link2 .link-right').hide();
				$('.page-content-behind-link2 .link-left').show();
			} else {
				$('.page-content-behind-data .residence').hide();
				
				$('.page-content-behind').delay(facebox_delay).animate( { width: '+='+d }, 300 );
				$('.page-content-behind-box').delay(facebox_delay+300).fadeIn(); 

				l = $('.page-content-behind-data .residence').length;
				
				for( i=0; i<l; i++ ) {
					$('.page-content-behind-data .residence:eq('+i+')').delay(facebox_delay+600+i*250).fadeIn('slow');
				}
						
				$('.page-content-behind-link2 .link-left').hide();
				$('.page-content-behind-link2 .link-right').show();
			}
		});
		
		var behind_width = $('.page-content-behind').css('width');
		$('.page-content-behind').width(0).show().animate({width: behind_width},function(){
			$('.page-content-behind-link').click();
		});
	} else {
		$('.page-content-behind').width(0);
	}
}

function menu_bar( class_name, bar_id, init_delay ) {	
	$(class_name).append('<div id="'+bar_id+'"></div>');
	$('#'+bar_id).hide();

	var init_menu = function() {
		if( $(class_name+' a.current_menu').length == 0 ) {
			$(class_name+' a').each(function(){
				var m = '/';
				if( class_name == '.menu-box' && $('.submenu-box').length>0 ) {
					m = '/resort/';
				} else if( $(this).hasClass('ajax_content') ) {
					m = $(this).attr('href');
				} else if( $(class_name+ ' a.ajax_content').length == 0 ) {
					m = window.location.href.match( $(this).attr('href').replace(/\//g,'\\/') )
				}
				//alert( class_name + ' ' + $('.submenu-box').length + ' ' + m );
				var s = $(this).attr('href'); 
				if( m == s && s != '/' ) {
					$(this).mouseover();
				}
			});
		}
	}
	var timeout = null;
  	 	
	$(class_name+' a').mouseover(function(){
		$(class_name+' a.current_menu').mouseout();
		$(class_name+' a.ajax_content').mouseout();
		
		$(this).animate({opacity:1.0},300).css('font-weight','700');
  	 	$(this).addClass('current_menu')
  	 		
  	 	$('#'+bar_id).show().animate({left:$(this).position().left + parseInt($(this).width()/2) - parseInt($('#'+bar_id).width()/2) + 'px'},500);
  	});
  	
  	$(class_name+' a').mouseout(function(){
		$(this).animate({opacity:0.7},400).css('font-weight','400');
  		$(this).removeClass('current_menu');
  		
	  	if( timeout != null ) {
  			clearTimeout(timeout);
  		}
  		
  		timeout = setTimeout(init_menu,400);
  	});
	
	setTimeout(init_menu,init_delay);
}

function menu_elements_effect() {
	$('.submenu-box a').each(function(i){
		n=$('.submenu-box a').length
		$(this).delay(800+(n-i)*200).fadeIn(250);
	});

	menu_bar( '.menu-box', 'menu_ptr', 700 );
	
	if( $('.submenu-box').length > 0 ) {
		menu_bar( '.submenu-box', 'submenu_ptr', 1900 );
	}
}

function facebox_handler() {
	/* Facebox */
	$('a[rel*=modal]').click(function(){
    	if( $('#facebox:visible').length > 0 ) {
    		$.facebox.close();
    	}
    });
    
	$('a[rel*=modal]').facebox();
}

function background_effect2() {
	$('.image-preview').peKenburnsSlider();
}

function background_effect() {
	var disabled = $('#gallery_disabled').val();
	/*
	$('.page-background img').each(function() {
	    var t = new Image();
	    t.src = $(this).attr('src');
	    if( !$(this).hasAttr('owidth') ) {
		$(this).attr('owidth',t.width);
	    } else {
		$(this).width($(this).attr('owidth'));
	    }
	    
	    if( !$(this).hasAttr('oheight') ) {
		$(this).attr('oheight',t.height);
	    } else {
		$(this).height($(this).attr('oheight'));
	    }
	    t = null;
	});
	*/
	var bg_img_width = $('.page-background img').width();
	var bg_img_height = $('.page-background img').height();
	
	var bg_width = parseInt( $('.page-background').css('width') );
	var bg_height = parseInt( $('.page-background').css('height') );
	
	var distance_w = bg_img_width - bg_width;
	var distance_h = bg_img_height - bg_height;
	
	if( disabled == 1 ) {
		//if( distance_h<0 ) distance_h *= -1;
		$('.image-preview img').eq(0).width(bg_width);
		var h=$('.image-preview img').eq(0).height();
		var hh=$('.page-header').height();
		var w=$('.image-preview img').eq(0).width();
		var ratio=w/h;
		
		if( h-hh>=bg_height ) {
		    $('.image-preview').css('overflow','hidden');
		    $('.image-preview img').eq(0).css('position','relative').css('top',-1*hh+'px');
		} else if( ratio*bg_height > bg_width ) {
		    $('.image-preview img').eq(0).height(bg_height).width(parseInt(ratio*bg_height));
		} else {
		    $('.image-preview img').eq(0).width(bg_width);
		}
		$('.peKenBurns').hide();
		return;
	}
	
	if( $(window).width()>=1500 ) {
		$('.peKenBurns img').each(function(idx){
			$(this).attr('data-src','/images/bg/'+(idx+1)+'b.jpg');
		});
	}
	
	$('.peKenBurns').peKenburnsSlider()
	
	/*
	$('.image-preview').crossSlide({
  		fade: 1.75
	}, [
  		{
    		src:  '/images/background/1.jpg',
    		alt:  '',
    		from: '10% 80% 0.6x',
    		to:   '100% 80% 0.6x',
    		time: 4
  		},
  		{
    		src:  '/images/background/2a.jpg',
    		alt:  '',
    		from: '40% 10% 0.90x',
    		to:   '100% 50% 0.90x',
    		time: 5
  		},
  		{
    		src:  '/images/background/3a.jpg',
    		alt:  '',
    		from: '80% 80% 0.94x',
    		to:   '100% 55% 0.94x',
    		time: 3
  		},
  		{
    		src:  '/images/background/4a.jpg',
    		alt:  '',
    		from: '100% 70% 0.65x',
    		to:   '0% 60% 0.65x',
    		time: 2
  		},
  		{
    		src:  '/images/background/5a.jpg',
    		alt:  '',
    		from: '50% 60% 1.0x',
    		to:   '110% 60% 1.0x',
    		time: 4
  		},
  		{
    		src:  '/images/background/6a.jpg',
    		alt:  '',
    		from: '60% 80% 0.82x',
    		to:   '60% 55% 0.82x',
    		time: 3
  		}
	], function(idx, img, idxOut, imgOut) {
  			if (idxOut == undefined)
  			{
    			// starting single image phase, put up caption
    			$('div.caption').text(img.alt).animate({ opacity: .7 })
  			}
  			else
  			{
    			// starting cross-fade phase, take out caption
    			$('div.caption').fadeOut()
  			}
	});
	*/
}

function mousewheel_handler() {

//compare the height of the scroll content to the scroll pane to see if we need a scrollbar
var difference = $('.page-content-data').height()-$('.page-content-box').height();//eg it's 200px longer
var init = false;

if( $('#slider-wrap').length != 0 ) {
	$("#slider-vertical").slider("value",100);
	$('#slider-wrap').remove();
}

if(difference>0)//if the scrollbar is needed, set it up...
{
	//difference += (5+parseInt( difference / $('.page-content-box').height() ))*30;
	difference += 50;
	
   var proportion = difference / $('.page-content-data').height();//eg 200px/500px
   var handleHeight = Math.round((1-proportion)*$('.page-content-box').height());//set the proportional height - round it to make sure everything adds up correctly later on
   handleHeight -= handleHeight%2; 
   
   handleHeight = 30;
	
   $(".page-content-data").before('<\div id="slider-wrap"><\div id="slider-vertical"><\/div><\/div>');//append the necessary divs so they're only there if needed
   
   $("#slider-wrap").height($(".page-content-box").height()-4);//set the height of the slider bar to that of the scroll pane


   //set up the slider 
   $('#slider-vertical').slider({
      orientation: 'vertical',
      min: 0,
      max: 100,
      value: 100,
      slide: function(event, ui) {//used so the content scrolls when the slider is dragged
         var topValue = -((100-ui.value)*difference/100);
         $('.page-content-data').css({top:topValue});//move the top up (negative value) by the percentage the slider has been moved times the difference in height
      },
      change: function(event, ui) {//used so the content scrolls when the slider is changed by a click outside the handle or by the mousewheel
         var topValue = -((100-ui.value)*difference/100);
         $('.page-content-data').css({top:topValue});//move the top up (negative value) by the percentage the slider has been moved times the difference in height
      }
   });

   //set the handle height and bottom margin so the middle of the handle is in line with the slider
   $(".ui-slider-handle").css({height:handleHeight,'margin-bottom':-0.5*handleHeight});
	
   var origSliderHeight = $("#slider-vertical").height();//read the original slider height
   var sliderHeight = origSliderHeight - handleHeight ;//the height through which the handle can move needs to be the original height minus the handle height
   var sliderMargin =  (origSliderHeight - sliderHeight)*0.5;//so the slider needs to have both top and bottom margins equal to half the difference
   $(".ui-slider").css({height:sliderHeight,'margin-top':sliderMargin});//set the slider height and margins
   
  	//create elements to hold the images for the scrollbar handle
  	$(".ui-slider-handle").append('<img id="scrollbar-grip" src="/img/bullet/8m.png" onfocus="blur()" />');
}//end if

$(".ui-slider").click(function(event){//stop any clicks on the slider propagating through to the code below
   	event.stopPropagation();
   });
   
$("#slider-wrap").click(function(event){//clicks on the wrap outside the slider range
	  var offsetTop = $(this).offset().top;//read the offset of the scroll pane
	  var clickValue = (event.pageY-offsetTop)*100/$(this).height();//find the click point, subtract the offset, and calculate percentage of the slider clicked
	  $("#slider-vertical").slider("value", 100-clickValue);//set the new value of the slider
}); 
	 
//additional code for mousewheel
$(".page-content-data,.page-content-box").mousewheel(function(event, delta){
	var speed = 5;
	var sliderVal = $("#slider-vertical").slider("value");//read current value of the slider
	
	sliderVal += (delta*speed);//increment the current value

	$("#slider-vertical").slider("value", sliderVal);//and set the new value of the slider
	
	event.preventDefault();//stop any default behaviour
});

}

function prepare_map() {
	var is_resort_map = $('#page-content-map').length;	
	
	if( is_resort_map ) {
		$('#page-content-map').hide();		
		
		$('#page-content-map').width($('.page-background').width()-340);
		$('#page-content-map').height($('.page-background').height());
		$('#page-content-map').css('left',parseInt(($('.page-background').width()*3/4-$('#page-content-map').width())/2)+'px');
		$('#page-content-map').css('top',parseInt(($('.page-background').height()-$('#page-content-map').height())/2)+'px');
		
		$('.page-content-map-element').each(function(){
			var myObj = $(this);			
			
			pos= myObj.attr('pos').split('-');
			myObj.css('top',parseInt(pos[0])-33+'px').css('left',parseInt(pos[1])-15+'px');
		});
		
		$('.bubbleInfo').each(function (i) {
	            var distance = 15;
	            var time = 450;
	            var hideDelay = 500;
	
	            var hideDelayTimer = null;
	
	            var beingShown = false;
	            var shown = false;
	            var trigger = $('.trigger', this);
	            var info = $('.popup', this).css('opacity', 0);
	            
	            //trigger.delay(i*950).bulletAnimation();
	            //trigger.css('opacity','0.87').css('filter','alpha(opacity=87');
		
	            $([trigger.get(0), info.get(0)]).mouseover(function () {
	                if (hideDelayTimer) clearTimeout(hideDelayTimer);
	                if (beingShown || shown) {
	                    // don't trigger the animation again
	                    return;
	                } else {
	                    // reset position of info box
	                    beingShown = true;
		
	                    info.css({
	                        top: -1*info.height()+2*distance+5,
	                        left: -1*parseInt(info.width()/2)+distance,
	                        display: 'block'
	                    }).animate({
	                        top: '-=' + distance + 'px',
	                        opacity: 1
	                    }, time, 'swing', function() {
	                        beingShown = false;
	                        shown = true;
	                    });
	                }
	
	                return false;
	            }).mouseout(function () {
	                if (hideDelayTimer) clearTimeout(hideDelayTimer);
	                hideDelayTimer = setTimeout(function () {
	                    hideDelayTimer = null;
	                    info.animate({
	                        top: '-=' + distance + 'px',
	                        opacity: 0
	                }, time, 'swing', function () {
	                        shown = false;
	                        info.css('display', 'none');
	                    });
	
	                }, hideDelay);

	                return false;
	            });
	        });

	      $('#resortmap_img').maphilight({fillColor:"fffcc8",fillOpacity:0.45,strokeColor:"000000",strokeWidth:2});
	      $('.location-map-2 map area').mouseover(function() {
	      	$('#resortmap_desc').html($(this).attr('alt'));
	      	//alert($(this).attr('alt'));
	      });
	      $('.location-map-2 map area').mouseout(function() {
	      	$('#resortmap_desc').html($('#resortmap_hint').html());
	      });
      
	      $('#resortmap_desc').html($('#resortmap_hint').html());

		$('.page-content-map-box').each(function(){
			$(this).css('margin-left',parseInt(($('#page-content-map').width()-$(this).find('img').width())/2)+'px');
		});
		$('.page-content-map-box').hide();
		$('#page-content-map').fadeIn('slow', function() {
			$('.location-map-1').fadeIn('slow');
		} );
	}
}

jQuery.fn.bulletAnimation = function() {
        var h = $(this).height();
        
        if( h > 30 && !$(this).hasClass('animation-up') ) {
        		this.animate({ height: '-=1px', marginTop: '+=1px' }, 150, function() {
                $(this).bulletAnimation();
        		});
        	} else if( h<35 ) {
        		if( !$(this).hasClass('animation-up') ) {
        			$(this).addClass('animation-up');
        		} else if( h+1 == 35 ) {
        			$(this).removeClass('animation-up');
        		}
        		
        		this.animate({ height: '+=1px', marginTop: '-=1px' }, 150, function() {
        			if( !$(this).hasClass('animation-up') ) {
        				$(this).delay(2000).bulletAnimation();
        			} else {
                	$(this).bulletAnimation();
               }
        		});
        	}
        return this;
};

jQuery.fn.hasAttr = function(name) {
    return this.attr(name) !== undefined;
}

function galleries_list() {
	$('.gallery-link').each(function() {
		$(this).click(function() {
			//$('.page-content').animate({width:'0'},1000);
		});
	});
}

function investment_btn_effect() {
	/*
	var btn2 = function() {
		$('.investment-btn-1').animate({opacity:1},1500,function(){$(this).animate({opacity:0.35},1000,btn2);});
	}
	
	$('.investment-btn-1').animate({opacity:0.35},1000,btn2);
	$('.investment-btn-2').hide();
	*/
	$('.investment-btn-2 img').css('position','relative');
	
	var btn2 = function() {
		$('.investment-btn-2').css('right','275px').width(8);
		$('.investment-btn-2 img').css('left','40px');
		
		$('.investment-btn-2').delay(3000).animate({'right':'-=210px'},2000);
		$('.investment-btn-2 img').delay(3000).animate({'left':'-=210px'},2000, btn2);
	}
	
	btn2();
}

$(document).ready(function(){
	resize_elements();
	
	facebox_handler();
	
	mousewheel_handler();
	
	investment_btn_effect();
	
	background_effect();
	
	galleries_list();
	
		prepare_intro();
		
		$('#preload').delay(150).fadeOut(600,function(){
			intro();
	
			menu_elements_effect();
	
			//prepare_map();	// moved to intro();
		});
});	// $(document).ready();

$(window).resize(function(){
	resize_elements();
	
	//background_effect();
	
	mousewheel_handler( false );
});
