jQuery.ajaxSetup ({  
	cache: false  
});
var ajax_load = '<img src="/images/loading.gif" alt="Loading..." />';
//var loadUrl = '/ajax.asp';
var loadUrl = '/ajax/q/';
var pageJustLoaded = false;

jQuery.jQueryRandom = 0;
jQuery.extend(jQuery.expr[":"],
{
    random: function(a, i, m, r) {
        if (i == 0) {
            jQuery.jQueryRandom = Math.floor(Math.random() * r.length);
        };
        return i == jQuery.jQueryRandom;
    }
});

jQuery(document).ready(function() {
	pageJustLoaded = true;
// SETUP DIALOG DIVS
	jQuery('#dialog').hide();
	jQuery('#dim').hide();
	jQuery('#dim').css('height', jQuery(document).height());
	jQuery('#dim').click(function() {
		jQuery('BODY').css('overflow','auto');
		jQuery('#dim').toggle();
		jQuery('#dialog').hide();
	});
	jQuery('#dynamicList LI:last').addClass('last');
	jQuery('.retailers LI:last').addClass('noborder');
	jQuery('#masthead H5').html('<img src="/images/masthead_servicemark.png" alt="The story of a traveling book club" />');
	//jQuery('#mastheadDefault').html('<img src="/images/masthead_servicemark.png" alt="The story of a traveling book club" />');

// ZEBRA STRIPE
	$(".zebra TR:odd").addClass("odd");
	$(".zebra LI:odd").addClass("odd");
	$(".zebra TR:even").addClass("even");
	$(".zebra LI:even").addClass("even");

// CAPTURE AJAX BUTTONS
	jQuery('.buttonOrder').click(function() {
		revealDialog('order');
		return false;
	});
	jQuery('.buttonContact').click(function () {
		revealDialog('contact');
		return false;
	});
	jQuery('.buttonSubscribe').click(function () {
		revealDialog('subscribe');
		return false;
	});
	jQuery('.buttonUnsubscribe').click(function () {
		revealDialog('unsubscribe');
		return false;
	});
	jQuery('.buttonPrivacy').click(function () {
		revealDialog('privacy');
		return false;
	});
	
	/*
	jQuery('#formSearch').submit(function () {
		alert(jQuery('#formSearch').val());
		jQuery('BODY').css('overflow','hidden');
		jQuery('#ajax').load(loadUrl + ' #' + element, null, function(){
			var s = jQuery('#formSearch').validate({
				submitHandler: function(form){
					jQuery(form).ajaxSubmit({
						target: "#ajax",
						success: function() { 
							$('#dialog').animate({borderWidth:'10px', width:'760px', height:'450px'},400);
						}
					});
				}
			});
			jQuery('#close').click(function () {
				jQuery('BODY').css('overflow','auto');
				jQuery('#dim').hide();
				jQuery('#dialog').css({'border':'0px solid #fff', 'width':'760px', 'height':'450px'});
				jQuery('#dialog').hide();
			});
			jQuery('#dialog').css('top', (jQuery(window).scrollTop() + 50) + 'px');
			jQuery('#dim').toggle();
			jQuery('#dialog').toggle();
		});
		return false;
	});
	*/

// PEEL
	jQuery('#pageflip IMG').stop() //On hover out, go back to original
			.animate({
				width: '60px',
				height: '62px'
			}, 1220);
	jQuery('#pageflip').hover(function() { //On hover...
		$('#pageflip IMG, .msg_block').stop()
			.animate({ //Animate and expand the image and the msg_block (Width + height)
				width: '307px',
				height: '319px'
			}, 500);
		} , function() {
		jQuery('#pageflip IMG').stop() //On hover out, go back to original size
			.animate({
				width: '60px',
				height: '62px'
			}, 220);
		jQuery('.msg_block').stop() //On hover out, go back to original size
			.animate({
				width: '60px',
				height: '60px'
			}, 200); //Note this one retracts a bit faster (to prevent glitching in IE)
	});

// NAV DESCRIPTION
	$("#topNav LI").hover(function() {
	//	$(this).css({'background':'#999 url(topnav_active.gif) repeat-x','border-bottom':'1px solid #999'});
		if(!pageJustLoaded) {
			$(this).addClass('hover');
			$(this).find('DIV').show();
		}else{
			pageJustLoaded = false;
		}
	}, function() {
	//	$(this).css({'background':'none','border-bottom':'1px solid #000'}); 
		$(this).removeClass('hover');
		$(this).find('DIV').hide();
	});
	jQuery(document).ready(function(){
		jQuery('IMG.dropShadow').wrap('<div class="wrap1"><div class="wrap2">' + '<div class="wrap3"></div></div></div>');
	});
});

// DIALOG BOX WITH AJAX
function revealDialog(element) {
	jQuery('BODY').css('overflow','hidden');
	jQuery('#ajax').load(loadUrl +  element + ' #' + element, null, function(){
		$(".zebra TR:odd").addClass("odd");
		$(".zebra LI:odd").addClass("odd");
		$(".zebra TR:even").addClass("even");
		$(".zebra LI:even").addClass("even");
		// VALIDATE FORM THEN AJAX SUBMIT
		var v1 = jQuery('#formContact').validate({
		submitHandler: function(form){
				jQuery(form).ajaxSubmit({
					target: "#ajax",
					success: function() { 
						$('#dialog').animate({borderWidth:'10px', width:'760px', height:'450px'},400);
						}  
				});
			}
		});
		var v2 = jQuery('#formSubscribe').validate({
			submitHandler: function(form){
				jQuery(form).ajaxSubmit({
					target: "#ajax",
					success: function() { 
						$('#dialog').animate({borderWidth:'10px', width:'760px', height:'450px'},400);
						}  
				});
			}
		});
		var v3 = jQuery('#formUnsubscribe').validate({
			submitHandler: function(form){
				jQuery(form).ajaxSubmit({
					target: "#ajax",
					success: function() { 
						$('#dialog').animate({borderWidth:'10px', width:'760px', height:'450px'},400);
						}  
				});
			}
		});
	});
	jQuery('#close').click(function () {
		jQuery('BODY').css('overflow','auto');
		jQuery('#dim').hide();
		jQuery('#dialog').css({'border':'0px solid #fff', 'width':'800px', 'height':'450px'});
		jQuery('#dialog').hide();

	});
	if(element == 'contact') {
		jQuery('#dialog').addClass('postcard');
	//	jQuery('#ajax').css({'overflow-y':'hidden'});
	}else{
		jQuery('#dialog').removeClass('postcard');
	//	jQuery('#ajax').css({'overflow-y':'scroll'});
	}
	jQuery('#dialog').css('top', (jQuery(window).scrollTop() + 50) + 'px');
	jQuery('#dim').toggle();
	jQuery('#dialog').toggle();
}

// RESIZE DIM ON WINDOW CHANGE
	jQuery(window).bind('resize', function(){
	   jQuery('#dim').css('height', jQuery(window).height());
	});

