// simple jQuery code for Nobrow pages, just to handle menu buttons
// nobrow02js.js		from contact10js.js 29oct09 of metal box project28 Oct 09 
//						30 jan 10  use jQuery to position footerdiv
//						 7 Feb 10  converted to WP (nb in /scripts/nobrow02js.js)
//                      14 feb 10  change BuyButton to <input type image... (remove .children()
//						20 Feb 10  position footerdiv for Blog layout too
//						 7 Mar 10  minimum page ht (600px for now)
//						20 Apr 10  add sign up button etc
//                       5 May 10  add trigger to submit form if shipzone radio buttons change
//						27 Jul 10  add validateCart (to ensure user has set shipzone)
//						 4 Dec 10  remove diagnostic from validateCart
//  nobrow21js.js		13 Feb 11  Phase 2 redesign, navigation images etc
//                      22 Feb 11  buttons in webshop sidebar
//                      23 Feb 11  animate tag display in webshop sidebar (and comment out)
//						24 Feb 11  sign up - update classname
//						 1 Mar 11  don't position footer by jQuery, so smooth scroll with readmorerighthere
//						 8 Mar 11  add back code to extend grey border (removed on 1mar11 by mistake)
//						13 Mar 11  in validateCart() add 'wordpress/' for Nobrow (different to ConorWP)
//						14 Mar 11  in postAddress (part of SignUp) add full nobrow url (different to ConorWP)
//						14 Mar 11  artist disjointed rollover change artistlistclass to lsblistclass
//  nobrow22js.js		16 Mar 11  convert to no-conflict (ie $ to jQuery throughout) so we can enqueue, 
//									 and not load jquery twice (as we were doing with Readmore plugin)

// global variables


// *************** to handle menu buttons **********************************

jQuery(document).ready(function() {
		
		// Preload all rollovers, for main menu items
		
		jQuery("#menudiv img").each(function() {
			// Set the original src
			rollsrc = jQuery(this).attr("src");
			rollON = rollsrc.replace('_up', '_down');
			newImg = new Image(); // create new image obj
			jQuery(newImg).attr("src", rollON); // set new obj's src, ie cache it
			//rollDown = rollsrc.replace('_up', '_down');
			//downImg = new Image(); // create new image obj
			//jQuery(downImg).attr("src", rollDown); // set new obj's src, ie cache it
		});

		
		// Navigation rollovers
		jQuery("#menudiv a").mouseover(function(){
			imgsrc = jQuery(this).children("img").attr("src");
			if (typeof(imgsrc) != 'undefined') {
				imgsrcOVER = imgsrc.replace('_up', '_down');
				jQuery(this).children("img").attr("src", imgsrcOVER);
			}
		});
		
		// on mouse down
		/*jQuery("#menudiv a").mousedown(function(){
			if (typeof(imgsrc) != 'undefined') {
				imgsrcDOWN = imgsrc.replace('_up', '_down');
				jQuery(this).children("img").attr("src", imgsrcDOWN);
			}
		});
		*/
		
		// Handle mouseout
		jQuery("#menudiv a").mouseout(function(){
			if (typeof(imgsrc) != 'undefined') {
				jQuery(this).children("img").attr("src", imgsrc);
			}
		});
		
		// and for secondary menu items
		jQuery("#menu2div img").each(function() {
			// Set the original src
			rollsrc = jQuery(this).attr("src");
			rollON = rollsrc.replace('_up', '_down');
			newImg = new Image(); // create new image obj
			jQuery(newImg).attr("src", rollON); // set new obj's src, ie cache it
			
		});

		// Navigation rollovers
		jQuery("#menu2div a").mouseover(function(){
			imgsrc = jQuery(this).children("img").attr("src");
			if (typeof(imgsrc) != 'undefined') {
				imgsrcOVER = imgsrc.replace('_up', '_down');
				jQuery(this).children("img").attr("src", imgsrcOVER);
			}
		});
		
		// Handle mouseout
		jQuery("#menu2div a").mouseout(function(){
			if (typeof(imgsrc) != 'undefined') {
				jQuery(this).children("img").attr("src", imgsrc);
			}
		});
		// end of secondary menu buttons
		
		// and for footer menu items
		jQuery("#footermenudiv img").each(function() {
			// Set the original src
			rollsrc = jQuery(this).attr("src");
			rollON = rollsrc.replace('_up', '_down');
			newImg = new Image(); // create new image obj
			jQuery(newImg).attr("src", rollON); // set new obj's src, ie cache it
			
		});

		// Navigation rollovers
		jQuery("#footermenudiv a").mouseover(function(){
			imgsrc = jQuery(this).children("img").attr("src");
			if (typeof(imgsrc) != 'undefined') {
				imgsrcOVER = imgsrc.replace('_up', '_down');
				jQuery(this).children("img").attr("src", imgsrcOVER);
			}
		});
		
		// Handle mouseout
		jQuery("#footermenudiv a").mouseout(function(){
			if (typeof(imgsrc) != 'undefined') {
				jQuery(this).children("img").attr("src", imgsrc);
			}
		});
		// end of footer menu buttons
		
		// and for webshop sidebar product sub-category buttons
		jQuery("#sidebarmenudiv img").each(function() {
			// Set the original src
			rollsrc = jQuery(this).attr("src");
			rollON = rollsrc.replace('_up', '_down');
			newImg = new Image(); // create new image obj
			jQuery(newImg).attr("src", rollON); // set new obj's src, ie cache it
			
		});

		// Navigation rollovers
		jQuery("#sidebarmenudiv a").mouseover(function(){
			imgsrc = jQuery(this).children("img").attr("src");
			if (typeof(imgsrc) != 'undefined') {
				imgsrcOVER = imgsrc.replace('_up', '_down');
				jQuery(this).children("img").attr("src", imgsrcOVER);
			}
		});
		
		// Handle mouseout
		jQuery("#sidebarmenudiv a").mouseout(function(){
			if (typeof(imgsrc) != 'undefined') {
				jQuery(this).children("img").attr("src", imgsrc);
			}
		});
		// end of webshop sidebar menu buttons
});

