if ( !window.console ) {
    // for flash debugging.. 
    window.console = {};
    console.log = function(){};
}

$(function() {

    // this site is oldschool and no nav.xml, kinda too lazy to add conditionals all over multiple html files.. 
    $('html.smartphone').each(function() {
	$('#tert-images a').attr('href', 'http://www.maunalani.com/mobile-photo-video.php');
	$('#photo-gallery a').attr('href', 'http://www.maunalani.com/mobile-photo-video.php');
    });

	//var theForm = $('#concierge');
/*    $('div#canoehouse-dining-reservations1').hide();
	$('div#canoehouse-dining-reservations2').hide();
	$('a#add-dining-reservation1').hide();
	$('a#add-dining-reservation2').hide();*/

	$("div#golf-accolades").flash(
	{src:"flash/golf-accolades.swf",
	 width:351,
	 height:110,
	 wmode:"transparent",
	 quality:"high"},
	 {version:"7"});
     
     window.openPrintWindow = function( path ) { 
	 var w = window.open( path );     
     }
	/*
     window.showPhotoGallery = function() {
		 if ( $('#dialog-home').length ) {
			$('#dialog-home').jqm({ajax:'../includes/photo-gallery-home.php', onShow: openFnHome, onHide: hideFn, target:'div#photo-content'}).jqmShow();
		 }
     }*/

     $('body.home-new').each(function() {
	 //if ( window.console ) console.log( $('#dialog-home').length )
	 $('#dialog-home').prependTo('body')
     });

     $("#flashobject").html('').flash({
        swf:"homepage-new.swf",
        width:"100%",
        height:"100%",
        params:{
            allowscriptaccess:"always",
            wmode:"transparent",
            flashvars:{
                xmlPath:'http://maunalani.slideshowpro.com/images.php?gallery=54988'
            }
        }
    });
	
    $('p.sIFR').sifr({path:'flash/sifr/', size:'36', color:'#591b0e' });
    
	var theForm = $('div#rfp form');
    $('div#rfp form div.add-reservation').each(function() {$(this).hide();});	
    $('div#rfp form a.add-dining-reservation').each(function() {
	if ($(this).attr('id') != 'dining-reservation0') {
	    $(this).hide();
	}
    });
	 var diningCounter = 0;
    $('a.add-dining-reservation').toggle(
	function() {
	    if ( diningCounter < 3 ) {
		diningCounter++;
		
		$(this).next().show();
		$('.add-reservation p').remove();
		$('#dining-reservation' + diningCounter + '').show();
		
	    }
		if ( diningCounter >= 2 ) {
			$('a#dining-reservation2').remove();
			}
	    return false;
	}, 
	function() {
	    $(this).next().hide();
	    $('#dining-reservation' + diningCounter+ '').hide();
	    diningCounter--;
	
	});
	
	$('div#rfp form div.spa-reservation').each(function() {$(this).hide();});
	 $('div#rfp form a.add-spa-reservation').each(function() {
	if ($(this).attr('id') != 'spa-reservation0') {
	    $(this).hide();
	}
    });	
   
	
    
	var spaCounter = 0;
    $('a.add-spa-reservation').toggle(
	function() {
	    if ( spaCounter < 3 ) {
		spaCounter++;
		
		$(this).next().show();
		$('#spa-reservation' + spaCounter + '').show();
		
	    }
		if ( spaCounter >= 2 ) {
			$('a#spa-reservation2').remove();
			}
	    return false;
	}, 
	function() {
	    $(this).next().hide();
	    $('#spa-reservation' + spaCounter+ '').hide();
	    spaCounter--;
	
	});
	
	$('div#rfp form div.golf-reservation').each(function() {$(this).hide();});
	 $('div#rfp form a.add-golf-reservation').each(function() {
	if ($(this).attr('id') != 'golf-reservation0') {
	    $(this).hide();
	}
    });

	var golfCounter = 0;
    $('a.add-golf-reservation').toggle(
	function() {
	    if ( golfCounter < 3 ) {
		golfCounter++;
		
		$(this).next().show();
		$('#golf-reservation' + golfCounter + '').show();
		
	    }
		if ( golfCounter >= 2 ) {
			$('a#golf-reservation2').remove();
			}
	    return false;
	}, 
	function() {
	    $(this).next().hide();
	    $('#golf-reservation' + golfCounter+ '').hide();
	    golfCounter--;
	
	});
	
	$('.styleswitch').click(function()
	{
		switchStylesheet($(this).attr("rel"));
		window.print();
		return false;
	});



	// JQModal 
	var openFn=function(hash){
		hash.w.show();
		$('#dialog').css({left:($(window).width()-$('#dialog').width())/2,top:$(window).scrollTop()+10});
	};
	/*
	var openFnHome=function(hash){
		hash.w.show();
		$('#dialog-home').css({left:($(window).width()-$('#dialog-home').width())/2,top:$(window).scrollTop()+10});
	};*/
	var hideFn=function(hash){
		hash.w.hide()
		hash.o.remove();
	};
	
	if ($('#dialog').length && $('html.smartphone').length == 0) {
		$('#dialog').jqm({ajax:'../includes/photo-gallery.php', trigger: 'li#photo-gallery a', onShow: openFn, onHide: hideFn, target:'div#photo-content'});
		$('#dialog').jqmAddTrigger('li#photos-sitemap a'); 
		$('#dialog').jqmAddTrigger('li#tert-images" a'); 
	}
	
	$('#html-photos').click(function(e){

	    if ( $('html.smartphone').length == 0 ) {
		e.preventDefault();

		showPhotoGallery()
	    }

	});
	
	$('.photo').each(function(){
		$(this).click(function(){
			var contents = $('.lightbox-hidden',$(this)).html();
			showPhoto(contents);  
			return false;
		});
	});
	
	$('#photos-wrapper div:nth-child(5n)').addClass('last');
	
	makeItScale();
		$(window).resize(function(){makeItScale();});

});

