//our cache object
var notesCache = {};
var hotelZonesCache = {};


window.addEvent("domready", function(){
	Element.implement({  
		appendHTML: function(html,where){  
			return this.grab(new Element('div',{'html':html}),where);  
		}  
	}); 
});

//Confirm box - redirects to loc if accepted else does nothing.
function confirmAndRedirect (loc, msg) {
	if(msg == '' || msg == null || msg == "undefined") {
		if(confirm('Are you sure you want to delete this?')) {
			window.location = loc;
		}
	} else {
		if(confirm(msg)) {
			window.location = loc;
		}	
	}
	
}

function id (x) {
	return document.getElementById(x);
}

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);
	}
	return [curleft,curtop];
}

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 toggleState () {
	var state = readCookie("slideState");
	//alert(state);
	if (state == "true") {
		createCookie("slideState","false",1);
		//alert("slideState is now false");
	} else {
		createCookie("slideState","true",1);
		//alert("slideState is now true");
	}
}

//Hover Box
var smartHoverBox = function(boxTimer, xOffset, yOffset, smartBoxSuffix, smartBoxClose) {
	var smartBoxes = $(document.body).getElements('[id$=' + smartBoxSuffix + ']');
	var closeElem = $(document.body).getElements('.' + smartBoxClose);  
	var closeBoxes = function() { smartBoxes.setStyle('display', 'none'); };
	closeElem.addEvent('click', function(){ closeBoxes() }).setStyle('cursor', 'pointer');
	var closeBoxesTimer = 0;	
	smartBoxes.each(function(item){	
		var currentBox = item.getProperty('id');
		currentBox = currentBox.replace('' + smartBoxSuffix + '', '');	 
 
		$(currentBox).addEvent('mouseleave', function(){
			closeBoxesTimer = closeBoxes.delay(boxTimer);									  
		});	
 
		item.addEvent('mouseleave', function(){
			closeBoxesTimer = closeBoxes.delay(boxTimer);								  
		});	
 
		$(currentBox).addEvent('mouseenter', function(){ 
			if($defined(closeBoxesTimer)) $clear(closeBoxesTimer); 
		});
 
		item.addEvent('mouseenter', function(){
			if($defined(closeBoxesTimer)) $clear(closeBoxesTimer);
		});  
 
 
 
		item.setStyle('margin', '0'); 
		$(currentBox).addEvent('mouseenter', function(){					 
				smartBoxes.setStyle('display', 'none');
				item.setStyles({ display: 'block', position: 'absolute' }).setStyle('z-index', '1000000'); 
 
				//coordinates and size vars and math 
				var windowSize = $(window).getSize();
				var windowScroll = $(window).getScroll();
				var halfWindowY = windowSize.y / 2;
				var halfWindowX = windowSize.x / 2;
				var boxSize = item.getSize();
				var inputPOS = $(currentBox).getCoordinates();
				var inputCOOR = $(currentBox).getPosition();
				var inputSize = $(currentBox).getSize(); 
				var inputBottomPOS = inputPOS.top + inputSize.y; 
				var inputBottomPOSAdjust = inputBottomPOS - windowScroll.y
				var inputLeftPOS = inputPOS.left + xOffset;
				var inputRightPOS = inputPOS.right;
				var leftOffset = inputCOOR.x + xOffset;  
 
				if(halfWindowY < inputBottomPOSAdjust) {
					item.setStyle('top', inputPOS.top - boxSize.y - yOffset);
					if (inputLeftPOS < halfWindowX) { item.setStyle('left', leftOffset); }
					else { item.setStyle('left', (inputPOS.right - boxSize.x) - xOffset); }; 
				}
				else {
					item.setStyle('top', inputBottomPOS + yOffset);
					if (inputLeftPOS < halfWindowX) { item.setStyle('left', leftOffset); } 
					else { item.setStyle('left', (inputPOS.right - boxSize.x) - xOffset); };
				};
		}).setStyle('cursor', 'pointer');	
	});
};

function toggle(x){document.getElementById(x).style.display=document.getElementById(x).style.display=="none"?"block":"none";document.getElementById(x).blur();}

var propertyDrop ;
var columnHolder;
function serviceGroupsNumberPropertiesSelected( type){
	if( columnHolder == undefined ){
		columnHolder = $('columnHolder');
	}
	//get property dropdown via ajax
	if( propertyDrop == undefined ){
		var req = new Request({
			url: 'scripts/ajax.php?type=getProperties',
			onSuccess: function (msg, responseXML){
				propertyDrop = msg;
				updateServiceGroupsColumnOptionns(  propertyDrop);
			}
		}
		).send("type="+type);                          
	}else{
		updateServiceGroupsColumnOptionns(  propertyDrop);
	}
}

function updateServiceGroupsColumnOptionns( propertyDrop){
	columnHolder.appendHTML('<div>  '+propertyDrop+' [ <a href="javascript:;" onClick="this.getParent(\'div\').destroy();">Remove</a> ]</div>', 'bottom' );
}
function checkComplete(id, complete, length){
	if( complete == length ){
		id.appendHTML('<hr/>', 'bottom');
	}
}

function addNewProperty( propertyID, id, showremove ){
	if( showremove == null ){ showremove == true; }
	if( propertyID.length > 1 ){
		var propertyCounter = 0;
		var complete = 0 ;
		for( var i = 0 ; i < propertyID.length; i++ ){
			var req = new Request({
				url: 'scripts/ajax.php?type=drawProperty',
				onComplete: function (msg, responseXML){
					var append = ''+msg;
					if( showremove ){
						append += ' [ <a href="javascript:;" onClick="this.getParent(\'div\').destroy();">Remove</a> ] '
					}
					id.appendHTML( append , 'bottom');
					
					complete++;
					checkComplete(id, complete, propertyID.length);
				}
			}
			).send("propertyID="+propertyID[i]);
		}
		
	}else{
		var req = new Request({
			url: 'scripts/ajax.php?type=drawProperty',
			onSuccess: function (msg, responseXML){
				var append = ''+msg;
					if( showremove ){
						append += ' [ <a href="javascript:;" onClick="this.getParent(\'div\').destroy();">Remove</a> ] '
					}
					append += '';
					id.appendHTML( append , 'bottom');
			}
		}
		).send("propertyID="+propertyID); 
		
	}
	
}


function setPopUpText( text ){
	var dialog = $("dialog_content");
	dialog.removeClass('load');
	dialog.set('html', text);
}
function setPopupTitle(title){
	$('dialog_title').set('html', title);
}
function showPopup(){
	$("pop_content").addClass("load");
	$("fb-modal").fade("in");
}

function getHotelsInZone(zone){
	setPopUpText('');
	setPopupTitle('Hotels in Zones');
	showPopup();
	if(notesCache[zone]) {
		setPopUpText(msg);
	}else{
		var req = new Request({
			url: 'scripts/ajax.php?type=getHotelsInZone',
			onSuccess: function (msg, responseXML){
				hotelZonesCache[zone] = msg;
				setPopUpText(msg);
			}
		}
		).send("zoneID="+zone); 
	}
}

function viewNotes( rateID ){
	setPopUpText('');
	setPopupTitle('Notes');
	showPopup();
	setPopUpText($('notesPopup_'+rateID).get('html'));
}

function popUpPopUp( popupID , title ){
	setPopUpText('');
	setPopupTitle(title);
	showPopup();
	setPopUpText($(popupID).get('html'));
}