// *** 23feb11 animate taglist in webshop sidebar *******************
//     this may be too annoying - just blank out these lines if not required
//     and ensure ht of contentdiv extends to sidebar, after slide down (8mar11)
jQuery(document).ready(function() {
	jQuery('ul.taglistclass').hide().slideDown(1000, function() {
									if (jQuery('#leftsidebardiv').height()) {
										sidebarht = jQuery('#leftsidebardiv').height();
										if (  sidebarht > jQuery('#contentdiv').height() ) {
											jQuery('#contentdiv').height(sidebarht);   // extend contentdiv to match sidebar ht
										}
									}								 					 
								 });
});


// ************** to highlight artist name when hover over artist picture on artist-category page   4mar11 ***********
// this assumes that pictures and names are in same order, ie if 5th pic hovered, then 5th name highlighted
// need mouseenter etc format in order to force the index number into the event
jQuery(document).ready( function() {
	jQuery('#artisttable img').each( function(i) {
  		jQuery(this).bind('mouseenter', {index:i}, function(e){
     		jQuery('ul.lsblistclass').find('li:eq('+e.data.index+')').addClass('selectedtag');
			}).
			bind('mouseleave', {index:i}, function(e){
     		jQuery('ul.lsblistclass').find('li:eq('+e.data.index+')').removeClass('selectedtag');
			});
    });
}); 
	

// *************** grey border  ************************
// force minimum page ht
// and ensure that grey border extends to bottom (grey line is border of contentdiv or blogdiv)
// extend blogdiv to be greater of blogdiv and rightsidebardiv on blog page 
// or greater of contentdiv and leftsidebardiv on others 
// but min page ht of 600 px. So content etc minht = 600 - 151(hdr) - c 140 (footer) = 310


