/*
+----------------------------------------------------------------------+
| Copyright (c) 2009 Matthew Kirkpatrick                               |
+----------------------------------------------------------------------+
| Authors: Matthew Kirkpatrick <matt@the-kirkpatricks.com>             |
+----------------------------------------------------------------------+
 Created:      September 29, 2006
 Last Updated: July 10, 2009
*/
/* CONTACT US
*/
contact_us = function (name, email, msg, mailto) {
	
	var result = new Ajax.Request (
		'/contact/processors/index.inc.php',
		{
			parameters: 'action=contact&name=' + name + '&email=' + email + '&msg=' + msg + '&mailto=' + mailto,
			method: 'post',
			evalScripts: true,
			onSuccess: function (feedback) {
				$('system_feedback').className = 'success';
				$('system_feedback').innerHTML = '<div style="margin: 20px 0px 20px 0px;">' + feedback.statusText + '</div>';
				$('iWindowBase').style.display		= 'none'; // close the iWindow*
				$('iWindowContainer').style.display	= 'none'; // close the iWindow*
				new Effect.BlindDown($('system_feedback'), {duration: 0.25}); // show feedback
				new Effect.BlindUp($('system_feedback'), {delay: 7, duration: 0.25}); // hide feedback
				//setTimeout("$('iWindowBase').style.display		= 'none'", 2000);
				//setTimeout("$('iWindowContainer').style.display	= 'none'", 2000);
				$('name').value		= '';
				$('email').value	= '';
				$('msg').value		= '';
				$('contact_form').style.display		= 'none';
				$('contact_confirm').style.display	= '';

			},
			
			onFailure: function (feedback) {
				$('system_feedback').className = 'error';
				$('system_feedback').innerHTML = '<div style="margin: 20px 0px 20px 0px;">' + feedback.statusText + '</div>';
				new Effect.BlindDown($('system_feedback'), {duration: 0.25});
				new Effect.BlindUp($('system_feedback'), {delay: 7, duration: 0.25});
			}
		}
	);
}

close_iWindow = function () {
	$('iWindowBase').style.display		= 'none'; // close the iWindow*
	$('iWindowContainer').style.display	= 'none'; // close the iWindow*
}

/* SLIDESHOW
*/
showImage = function (id, img, w, h) {
	
	if (id == 12) { id = 1; } else { id = id + 1; }
	
	$('iWindowBase').style.display		= ''; // open the iWindow*
	$('iWindowContainer').style.display	= ''; // open the iWindow*
	
	new Ajax.Updater(
		$('slideshow_display'),
		'/files_images/slide_image.inc.php',
		{
			parameters:		'id=' + id + '&img=' + img + '&w=' + w + '&h=' + h,
			method:			'post',
			evalScripts:	true
		}
	);
	new Effect.Appear($('slideshow_display'), { from: 0, to: 1, duration: 1.0});
	//$('slideshow_display').innerHTML = '<img src="' + img + '" width="' + w + '" height="' + h + '" border="0" onclick="showImage(' + id + ', \'/files_images/nature/medium/' + id + '.jpg\', \'' + w + '\', \'' + h + '\');" style="cursor: pointer; border: 1px solid #000000;" /><br /><br /><p style="text-align: center;">[ <a href="#" onclick="close_iWindow(); return false;" style="color: #ffffff; font-weight: bold;">close</a> ]</p>';
	
}

/* SIGNUP
*/
signMeUp = function (email) {
	
	var result = new Ajax.Request (
		'/files_includes/_accounts.inc.php',
		{
			parameters: 'action=signup&email=' + email,
			method: 'post',
			evalScripts: true,
			onSuccess: function (feedback) {
				$('system_feedback').className = 'success';
				$('system_feedback').innerHTML = '<div style="margin: 20px 0px 20px 0px;">' + feedback.statusText + '</div>';
				$('iWindowBase').style.display		= 'none'; // close the iWindow*
				$('iWindowContainer').style.display	= 'none'; // close the iWindow*
				new Effect.BlindDown($('system_feedback'), {duration: 0.25}); // show feedback
				new Effect.BlindUp($('system_feedback'), {delay: 7, duration: 0.25}); // hide feedback
				//setTimeout("$('iWindowBase').style.display		= 'none'", 2000);
				//setTimeout("$('iWindowContainer').style.display	= 'none'", 2000);
			},
			
			onFailure: function (feedback) {
				$('system_feedback').className = 'error';
				$('system_feedback').innerHTML = '<div style="margin: 20px 0px 20px 0px;">' + feedback.statusText + '</div>';
				new Effect.BlindDown($('system_feedback'), {duration: 0.25});
				new Effect.BlindUp($('system_feedback'), {delay: 7, duration: 0.25});
			}
		}
	);
}

