// Global variables that can be used by ALL the function son this page.
var imgFalse = '/images/pas/icons/unchecked.gif';
var imgTrue = '/images/pas/icons/checked.gif';

// This function runs when the page is loaded, put all your other onload stuff in here too
$( function() {
	var oldPageName = 1 ;
	
	/* Handle pagination of the results pages */
	$( '.pagingComponent a' ).bind( 'click', function() {
		// Calculate which page to go to
		var noOfPages = ( $( '.pagingComponent a' ).size() - 2 );
		var newPageNumber = ( $( '.pagingComponent .active a' ).html() / 1 );
		if ( ( $( this ).html() == 'prev' ) && ( newPageNumber > 1 ) ) {
			newPageNumber = newPageNumber - 1;
		} else if ( ( $( this ).html() == 'next' ) && ( newPageNumber < noOfPages ) ) {
			newPageNumber = newPageNumber + 1;
		} else if ( ( $( this ).html() != 'prev' ) && ( $( this ).html() != 'next' ) ) {
			newPageNumber = ( $( this ).html() / 1 );
		}
		// Change the paged data
		$( '.resultsDisplay .featureColumn' ).hide();
		//var startIndex = ( ( oldPageName * 10 ) - 10 );
		//var fadeSpeedOut = 100;
		//var fadeSpeedIn = 100;
		//var ElemIndex = 10;
		//for ( var itemNo = (startIndex + 10) ; itemNo >= ( startIndex ); itemNo-- ) {
		//	var animTime = (11 - ElemIndex) * fadeSpeedOut ;
		//	$( '.resultsDisplay .featureColumn:eq(' + itemNo + ')' ).animate({opacity: 1.0}, animTime).fadeOut("slow");
		//	ElemIndex-- ;
		//}
		
		var startIndex = ( ( newPageNumber * 10 ) - 10 );
		//var ElemIndex = 1;
		for ( var itemNo = startIndex; itemNo < ( startIndex + 10 ); itemNo++ ) {
			$( '.resultsDisplay .featureColumn:eq(' + itemNo + ')' ).show();
			//var animTime = ElemIndex * fadeSpeedIn;
			//$( '.resultsDisplay .featureColumn:eq(' + itemNo + ')' ).animate({opacity: 1.0}, animTime).fadeIn("slow");
			//ElemIndex++;
		}
		// Change the hilighted page
		$( '.pagingComponent .active' ).removeClass( 'active' );
		$( '.pagingComponent a:eq(' + newPageNumber + ')' ).parent().addClass( 'active' );
		
		oldPageName = newPageNumber ;
		return false;
	} );
	/* Find Break Button */
	$( '.btnFindBreak' ).bind( 'click', function() {
		$( '.btnFindBreak' ).hide();
		$( '.formContainer' ).fadeIn( 'fast' );
		return false;
	} );
	/* Social Bookmarks Code */
	$( '.sbDigg a,.sbFacebook a,.sbReddit a,.sbDelicious a,.sbStumble a' ).popupwindow();
	$("#navTheme").treeview({
		persist: "location",
		collapsed: true,
		unique: true
	});
	/* Browse Breaks Button */
	$( '.btnBrowse a' ).bind( 'click', function() {
		$( '#browseBreaksForm' ).submit();
		return false;
	} );
	/* Search heading */
	$( '.navSearch' ).bind( 'click', function() {
		$( '#topSearch' ).fadeIn( 'fast' );
		return false;
	} );
	/* Search fields */
	$( '.searchInput' ).bind( 'focus', function() {
		$( this ).val( '' );
	} );
	/* Search Button */
	$( '#searchSubmit' ).bind( 'click', function() {
		$( '#searchForm' ).submit();
		return false;
	} );
	$( '#searchSubmitTop' ).bind( 'click', function() {
		$( '#searchFormTop' ).submit();
		return false;
	} );

	/* Sort options code */
	$( 'span.option a' ).live( 'click', function() {
		// Change link hilighting
		$( 'span.option:not(a)' ).wrapInner( '<a href="#"></a>' );
		var currText = $( this ).html();
		$( this ).parents( 'span.option' ).html( currText );
		if ( currText == 'Map' ) {
			// Do show/hide
			$( 'div.resultsDisplay' ).hide();
			$( '.pagingComponent' ).hide();
			$( 'div.mapDisplay' ).show();
			// Fix for bad rendering of map
			drawHXMap1();
		} else {
			// Do show/hide
			$( 'div.mapDisplay' ).hide();
			$( 'div.resultsDisplay' ).show();
			$( '.pagingComponent' ).show();
			// Sort the results
			//$( 'div.resultsDisplay' ) //resultsDisplay
			$( 'div.featureColumn' ).tsort( 'span.sortBy' + currText );
			// Reset to first page (if pagination is on)
			if ( $( '.pagingComponent a' ).size() > 0 ) {
				$( '.pagingComponent a:eq(1)' ).click(); // Does not change page if already selected	
			}
		}
		return false;
	} );
	/* Select All Code */
	$( '.selectAllLink' ).bind( 'click', function() {
		$( this ).parents( 'fieldset' ).find( 'input:checkbox' ).attr( 'checked', true );
		$( this ).parents( 'fieldset' ).find( 'img' ).attr( 'src', imgTrue );
		return false;
	} );
	$( '.unselectAllLink' ).bind( 'click', function() {
		$( this ).parents( 'fieldset' ).find( 'input:checkbox' ).removeAttr( 'checked' );
		$( this ).parents( 'fieldset' ).find( 'img' ).attr( 'src', imgFalse );
		return false;
	} );
	/* CHECK BOX CODE */
	replaceChecks();
	
	/* IE FIX FOR DROPDOWNS THAT ARE TO SMALL */
	if ( !jQuery.support.leadingWhitespace ) {
		$( '#hxSeatType' ).css( 'width', '240px' );
		$( '#hxSeatType' ).parent().find( 'label' ).css( 'width', '240px' );
		$( '.roomType' ).css( 'width', '170px' );
	}
	
	
	// sign up form on header
	$('div#formContainer').bgiframe() ;
	
	$('a.signUp').bind('click', function() {
		$('div#formContainer').slideToggle("slow") ;
		return false ;
	});
	
	
	//$('div#formContainer').animate({opacity: 1.0}, 3000).slideUp("slow") ; // slide up after 3 seconds
	
	$('#signUpClose').bind('click', function() {
		// close the sign up form
		$('div#formContainer').slideUp("slow") ;
		
		var date = new Date();
		date.setTime(date.getTime() + (1 * 24 * 60 * 60 * 1000));
		$.cookie('signUp', 'Done', { path: '/', expires: date });
		
	}) ;
	
	// diplay the sign up form if the person hasn't closed it or registered
	//MCS 04/08/2009 - Marketing ask to not to display the form, but need to click to display it
	/*if ( !$.cookie('signUp') ) {
		$('div#formContainer').slideDown("slow") ;
	}*/
	
} );

