$(document).ready(function() {
	$('body').append($('#overlayAJAX2'));
	$('#overlayAJAX2').jqm({trigger: 'a.overlayAJAX2'});
	$('.overlayAJAX2').click(function() {							  
		doAJAXoverlay2($(this).attr("id"));
		return false;
	  });
	
	
	
	$('#overlayAJAX').jqm({trigger: 'a.overlayAJAX'});
	$('.overlayAJAX').click(function() {
	   if ($(this).attr("href")=='#Specs')
	   {
	   	doAJAXoverlay('spec_defs','HTML',$(this).attr("id").replace(/Spec/, ""),$(this).attr("title"));
	   }
	   if ($(this).attr("href")=='#Compare')
	   {
	   	doCompare('<?php echo $ProdID; ?>,'+loopSelected('CompareTo'));
	   }
		
		return false;
	  });
 	
	// Outbound link tracking
		$('.outboundDealer').click(function(){   
			urchinTracker('/DealerLocater/'+$(this).attr("href"));
		});
		
		$('.outboundLink').click(function(){   
			urchinTracker('/OutboundLinks/'+$(this).attr("href"));
		});
		
		$('.directLink').click(function(){   
			urchinTracker('/DirectLinks/'+$(this).attr("title")+'/'+$(this).attr("href"));
		});
	// end Outbound link tracking
	
	$('#YourEmail').focus(function(){   
		$(this).val('');
 
	});
	$('#YourEmail').blur(function(){
		if ($(this).val() == '') {						 	
			$(this).val('Your Email');
		}
	});
	
	
	$('#FriendsEmail').focus(function(){   
		$(this).val('');
 
	});
	$('#FriendsEmail').blur(function(){
		if ($(this).val() == '') {						 	
			$(this).val('Friend\'s Email');
		}
	});
	
	
	$('#headerEmailAddress').focus(function(){   
		$(this).val('');
 
	});
	$('#headerEmailAddress').blur(function(){
		if ($(this).val() == '') {						 	
			$(this).val('Join Our Email List');
		}
	});
	
	
	$('#Search').focus(function(){   
		$(this).val('');
 
	});
	$('#Search').blur(function(){
		if ($(this).val() == '') {						 	
			$(this).val('Search');
		}
	});
	
	
  $('#currencyContent').hide();
  $('#bookmarkContent').hide();
  $('#cartContent').hide();
  $('#forwardContent').hide();
  
  $('#bookmarkContent-slidetoggle').click(function() {
	
	$('#systemMessage').hide();
	$('#cartContent').hide();
	$('#forwardContent').hide();
	$('#currencyContent').hide();
    $('#bookmarkContent').slideToggle(400);
    return false;
  });
  
  $('#currencyContent-slidetoggle').click(function() {
	
	$('#systemMessage').hide();
	$('#cartContent').hide();
	$('#forwardContent').hide();
	$('#bookmarkContent').hide();
    $('#currencyContent').slideToggle(400);
    return false;
  });
  
  $('#').hide();
  
  
   
  $('#forwardContent-slidetoggle').click(function() {
	$('#currencyContent').hide();
	$('#bookmarkContent').hide();
	$('#systemMessage').hide();
	$('#cartContent').hide();
    $('#forwardContent').slideToggle(400);
    return false;
  });
  
  
   
  $('#cartContent-slidetoggle').click(function() {
	$('#currencyContent').hide();
	$('#bookmarkContent').hide();
	$('#systemMessage').hide();
	$('#forwardContent').hide();
    $('#cartContent').slideToggle(400);
    return false;
  });
  
  
  

  
  //borrowed from jQuery easing plugin
	//http://gsgd.co.uk/sandbox/jquery.easing.php
	$.easing.backout = function(x, t, b, c, d){
		var s=1.70158;
		return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b;
	};
	
	$('#image-scroller').scrollShow({
		elements:'img',//elements selector (relative to view)
		itemSize:{
			height:54,
			width:124
		},
		view:'#image-scroller-view',
		content:'#image-scroller-images',
		easing:'backout',
		wrappers:'',
		navigators:'a[id]',
		navigationMode:'s',
		circular:false,
		start:0
	});
	
	// Popup bubble
		$(function () {
			$('.bubbleInfo').each(function () {
				var distance = 10;
				var time = 250;
				var hideDelay = 500;
	
				var hideDelayTimer = null;
	
				var beingShown = false;
				var shown = false;
				var trigger = $('.trigger', this);
				var info = $('.popup', this).css('opacity', 0);
	
	
				$([trigger.get(0), info.get(0)]).mouseover(function () {
					if (hideDelayTimer) clearTimeout(hideDelayTimer);
					if (beingShown || shown) {
						// don't trigger the animation again
						return;
					} else {
						// reset position of info box
						beingShown = true;
	
						info.css({
							top: -90,
							left: -33,
							display: 'block'
						}).animate({
							top: '-=' + distance + 'px',
							opacity: 1
						}, time, 'swing', function() {
							beingShown = false;
							shown = true;
						});
					}
	
					return false;
				}).mouseout(function () {
					if (hideDelayTimer) clearTimeout(hideDelayTimer);
					hideDelayTimer = setTimeout(function () {
						hideDelayTimer = null;
						info.animate({
							top: '-=' + distance + 'px',
							opacity: 0
						}, time, 'swing', function () {
							shown = false;
							info.css('display', 'none');
						});
	
					}, hideDelay);
	
					return false;
				});
			});
		});
	// end popup bubble
	
	// GA Outbound Click Tracking
	$('#forwardContent-slidetoggle').click(function() {
		// TO DO: write tracking code
		return false;
	  });
	// end GA
	
	
  
});

