
String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}

function left_hand_menu(div)
{	
	newHieght = document.getElementById(div).height;
	//alert(newHieght);
	div = div-1;
	newHieght = (div*24)+10;
	//alert(newHieght);
	new Effect.Move('left_hand_menu_dynamic',{x:15 ,y: newHieght, mode: 'absolute',duration: 0.4});
}

function left_hand_menu_out()
{	
	new Effect.Move('left_hand_menu_dynamic',{x:15 ,y: 10, mode: 'absolute',duration: 0.4});
}


function ajax_login(url,div,user,pass)
{
	 params_string = "user="+user+"&pass="+pass;
	 AJAX_fetch_populate(div,url,'get',params_string,true,true);
	
	
}


function ajax_post_comment(msg)
{
	if (msg.trim() == "") {
		alert("I'm sorry you must actually put something in the box!\nGive it another go...");
		return;
	}

	msg = escape(msg);
	
	var comment_field;
	comment_field = $F('comment_field');
	
	var field_value;
	field_value = $F('field_value');
	
	var other_fields;
	other_fields = $F('other_fields');
	
	params = "post_comment=true&msg="+msg.trim()+"&field="+comment_field+"&value="+field_value+"&other_fields="+other_fields;

	$('message').value = "";
	
	$('comments_container').style.display = "block";
	
	AJAX_fetch_append('comments_container','/ajax/post_comments.php','get',params,true,true);
	
}

function AJAX_fetch_populate(div,file,method,params_string,no_status,no_errors) {
 // alert(params_string);
    var AJAX_on_error;
    
    //Specify whether to turn loading status on or off.
    if (!no_status) {
        status_loading(1);
    }
    
    //Specify whether to turn errors on or off.
    if (!no_errors) {
        //TODO:: This doesnt work yet. Not sure how to specify dynamically.
        AJAX_on_error = "AJAX_error";
    }
    
    var objupdate = new Ajax.Updater(
        {success: div},
        file,
        {
            method: method,
            parameters: params_string,
            evalScripts: true,
            onSuccess: AJAX_success,
            onFailure: AJAX_error
        }
    );
}


function AJAX_fetch_append(div,file,method,params_string,no_status,no_errors) {
 // alert(params_string);
    var AJAX_on_error;
    
    //Specify whether to turn loading status on or off.
    if (!no_status) {
        status_loading(1);
    }
    
    //Specify whether to turn errors on or off.
    if (!no_errors) {
        //TODO:: This doesnt work yet. Not sure how to specify dynamically.
        AJAX_on_error = "AJAX_error";
    }
    
   	
	var objupdate = new Ajax.Updater(
        {success: div},
        file,
        {
            method: method,
            parameters: params_string,
            evalScripts: true,
            insertion: Insertion.Bottom,
			onSuccess: AJAX_success,
            onFailure: AJAX_error
        }
    );
}


//JPK
//General success function for ajax requests.
//At the moment, just handling loading message. Could be extended down the track.
function AJAX_success(success) {
    //status_loading(0);
}

//JPK
//General Ajax error handler.
function AJAX_error(request) {
  //  alert("Stub Function for an AJAX Error.\n\nNeed to handle this bastard!!!\n\n".request);
}


function add_image_to_gallery(new_image_container)
	{
		gallery_thumb_table =  $('gallery_table_thumbs');
		
		image_uploader = $('image_uploader');
		
		if(image_uploader.contentDocument){
			iframeSrc = image_uploader.contentDocument;
		}else if (image_uploader.contentWindow){
			iframeSrc = image_uploader.contentWindow;
		}else if (image_uploader.document){
			iframeSrc = image_uploader.document;
		}
		
		
		
		//alert (image_uploader.id);
		 new_image = iframeSrc.getElementById(new_image_container).innerHTML;
		 insert = new Insertion.Top(gallery_thumb_table,new_image);
	
	}
function expand__minimise_div(div)
	{
		div_object = $(div);
		Effect.toggle(div_object,'blind',{duration: '0.3'});
		/*if(div_object.style.display == "none")
			{
				div_object.style.display = "";
			}
			else
			{
				div_object.style.display = "none";
			}
		*/
	}
