/*
* File: header.js
* Purpose: Common javascript functions that will be shared throughout the website.
* 
* History: 07/27/2010 Brian - Created.
*
*/

//**************************************** GLOBAL VARIABLES


/*
 * Called when the page is ready. Set the default settings for any pop-up
 * that will be displayed. Every pop-up will be displayed in a iframe.
 */
/*$(function(){
    $.nyroModalSettings({
        type: 'iframe',
        closeButton: false,
        windowResize: true,
        titleFromIframe: null,
        title: null,
				width: null,
				height: null,
				minWidth: 680,
				minHeight: 680
		    });
});*/

function repimpLinks(){
	$('.nyroModal', document).nyroModal();
}

//called when DOM is ready.
$(document).ready(function(){
	displayMessage();
	//Fix IE6 homepage.
	if($.browser.msie && $.browser.version == "6.0"){
		ps = document.getElementById("property-search");
		if(typeof(ps) != "undefined"){
			$(ps).css("top","15px");
			$(ps).css("right","20px");
			$(ps).css("z-index","5");
			$(ps).css("position","absolute");
		//	ps.style.zIndex = "5";
		//	ps.style.position = "absolute";
		//	ps.style.top = "15px";
		//	ps.style.right = "20px";
		
		}
	}
	
});


/*
 * Save a property to a new gallery. Perform ajax call to add the gallery.
 */
function savePropertyToNewGallery(mls_no, name, description){
	if(!description || description == 'undefined' || description == ''){
		description = " ";
	}
	
	//ajax call to add a property to a new gallery.
	$.post(
		"/my-hawaii-life/galleries/create-gallery-and-save-property.php",
		{
			name: name,
			description: description,
			mls_no: mls_no
		},
		function(response){
			//remove pop-up.
			parent.$.nyroModalRemove();
			
			//saving property was a success.
			if(response.successFlg == 1){
				parent.$("#p_save"+mls_no).html('<span id="p_save'+mls_no+'" class="tick"><a href="/my-hawaii-life/galleries/list.php">Saved</a></span>');				
			}
			
			//display confirmation/error message.
			displayMessage();
		},
		"json"
	);
}

/*
 * Save a property to an existing gallery. Perform ajax call to add the gallery.
 */
function savePropertyToGallery(mls_no, gallery_id){
	
	//ajax call to add property to an existing gallery.
	$.post(
		"/my-hawaii-life/galleries/addprop-process.php",
		{
			gallery_id : gallery_id,
			mls_no : mls_no
		},
		function(response){
			//remove pop-up.
			parent.$.nyroModalRemove();
			
			//saving property was a success.
			if(response.successFlg == 1){
				$("#p_save"+mls_no).html('<span id="p_save'+mls_no+'" class="tick"><a href="/my-hawaii-life/galleries/list.php">Saved</a></span>');
			}
			
			//display confirmation/error message.
			displayMessage();		
		},
		"json"
	);
}


function removeProperty(mls, galleryID){

	//ajax call to remove a gallery.
	$.post(
		"/my-hawaii-life/galleries/remove-property.php",
		{
			mls_no : mls,
			gallery_id : galleryID
		},
		function(response){
			if(response.successFlg == 1){
				alert($("#gallery"+galleryID+'_'+mls).length);
				
				$("#gallery"+galleryID+'_'+mls).fadeOut("slow");
				
			//problem removing property.
			}else{
				//reload page.
				window.parent.location = window.parent.location;				
			}
		},
		"json"
	);
}

function popupInit(closeBtnObj, wrapElementHeight, width){
	
	//set onCLick event for the close link. remove the pop-up.
	closeBtnObj.click(function(evt){
		evt.preventDefault();		 	 
	 	$.nyroModalRemove();
	 							 	
		return false;
									
	});		

	adjustPopupSize(wrapElementHeight, width);
		
	displayMessage();
}

/*
 * Called when the page is ready (on the detail and photos pages at least). Set the size of the page since this page
 * will be called as a pop-up window. This page will be viewed inside an iframe.
 */
function adjustPopupSize(wrapElementHeight, width){
		
	//Set default width
	if(width === undefined){
		width = 680;
	}
	
	var popup_body_height = wrapElementHeight + 50;
	var main_window_height = $(document).height();
	var specific_height = 0;
	//determine what height to use.
	if(popup_body_height < main_window_height){
		specific_height = popup_body_height;
	}else{
		specific_height = main_window_height;
	}

	//Make the changes to height and width	
	if($.browser.msie && $.browser.version == '6.0'){
		document.getElementById("nyroModalWrapper").style.width = (width+18)+"px";
		document.getElementById("nyroModalWrapper").style.overflow = "hidden";
		document.getElementById("nyroModalContent").style.width = (width+30)+"px";
		document.getElementById("nyroModalContent").style.position = "absolute";
		document.getElementById("nyroModalContent").style.top = "0px";
		document.getElementById("nyroModalContent").style.left = "-20px";
	}else{
		$.nyroModalSettings({
			width: width,
			height: specific_height,
			minWidth: width,
			minHeight: specific_height
		});
	}
		
}

// ------------------------------------------------------------ MY HL POPUP FUNCTIONS

/*
 * Called when access to a page is denied.
 * Will load the login popup
 */
 function showLogin(){
    $('#loginLink').click();
 }


/** 
 * This function is used to trigger a popup from a textfield.
 * Ideally, this whole clumsy routine would be replace by a simple form element that posts
 * to a popup. Not sure if it can be done and this routine works for now...
 */
	function checkEnter(e, clickLink){ //e is event object passed from function invocation
		var characterCode; //literal character code will be stored in this variable
		
		if(e && e.which){ //if which property of event object is supported (NN4)
			e = e;
			characterCode = e.which; //character code is contained in NN4's which property
		}else{
			e = event
			characterCode = e.keyCode; //character code is contained in IE's keyCode property
		}
		
		if(characterCode == 13){ //if generated character code is equal to ascii 13 (if enter key)
			$('#'+clickLink).click();
			return false;
		}else{
			return true;
		}
	}