/* LOGIN
*/
logMeIn = function (email, pass, parameters) {
	
	var result = new Ajax.Request (
		'/files_includes/_accounts.inc.php',
		{
			parameters: 'action=login&email=' + email + '&pass=' + pass + '&' + prameters,
			method: 'post',
			evalScripts: true,
			onSuccess: function (feedback) {
				$('system_feedback').className = 'success';
				$('system_feedback').innerHTML = '<div style="margin: 20px 0px 20px 0px;">' + feedback.statusText + '</div>';
				$('iWindowBase').style.display		= 'none'; // close the iWindow*
				$('iWindowContainer').style.display	= 'none'; // close the iWindow*
				new Effect.BlindDown($('system_feedback'), {duration: 0.25}); // show feedback
				new Effect.BlindUp($('system_feedback'), {delay: 7, duration: 0.25}); // hide feedback
				//setTimeout("$('iWindowBase').style.display		= 'none'", 2000);
				//setTimeout("$('iWindowContainer').style.display	= 'none'", 2000);
			},
			
			onFailure: function (feedback) {
				$('system_feedback').className = 'error';
				$('system_feedback').innerHTML = '<div style="margin: 20px 0px 20px 0px;">' + feedback.statusText + '</div>';
				new Effect.BlindDown($('system_feedback'), {duration: 0.25});
				new Effect.BlindUp($('system_feedback'), {delay: 7, duration: 0.25});
			}
		}
	);
}

/* PAGE NAV
*/
pageNav = function (show, hide, parameters) {
	// hide all
	for (var i = 0; i < hide.length; i++) {
		if ($('pageNav_' + hide[i])) { $('pageNav_' + hide[i]).className = 'pageNav'; }
	}
	// show
	if ($(show)) { $(show).className = 'pageNavHL'; }
	
	new Ajax.Updater (
		$('content'),
		'/' + show.replace('pageNav_','') + '/files_content/index.php',
		{
			parameters: parameters,
			method: 'post',
			evalScripts: true
		}
	);

}

/* BASIC PHP INCLUDE SIMULATOR
*/
php_include = function (locationID, include_file_path, parameters) {

	if (locationID.length > 0) { // simple single or multiple file include

		for (var i = 0; i < locationID.length; i++) {
	
			new Ajax.Updater (
				locationID[i],
				include_file_path[i],
				{
					parameters: parameters[i],
					method: 'post',
					evalScripts: true
				}
			);
	
		}

	} else { // single or multiple file include with div population
	
		for (var i = 0; i < include_file_path.length; i++) {
	
			new Ajax.Request (
				include_file_path[i],
				{
					parameters: parameters[i],
					method: 'post',
					evalScripts: true
				}
			);
	
		}
	}
}

/* FIND AN ELEMENT'S ABSOLUTE POSITION
*/
getPos = function (obj) {
	var pos = { x: obj.offsetLeft || 0, y: obj.offsetTop || 0 };
	while (obj = obj.offsetParent) {
		pos.x += obj.offsetLeft || 0;
		pos.y += obj.offsetTop || 0;
	}
	return pos;
}

/* AJAX FEEDBACK USAGE:
Place this XHTML where ever you want the error to show up.
<div id="ajax_success" style="display: none;"><div class="feedback" id="success" style="font-size: 9px; padding: 2px;"></div><br /></div>
<div id="ajax_failure" style="display: none;"><div class="feedback" id="error" style="font-size: 9px; padding: 2px;"></div><br /></div>

This is the usage in the ajax statement
new Ajax.Request('./to/some/php/file.php', {parameters: pars, method: 'post', evalScripts: true, onSuccess: ajax_success, onFailure: ajax_failure});

SOMEWHERE INSIDE file.php
if ( conditional TRUE ) { header('HTTP/1.1 200 You successfully blah blah blah!'); }
	else { header('HTTP/1.1 400 Could not update blah blah blah.'); }
*/
ajax_success = function (feedback) {
	$('success').style.display = 'block';
	$('success').innerHTML = feedback.statusText;
	$('ajax_success').style.display = 'block';
	new Effect.Fade($('ajax_success'), {delay: 5, duration: 2.5});
	new Effect.BlindUp($('ajax_success'), {delay: 5, duration: 2.5});
}

