/**
 * jQuery onload
 */
$(document).ready(function() {
	// Cufon replacement
	Cufon.replace('h2.title');
	Cufon.replace('#colLeft h2');
});

jQuery.fn.center = function () {
  this.css("position","absolute");
  this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px");
  //this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
  return this;
}


// JavaScript Document
var currentUrl = false;
function roll(img_name1, img_src1,img_url)
{
	document[img_name1].src = img_src1;
	currentUrl = img_url;
}

function popUp(url,width,height){

    attrib = 'width=' + width + ',height='+ height+ ',top=10,left=10,scrollbars=yes';
    popwindow = window.open(url,'PopUp',attrib);
}


/**
 * THIS IS TAKEN FROM
 * http://www.quirksmode.org/js/findpos.html
 */
function findPos(obj) {
  var curleft = curtop = 0;
  if (obj.offsetParent) {
    curleft = obj.offsetLeft
    curtop = obj.offsetTop
    while (obj = obj.offsetParent) {
      curleft += obj.offsetLeft
      curtop += obj.offsetTop
    }
  }
  return [curleft,curtop];
}

function loadjs(filename) {	
	elScript = document.createElement('script');
	elScript.setAttribute("type","text/javascript");
	elScript.setAttribute("src", filename);
	document.getElementsByTagName('head')[0].appendChild(elScript);
}

function loadcss(filename) {	
	elScript = document.createElement('link');
	elScript.setAttribute("type","text/css");
	elScript.setAttribute("rel","stylesheet");
	elScript.setAttribute("href", filename);
	document.getElementsByTagName('head')[0].appendChild(elScript);
}

function clearUploads(form) {
	$(form).find(':input:file').each(function() {
		$(this).val(""); 
		$(this).replaceWith($(this).clone(true));		
	});
}

function updateUploadLists(form, resultframe) {
	$(form).find(':input:file').each(function() {
		var name = this.getAttribute("name"); 
		var newElement = $(resultframe).contents().find('#fileUpload'+name); 
		if (newElement.length > 0) {
			$('#fileUpload'+name).replaceWith(newElement);
		} 
	});
}

function focusAccordionElement(accordionFormId, formElement) {
	var elAccordionForm = $(accordionFormId);
  var elParts = elAccordionForm.find(".ui-accordion-content");
  elParts.each(function(index) {
  	var found = ($(this).find("#"+formElement.getAttribute("id")).length > 0);
  	if (found) elAccordionForm.accordion("activate", index);
  });
  formElement.focus();
}

function saveForm(form, info) { 
	var formTarget = form.target;
	
	// Show the save popover
	$(info).center();
	$(info).fadeIn("slow");
	
	// Create the iframe... 
	var rndIframeId = "save_iframe" + Math.floor(Math.random()*10000);
	var iframe;
	try {
	  iframe = document.createElement('<iframe name="'+rndIframeId+'">');
	} catch (ex) {
	  iframe = document.createElement('iframe');
	} 
	iframe.setAttribute("id",rndIframeId); 
	iframe.setAttribute("name",rndIframeId); 
	iframe.setAttribute("width","0"); 
	iframe.setAttribute("height","0"); 
	iframe.setAttribute("border","0"); 
	iframe.setAttribute("style","width: 0; height: 0; border: none;");   
	
	// Add to document... 
	form.parentNode.appendChild(iframe); 
	
	iframeId = document.getElementById(rndIframeId);   
	// Add event... 
	var eventHandler = function()  {  
		if (iframeId.detachEvent) 
			iframeId.detachEvent("onload", eventHandler); 
		else iframeId.removeEventListener("load", eventHandler, false);   
		// Message from server... 
		if (iframeId.contentDocument) { 
			content = iframeId.contentDocument.body.innerHTML;
		} else if (iframeId.contentWindow) { 
			content = iframeId.contentWindow.document.body.innerHTML; 
		} else if (iframeId.document) { 
			content = iframeId.document.body.innerHTML; 
		}  
		// Hide the info div
		$(info).hide();

		// Update file upload fields
		updateUploadLists(form, iframeId);
		
		// Clear the upload inputs
		clearUploads(form);
		
		// Delete the iframe... 
		setTimeout('iframeId.parentNode.removeChild(iframeId)', 250); 
		
		// Set properties of form back to it's original properties... 
		$(form).attr('target',formTarget);
		
		var elFormaction = $(form).find('#formaction'); 
		if (elFormaction.length > 0) {
			elFormaction.val('');
		}
	}   
	if (iframeId.addEventListener) 
		iframeId.addEventListener("load", eventHandler, true); 
	if (iframeId.attachEvent) 
		iframeId.attachEvent("onload", eventHandler);   
	
	// Set temporary properties of form... 
	$(form).attr('target',rndIframeId);
	
	var elFormaction = $(form).find('#formaction'); 
	if (elFormaction.length > 0) {
		elFormaction.val('save');
	} else {
		var elFa;
		try {
			elFa = document.createElement('<input id="formaction" name="formaction" />');
		} catch (ex) {
			elFa = document.createElement('input');
		} 
		elFa.setAttribute("id","formaction"); 
		elFa.setAttribute("name","formaction");
		elFa.setAttribute("type","hidden");
		elFa.setAttribute("value","save");
		form.appendChild(elFa); 
	} 	
		
	// Submit the form... 
	form.submit();	
} 