function youtube_preview(youtubeid,close_preview)
	{
		
		if(youtubeid == "")
			{
				alert("No Youtube ID entered");
				return;
			}
		if(close_preview == false)
		{
		
		var objBody = document.getElementsByTagName("body").item(0);
		var objOverlay = document.createElement("div");
		objOverlay.setAttribute('id','overlay');
		objOverlay.style.display = 'none';
		objBody.appendChild(objOverlay);
			
				
		var objLightbox = document.createElement("div");
		objLightbox.setAttribute('id','lightbox');
		objLightbox.style.display = 'none';
		objBody.appendChild(objLightbox);
	
		var objOuterImageContainer = document.createElement("div");
		objOuterImageContainer.setAttribute('id','outerImageContainer');
		objLightbox.appendChild(objOuterImageContainer);

		var objImageContainer = document.createElement("div");
		objImageContainer.setAttribute('id','imageContainer');
		objOuterImageContainer.appendChild(objImageContainer);
		
		var arrayPageSize = getPageSize();
		Element.setHeight('overlay', arrayPageSize[1]);
		new Effect.Appear('overlay', { duration: 0.2, from: 0.0, to: 0.8 });
		
		var arrayPageSize = getPageSize();
		var arrayPageScroll = getPageScroll();
		var lightboxTop = arrayPageScroll[1] + (arrayPageSize[3] / 15);

		Element.setTop('lightbox', lightboxTop);
		Element.show('lightbox');
		youtube_html = '<div><object width="425" height="350"><param name="movie" value="http://www.youtube.com/v/'+youtubeid+'"></param><param name="wmode" value="transparent"></param><embed src="http://www.youtube.com/v/'+youtubeid+'" type="application/x-shockwave-flash" wmode="transparent" width="425" height="350"></embed></object></div>';
		youtube_html +=	'<div onclick="youtube_preview(\''+youtubeid+'\',true)">Close Preview</div>';
						
		objImageContainer.innerHTML = youtube_html;
		}
		else
		{
		new Effect.Fade('overlay'); // ,{ duration: 0.2, from: 0.0, to: 0.8 })
		new Effect.Fade('lightbox'); // ,{ duration: 0.2, from: 0.0, to: 0.8 }
		$('imageContainer').innerHTML = '';
		}
		
	}
	
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}	

// getPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.org
//
function getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}


function expand_event_timing(elm)	{
		//alert(elm.id+"_div");
		event_div = elm.id+"_div";
		if(elm.id == "recurring_yes")
			{
				// lets check if the other div 
				recurring_div = $('recurring_yes_div');
				if(recurring_div.style.display == "none")
					{
						expand__minimise_div("recurring_yes_div");
						expand__minimise_div("recurring_no_div");
					}
			} 
		else if(elm.id == "recurring_no")	
			{
				non_recurring_div = $('recurring_no_div');
				if(non_recurring_div.style.display == "none")
					{
						expand__minimise_div("recurring_no_div");
						expand__minimise_div("recurring_yes_div");
						
					}
				
			}
}

function set_contact_details(chkObj) {
	//var frmObj = document.signup
	
	if (chkObj.checked) {
		document.sign_up['contact[phone]'].value 			= document.sign_up['pub[pub_phone]'].value;
		document.sign_up['contact[fax]'].value 				= document.sign_up['pub[pub_fax]'].value;
		document.sign_up['contact[address]'].value 			= document.sign_up['pub[pub_address]'].value;
		document.sign_up['contact[suburb]'].value 			= document.sign_up['pub[pub_suburb]'].value;
		document.sign_up['contact[state]'].selectedIndex 	= document.sign_up['pub[pub_state]'].selectedIndex;
		document.sign_up['contact[postcode]'].value 		= document.sign_up['pub[pub_postcode]'].value;
		document.sign_up['contact[email]'].value 			= document.sign_up['pub[pub_email]'].value;
	} else {
		document.sign_up['contact[phone]'].value 			= "";
		document.sign_up['contact[fax]'].value 				= "";
		document.sign_up['contact[address]'].value 			= "";
		document.sign_up['contact[suburb]'].value 			= "";
		document.sign_up['contact[state]'].selectedIndex 	= 0;
		document.sign_up['contact[postcode]'].value 		= "";
		document.sign_up['contact[email]'].value 			= "";
	}
}

function addComment() {
	document.location.href = '#add_comment';
	$('message').value = '';
	$('message').focus();
}

var currentSearchRequest = false;
var lastSentQuery = ""
var tm = setTimeout("searchOnTheFlyCheck",300);
function searchOnTheFly() {
	var currentStr = $('search_input').value;
	
	if ((currentStr.length > 3) && (!currentSearchRequest)) {
		var url = "/search_results_ajax.php";
		var pars = "q=" + currentStr;
		
		currentSearchRequest = true;
		lastSentQuery = currentStr;
		
		var ajaxObj = new Ajax.Request(
			url, 
			{
				method: 'get', 
				parameters: pars, 
				onComplete: searchOnTheFlyResponse
			});		
	}
}

function searchOnTheFlyCheck() {
	if ((!currentSearchRequest) && ($('search_input').value != lastSentQuery)) {
		searchOnTheFly();
	}
}

function searchOnTheFlyResponse(requestObj) {
	currentSearchRequest = false;
	
	if (requestObj.responseText != "") {
		$('searchContent').innerHTML = requestObj.responseText;
		$('searchOnTheFlyResults').style.display = "block";
	}
}

function closeSearch() {
	$('searchOnTheFlyResults').style.display = "none";
}
