// declare image location for general use

// email general bits
	var goodMail = false;

window.addEvent('domready', function(){
	var genBg = $('popbg');
	var eleScrollLen = $$('.popScroll').length;
	if(eleScrollLen > 0){
		var eleScroll = $$('.popScroll')
		eleScroll.each(function(ele){
			var scrolltowrite = new Fx.SmoothScroll({duration:1000, links: ele});
		});
	}
	if(genBg){
		screenPos();
		genBg.addEvent('click', function(e){fadeOut(e);});
	}		
	$$('.popOpen').addEvent('click', function(e){injectDivs(e,this,'compare');});
	if($('popTerms')){
		$('popTerms').addEvent('click', function(e){injectDivs(e,this,'terms');});
	}
	var pwCount = $$('.popWarranty').length;
	if(pwCount > 0){
		$$('.popWarranty').addEvent('click', function(e){injectDivs(e,this,'warranty');});
	}
	var servCount = $$('.popService').length;
	if(servCount > 0){
		$$('.popService').addEvent('click', function(e){injectDivs(e,this,'service');});
	}
	if($('mailfriend')){
		$('mailfriend').addEvent('click', function(e){injectDivs(e,this,'mail');});
	}
	$$('.closePop').addEvent('click', function(e){fadeOut(e);});
	
	// close the screen 
	if($('popbg')){
		document.addEvent('keydown', function(event){
			if(event.key == 'esc'){
				var useEsc = genBg.get('opacity');
				if(useEsc > 0){
					escFadeOut();
				}
			}
		});
	}
	
});

//function to get the sceen & browser window dimentios and place the pop up accordingly
function screenPos(){
	var contSize = $('container').getSize(); //get the container height
	if($('footblock')){
		var footLSize = $('footblock').getSize(); //get the container height
	}
	var footSize = $('footer').getSize(); //get the container height
	
	// set the total screen height
	if($('footblock')){
		var screenHeight =  contSize.y + footLSize.y + footSize.y + 18;
	} else{
		var screenHeight =  contSize.y + footSize.y + 18;
	}
	
	var win = window.getSize(); // get windows dimensions
	var screenSizeW = win.x; // get the windows width
	var screenSizeH = win.y; // get the windows height
	
	//set the height of the backgrond div
	$('popbg').set({
		'styles':{
			'height': screenHeight
		}
	});
	
	//set  the poistion on the viewing panel;
	var popView = $('pop_view').getSize(); // get the size of the containing div
	// set the x position
	var popVX = (screenSizeW/2) -  (popView.x/2);
	var popVY = (screenSizeH/2) -  (popView.y/2);
	if(popVY < 0){
		popVY = 0;
	}
	$('pop_view').set({
		'styles':{
			'left': popVX
		}
	});
}

//fading in function
function fadeIn(e){
	e.stop();
	$('popbg').fade('0','0.55');
	$('pop_view').fade('0','1');
}
//closing fade function
function fadeOut(e){
	e.stop();
	$('popbg').fade('0.55','0').get('tween').chain(function(){
		$('popbg').dispose();
	});
	$('pop_view').fade('1','0').get('tween').chain(function(){	
		$('pop_view').dispose();
	});
}
//closing fade function
function escFadeOut(){
	$('popbg').fade('0.55','0');
	$('pop_view').fade('1','0');
}

// create the pop up
function injectDivs(e,ele,type){
	e.stop;
	var escClose = true;
	var fixedPop = false;
	var popBg = new Element('div',{'id':'popbg','class':'hide'});
	var popView = new Element('div',{'id':'pop_view','class':'hide'});
	var popClose = new Element('div',{'id':'poptop'});
	var popHold;
	switch(type){
		case 'mail': popHold = new Element('div',{'id':'mailBlock', 'class':'mainPopInfo'}); type = 'mail'; fixedPop = true; break;
		case 'compare': popHold = new Element('div',{'id':'compareProducts', 'class':'mainPopInfo'}); type = 'compare'; break;
		case 'terms': popHold = new Element('div',{'id':'injectTerms', 'class':'mainPopInfo'}); type = 'terms'; break;
		case 'service': popHold = new Element('div',{'id':'injectService', 'class':'mainPopInfo'}); type = 'service'; break;
		case 'warranty': popHold = new Element('div',{'id':'injectWarranty', 'class':'mainPopInfo'}); type = 'warranty'; break;
		default: popHold = new Element('div',{'id':'compareProducts', 'class':'mainPopInfo'}); type = 'compare'; break;
	}
	//inject the needed divs
	popBg.inject($('footer'),'after');
	popView.inject($('popbg'),'after');
	popClose.inject($('pop_view'));
	popHold.inject($('poptop'),'after');
	
	//set the required content and styling
	$('popbg').setStyle('opacity','0');
	$('pop_view').setStyle('opacity','0');
	if(fixedPop){
		$('pop_view').addClass('fixedPop');
	}
	if(type == 'mail'){
		$('pop_view').addClass('mailpop');
	}
	$('popbg').removeClass('hide');
	$('pop_view').removeClass('hide');
	$('poptop').set('html', '<p class="textRight"><a href="#" title="Close" class="closePop"><img src="'+siteAddr+'_img/front_end/buttons/close.gif" alt="Close" /></a></p>');
	switch(type){
		case 'mail':mailInject(); break;
		case 'compare':compareProducts(); break;
		case 'terms':injectTerms(); break;
		case 'warranty':injectWarranty(); break;
		case 'service':
			var kWClass = ele.get('class');
			var getServ = kWClass.split(' '); 
			var servID = getServ[2].replace('serv', '');
			injectService(servID); 
			break;
		default:compareProducts(); break;
	}
	
	//position the screen, fade in and scroll to the top of the page
	screenPos();
	fadeIn(e);
	// set the bits for closing windows
	$('popbg').addEvent('click', function(e){fadeOut(e);});
	$$('.closePop').addEvent('click', function(e){fadeOut(e);});
	document.addEvent('keydown', function(event){
		if(event.key == 'esc' && escClose){
			var useEsc = $('popbg').get('opacity');
			if(useEsc > 0){
				fadeOut(event);
			}
			escClose = false;
		}
	});
}

