$(document).ready(function(){
	
	// UTIL
	Array.prototype.unique = function() {
		var a = [];
		var l = this.length;
		for(var i=0; i<l; i++) {
			for(var j=i+1; j<l; j++) {
				// If this[i] is found later in the array
				if (this[i] === this[j])
					j = ++i;
			}
			a.push(this[i]);
		}
    	return a;
	};
  
	Array.prototype.sortNum = function () {
		return this.sort( function (a,b) { return a-b; } );
	};

	Array.prototype.exists = function(search){
		for (var i=0; i<this.length; i++)
			if (this[i] == search) return true;
		return false;
	};

	// ACCT
	
	// remove "Email address" from input on focus
	$("input#UserEmail").focus(function () {
		if ($(this).attr("value") == "Email address") {
			$(this).attr("value", "");
		}         
    });
	
	// remove "Password" from input on focus
	$("input#UserPassword").focus(function () {
		if ($(this).attr("value") == "Password") {
			$(this).attr("value", "");
		}         
    });
    
	// bind submit
	$("a.loginSubmit").bind("click", function () {
		$("form#UserLoginForm").submit();
		return false;
	});
	
	// TERMS
	
    // redirect user to register page after clicking cancel on the terms page
    $("img#cancelTerms").click(function () {
		location.href = '/users/add/';        
    });
    
	$("img#cancelTerms").hover(
		function () {$(this).css({'cursor' : 'pointer'})},
		function () {}
	);
	
		// remove "Email address" from input on focus
	$("input#UserEmail").focus(function () {
		if ($(this).attr("value") == "Email address") {
			$(this).attr("value", "");
		}         
    });
	
	// remove "Password" from input on focus
	$("input#UserPassword").focus(function () {
		if ($(this).attr("value") == "Password") {
			$(this).attr("value", "");
		}         
    });
    

    // !! manually handle recover password submission
    $("input#btnRecoverPassword").click(function () {
		var email = $("input#regEmail").attr("value");
		location.href = '/users/recoverpassword/'+email;        
    });
	

	/*******
	 * 
	 * // MENU ROLLOVERS 
	 */

	switch (GET_thisLayout()) {
		
		case '0':
			$("#horz_menu").css({'background-position' : '0 84px'});
			$("a.nav_main").css({'color' : '#E2D0A7'});
		break;		
		case '1':
			// book private dining
			$("#horz_menu").css({'background-position' : '0 28px'});
			$("a.nav_book").css({'color' : '#fff'});
		break;
		case '2':
			// directory
			$("#horz_menu").css({'background-position' : '0 42px'});
			$("a.nav_dir").css({'color' : '#fff'});
		break;
		case '3':
			$("#horz_menu").css({'background-position' : '0 56px'});
			$("a.nav_about").css({'color' : '#fff'});
		break;
		case '4':
			$("#horz_menu").css({'background-position' : '0 70px'});
			$("a.nav_myres").css({'color' : '#fff'});
		break;
	}
	
	initMenuRollover = function () {
		switch (GET_thisLayout()) {
			case '0':
				$("#horz_menu").css({'background-position' : '0 84px'});
				$("a.nav_book").css({'color' : '#E2D0A7'});
			break;		
			case '1':
				$("#horz_menu").css({'background-position' : '0 28px'});
				$("a.nav_book").css({'color' : '#fff'});
			break;
			case '2':
				$("#horz_menu").css({'background-position' : '0 42px'});
				$("a.nav_dir").css({'color' : '#fff'});
			break;
			case '3':
				$("#horz_menu").css({'background-position' : '0 56px'});
				$("a.nav_about").css({'color' : '#fff'});
			break;
			case '4':
				$("#horz_menu").css({'background-position' : '0 70px'});
				$("a.nav_myres").css({'color' : '#fff'});
			break;
		}
	}
	
	$("a.nav_main").hover(
		// roll over
		function () {
			var h_pos = parseFloat($(this).attr('h_pos'));
			$("#horz_menu").css({'background-position' : '0 '+h_pos+'px'});
			$("a.nav_main").css({'color' : '#E2D0A7'});
			$(this).css({'color' : '#fff'});
			
		},
		// roll off
		function () {
			//$("#horz_menu").css({'background-position' : '0 7px'});
			$(this).css({'color' : '#E2D0A7'});
			initMenuRollover();
		}
	); 


	/*******
	 * 
	 * // ARROW ROLLOVERS 
	 */

	$("div.arrow_left_static").hover(
		function () {
			if ($(this).css('background-color') == "transparent") {				
			} else {
				$(this).css({'background-position' : '0 0px'});
				$(this).css({'cursor' : 'pointer'});
			}
		},
		function () {
			if ($(this).css('background-color') == "transparent") {	
			} else {
				$(this).css({
					'background-position': '0 -14px'
				});
			}
		}
	); 
	
	$("div.arrow_left_trigger").hover(
		function () {
			if ($(this).css('background-color') == "transparent") {
			} else {
				$("div.arrow_left_static").css({
					'background-position': '0 0px'
				});
			}
		},
		function () {
			if ($(this).css('background-color') == "transparent") {
			} else {
				$("div.arrow_left_static").css({
					'background-position': '0 -14px'
				});
			}
		}
	);
	

	// PAGINATION
	RENDER_pagination = function (totalPages, currentPage, type) {
		
		if (!type) {
			s_type = "search_adv_results";
		}
		else {
			if (type == "basic") {
				s_type = "search_basic_results";
			}
			else {
				s_type = "search_adv_results";
			}
		}
	
		// clear menu
		$("div#menu_pagination").empty();
		var pg_str = "";
		
		pg_str = "<table class='PaginationMenuOuter'><tr>";
		if ((currentPage) == 1) {
			pg_str += "<td class='pg_cntrl td_back'><div totalPages='" + totalPages + "' currentPage='" + currentPage + "' class='pg_cntrl' id='blank_pg_back'>&nbsp;</div></td>";	
		} else {
			pg_str += "<td class='pg_cntrl td_back'><div totalPages='" + totalPages + "' currentPage='" + currentPage + "' class='pg_cntrl' id='pg_back'>&nbsp;</div></td>";	
		}
		pg_str += "<td class='pg_num1'><div>" + currentPage + "</div></td>";	
		pg_str += "<td><div>of&nbsp;&nbsp;"+totalPages+"</div></td>";
		pg_str += "<td class='spacer_num'><div>&nbsp;</div></td>";
		if ((currentPage) == (totalPages)) {
			pg_str += "<td class='pg_cntrl td_next'><div totalPages='" + totalPages + "' currentPage='" + currentPage + "' class='pg_cntrl' id='blank_pg_next'>&nbsp;</div></td>";
		} else {
			pg_str += "<td class='pg_cntrl td_next'><div totalPages='" + totalPages + "' currentPage='" + currentPage + "' class='pg_cntrl' id='pg_next'>&nbsp;</div></td>";
		}
		pg_str += "</tr></table>";
		
		$("div#menu_pagination_top").append(pg_str);
		$("div#menu_pagination_bot").append(pg_str);
		
		
	}
	
	back_next_mouseover_num = function (e) {
		if ($(this).attr("id") == "pg_back") {
			if ($(this).attr("currentPage") != 1) {
				$("body").css('cursor', 'pointer');
			}
		}
		
		if ($(this).attr("id") == "pg_next") {
			if ($(this).attr("currentPage") != $(this).attr("totalPages")) {
				$("body").css('cursor', 'pointer');
			}
		}
	}

	back_next_mouseout_num = function (e) {
		$("body").css('cursor', 'default');		
	}
		
	back_next_pagination_num = function (e) {
		
		if ($(this).attr("id") == "pg_back") {
			if ($(this).attr("currentPage") != 1) {
				location.href = "/restaurants/search_adv_results/page:" + (parseFloat($(this).attr("currentPage"))-1);
			}
		}
		
		if ($(this).attr("id") == "pg_next") {
			if ($(this).attr("currentPage") != $(this).attr("totalPages")) {
				location.href = "/restaurants/search_adv_results/page:" + (parseFloat($(this).attr("currentPage"))+1);
			}
		}

	}
	
	back_next_mouseover_ltr = function (e) {
		if ($(this).attr("id") == "pg_back") {
			if ($(this).attr("currentPage") != 'A') {
				$("body").css('cursor', 'pointer');
			}
		}
		
		if ($(this).attr("id") == "pg_next") {
			if ($(this).attr("currentPage") != 'Z') {
				$("body").css('cursor', 'pointer');
			}
		}
	}

	back_next_mouseout_ltr = function (e) {
		$("body").css('cursor', 'default');		
	}
	
	back_next_pagination_ltr = function (e) {
		
		if ($(this).attr("id") == "pg_back") {
			if ($(this).attr("currentPage") != 'A') {
				location.href = "/restaurants/directory/" + $(this).attr("nextPage");
			}
		}
		
		if ($(this).attr("id") == "pg_next") {
			if ($(this).attr("currentPage") != 'Z') {
				location.href = "/restaurants/directory/" + $(this).attr("nextPage");
			}
		}

	}
});


function addCommas(nStr, dollars){
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	dollars = dollars ? '$' : '';
	return dollars + x1 + x2;
}