//stylesheet switcher for driving directions
function switchStylesheet(sheet) {
	$('link[@rel*=style][@title]').each(function() {
			$(this).attr('disabled', true);
			if ($(this).attr('title') == sheet) {
				$(this).attr('disabled', false);
			}
	});
}

function showPhotoGallery() {
		$('#dialog-home').jqm().css({left:($(window).width()-$('#dialog-home').width())/2,top:$(window).scrollTop()+10}).jqmShow();
}

function showPhoto(contents) {
		$('#photo').jqm().css({left:($(window).width()-$('#photo').width())/2,top:$(window).scrollTop()+145}).jqmShow();
		$('#gallery-wrapper').html(contents);
		var photo = $('#gallery-wrapper img').attr('src');
		$('.print-photo').attr('href','/photo.php?photo='+photo);
}

function openPreview(urlrequest) {
	window.open(urlrequest, 'myWindow', 'width=940,height=700,left=100,top=100,toolbar=No,location=No,scrollbars=No,status=No,resizable=No,fullscreen=No');
	document.getElementById('flashCallBox').value = urlrequest;
}

function makeItScale() {
	var imgRatio = 1800/936;
	var winRatio = ($(window).width())/($(window).height());
	var usableW = $(window).width();
	var usableH = $(window).height();
	if (winRatio > imgRatio){
		$('#masthead-html img').css({width:usableW,height:'',marginLeft:-usableW/2});
	}
	else {
		$('#masthead-html img').css({width:'',height:usableH,marginLeft:-usableW/2});	
	}
}

$(document).ready(function() {
	$('#loading-overlay').css('opacity',0.8).fadeOut(800);
	$('#masthead-html img:eq(0)').show();
	function animateFunc() {
		$('#masthead-html :first-child').fadeOut(1800).next('img').fadeIn(1800)
         .end().appendTo('#masthead-html');
	}
	setInterval(function(){
		animateFunc();
      }, 
      8000);
});

