/*------------------------------------------------------------------------
# JoomFlix - Javascript file
# ------------------------------------------------------------------------
# Copyright (C) 2009 Total Design and Technology Ltd. All Rights Reserved.
# @license - GPL
# Author: Royce Harding
# Websites:  http://www.tdandt.com
# This file may not be redistributed in whole or significant part.
-------------------------------------------------------------------------*/
var jf_ajax = new Array();
var jf_timer = 0;


function jf_ShowPopup (hoveritem, type, id, e, image, base) {

	// first hide any existing popups
	jf_HidePopup();	
	
	var tempX = 0;
	var tempY = 0;

	if (e.pageX) {
		tempX = e.pageX
	} else if (e.clientX) {
		tempX = window.event.clientX + (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
	} else {
		tempX = NULL;
	}
	if (e.pageY) {
		tempY = e.pageY
	} else if (e.clientY) {
		tempY = window.event.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
	} else {
		tempY = NULL;
	}
	jf_timer = setTimeout(function() { jf_ShowPopupNow(hoveritem, type, id,  image, tempY, tempX, base); }, 750);
}

function jf_ShowPopupNow(hoveritem, type, id,  image, tempY, tempX, base)	{	

	var topoffset = 200;
	var leftoffset = 50;
	
	// see if the popup already exists on the page
	hp = document.getElementById('jf_popup');
	
	if (!(hp)) {
		// if it doesn't exist, then create it and get a handle
		jf_createPopup(base, image);
		hp = document.getElementById('jf_popup');
	}
		
	if (tempX < 0){tempX = 0}
	if (tempY < 0){tempY = 0}		
	hp.style.top  = (tempY - topoffset) + 'px';
	hp.style.left = (tempX + leftoffset) + 'px';			

	// call the ajax function to get the data for the popup
	var index = jf_ajax.length;
	jf_ajax[index] = new sack();
	jf_ajax[index].requestFile = 'index.php?option=com_joomflix&task='+type+'&id='+id;	
	jf_ajax[index].onCompletion = function(){ jf_populatePopup(index) };
	jf_ajax[index].onLoading = function(){ jf_showWaitMessage(base) };	
	//alert (jf_ajax.join("."));
	jf_ajax[index].runAJAX();		// Execute AJAX function

	// set the visibility of the popup
	hp.style.visibility = "Visible";
}

function jf_HidePopup()
{
	hp = document.getElementById('jf_popup');
	
	if (hp) {
		document.getElementById('jf_popupheader').innerHTML = '';
		document.getElementById('jf_popupbody').innerHTML = '';
		document.getElementById('jf_popupfooter').innerHTML = '';			
		hp.style.visibility = "Hidden";	
	}
}

function jf_populatePopup(index)
{

// code to check that DOMParser exists!
        if(typeof(DOMParser) == 'undefined') {
       DOMParser = function() {}
       DOMParser.prototype.parseFromString = function(str, contentType) {
        if(typeof(ActiveXObject) != 'undefined') {
         var xmldata = new ActiveXObject('MSXML.DomDocument');
         xmldata.async = false;
         xmldata.loadXML(str);
         return xmldata;
        } else if(typeof(XMLHttpRequest) != 'undefined') {
         var xmldata = new XMLHttpRequest;
         if(!contentType) {
          contentType = 'application/xml';
         }
         xmldata.open('GET', 'data:' + contentType + ';charset=utf-8,' + encodeURIComponent(str), false);
         if(xmldata.overrideMimeType) {
          xmldata.overrideMimeType(contentType);
         }
         xmldata.send(null);
         return xmldata.responseXML;
        }
       }
      }
	try //Internet Explorer
	  {
		xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.async="false";
		xmlDoc.loadXML(trim(jf_ajax[index].response));
	  }
	catch(e)
	  {
	  try //Firefox, Mozilla, Opera, etc.
		{
		  parser=new DOMParser();
		  xmlDoc=parser.parseFromString(trim(jf_ajax[index].response),"text/xml");
		}
	  catch(e) {alert(e.message)}
	  }

		var header = xmlDoc.getElementsByTagName('header')[0].childNodes[0].nodeValue
		var body = xmlDoc.getElementsByTagName('body')[0].childNodes[0].nodeValue
		var footer = xmlDoc.getElementsByTagName('footer')[0].childNodes[0].nodeValue
		
		document.getElementById('jf_popupheader').innerHTML = header;
		document.getElementById('jf_popupbody').innerHTML = body;
		document.getElementById('jf_popupfooter').innerHTML = footer;	

}

function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

function jf_showWaitMessage(base) {

	var MyHttpLoading = '<img src="' + base + 'ajax_loading/jf_ajax_loader_vert_bar2.gif" border="0" alt="running" />';
	document.getElementById('jf_popupbody').innerHTML = MyHttpLoading;
}

function jf_createPopup(base, image) {

	var bodyRef = document.getElementsByTagName("body").item(0);
	var jf_popup = document.createElement("div");
	bodyRef.appendChild(jf_popup);
	jf_popup.onclick = jf_HidePopup; // Note - must be a reference to a function name, not a function call!
	jf_popup.id = 'jf_popup';
	jf_popup.name = 'jf_popup';
	jf_popup.className = 'jf_popup';	

	var jf_dialog = document.createElement("div");
	jf_popup.appendChild(jf_dialog);
	jf_dialog.name = 'jf_dialog';
	jf_dialog.className = 'jf_dialog';

	var jf_content = document.createElement("div");
	jf_dialog.appendChild(jf_content);
	jf_content.name = 'jf_content';
	jf_content.className='jf_content';
	jf_content.style.backgroundImage = "url(" + base + image + ")"; 
	  
	var jf_wrapper = document.createElement("div");
	jf_content.appendChild(jf_wrapper);
	jf_wrapper.name = 'jf_wrapper';
	jf_wrapper.className='jf_wrapper';  
	  
	var jf_t = document.createElement("div");
	jf_wrapper.appendChild(jf_t);
	jf_t.name = 'jf_t';
	jf_t.className='jf_t';
	jf_t.style.backgroundImage = "url(" + base + image + ")";
	  
	var jf_hd = document.createElement("div");
	jf_wrapper.appendChild(jf_hd);
	jf_hd.name = 'jf_hd';
	jf_hd.className='jf_hd';  

	var jf_h1_1 = document.createElement("h1");
	jf_hd.appendChild(jf_h1_1);
	jf_h1_1.name = 'jf_h1_1';
	jf_h1_1.className='jf_h1_1';  
	  
	var jf_popupheader = document.createElement("span");
	jf_h1_1.appendChild(jf_popupheader);
	jf_popupheader.id = 'jf_popupheader';
	jf_popupheader.name = 'jf_popupheader';
	jf_popupheader.className='jf_popupheader'; 

	var jf_bd = document.createElement("div");
	jf_wrapper.appendChild(jf_bd);
	jf_bd.name = 'jf_bd';
	jf_bd.className='jf_bd';  
	  
	var jf_popupbody = document.createElement("span");
	jf_bd.appendChild(jf_popupbody);
	jf_popupbody.id = 'jf_popupbody';
	jf_popupbody.name = 'jf_popupbody';
	jf_popupbody.className='jf_popupbody'; 
	  
	var jf_ft = document.createElement("div");
	jf_wrapper.appendChild(jf_ft);
	jf_ft.name = 'jf_ft';
	jf_ft.className='jf_ft';  
	  
	var jf_popupfooter = document.createElement("span");
	jf_ft.appendChild(jf_popupfooter);
	jf_popupfooter.id = 'jf_popupfooter';
	jf_popupfooter.name = 'jf_popupfooter';
	jf_popupfooter.className='jf_popupfooter';   
	  
	var jf_b = document.createElement("div");
	jf_dialog.appendChild(jf_b);
	jf_b.name = 'jf_b';
	jf_b.className='jf_b';
	jf_b.style.backgroundImage = "url(" + base + image + ")";

	var jf_b_empty_child = document.createElement("div");
	jf_b.appendChild(jf_b_empty_child);
	jf_b_empty_child.name = 'jf_b_empty_child';
	jf_b_empty_child.className='jf_b_empty_child'; 
	jf_b_empty_child.innerHTML='&nbsp;&nbsp;[x]';
	jf_b_empty_child.style.backgroundImage = "url(" + base + image + ")";
}


function compareprice(ItemId, AmazonId, Prices, Tag){
	
	var jf_amazonajax = new Array();
	var index = jf_amazonajax.length;
	jf_amazonajax[index] = new sack();
	//alert('index.php?option=com_joomflix&task=amazonprices&ItemId='+ItemId+'&AmazonId='+AmazonId+'&Tag='+Tag);
	jf_amazonajax[index].requestFile = 'index.php?option=com_joomflix&task=amazonprices&ItemId='+ItemId+'&AmazonId='+AmazonId+'&Tag='+Tag;
	jf_amazonajax[index].onCompletion = function(request){document.getElementById('price_spinner_'+ItemId).style.display = 'none'; document.getElementById(Prices).innerHTML = jf_amazonajax[index].response};
	jf_amazonajax[index].onLoading = function(request){document.getElementById('price_spinner_'+ItemId).style.display = 'block'};	
	jf_amazonajax[index].runAJAX();
	
}
