jQuery(function()
{
	// this initialises the demo scollpanes on the page.
	jQuery('#scroll').jScrollPane();
	
	var isResizing;

	// and the body scrollpane
	var setContainerHeight = function()
	{
		// IE triggers the onResize event internally when you do the stuff in this function
		// so make sure we don't enter an infinite loop and crash the browser
		if (!isResizing) { 
			isResizing = true;
			var w = jQuery(window);
			var c = jQuery('#container');
			var p = (parseInt(c.css('paddingLeft')) || 0) + (parseInt(c.css('paddingRight')) || 0);
			jQuery('body>.jScrollPaneContainer').css({'height': w.height() + 'px', 'width': w.width() + 'px'});
			c.css({'height': (w.height()-p) + 'px', 'width': (w.width() - p) + 'px', 'overflow':'auto'});
			c.jScrollPane();
			isResizing = false;	
		}
	}
	jQuery(window).bind('resize', setContainerHeight);
	setContainerHeight();
	
	// it seems like you need to call this twice to get consistantly correct results cross browser...
	setContainerHeight();
	
});

jQuery(document).ready(function(){
  // homepage slider
  slide_count = jQuery("#slider-items img").length;
  if (slide_count > 0) { slide_start(); }

  // collections gallery
  collection_count = jQuery("#collection-items img").length;
  jQuery('#collection-prev').click(function() {
	  collection_prev();
	  return false;
  });
  jQuery('#collection-next').click(function() {
	  collection_next();
	  return false;
  });

  // shopp categories
  jQuery('ul.shopp_categories li a').click(function() {
	  if (jQuery(this).parent().find('ul').length > 0) {
		  jQuery(this).parent().find('ul').show();
		  return false;
	  }
  });

  // product rating
  var user_rated_val = jQuery("#user_rated").val();
  if (user_rated_val == 0) {
	  jQuery("#rating-container a").hover(
		  function() {
			  var thisid = jQuery(this).attr('id');
			  var idnmb = parseInt(thisid.substring(2));
			  for (var r=1; r<=idnmb; r++) {
				  jQuery("#pr"+r).addClass('hover');
			  }
		  }, 
		  function() {
			  jQuery("#rating-container a").removeClass('hover');
		  }
	  );
	  jQuery("#rating-container a").click(function() {
		  jQuery("#rating-loading").show();
		  var thisid = jQuery(this).attr('id');
		  var rating = parseInt(thisid.substring(2));
		  var pid = jQuery("#rating_pid").val();
		  jQuery.post(
			  js_siteurl+'index.php',
			  {
				FormAction: 'shopp_rating_submit',
				pid: pid,
				rating: rating
			  },
			  function(data) {
				  jQuery.post(
					  js_siteurl+'index.php',
					  {
						FormAction: 'shopp_rating_show',
						pid: pid
					  },
					  function(data) {
						  jQuery("#rating-container").html(data);
						  jQuery("#rating-loading").hide();
					  }
				  );
			  }
		  );
		  return false;
	  });
  } else {
	  jQuery("#rating-container a").css("cursor", "normal");
  }
});

// homepage slider
var slide_count = 0;
var slide_speed = 8000;
var slide_timeout = false;
var slide_current = 1;

function slide_next() {
  slide_stop();
  var nnumb = slide_current + 1;
  if (nnumb > slide_count) { nnumb = 1; }
  slide_change(nnumb);
  slide_start();
}
function slide_dot(tnmb) {
  slide_stop();
  slide_change(tnmb);
  slide_start();
}
function slide_change(nnmb) {
  var leftpos = (926 * nnmb) - 926;

  jQuery("#slider-items").animate({left: -leftpos}, 1000);

  jQuery("#slider-desc"+slide_current).hide(10,
	  function() {
		jQuery("#slider-desc"+nnmb).animate({width: "show"}, 500);
	  }
  );
  jQuery("#slider-dot"+slide_current).removeClass('on');
  jQuery("#slider-dot"+nnmb).addClass('on');
  slide_current = nnmb;
}
function slide_start() {
  slide_timeout = setTimeout("slide_next()", slide_speed);
}
function slide_stop() {
  clearTimeout(slide_timeout);
}