jQuery.fn.popupwindow = function(p){
	var profiles = p || {};
	return this.each(function(index){
		var settings, parameters, mysettings, b, a;
		// for overrideing the default settings
		mysettings = (jQuery(this).attr("rel") || "").split(",");
		
		settings = {
			height:600, // sets the height in pixels of the window.
			width:800, // sets the width in pixels of the window.
			toolbar:0, // determines whether a toolbar (includes the forward and back buttons) is displayed {1 (YES) or 0 (NO)}.
			scrollbars:0, // determines whether scrollbars appear on the window {1 (YES) or 0 (NO)}.
			status:0, // whether a status line appears at the bottom of the window {1 (YES) or 0 (NO)}.
			resizable:1, // whether the window can be resized {1 (YES) or 0 (NO)}. Can also be overloaded using resizable.
			left:0, // left position when the window appears.
			top:0, // top position when the window appears.
			center:0, // should we center the window? {1 (YES) or 0 (NO)}. overrides top and left
			createnew:1, // should we create a new window for each occurance {1 (YES) or 0 (NO)}.
			location:0, // determines whether the address bar is displayed {1 (YES) or 0 (NO)}.
			menubar:0 // determines whether the menu bar is displayed {1 (YES) or 0 (NO)}.
		};

		// if mysettings length is 1 and not a value pair then assume it is a profile declaration
		// and see if the profile settings exists

		if(mysettings.length == 1 && mysettings[0].split(":").length == 1){
			a = mysettings[0];
			// see if a profile has been defined
			if(typeof profiles[a] != "undefined") {
				settings = jQuery.extend(settings, profiles[a]);
			}
		}else{
			// overrides the settings with parameter passed in using the rel tag.
			for(var i=0; i < mysettings.length; i++){
				b = mysettings[i].split(":");
				if(typeof settings[b[0]] != "undefined" && b.length == 2){
					settings[b[0]] = b[1];
				}
			}
		}

		// center the window
		if (settings.center == 1){
			settings.top = (screen.height-(settings.height + 110))/2;
			settings.left = (screen.width-settings.width)/2;
		}

		parameters = "location=" + settings.location + ",menubar=" + settings.menubar + ",height=" + settings.height + ",width=" + settings.width + ",toolbar=" + settings.toolbar + ",scrollbars=" + settings.scrollbars  + ",status=" + settings.status + ",resizable=" + settings.resizable + ",left=" + settings.left  + ",screenX=" + settings.left + ",top=" + settings.top  + ",screenY=" + settings.top;

		jQuery(this).bind("click", function(){
			var name = settings.createnew ? "PopUpWindow" + index : "PopUpWindow";
			window.open(this.href, name, parameters).focus();
			return false;
		});
	});
};


function closeSignUp()
{
	$('div#formContainer').animate({opacity: 1.0}, 3000).slideUp("slow") ; // slide up after 3 seconds
}


// CHECK BOX CODE
function replaceChecks() {
	// Get all the input fields on the page
	$( 'input:checkbox' ).each( function() {
		// Check if the checkbox is checked
		if ( $( this ).attr( 'checked' ) ) {
			var imgSrc = imgTrue;
		} else {
			var imgSrc = imgFalse;
		}
		// Create a new image
		var newImg = $( '<img />' ).attr( 'src', imgSrc ).attr('alt', 'checkbox').click( function() {
			var closestInput = $( this ).parent().find( 'input' );
			if ( closestInput.attr( 'checked' ) ) {
				closestInput.removeAttr( 'checked' );
				$( this ).attr( 'src', imgFalse );
			} else {
				closestInput.attr( 'checked', true );
				$( this ).attr( 'src', imgTrue );
			}
		} );
		// Place image in front of the checkbox and hide the checkbox
		$( this ).before( newImg ).css( 'display', 'none' );
	} );
}