ajax_failure = function (feedback) {
	$('error').style.display = 'block';
	$('error').innerHTML = feedback.statusText;
	$('ajax_failure').style.display = 'block';
	new Effect.Fade($('ajax_failure'), {delay: 7, duration: 2.5});
	new Effect.BlindUp($('ajax_failure'), {delay: 7, duration: 2.5});
}

ajax_success_bright = function (feedback) {
	$('success_bright').style.display = 'block';
	$('success_bright').innerHTML = feedback.statusText;
	$('ajax_success').style.display = 'block';
	new Effect.Fade($('ajax_success'), {delay: 5, duration: 2.5});
	new Effect.BlindUp($('ajax_success'), {delay: 5, duration: 2.5});
}

ajax_failure_bright = function (feedback) {
	$('error_bright').style.display = 'block';
	$('error_bright').innerHTML = feedback.statusText;
	$('ajax_failure').style.display = 'block';
	new Effect.Fade($('ajax_failure'), {delay: 7, duration: 2.5});
	new Effect.BlindUp($('ajax_failure'), {delay: 7, duration: 2.5});
}

/* AJAX STATUS:
Place this XHTML where ever you want the error to show up.
<div id="ajax_status" style="display: none;"><img src="/files_images/ajax_status/indicator_000000.gif" alt="Loading . . . Please Wait!" title="Loading . . . Please Wait!" name="ajax_status_img" border="0" id="ajax_status_img" /></div>

This is the usage in the ajax statement
new Ajax.Request('./to/some/php/file.php', {parameters: pars, method: 'post', evalScripts: true, onLoading: ajax_loading, onComplete: ajax_complete});
*/
ajax_loading = function () {
	if ($('ajax_status')) { $('ajax_status').style.display = 'block'; }
}

ajax_complete = function () {
	if ($('ajax_status')) { $('ajax_status').style.display = 'none'; }
}

// HELP FRAME GENERATOR
help_frame_generator = function (findme) {
	var help_frame_data = '<div class="help_frame_container" id="help_frame_container" style="display: none;"></div><div class="help_frame_border" id="help_frame_border"><iframe src="' + findme + 'assistant/index.php" frameborder="0" class="help_frame" id="help_frame"></iframe></div>';
	document.body.insertAdjacentHTML('beforeEnd', help_frame_data);
}

// SUBMIT ON ENTER
submitOnEnter = function (myfield, e) {
	var keycode;
	if (window.event) { keycode = window.event.keyCode; }
		else if (e) { keycode = e.which; }
		else { return true; }
	if (keycode == 13) {
		myfield.form.submit();
		return false;
	}
	else { return true; }
}

// CONFIRM SUBMIT
confirmSubmit = function (msg) {
	var agree = confirm(msg);
	if (agree) { return true; }
		else { return false; }
}

// CONFIRM FORM SUBMIT
confirmFormSubmit = function (form_id, msg) {
	if (document.getElementById) { var	myForm	= document.getElementById(form_id); }
	var agree = confirm(msg);
	if (agree) {
		myForm.submit();
		return false;
	} else { return false; }
}

// UPDATE PARENT
NL_updateParent = function (URL) { //v1.0
  opener.document.location = URL;
}

switchDisplay = function (show, hide) {
	// hide all
	for (var i = 0; i < hide.length; i++) {
		if ($(hide[i])) { $(hide[i]).style.display	= 'none'; }
	}
	// show all
	for (var i = 0; i < show.length; i++) {
		if ($(show[i])) { $(show[i]).style.display	= ''; }
	}

}

/* TRIM!
*/
trimStr = function (str) {
	var	str = str.replace(/^\s\s*/, ''),
		ws = /\s/,
		i = str.length;
	while (ws.test(str.charAt(--i)));
	return str.slice(0, i + 1);
}

/**
 * Emulates insertAdjacentHTML(), insertAdjacentText() and 
 * insertAdjacentElement() three functions so they work with Netscape 6/Mozilla
 * by Thor Larholm me@jscript.dk
 */
