var currentSlide = 1;
var introInterval = -1;

jQuery(document).ready(function() {
	if(jQuery('#slides')) {
		jQuery('#slides .slide.num1').show();
	
		jQuery('#slidesmenu ul li a').click(function(e, keepPlay) {
			if(!keepPlay && introInterval != -1) clearInterval(introInterval);
		
			jQuery('#slidesmenu ul li a.active').removeClass('active');
			jQuery('#slidesmenu ul li a.active').href
			var pos = jQuery(this).parent().prevAll('li').length;
			var nextSlide = (pos + 1);
			jQuery(this).addClass('active');
			jQuery('#slides .slide.num' + currentSlide).fadeOut('fast');
			jQuery('#slides .slide.num' + nextSlide).fadeIn('slow');
			currentSlide = nextSlide;
			e.preventDefault();
		});
	
		function autoAnimate() {
			if(currentSlide == -1) return false;
			jQuery('#slidesmenu ul li a').eq(currentSlide%jQuery('#slidesmenu ul li a').length).trigger('click',[true]);
		}

		introInterval = setInterval(function(){autoAnimate()}, 5000);
	}
	
	var popupStatus = 0;
	
	//SETTING UP POPUP
	//0 means shown; 1 means hidden;
	//centering popup
	function centerPopup() {
		//request data for centering
		var windowWidth = document.documentElement.clientWidth;
		var windowHeight = document.documentElement.clientHeight;
		var popupHeight = jQuery("#popupBuynow").height();
		var popupWidth = jQuery("#popupBuynow").width();
		//centering
		jQuery("#popupBuynow").css({
			"position": "absolute",
			"top": windowHeight/2-popupHeight/2,
			"left": windowWidth/2-popupWidth/2
		});
		//only need force for IE6
		jQuery("#backgroundPopup").css({"height": windowHeight});
	}
	
	//loading popup with jQuery magic!  
	function showPopup() {  
		//loads popup only if it is disabled  
		if(popupStatus == 0) {  
			jQuery("#backgroundPopup").css({"opacity": "0.7"});
			jQuery("#backgroundPopup").fadeIn("slow");
			jQuery("#popupBuynow").fadeIn("slow");
			popupStatus = 1;
		}
	}
	
	//disabling popup with jQuery magic!
	function hidePopup() {
		//disables popup only if it is enabled
		if(popupStatus==1) {
			jQuery("#backgroundPopup").fadeOut("slow");
			jQuery("#popupBuynow").fadeOut("slow");
			popupStatus = 0;
		}
	}
	
	//CLOSING POPUP  
	//Click the x event!  
	jQuery("#popupBuynowClose").click(function() {
		hidePopup();  
	});

	//Click out event!
	jQuery("#backgroundPopup").click(function() {
		hidePopup();  
	});
	
	//Press Escape event!  
	jQuery(document).keypress(function(e) {  
		if(e.keyCode == 27 && popupStatus == 1){  
			hidePopup();
		}
	});
	
	jQuery("#buynow").click(function() {
		//centering with css
		centerPopup();
		//show popup
		showPopup();
	});
	
	jQuery("#buynow1").click(function() {
		//centering with css
		centerPopup();
		//show popup
		showPopup();
	});
	
	jQuery("input[name='continue']").click(function() {
		window.location.href = "https://secure.esellerate.net/secure/prefill.aspx?s=STR2442534565&cmd=BUY&_CartItem0.SkuRefNum=SKU71298310678"
	});
	
	jQuery("input[name='payment_type']").click(function() {
		if(jQuery("input[name='payment_type']:checked").val() == "paypal") {
			jQuery("#esellerate_ix1").hide();
			jQuery("#paypal_ix1").show();
			jQuery("#paypal_ix2").show();
			jQuery("#popupBuynow").height(300);
		} else {
			jQuery("#paypal_ix1").hide();
			jQuery("#paypal_ix2").hide();
			jQuery("#esellerate_ix1").show();
			jQuery("#popupBuynow").height(230);
		}
	});
});

function validatePhoneSN() {
	if(document.paypalForm.os0.value == '') {
		alert("Please enter Serial Number of your iPhone.");
		return false;
	}
	return true;
}