var showHelpBox = false;

function t(n) { try { console.log(n); } catch(e) { }; }

window.__jScrollPaneOptions = {
	showArrows:true,
	scrollbarWidth:17, 
	scrollbarMargin:0
};

var page_id;
var last_page_id; 
var nav_level;
var navIsClosed = true;
var FlashIsNotSupported = true; //default assumption is that flash is supported
var bg_category; // possible values: 'intro', 'bh', 'sf', or 'sfbar'  // not anymore, see updateBackground
var bgTimer;

var BG_DELAY = 8000;  //miliseconds
var BG_WIDTH = 1024;
var BG_HEIGHT = 768;


if(document.location.hash == '') {

	if( document.location.host.indexOf('crescentbh.com') > -1 ) {
		window.location.assign( document.location.protocol + '//' + document.location.host + '#beverlyhills' );
		window.location.reload();
	}
	else if( document.location.host.indexOf('crescentsf.com') > -1 ) {
		window.location.assign( document.location.protocol + '//' + document.location.host + '#sanfrancisco' );
		window.location.reload();
	}

}

$(document).ready(function() {	
	if(document.location.href.indexOf('#') == -1) document.location += '#home';

	if (FlashIsNotSupported) {
		// no flash, so create javascript images
		//$('#flashContent').empty();

		var s = window._backgroundSlides;
		
		for(var k in s) {
			var bgskey = k;
			if(bgskey == 'background_intro') bgskey = 'intro';
			var d = $('<div class="background" id="background_'+bgskey+'"></div>');

			for(var i=0; i<s[k].length; ++i) {
				d.append( $('<img src="'+s[k][i]+'">') );
			}
			$('#flashContent').append(d);
		}
	}

	externalLinks(); // allows html links with rel="external" to open in new tab/window
	positionFooter(); // sets footer at the bottom of viewport
	
	addNavBtnHandlers(); //event handlers for nav close/open arrow	 
	if ($(window).height()<500) {
		changeCSSTop('70px');
	}

	if(showHelpBox) {
		setTimeout(function() {
			if(page_id == 'home' && navIsClosed && !window._showed_help_box) {
				var img = $('<img id="imgHelpBox" src="assets/images/click-arrow-to-begin.png" width="140" height="34">')
					.css('display', 'block').css('position', 'absolute').css('left', -140).css('top', $('div#logo').position().top + $('div#logo').height() + 1)
					.css('opacity', 0).css('z-index', 99);
				$('body').append(img);
	
				//img.fadeTo(300, 1);
				img.animate({
					opacity: 1,
					left: 0
				}, 300);
				window._showed_help_box = true;
			} else window._showed_help_box = true;
		}, 7000);
	}
});

window.onorientationchange = function() {
	if ($(window).height()<500) {
		changeCSSTop('70px');
	} else {
		changeCSSTop('215px');	
	}

	/*
	var orientation = window.orientation;
  switch(orientation) {
    case 0:
      document.body.setAttribute("class","portrait");
      break;          
    case 90:
      document.body.setAttribute("class","landscapeLeft");
      break;    
    case -90:  
      document.body.setAttribute("class","landscapeRight");
      break;
  }
	*/
}

function changeCSSTop(_top) {
	$('#nav').css({top: _top})
	$('.content').css({top: _top})
	$('#logo').css({top: _top})
}

window.onresize = function() {
	positionFooter();
	if (FlashIsNotSupported) resize_background();
}

//JQuery address
$.address.change(function(event) {

	page_id = event.value;	
	if (last_page_id == undefined) { //first visit to Website 
		if (FlashIsNotSupported) {
			resize_background();	
			updateBackground();
		}

	} else {
		updatePage();	
	}
}); 

function showFooterLogo() {
	if($.browser.msie){
		$('#footer').show();
		$('#logo').show();
		updatePage();	
	} else {
		$('#footer').fadeIn(1000);
		$('#logo').fadeIn(1000, function() {
			updatePage();	
		});	
	}
} 