if (typeof HTMLElement!="undefined" && !HTMLElement.prototype.insertAdjacentElement) {
	HTMLElement.prototype.insertAdjacentElement = function (where,parsedNode) {
	  switch (where){
		case 'beforeBegin':
			this.parentNode.insertBefore(parsedNode,this); break;
		case 'afterBegin':
			this.insertBefore(parsedNode,this.firstChild); break;
		case 'beforeEnd':
			this.appendChild(parsedNode); break;
		case 'afterEnd':
			if (this.nextSibling) { this.parentNode.insertBefore(parsedNode,this.nextSibling); }
				else { this.parentNode.appendChild(parsedNode); }
			break;
	  }
	};

	HTMLElement.prototype.insertAdjacentHTML = function (where,htmlStr) {
		var r = this.ownerDocument.createRange();
		r.setStartBefore(this);
		var parsedHTML = r.createContextualFragment(htmlStr);
		this.insertAdjacentElement(where,parsedHTML);
	};

	HTMLElement.prototype.insertAdjacentText = function (where,txtStr) {
		var parsedText = document.createTextNode(txtStr);
		this.insertAdjacentElement(where,parsedText);
	};
}

var toQueryComponent = function (input) {
    if (!input.name || input.disabled)
        return "";

    var n = urlencode(input.name);

    switch (input.type) {
    case "text":
    case "password":
    case "submit":
    case "hidden":
        return n + "=" + urlencode(input.value);
    case "textarea":
        // normalize line breaks as CR LF pairs as per RFC 1866
        var v = input.value.split(/\r\n|\r|\n/).join("\r\n");
        return n + "=" + urlencode(v);
    case "checkbox":
    case "radio":
        if (!input.checked)
            return "";
        var v = getRealValue(input);
        if (v === null) v = "on";
        return n + "=" + urlencode(v);
    case "select-one":
    case "select-multiple":
        var nvp = [];
        var opt, i = 0;
        while ((opt = input.options[i++]) != null) {
            if (opt.selected) {
                var v = getRealValue(opt);
                if (v === null) v = opt.text;
                // older versions of IE do not support Array.push
                nvp[nvp.length] = n + "=" + urlencode(v);
            }
        }
        return nvp.join("&");
    default:
        // input types reset, button, image, and file not implemented
        return "";
    }
}

var urlencode = function (str) {
    var v;
    try { v = encodeURIComponent(str); } catch (e) { v = escape(str); }
    //return v.replace(/%20/g,"+");
    return v;
}

var getRealValue = function (input) {
    var attr = input.getAttributeNode("value");
    return (attr && attr.specified) ? input.getAttribute("value") : null;
}

var buildQueryString = function (form) {
    var str = "";
    var element, i = 0;
    while ((element = form.elements[i++]) != null) {
        var qc = toQueryComponent(element);
        if (qc != "") str += "&" + qc;
    }
    return str.substring(1);
}

strpos = function (haystack, needle, offset) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // *     example 1: strpos('Kevin van Zonneveld', 'e', 5);
    // *     returns 1: 14
    var i = haystack.indexOf(needle, offset); // returns -1
    return i >= 0 ? i : false;
}

//--------------------------------------------------------------------//
//                        MACROMEDIA DEFAULT JS                       //
//--------------------------------------------------------------------//
MM_goToURL = function () { //v3.0
  var i, args=MM_goToURL.arguments; document.MM_returnValue = false;
  for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"+args[i+1]+"'");
}

MM_preloadImages = function () { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

MM_swapImgRestore = function () { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

MM_findObj = function (n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

MM_swapImage = function () { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

MM_displayStatusMsg = function (msgStr) { //v1.0
  status=msgStr;
  document.MM_returnValue = true;
}

MM_openBrWindow = function (theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

MM_popupMsg = function (msg) { //v1.0
  alert(msg);
}

MM_jumpMenu = function (targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

MM_jumpMenuGo = function (selName,targ,restore){ //v3.0
  var selObj = MM_findObj(selName); if (selObj) MM_jumpMenu(targ,selObj,restore);
}

MM_changeProp = function (objName,x,theProp,theValue) { //v6.0
  var obj = MM_findObj(objName);
  if (obj && (theProp.indexOf("style.")==-1 || obj.style)){
    if (theValue == true || theValue == false)
      eval("obj."+theProp+"="+theValue);
    else eval("obj."+theProp+"='"+theValue+"'");
  }
}
//--------------------------------------------------------------------//