function injectService(servID) { 
	var service = siteAddr+"../_services/checkout.php?action=service&service_id="+servID;
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null) {
		alert ("Browser does not support HTTP Request");
		return;
	} 
	xmlHttp.onreadystatechange=displayService;
	xmlHttp.open("GET",service,true);
	xmlHttp.send(null);
	return false;	
}

function displayService() {
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { 
		var tmp = xmlHttp.responseText;
		if(tmp != '') {
			$('injectService').set('html',tmp);
		}
	}	
}

function injectWarranty() {
	var service = siteAddr + "_services/basket.php?action=warranty";
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null) {
		alert ("Browser does not support HTTP Request");
		return;
	} 
	xmlHttp.onreadystatechange=displayWarranty;
	xmlHttp.open("GET",service,true);
	xmlHttp.send(null);
	return false;	
}

function displayWarranty(){
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { 
		var tmp = xmlHttp.responseText;
		if(tmp != '') {
			$('injectWarranty').set('html',tmp);
		}
	}
}

/* for compare */
function GetXmlHttpObject()	{
	var xmlHttp=null;
	try	{
 // Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e)	{
 // Internet Explorer
		try	{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)	{
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
 	return xmlHttp;
}

function compareProducts() {
	
	var service = siteAddr + "_services/compare.php?action=load_compare";
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null) {
		alert ("Browser does not support HTTP Request");
		return;
	} 
	xmlHttp.onreadystatechange=showCompare;
	xmlHttp.open("GET",service,true);
	xmlHttp.send(null);
	return false;
}

function showCompare() {
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { 
		var tmp = xmlHttp.responseText;
		if(tmp != '') {
			$('compareProducts').set('html', tmp);
		}
	}
}

function addCompareProduct(product_id) {
	var checked = 0;
	if(document.getElementById('id_compare_'+product_id).checked == true) { checked = 1; }
	var service = siteAddr + "_services/compare.php?action=add_compare&product_id="+product_id+"&checked="+checked;
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null) {
		alert ("Browser does not support HTTP Request");
		return;
	} 
	xmlHttp.onreadystatechange=showCompareSide;
	xmlHttp.open("GET",service,true);
	xmlHttp.send(null);
	return false;
}

function showCompareSide() {
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { 
		var tmp = xmlHttp.responseText;
		if(tmp != '') {
			tmps = tmp.split('||');
			check = tmps[0].split('#');
			if(check[1] == 1) { 
				document.getElementById('comp_form_'+check[0]).className = 'compare_off compare clear';	
			} else {
				document.getElementById('comp_form_'+check[0]).className = 'compare_off clear';
				document.getElementById('comp_form_'+check[0]).checked = false;
			}
			$('compProds').set('html', tmps[1]);
		}
	}
}

// for mail to a friend
function mailInject(){
	var mailProd = $$('.genhtag h1 b').get('text');
	var mailText = '<h3>Mail a Friend</h3><p class="extraMarg">Email the <b>' + mailProd + '</b> page link to your friend.</p><p class="extraMarg">Simply fill out the form below and then send your resulting email to your friend.<form name="eMailer"><input id="friendEmail" type="text" name="email" value="Enter your friends email address" title="Enter your friends email address" class="textarea"><input id="theirname" type="text" name="theirname" value="Enter your name" title="Enter your name" class="textarea"><p id="sendMail" class="noMarg textRight"><img src="'+siteAddr+'_img/front_end/buttons/sendemail.gif" title="Send your email" alt ="Send your email" /></p></form>';
	
	$('mailBlock').set('html',mailText);
	$$('.textarea').addEvent('focus', function(e){inputClear(this.id);});
	$('sendMail').addEvent('click', function(e){mailThisUrl();});
}

function mailThisUrl(){
	var theirName = $('theirname').value;
	var initialsubj="Think this may interest you";
	var initialmsg="Hi,%0D%0A%0D%0AFound this product and thought of you: %0D%0A%0D%0A" + window.location + '%0D%0A%0D%0AThanks%0D%0A%0D%0A' + theirName;
	checkEmailAddress(document.eMailer.email, theirName);
	if(goodMail){
		window.location = "mailto:"+document.eMailer.email.value+"?subject="+initialsubj+"&body="+initialmsg;
   	}
}

function checkEmailAddress(field, theirName) {
	var goodEmail = field.value.match(/\b(^(\S+@).+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.org)|(\.info)|(\.sex)|(\.biz)|(\.aero)|(\.coop)|(\.museum)|(\.name)|(\.pro)|(\..{2,2}))$)\b/gi);
	if (goodEmail && theirName != 'Enter your friends email address' && theirName != '') {
		goodMail = true;
	}
	else {
		alert('Please ensure you have entered a valid email address and name');
		field.focus();
		field.select();
		goodMail = false;
   }
}

//for terms pop up
function injectTerms() {
	var service = siteAddr + "_services/checkout.php?action=terms";
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null) {
		alert ("Browser does not support HTTP Request");
		return;
	} 
	xmlHttp.onreadystatechange=displayTerms;
	xmlHttp.open("GET",service,true);
	xmlHttp.send(null);
	return false;	
}

function displayTerms(){
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { 
		var tmp = xmlHttp.responseText;
		if(tmp != '') {
			$('injectTerms').set('html',tmp);
		}
	}
}