function updatePage() {
	updateBackground();

	//alert('page_id: '+page_id+' \nlast_page_id: '+last_page_id);
	if (page_id == "/") { //first visit to homepage
		last_page_id = '/';
	} 
	else if (page_id == "home") {
		$('#nav a').removeClass();  
		$('img#sitelogo').attr('src', 'assets/images/logo.png');
		closeAll();
	} 
	else {
		var alink = $('a[href=#' + page_id + ']');
		$('#nav a').removeClass();
		alink.attr('class', 'selected');
		$('#nav').animate({width:'show'},250,'linear',function() {
	  		updateNavbar(alink);
		});						
		$('#nav_btn').animate({marginLeft:"0px"},750);				
	}
}  

function updateNavbar(alink) {


	var k = alink.attr('href').substr(1).substr(0, alink.attr('href').indexOf('-')-1);
	if(!k) k = alink.attr('href').substr(1);
	var pclass = alink.parent().parent().attr('class');

	// logo change
	if(k == 'sanfrancisco' || k == 'beverlyhills') $('img#sitelogo').attr('src', 'assets/images/logo_'+k+'.png');
	else $('img#sitelogo').attr('src', 'assets/images/logo.png');

	// sanfrancisco and beverlyhills have the special drop down menu functionality
	if ((k == 'sanfrancisco' || k == 'beverlyhills') && pclass == "nav_2") {

		//t('nav_2');
		nav_level = 2; 
		alink.parent().parent().show(); //show nav_2 list
		alink.parent().parent().siblings().attr('class', 'selected');			
	
		//show nav_3 list		
		if($.browser.msie){ 
			alink.siblings().animate({height:'show'},1000);				
		} else {
			alink.siblings().animate({height:'show',opacity:'show'},1000);		
		}
	
		last_page_id = page_id;
	}
	// this will handle pages with a sub menu, like PRESS
	else if(pclass == 'nav_2') {
		//t(1);
		nav_level = 3; 
		alink.parent().parent().show(); //show nav_2 list
		alink.parent().parent().show(); //show nav_3 list
		alink.parent().parent().siblings().attr('class','selected');
		updateContent('513px');
	}
	else if (pclass == "nav_3") {
		//t('nav-3');
		nav_level = 3;
		alink.parent().parent().show(); //show nav_3 list
		alink.parent().parent().siblings().attr('class','selected');			
		alink.parent().parent().parent().parent().show(); //show nav_2 list				
		alink.parent().parent().parent().parent().siblings().attr('class','selected');
		updateContent('513px');
	} 
	else {
		//t('pirmary nav or footer');
		nav_level = 1;
		if (alink.siblings().length>0) { 
			//has subnav
			if ((last_page_id!='/') && (last_page_id!='home') && ($('#'+last_page_id).attr('class')=='content')) {
				if($.browser.msie){
					$('#'+last_page_id).animate({width:'hide'}, 500, function() {	
						$('#nav>li ul').hide();					
						alink.siblings().animate({width: 'show'});		
					});				
				} else {
					$('#'+last_page_id).animate({width:'hide',opacity:'hide'}, 500, function() {	
						$('#nav>li ul').hide();					
						alink.siblings().animate({width: 'show'});		
					});					
				} 
			} else {
				$('#nav>li ul').hide();					
				alink.siblings().animate({width: 'show'});
			}		
			last_page_id = page_id;								
		} else { 	
			//no subnav
			$('#nav>li ul').hide();						
			updateContent('352px');			
		}		
	}
}

function addNavBtnHandlers() {
	$('#nav_btn').mouseover(function() {
		$('#nav_btn').attr('src','assets/images/arrow_over.png');						
	});

	$('#nav_btn').mouseout(function() {
 		$('#nav_btn').attr('src','assets/images/arrow.png');									
	});

	$('#nav_btn').click(function() {
		$('img#imgHelpBox').fadeTo(500, 0);

		if (!navIsClosed) {
			//nav is open
			closeAll();	
		} else { //nav is closed
				$('#nav_btn').animate({marginLeft:"0px"},750);			
			
			if ((page_id == '/') || (page_id=='home')) {
				$('#nav').animate({width: 'show'});		
			} else {
				updatePage();			 
			}		  
			
			navIsClosed = false;
		}	
	});
}

