function S4() 
{
   return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
}

String.prototype.startsWith = function(str) 
{
	return (this.match("^"+str)==str);
}

String.prototype.endsWith = function(str) 
{
	return (this.match(str+"$")==str);
}

$.fn.wait = function(time, type) {
        time = time || 1000;
        type = type || "fx";
        return this.queue(type, function() {
            var self = this;
            setTimeout(function() {
                $(self).dequeue();
            }, time);
        });
    };
    

    
function createCookie(name,value,days) {
    	if (days) {
    		var date = new Date();
    		date.setTime(date.getTime()+(days*24*60*60*1000));
    		var expires = "; expires="+date.toGMTString();
    	}
    	else var expires = "";
    	document.cookie = name+"="+value+expires+"; path=/";
    }

    function readCookie(name) {
    	var nameEQ = name + "=";
    	var ca = document.cookie.split(';');
    	for(var i=0;i < ca.length;i++) {
    		var c = ca[i];
    		while (c.charAt(0)==' ') c = c.substring(1,c.length);
    		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    	}
    	return null;
    }

    function eraseCookie(name) {
    	createCookie(name,"",-1);
    }    
    
function submitTreForm(e, link)
{
	var url = "/searches/tre_search";
	var history='';
	if(typeof link != 'undefined' && link)
	{
		// use passed in link as url and do not collect form data
		url = link;
		history = link;
	}
	else
	{
		var data = jQuery(this).serialize();	
		history = url+"?"+data;
	}
    $.history.load("ajax_load"+history);
	
	
	return false;
	
}

var last_call_was_ajax = false;

function loadPage(hash)
{
	// hash doesn't contain the first # character.
	if(hash) 
	{
		if(hash.startsWith("ajax_load"))
		{
			hash = hash.substring("ajax_load".length);
			$('div.main-body-container').mask('');
			
			last_call_was_ajax = true;
			jQuery.ajax({
			   type: "GET",
			   url: hash,
			   complete: function(XMLHttpRequest, textStatus)
			   {
			   		$('div.main-body-container').unmask();
			   },
			   success: function(msg){
				   if(msg.startsWith('Javascript:'))
				   {
					   msg = msg.substring(11);
					   eval(msg);
				   }
				   else
				   {
					   $("#right-side-content").html(msg);
				   }
			   }
			 });
			return false;
		}
		else
		{
			if(last_call_was_ajax)
			{
				last_call_was_ajax = false;
				location.reload();
				return false;
			}
			return true;
		}
	} else {
		if(last_call_was_ajax)
		{
			last_call_was_ajax = false;
			location.reload();
			
			return false;
		}
		else
		{
			//start page
			return true;
		}
	}
}

function showGoogleMap(config)
{
	/*
	 * show on the map
	 */
	$('#googlemap-container').show();
	$('#googlemap').gMap(config); 		
	//store config to be used in popup window
	var config_str =  $.toJSON(config);
	$('#googlemap').attr('config', config_str);
}


function openGoogleMapWin2(key, height,width) {
    var offset_x,offset_y, win_width, win_height, padding;

    var config = getCurrentMapConfig();

	$('#googlemap_popup').modal({
		onOpen: function (dialog) {
		dialog.overlay.fadeIn('slow', function () {
			dialog.container.slideDown('slow', function () {
				 $('#googlemap_popup').show();
                 $('#googlemap_popup').gMap(config);
                 dialog.data.fadeIn('slow');
			});
		});
	},
	onClose: function (dialog) {
		dialog.data.fadeOut('slow', function () {
			dialog.container.slideUp('slow', function () {
				dialog.overlay.fadeOut('slow', function () {
					$.modal.close(); // must call this!
					$('#googlemap_popup').hide();
				});
			});
		});
	},
	closeClass: "modal-close",
	containerId: 'map-container'
	});

}

function getCurrentMapConfig()
{
	var config = $('#googlemap').attr('config');
    config = $.evalJSON(config);
 
    return config;
}

