function fetch(resultID, formID, scriptURL) {
	$(resultID).removeClass().html('');
	$.ajax({
		url: scriptURL, type: "POST",
		data: $(formID).serialize(),
		complete: function (xmlreq, textStatus) {
			if ( xmlreq.status != 200 ) {
				$(resultID).addClass('error').html(xmlreq.responseText);
				return;
			}
			$(resultID).addClass('success').html(xmlreq.responseText);
		}
	});
}

String.prototype.reverse = function() { 
	return this.split('').reverse().join('');
};

$(document).ready(
	function() {
		$('span.reverse_email').each(function() {
			var $this = $(this);
			var mailto = $(this).text().reverse();
			if ($this.attr('subject'))
				mailto += '?subject=' + $this.attr('subject');
			$this.wrap('<a href="mailto:'+mailto+'" rel="nofollow"></a>');
		});
		
		$('a.magnifyingGlass, #imageList_3 a, a.fancybox, .imageGallery a').fancybox({
			'hideOnContentClick'	: false,
			'hideOnOverlayClick'	: true,
			'centerOnScroll'		: true,
			'overlayShow'			: true,
			'overlayOpacity'		: 0,
			'speedIn'				: 300,
			'speedOut'				: 110,
			'changeSpeed'			: 200,
			'padding'				: 0,
			'margin'				: 0
		});
		
		$('.input_wrapper').mouseover(function() {
			$(this).addClass("hover");
			$(this).siblings().addClass("hover");
		});
		
		$('.input_wrapper').mouseout(function() {
			$(this).removeClass("hover");
			$(this).siblings().removeClass("hover");
		});
		
		$('.input_wrapper input').focus(function() {
			$(this).parent().parent().addClass("active");
			$(this).parent().parent().siblings().addClass("active");
		});
		
		$('.input_wrapper input').blur(function() {
			$(this).parent().parent().removeClass("active");
			$(this).parent().parent().siblings().removeClass("active");
		});
		
		$('.input_wrapper select').mousedown(function() {
			$(this).parent().parent().addClass("press");
			$(this).parent().parent().siblings().addClass("press");
		});
		
		$('.input_wrapper select').mouseup(function() {
			$(this).parent().parent().removeClass("press");
			$(this).parent().parent().siblings().removeClass("press");
		});
		
		$('.input_wrapper select').focus(function() {
			$(this).parent().parent().addClass("active");
			$(this).parent().parent().siblings().addClass("active");
		});
		
		$('.input_wrapper select').blur(function() {
			$(this).parent().parent().removeClass("active");
			$(this).parent().parent().siblings().removeClass("active");
		});
		
		$('.input_wrapper select').change(function() {
			$(this).blur();
		});
		
		$('.textarea_wrapper').mouseover(function() {
			$(this).addClass("hover");
			$(this).siblings().addClass("hover");
		});
		
		$('.textarea_wrapper').mouseout(function() {
			$(this).removeClass("hover");
			$(this).siblings().removeClass("hover");
		});
		
		$('.textarea_wrapper textarea').focus(function() {
			$(this).parent().parent().parent().addClass("active");
			$(this).parent().parent().parent().siblings().addClass("active");
		});
		
		$('.textarea_wrapper textarea').blur(function() {
			$(this).parent().parent().parent().removeClass("active");
			$(this).parent().parent().parent().siblings().removeClass("active");
		});
				
		$("#paymentMethod .checkout a").click(function(event) {
            $("#paymentMethod .checkout").removeClass('active');
            $(this).parent().addClass('active');
            return false;
		});
		
		$(".selectbox select").click(function() {
			var value =  $(this).find('option:selected').val();
			var text =  $(this).find('option:selected').text();
			if(value == "") {
				$(this).prev('span').html("");
			} else {
				$(this).prev('span').html(text);
			}
		});
		
		// Function for controlling tooltip popup on Espionage page.
		$(function() {
			// note: this was for Espionage 1.x, no longer used
/*			$('.popup_wrapper').each(
				function() {
					// options
					var distance = 40;
					var time = 250;
					var hideDelay = 600;
					
					var hideDelayTimer = null;
					
					// tracker
					var beingShown = false;
					var shown = false;
					
					var trigger = $('.trigger', this);
					var popup = $('.popup', this).css('opacity', 0);
			
					// set the mouseover and mouseout on both element
					$([trigger.get(0), popup.get(0)]).mouseover(
						function() {					
							// stops the hide event if we move from the trigger to the popup element
							if (hideDelayTimer) clearTimeout(hideDelayTimer);
			
							// don't trigger the animation again if we're being shown, or already visible
							if (beingShown || shown) {
								return;
							} else {
								beingShown = true;
							
								// reset position of popup box
								popup.css({
									top: 90,
									left: 538,
									display: 'block' // brings the popup back in to view
								})
						
								// (we're using chaining on the popup) now animate it's opacity and position
								.animate({
										top: '-=' + distance + 'px',
										opacity: 1
									},
									time,
									'swing',
									function() {
										// once the animation is complete, set the tracker variables
										beingShown = false;
										shown = true;
									}
								);
							}
						}
					).mouseout(
						function() {
							// reset the timer if we get fired again - avoids double animations
							if(hideDelayTimer) clearTimeout(hideDelayTimer);
							
							// store the timer so that it can be cleared in the mouseover if required
							hideDelayTimer = setTimeout(
								function() {
									hideDelayTimer = null;
									popup.animate({
											top: '-=' + distance + 'px',
											opacity: 0
										},
										time,
										'swing',
										function() {
											// once the animate is complete, set the tracker variables
											shown = false;
											// hide the popup entirely after the effect (opacity alone doesn't do the job)
											popup.css('display', 'none');
										}
									);
								},
								hideDelay
							);
						}
					);
				}
			);
*/
			// Espionage 2.x stuff
			$('.popup_wrapper_alt').each(
				function() {
					// options
					var distance = 60;
					var time_download = 250;
					var time_buy = 250;
					var hideDelay = 600;
					
					var hideDelayTimer_download = null;
					var hideDelayTimer_buy = null;
					
					// tracker
					var beingShown_download = false;
					var shown_download = false;
					var beingShown_buy = false;
					var shown_buy = false;
					
					var trigger_download = $('.trigger_download', this);
					var popup_download = $('.downloadPopup', this).css('opacity', 0);
					
					// set the mouseover and mouseout on both element
					$([trigger_download.get(0), popup_download.get(0)]).mouseover(
						function() {					
							// stops the hide event if we move from the trigger to the popup element
							if (hideDelayTimer_download) clearTimeout(hideDelayTimer_download);
			
							// don't trigger the animation again if we're being shown, or already visible
							if (beingShown_download || shown_download) {
								return;
							} else {
								beingShown_download = true;
							
								// reset position of popup box
								popup_download.css({
									top: -155,
									left: 435,
									display: 'block' // brings the popup back in to view
								})
						
								// (we're using chaining on the popup) now animate it's opacity and position
								.animate({
										top: '-=' + distance + 'px',
										opacity: 1
									},
									time_download,
									'swing',
									function() {
										// once the animation is complete, set the tracker variables
										beingShown_download = false;
										shown_download = true;
									}
								);
							}
						}
					).mouseout(
						function() {
							// reset the timer if we get fired again - avoids double animations
							if(hideDelayTimer_download) clearTimeout(hideDelayTimer_download);
							
							// store the timer so that it can be cleared in the mouseover if required
							hideDelayTimer_download = setTimeout(
								function() {
									hideDelayTimer_download = null;
									popup_download.animate({
											top: '-=' + distance + 'px',
											opacity: 0
										},
										time_download,
										'swing',
										function() {
											// once the animate is complete, set the tracker variables
											shown_download = false;
											// hide the popup entirely after the effect (opacity alone doesn't do the job)
											popup_download.css({
												display: 'none'
											})
										}
									);
								},
								hideDelay
							);
						}
					);
					/*
					var trigger_buy = $('.trigger_buy', this);
					var popup_buy = $('.buyPopup', this).css('opacity', 0);
					
					// set the mouseover and mouseout on both element
					$([trigger_buy.get(0), popup_buy.get(0)]).mouseover(
						function() {					
							// stops the hide event if we move from the trigger to the popup element
							if (hideDelayTimer_buy) clearTimeout(hideDelayTimer_buy);
			
							// don't trigger the animation again if we're being shown, or already visible
							if (beingShown_buy || shown_buy) {
								return;
							} else {
								beingShown_buy = true;
							
								// reset position of popup box
								popup_buy.css({
									//top: -155,
									top: -95,
									left: 535,
									display: 'block' // brings the popup back in to view
								})
						
								// (we're using chaining on the popup) now animate it's opacity and position
								.animate({
										top: '-=' + distance + 'px',
										opacity: 1
									},
									time_buy,
									'swing',
									function() {
										// once the animation is complete, set the tracker variables
										beingShown_buy = false;
										shown_buy = true;
									}
								);
							}
						}
					).mouseout(
						function() {
							// reset the timer if we get fired again - avoids double animations
							if(hideDelayTimer_buy) clearTimeout(hideDelayTimer_buy);
							
							// store the timer so that it can be cleared in the mouseover if required
							hideDelayTimer_buy = setTimeout(
								function() {
									hideDelayTimer_buy = null;
									popup_buy.animate({
											top: '-=' + distance + 'px',
											opacity: 0
										},
										time_buy,
										'swing',
										function() {
											// once the animate is complete, set the tracker variables
											shown_buy = false;
											// hide the popup entirely after the effect (opacity alone doesn't do the job)
											popup_buy.css({
												display: 'none'
											})
										}
									);
								},
								hideDelay
							);
						}
					);
					/**/
				}
			);
		});
	}
);

// // ie6 png fix (doesn't work)
// $(document).ready(function() {
// 	$('#content_wrapper, #header_wrapper').supersleight({shim: '/includes/images/transparent.gif'});	
// });