jQuery(document).ready(function() {
		var innerht;
		var minht = 310;
		 
		if (jQuery('#blogdiv').height()) {
			innerht = jQuery('#blogdiv').height();
			if (innerht < minht) {
				innerht = minht;
				jQuery('#blogdiv').height(minht);   // extend blogdiv to minht 
			}
			if (jQuery('#rightsidebardiv').height()) {
				sidebarht = jQuery('#rightsidebardiv').height();
				if (  sidebarht > innerht ) {
					jQuery('#blogdiv').height(sidebarht);   // extend blogdiv to match sidebar ht
				}
			}
		} else if (jQuery('#contentdiv').height()) {
			innerht = jQuery('#contentdiv').height();
			if (innerht < minht) {
				innerht = minht;
				jQuery('#contentdiv').height(minht);   // extend contentdiv to minht
			}
			if (jQuery('#leftsidebardiv').height()) {
				sidebarht = jQuery('#leftsidebardiv').height();
				if (  sidebarht > innerht ) {
					jQuery('#contentdiv').height(sidebarht);   // extend contentdiv to match sidebar ht
				}
			}
		} 
});



// *************** to handle Buy Buttons **********************************

jQuery(document).ready(function() {
		
		// Preload all rollovers
		jQuery(".buyImg").each(function() {
			// Set the original src
			rollsrc = jQuery(this).attr("src");
			rollON = rollsrc.replace('buy_up', 'buy_over');
			newImg = new Image(); // create new image obj
			jQuery(newImg).attr("src", rollON); // set new obj's src, ie cache it
			rollDown = rollsrc.replace('buy_up', 'in_cart');
			downImg = new Image(); // create new image obj
			jQuery(downImg).attr("src", rollDown); // set new obj's src, ie cache it
		});

		
		// Navigation rollovers
		// mouse over
		jQuery(".buyImg").mouseover(function(){
			imgsrc = jQuery(this).attr("src");
			if (typeof(imgsrc) != 'undefined') {
				imgsrcOVER = imgsrc.replace('buy_up', 'buy_over');
				jQuery(this).attr("src", imgsrcOVER);
			}
		});
		
		// on mouse down change to 'in cart'
		// but it may be better to drive this from analysis of cart (not mouse-down?)
		jQuery(".buyImg").mousedown(function(){
			if (typeof(imgsrc) != 'undefined') {
				imgsrcDOWN = imgsrc.replace('buy_up', 'in_cart');
				jQuery(this).attr("src", imgsrcDOWN);
			}
		});
		
		
		// Handle mouseout
		jQuery(".buyImg").mouseout(function(){
			if (typeof(imgsrc) != 'undefined') {
				// if product in cart then leave as 'in-cart' (this code only v simple for now)
				// will need to check cart etc etc ?
				// ie revert to buy_up only if not clicked ?
				if (jQuery(this).attr("src") == imgsrcOVER) {
					jQuery(this).attr("src", imgsrc);
				}
			}
		});
		
});



// ******************** to handle Sign up (mailinglist) button

jQuery(document).ready(function() {
		
		
		// if user clicks SignUp button
		jQuery('.signup a').click( function () {
											  
					postAddress();
					return false; 
	 	});
		
		
		// or if user presses CR while in text box (ie signifying submit)	
		jQuery('#email').keyup( function(event) {
			if ( (event.which && event.which == 13) || (event.keyCode && event.keyCode == 13) ) {
				event.preventDefault();
				postAddress();
				return false;
			}
    	});
		
		// and prevent submission of form (if user presses CR)
		jQuery('#mailinglist').submit( function() {
			   return false;
		});

						   
						   
});