jQuery.preloadImages = function()
{
  for(var i = 0; i<arguments.length; i++)
  {
    jQuery("<img>").attr("src", arguments[i]);
  }
}

function MM_swapImgRestore() { //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;
}
function MM_preloadImages() { //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];}}
}

function MM_findObj(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;
}

function MM_swapImage() { //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];}
}
function MM_validateForm() { //v4.0
  if (document.getElementById){
    var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
    for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=document.getElementById(args[i]);
      if (val) { nm=val.id; if ((val=val.value)!="") {
        if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
          if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
        } else if (test!='R') { num = parseFloat(val);
          if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
          if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
            min=test.substring(8,p); max=test.substring(p+1);
            if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
      } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
    } if (errors) alert('The following error(s) occurred:\n'+errors);
    document.MM_returnValue = (errors == '');
} }

function reload_map(url_str)
{
    iframe = document.getElementById('MapFrame'); 
	iframe.src = 'map_zip.php'+url_str;
}



function handleAJAXResponse() {
  if (http.readyState == 4) {
	//document.getElementById('overlayAJAXTxt').innerHTML = http.responseText;
	document.getElementById('overlayAJAXTxt').innerHTML = http.responseText;
  }
}

function getHTTPObject() {
  var xmlhttp;
  if (typeof XMLHttpRequest == 'undefined') {
  	XMLHttpRequest = function() { return new ActiveXObject('Microsoft.XMLHTTP') }
  }
  
  
  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
	try {
	  xmlhttp = new XMLHttpRequest();
	} catch (e) {
	  xmlhttp = false;
	}
  }
  return xmlhttp;
}
var http = getHTTPObject(); // We create the HTTP Object

function loopSelected(selection)
{
  var selectedArray = new Array();
  var selObj = document.getElementById(selection);
  var i;
  var count = 0;
  for (i=0; i<selObj.options.length; i++) {
	if (selObj.options[i].selected) {
	  selectedArray[count] = selObj.options[i].value;
	  count++;
	}
  }
  return selectedArray;
}

function clearAJAXoverlay() {
	document.getElementById('overlayAJAXTxt').innerHTML = '<div style="padding:20px; background:#FFFFFF;text-align:center;font-size:18px;font-weight:bold;"><img src="http://www.celestron.com/c3/images/loading.gif" align="middle" /> &nbsp;&nbsp; LOADING...</div></div></div>';
}


function doCompare(ProdIDs) {
  clearAJAXoverlay();
  http.open("GET", "ajax_compare_prods.php?ProdIDs=" + escape(ProdIDs), true);
  http.onreadystatechange = handleAJAXResponse;
  http.send(null);
}


function doAJAXoverlay(Table,Field,ID,Title) {
  clearAJAXoverlay();
  http.open("GET", "ajax_get_content.php?Table=" + escape(Table)+"&Field=" + escape(Field)+"&ID=" + escape(ID)+"&Title=" + escape(Title), true);
  http.onreadystatechange = handleAJAXResponse;
  http.send(null);
}


function doAJAXoverlay2(Content) {
  clearAJAXoverlay2();
  http.open("GET", "ajax_overlay.php?Content=" + escape(Content), true);
  http.onreadystatechange = handleAJAXResponse2;
  http.send(null);
}
function handleAJAXResponse2() {
  if (http.readyState == 4) {
	//document.getElementById('overlayAJAXTxt').innerHTML = http.responseText;
	document.getElementById('overlayAJAXTxt2').innerHTML = http.responseText;
  }
}
function clearAJAXoverlay2() {
	document.getElementById('overlayAJAXTxt2').innerHTML = '<div style="padding:20px; background:#FFFFFF;text-align:center;font-size:18px;font-weight:bold;"><img src="http://www.celestron.com/c3/images/loading.gif" align="middle" /> &nbsp;&nbsp; LOADING...</div></div></div>';
}