// collections gallery
var collection_count = 0;
var collection_current = 1;
function collection_preview(cnmb) {
	if (cnmb != collection_current) {
		jQuery("#collection-image"+collection_current).fadeOut(700);
		jQuery("#collection-image"+cnmb).fadeIn(700);

		jQuery("#collection-info"+collection_current).fadeOut(300);
		jQuery("#collection-info"+cnmb).fadeIn(700);

		collection_current = cnmb;
	}
}

function collection_next() {
	var nnmb = collection_current + 1;
	if (nnmb > collection_count) { nnmb = 1; }
	collection_preview(nnmb);
}

function collection_prev() {
	var pnmb = collection_current - 1;
	if (pnmb < 1) { pnmb = collection_count; }
	collection_preview(pnmb);
}

var shopp_preview_current = 1;
function shopp_preview_img(pnmb) {
	if (pnmb != shopp_preview_current) {
		jQuery("#prod-preview"+shopp_preview_current).fadeOut(700);
		jQuery("#prod-preview"+pnmb).fadeIn(700);
		shopp_preview_current = pnmb;
	}
}

// email a friend
function email_friend_hide_show(act) {
	if (act) {
		jQuery("#email_friend_lightbox").show();
	} else {
		jQuery("#email_friend_lightbox").hide();
	}
}

function email_friend_submit() {
	var ef_error = '';
	var ef_product_name_val = trim(jQuery("#ef_product_name").val());
	var ef_product_url_val = trim(jQuery("#ef_product_url").val());
	var ef_your_email_val = trim(jQuery("#ef_your_email").val());
	var ef_frields_email_val = trim(jQuery("#ef_frields_email").val());
	var ef_comment_val = trim(jQuery("#ef_comment").val());

    if (ef_your_email_val == '') {
		ef_error += "Your Email is required.\n";
    } else if (!/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(ef_your_email_val)) {
		ef_error += "Your Email is not valid.\n";
    }
    if (ef_frields_email_val == '') {
		ef_error += "Friend's Email is required.\n";
	} else {
		var ef_frields_email_arr = ef_frields_email_val.split(',');
		for (var e=0; e<ef_frields_email_arr.length; e++) {
			frield_email = trim(ef_frields_email_arr[e]);
			if (!/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(frield_email)) {
				ef_error += "Email '"+frield_email+"' is not valid.\n";
			}
		}
	}

    if (ef_error == '') {
		jQuery.post(
		  js_siteurl+'index.php',
		  {
			FormAction: 'email_friend_submit',
			ef_product_name: ef_product_name_val,
			ef_product_url: ef_product_url_val,
			ef_your_email: ef_your_email_val,
			ef_frields_email: ef_frields_email_val,
			ef_comment: ef_comment_val
		  },
		  email_friend_submit_success
		);
    } else {
	    alert(ef_error);
    }

	return false;
}

function email_friend_submit_success(data) {
  document.email_friend_form.reset();
  email_friend_hide_show(false);
  alert("Email was successfully sent.");
}

function subscribe_submit() {
	jQuery("#subscribe_errors").hide();
	var error = "";
	var subscribe_name_value = trim(jQuery("#subscribe_name").val());
	var subscribe_email_value = trim(jQuery("#subscribe_email").val());
	if (subscribe_name_value == '') {
		error += 'Name is required.<br>';
	}
	if (subscribe_email_value == '') {
		error += 'Email is required.<br>';
	} else if (!/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(subscribe_email_value)) {
		error += 'Email is not valid.<br>';
	}
	if (error != "") {
		jQuery("#subscribe_errors").html(error);
		jQuery("#subscribe_errors").animate({height: "show"}, 300);
		return false;
	} else {
		return true;
	}
}

function trim(str) {
  return str.replace(/^\s+|\s+$/g,"");
}