function closeAll() {
	navIsClosed = true;		
	$('#nav_btn').animate({marginLeft:"-280px"},750);	
	
	if ($('#'+last_page_id).attr('class')=='content') {
		$('#'+last_page_id).animate({width: 'hide'},500, 'linear', function(){
			$('#nav>li ul').hide();			
			$('#nav').animate({width: 'hide'});			
		});

	} else {

		$('#nav').animate({width: 'hide'});							
	}	
	last_page_id = page_id; 			
}

function updateContent(left) {
  if ((last_page_id != '/') && ($('#'+last_page_id).attr('class')=='content')) {
		if($.browser.msie){  	
  		$('#'+last_page_id).animate({width:'hide'}, 500, function() {		
  			if (nav_level != 3) $('#nav>li ul').hide();		
  			displayContent(left);
  		});
		} else {
  		$('#'+last_page_id).animate({width:'hide',opacity:'hide'}, 1000, function() {
    		if (nav_level != 3) $('#nav>li ul').hide();		
      		displayContent(left);
      });		
		}
  } else {
	  displayContent(left);
	}
}

function displayContent(left) {	

	$('#'+page_id).css('left',left);
	
  //we need to set display to block before calling jScrollPane so that it
	//can calculate height of content...then we set display to none after
	//jScrollPane is done  	
	$('#'+page_id).css({visibility:'hidden', display:'block'});

	$(function() {
		$('#'+page_id+ ' .scroll-pane').jScrollPane(window.__jScrollPaneOptions);
		});	
	$('#'+page_id).css({visibility:'visible', display:'none'});	

	//display content
	if($.browser.msie){
		$('#'+page_id).animate({width:'show'},500);	
	} else {
		$('#'+page_id).animate({width:'show',opacity:'show'},500);
	}
	last_page_id = page_id;				
  if (page_id=='sanfrancisco-photos') gallery(page_id);
  if (page_id=='beverlyhills-photos') gallery(page_id);	
	navIsClosed = false;
}

//Adds target="_blank" to external links
function externalLinks() {
	if (!document.getElementsByTagName) return;
	var anchors = document.getElementsByTagName("a");
	for (var i=0; i<anchors.length; i++) {
		var anchor = anchors[i];
		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") anchor.target = "_blank";
	}
}

function resize_background() {							
	var ratio = BG_HEIGHT/BG_WIDTH;				
	var browserWidth = $(window).width();
	var browserHeight = $(window).height();				
	var images = $('.background img');

	if ((browserHeight/browserWidth) > ratio){
		images.height(browserHeight);
		images.width(browserHeight/ratio);
	} else {
		images.height(browserWidth * ratio);
		images.width(browserWidth);				
	}										
}

function gallery(id) {
	var id='#'+id;
	var slideNumber = 1;
 
	$(id + '>img').hide(); 
	$(id + '>img:nth-child('+slideNumber+')').show(); 
	nextBtn = $(id + ' .controls .next');
	prevBtn = $(id + ' .controls .prev');
	prevBtn.hide();
	nextBtn.show();
	prevBtn.unbind('click');
	nextBtn.unbind('click');
	slidesCount = $(id + '>img').length; 
 
	prevBtn.click(function() { 
		if (slideNumber>1) {
			$(id + '>img:nth-child('+slideNumber+')').fadeOut(1000);
			slideNumber--;  
			$(id + '>img:nth-child('+slideNumber+')').fadeIn(1000);
 		} 
		updateCursor(slideNumber,slidesCount,prevBtn,nextBtn); 
	});   
 
	nextBtn.click(function() { 
		if (slideNumber<slidesCount) {
			$(id + '>img:nth-child('+slideNumber+')').fadeOut(1000);
			slideNumber++;  
			$(id + '>img:nth-child('+slideNumber+')').fadeIn(1000);
		} 
		updateCursor(slideNumber,slidesCount,prevBtn,nextBtn); 
	});    
}

function updateCursor(slideNumber,slidesCount,prev,next) {
  if (slideNumber == 1) {
		prev.fadeOut(1000);			
	} else if (slideNumber == slidesCount) {
		next.fadeOut(1000);
	} else {
		next.fadeIn(1000);
		prev.fadeIn(1000);							
	}	
}	
	
function positionFooter() {  
	//alert($(window).height());
	if (document.getElementById("footer")) {
		var footerY = $(window).height()-30;  //don't hardcode the footer height
		document.getElementById("footer").style.top = footerY+"px";
	}
}