function mustLoginModal(msg, show_login, show_register)
{
	var container_id="mustlogin-container_both";
	
	$('#mustlogin_popup caption.mustlogin_msg').html(msg);
	$('a.open_register').click(function() {
		$('#mustlogin_popup td.register_section').show();
		$('#mustlogin_popup td.submit_register').show();
		$('#mustlogin_popup td.login_section').css('border-right-style', 'solid');
		$('#mustlogin_popup label.intro').show();
		if($.browser.msie && $.browser.version.substr(0,1)< 8)
		{
			$('#mustlogin-container_single').width(430);
			$('#mustlogin-container_single').height(300);
		}
		else
		{
			$('#mustlogin-container_single').width(410);
			$('#mustlogin-container_single').height(270);
		}
		return;
	});

	$('a.open_login').click(function() {
		$('#mustlogin_popup td.login_section').show();
		$('#mustlogin_popup td.submit_login').show();
		$('#mustlogin_popup td.login_section').css('border-right-style', 'solid');
		$('#mustlogin_popup label.intro').show();
		if($.browser.msie && $.browser.version.substr(0,1)< 8)
		{
			$('#mustlogin-container_single').width(430);
			$('#mustlogin-container_single').height(300);
		}
		else
		{
			$('#mustlogin-container_single').width(410);
			$('#mustlogin-container_single').height(270);
		}
	});
	
	$('#mustlogin_popup td.login_later a').click(function() {
		$.modal.close();
		$('#mustlogin_popup').hide();
	})
	
	if(show_login == true)
	{
		$('#mustlogin_popup td.login_section').show();
		$('#mustlogin_popup td.submit_login').show();
		$('#mustlogin_popup td.login_section').css('border-right-style', 'solid');
		$('#mustlogin_popup label.intro').show();
	}
	else
	{
		$('#mustlogin_popup td.login_section').hide();
		$('#mustlogin_popup td.submit_login').hide();
		$('#mustlogin_popup label.intro').hide();
		container_id = "mustlogin-container_single";
		$('#mustlogin_popup td.login_later').hide();
	}
	
	if(show_register == true)
	{
		$('#mustlogin_popup td.register_section').show();
		$('#mustlogin_popup td.submit_register').show();
	}
	else
	{
		$('#mustlogin_popup td.register_section').hide();
		$('#mustlogin_popup td.submit_register').hide();
		$('#mustlogin_popup label.intro').hide();
		$('#mustlogin_popup td.login_section').css('border-right-style', 'none');
		container_id = "mustlogin-container_single";
		$('#mustlogin_popup td.login_later').hide();
	}
	
	$('#mustlogin_popup').modal({
		onOpen: function (dialog) {
		dialog.overlay.fadeIn('slow', function () {
			dialog.container.slideDown('slow', function () {
				 $('#mustlogin_popup').show();
                 dialog.data.fadeIn('slow');
			});
		});
	},
	onClose: function (dialog) {
		dialog.data.fadeOut('slow', function () {
			dialog.container.slideUp('slow', function () {
				dialog.overlay.fadeOut('slow', function () {
					$.modal.close(); // must call this!
					$('#mustlogin_popup').hide();
				});
			});
		});
	},
	closeClass: "modal-close",
	containerId: container_id
	});

}

function addToWishlist(id, callback, el)
{
	$('div.main-body-container').mask('');
	
	jQuery.ajax({
		   type: "GET",
		   url: '/addwishlist?destid='+id,
		   complete: function(XMLHttpRequest, textStatus)
		   {
		   		$('div.main-body-container').unmask();
		   },
		   success: function(msg){

			   /*
			    * update hash to ensure back button works
			    */
			   $.bbq.pushState('#ajax_load/userwishlist');
			   callback(el, msg);
		   }
		 });

	 return false;
}

function removeFromWishlist(id, callback, el)
{
	$('div.main-body-container').mask('');
	
	jQuery.ajax({
		   type: "GET",
		   url: '/removewishlist?destid='+id,
		   complete: function(XMLHttpRequest, textStatus)
		   {
		   		$('div.main-body-container').unmask();
		   },
		   success: function(msg){
			   /*
			    * update hash to ensure back button works
			    */
			   $.bbq.pushState('#ajax_load/userwishlist');
			   callback(el, msg);
			   
		   }
		 });

	 return false;
}

function login()
{
	var data = jQuery(this).serialize();
	$('div.main-body-container').mask('');
	
	jQuery.ajax({
		   type: "POST",
		   url: '/users/ajlogin',
		   data: data,
		   complete: function(XMLHttpRequest, textStatus)
		   {
		   		$('div.main-body-container').unmask();
		   },
		   success: function(msg){
			   var stat = eval('(' + msg + ')');
			   if(stat.status == 'ok')
			   {
					$.modal.close(); // must call this!
					$('#mustlogin_popup').hide();
					// update header
					$('#user-login-box').hide();
					$('#user-nickname').attr('href', '/users/view/'+stat.id);
					$('#user-nickname').html(stat.nickname);
					$('#user-welcome-box').show();
					var uw = eval('(' + stat.user_wishlist + ')');
					$('#wishlist_user_list').html(uw.user_wishlist);
					if(uw.wishlist_alert ==  true)
					{
						$('img.wishlist_alert').show();
					}
					else
					{
						$('img.wishlist_alert').hide();
					}		
					createCookie('fp_loggedin', 'yes');
			   }
			   else if(stat.status == 'failed')
			   {
				   $('#mustlogin_popup div.error').html(stat.error);
			   }
		   }
		 });

	 return false;
}

function register()
{
	var data = jQuery(this).serialize();
	$('div.main-body-container').mask('');
	
	jQuery.ajax({
		   type: "POST",
		   url: '/users/ajregister',
		   data: data,
		   complete: function(XMLHttpRequest, textStatus)
		   {
		   		$('div.main-body-container').unmask();
		   },
		   success: function(msg){
			   var stat = eval('(' + msg + ')');
			   if(stat.status == 'ok')
			   {
					$.modal.close(); // must call this!
					$('#mustlogin_popup').hide();
					// update header
					$('#user-login-box').hide();
					$('#user-nickname').attr('href', '/users/view/'+stat.id);
					$('#user-nickname').html(stat.nickname);
					$('#user-welcome-box').show();
					var uw = eval('(' + stat.user_wishlist + ')');
					$('#wishlist_user_list').html(uw.user_wishlist);
					if(uw.wishlist_alert ==  true)
					{
						$('img.wishlist_alert').show();
					}
					else
					{
						$('img.wishlist_alert').hide();
					}		

					createCookie('fp_loggedin', 'yes');
			   }
			   else if(stat.status == 'failed')
			   {
				   $('#mustlogin_popup div.error').html(stat.error);
			   }
		   }
		 });

	 return false;
}