// get email address from input box and check if it is a valid email format
// if so, post it to mlist_insert.php and put response (OK or you're already there!) in input field 
function postAddress() {
	var emailaddress =  jQuery('#email').val();	
	
	// check if it looks like email format 
	reg = /^\s*\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+\s*$/

	if (reg.test(emailaddress)) {	// valid format ?
	
		// pass to mlist_insert.php file (ajax / post)
		jQuery.post('http://www.nobrow.net/wordpress/wp-content/plugins/mlist/mlist_insert.php', {'email': emailaddress}, function( response) {
						// return result to email box
						jQuery('#email').val(response);
		});			
		
	} else {
		jQuery('#email').val("Invalid email address ?");
		jQuery('#email').focus();
		jQuery('#email').select();
	}
	
}



// *********************** end of SignUp ***************************************




// ********** resubmit shop cart form (for recalc & redisplay) if ship zone (or method) radio buttons changed ********
// **********  or qty field changed (trickier) ***************

var myTimer = null;	// timer to allow delay in typing qty

// func called if delay exceeded when typing qty (ie finished (?) typing)
function clearValues() {
	if (myTimer) {
		window.clearTimeout(myTimer);
		myTimer = null;
	}
	jQuery("#shopcartform td.amts").text(" ");
	jQuery("#shopcartform").trigger("submit");
};

jQuery(document).ready(function() {
	// nb have to use click (not change) because IE doesn't trigger change until buttongroup loses focus
	// if a shipzone radiobutton clicked, clear linetotals and totals, and submit form for recalc & redisplay
	jQuery("#shopcartform input[name='shipzonebutton']").click( function() {
		jQuery("#shopcartform td.linetotalclass").text(" ");
		jQuery("#shopcartform td.amts").text(" ");
		jQuery("#shopcartform").trigger("submit");
	});
	// if a shipmethod radiobutton clicked, just clear shipcharge and grand total, and submit form for recalc & redisplay
	jQuery("input[name='shipmethodbutton']").click( function() {
		jQuery("#shopcartform td[headers='cartItem scharge']").text(" ");  // have to change if hdrs get changed
		jQuery("#shopcartform td[headers='cTotal cartTotal']").text(" ");
		jQuery("#shopcartform").trigger("submit");
	});
	
	// update if user exits Qty field (tab,  mouse), bit of a catch all
	jQuery('#shopcartform .carttext:text').blur( function() {
		//if timer running, stop it
		if (myTimer) {
			window.clearTimeout(myTimer);
			myTimer = null;
		}
		jQuery(this).parent().parent().next().text(" ");  // linetotal for this line
		jQuery("#shopcartform td.amts").text(" ");
		jQuery("#shopcartform").trigger("submit");
	});

	// update if user is typing into qty field but pauses (no keypress for 3 secs)
	// this is for when he has finished typing but doesn't tab out (ie blur)
	// is this too quick ? too slow ?
	jQuery('#shopcartform .carttext:text').keyup(function(event) {
		jQuery(this).parent().parent().next().text(" ");				// linetotal for this line
		// if key is CR then don't delay (and allow normal default submit process)
		if (event.keyCode == 13 ) {
			if (myTimer) {
				window.clearTimeout(myTimer);
				myTimer = null;
			}
			jQuery("#shopcartform td.amts").text(" ");
		} else {
			if (myTimer) {
				window.clearTimeout(myTimer);
			}
			myTimer = window.setTimeout( 'clearValues()', 3000 );
		}
	});

	

});


// 27jul10. System doesn't set shipzone radio button anymore
//          (in order to force user to set it, and hopefully reduce discrepancies)
// so now need to check if user has set the shipZone 
function validateCart() {
	//alert("validateCart");
	// has shipzonebutton been set by user ? check to see if a button is selected
	zoneButton = jQuery('input:radio[name=shipzonebutton]:checked').val();
	
	if (zoneButton == undefined) {
		// if not, tell user he must select one
		jQuery('#shipzoneplease').hide().show('slow', function() {
			jQuery('#shipzoneplease').parent().addClass('redBorder');
		});
	} else {
		// if one is set then proceed to paypal express as normal
		jQuery('#shipzoneplease').hide();
		//alert("zoneButton="+zoneButton);
		window.location.href = "wordpress/wp-content/plugins/eshop/ppx_setexpress.php";
		
	}
	
}