function addBG(id) {
	if (FlashIsNotSupported) {
		if (bgTimer != null) clearInterval(bgTimer);								
		$('.background img').hide();
	
		$(id+' img:last-child').fadeIn(1000,function() {
			$(id+ ' img').show();				
			if (last_page_id == undefined) showFooterLogo(); //first visit to Website 
		});
	
		if ($(id+ ' img').length>1) {
			bgTimer = setInterval(function() {
				$(id + ' :last-child').fadeOut(1000, function() {
					$(id + ' :last-child').prependTo(id);
					$(id + ' :first-child').show();				
				});
			}, BG_DELAY);				
		}		
	} else { //flash is supported 
		//if (last_page_id == undefined) showFooterLogo(); //first visit to Website 	
	}
}


function idSend(id) {   
	//var id = "hotel_1";
	var flashMovie = document.getElementById("flashContent");
	if (flashMovie) { 
		 flashMovie.sendIdToFlash(id);
	}
}

function updateBackground() {

	if(!page_id) {
		page_id = document.location.href.substr(document.location.href.indexOf('#')+1);
	}

	if(FlashIsNotSupported) {
		/*
		if(bg_category != 'default') {
			addBG('#background_intro');
			bg_category = 'default';
		}
		*/
		var bg_category_old = bg_category;
		switch(page_id) {
			case 'home': bg_category = 'intro'; break;
			default: bg_category = page_id;
		}
		if(! $('#background_'+bg_category + ' img').length ) bg_category = 'intro';
		if(bg_category != bg_category_old) addBG('#background_'+bg_category);

	}
	else {
		if(page_id && window._backgroundSlides[page_id]) {
			if(bg_category != page_id) {
				idSend(page_id);
				bg_category = page_id;
			}
		}
		else if(page_id.indexOf('beverlyhills')!= -1)   {
			if(bg_category != 'beverlyhills') {
				idSend('beverlyhills');
				bg_category = 'beverlyhills';
			}
		}
		else if(page_id.indexOf('sanfrancisco')!= -1)   {
			if(bg_category != 'sanfrancisco') {
				idSend('sanfrancisco');
				bg_category = 'sanfrancisco';
			}
		}
		else {
			if(bg_category != 'default') {
				idSend('default');
				bg_category = 'default';
			}
		}
	}

	return;
  if ((page_id.indexOf('beverlyhills')== -1) && (page_id.indexOf('sanfrancisco') == -1)) { 
		if (bg_category != 'intro') {			
			//alert('intro');
			(FlashIsNotSupported) ? addBG('#background_intro') : idSend('background_intro');
			bg_category = 'intro';
		}
	} else if (page_id.indexOf('beverlyhills')!= -1)   {
		if (bg_category != 'bh') {
		  //alert('bh');
			(FlashIsNotSupported) ? addBG('#background_bh') : idSend('background_bh');					 
			bg_category = 'bh';
		}
  } else if (page_id == 'sanfrancisco-bar') {
	  if (bg_category != 'sfbar') {
			//alert('sfbar');
			(FlashIsNotSupported) ? addBG('#background_sfbar') : idSend('background_sfbar');
			bg_category = 'sfbar'; 	 						 	
		}
	} else {
		if (bg_category != 'sf') {
			//alert('sf');
			(FlashIsNotSupported) ? addBG('#background_sf') : idSend('background_sf');
			bg_category = 'sf';		 	 		
		}
	}
}

function preload() {
	img1 = new Image();
	img2 = new Image();
	img3 = new Image();
	img4 = new Image();
	img5 = new Image();
	img5.src="assets/images/content_bg_less_opacity.png";	
	img1.src="assets/images/bh_gallery/slide1.jpg"; 		
	img2.src="assets/images/bh_gallery/slide2.jpg";	
	img3.src="assets/images/bh_gallery/slide3.jpg";		
	img4.src="assets/images/bh_gallery/slide4.jpg";
}

$(document).load(function() {
	i50 = new Image(); i50.src = 'assets/images/logo_sanfrancisco.png';
	i51 = new Image(); i51.src = 'assets/images/logo_beverlyhills.png';
	i52 = new Image(); i52.src = 'assets/images/click-arrow-to-begin.png';
});

