
$(window).load(function(){
	if($('div.thumbnails').data('jsp'))
		$('div.thumbnails').data('jsp').reinitialise();
	$('.jspDrag').addTouch();
});
$(function(){
	var notificationBox = $('<div />', {
		id: 'notification-box'
	});
	notificationBox = $(notificationBox, {'class': 'ui-helper-hidden'});
	notificationBox.appendTo('body').notificationBox();
	$('div.thumbnails').jScrollPane({
		showArrows: true
	});
	$('a.search-btn').click(function(){
		$('#search-box').slideToggle();
		return false;
	});
	$('#search-form').submit(function(evt){
		var action = this.action
		$.get(action + '.json',$(this).serialize(), function(data){
			$('div.thumbnail-holder').html(data.html);
			if($('div.thumbnails').data('jsp')) {
				setTimeout(function(){
					$('div.thumbnails').data('jsp').reinitialise();
				}, 500);
				setTimeout(function(){
					$('div.thumbnails').data('jsp').reinitialise();
				}, 1000);
				setTimeout(function(){
					$('div.thumbnails').data('jsp').reinitialise();
				}, 1500);
				setTimeout(function(){
					$('div.thumbnails').data('jsp').reinitialise();
				}, 2000);
				setTimeout(function(){
					$('div.thumbnails').data('jsp').reinitialise();
				}, 5000);
			}
		}, 'json');
		evt.stopImmediatePropagation();
		return false;
	});
	$('img.my-gallery-item, div.my-gallery-popup').mouseover(function(){
		var $this = $(this);
		var itemid = $this.attr('galleryid');
		$('div.my-gallery-popup[galleryid=' + itemid +']').show();
	}).mouseleave(function(){
		var $this = $(this);
		var itemid = $this.attr('galleryid');
		$('div.my-gallery-popup[galleryid=' + itemid +']').hide();
	});
	$('a.email-signup-link').click(function(){
		$('div.email-popup').toggle(); //'slide', {direction: 'down'});
		return false;
	});
	$('a.email-popup-btn').click(function(evt){
		evt.stopPropagation();
		var $this = $(this);
		var $email = $('input[name=EmailSignup]');
		$.post($this.attr('href'), {Email: $email.val()}, function(data){
			if(data.errors.length) {
				alert(data.errors.join('\n'));
			}
			if(data.messages.length) {
				alert(data.messages.join('\n'));
				$email.val('');
				$('a.email-signup-link').first().click();
			}
		}, 'json');
		return false;
	});
	$('input[name=SearchTerm]').keyup(function(e) {
		if(e.keyCode == 13) {
			$('a.dropdown-search-btn').click();
		}
	});
	$('a.search-link').click(function(){
		$('div.search-dropdown').slideToggle();
		return false;
	});
	$('a.dropdown-search-btn').click(function(){
		var term = $(this).closest('div').find('input').val();
		window.location = $(this).attr('href') + '/' + term;
		return false;
	});
	$('#search-form select').live('change', function(){
		return $(this.form).submit();
	});
	$('a.view-details-slider').live('click', function(){
		window.location.hash = '#view-details';
		$('a.hide-details-slider').show();
		$(this).hide();
		$('#main-content-wrapper').scrollTo($('div.piece-details').width()-10, 800, {
			axis:'x'
		});
		return false;
	});
	$('a.add-to-my-gallery').live('click', function(){
		$.get($(this).attr('href') +'.json', {}, function(data){
			notificationBox.html(data.message).notificationBoxShow();
			setTimeout(function(){
				notificationBox.notificationBoxHide()
				}, 1500);
			if(data.linktext)
				$('.my-gallery-link').html(data.linktext);
		}, 'json');
		
		
		return false;
	});
	$('a.remove-from-my-gallery').live('click', function(){
		$.post($(this).attr('href'),{}, function(){
			window.location.reload();
		});
		return false;
	});
	$('a.hide-details-slider').live('click', function(){
		window.location.hash = '#hide-details';
		$('a.view-details-slider').show();
		$(this).hide();
		$('#main-content-wrapper').scrollTo('0px', 800, {
			axis:'x'
		});
		return false;
	});
	$('div.piece-wrap').live('click', function(){
		if($('a.view-details-slider').is(':hidden'))
			$('a.hide-details-slider').click();
		else
			$('a.view-details-slider').click();
	});
	$('a.fancy-dialog').live('click', function(){
		fancyDialog($(this).attr('href'));
		return false;
	});
	$('a.show-additional-images').live('click', function() {
		$.fancybox.showActivity();
		$.get($(this).attr('href') + '.json', {}, function(data){
			if(data.length)
				$.fancybox(data);
			else
				$.fancybox('Sorry, no additional images found');
		}, 'json');
		return false;
	});
	$('a.card-validation-popup').click(function(evt){
		$.fancybox('For Visa and Mastercard: The last three digits in reverse italics ' +
			'on the back of the card above the signature line, immediately following ' +
			'the credit card account number.<br/><br/>For American Express: The four ' +
			'digit code on the front of the card, above and to the right of the account number.');
		evt.stopPropagation();
		return false;
	})
	$('a.add-to-cart').live('click', function() {
		//return false;
		});
	$('#copy-shipping-info').click(function(){
		if($(this).attr('checked'))
			copyAddressInfo($(this).closest('form'));
	});
	$('a.update-quantity').live('click', function(){
		var count = $(this).closest('tr').find('input[name=ProdCount]').val();
		window.location = $(this).attr('href') + '/' + parseInt(count, 10);
		return false;
	});
	$('input[name=PayMethod]').click(function(){
		var $credit_div = $('#credit-card-information');
		var $check_div = $('#check-information');
		var $amazon_div = $('#amazon-information');
		var choice = $(this).val();
		if($credit_div.is(':visible') && choice != '1')
			$credit_div.slideNewUp();
		if($check_div.is(':visible') && choice != '2')
			$check_div.slideNewUp();
		if($amazon_div.is(':visible') && choice != '3')
			$amazon_div.slideNewUp();
		switch(choice) {
			case '1':
				if($credit_div.is(':hidden'))
					$credit_div.slideDown();
				break;
			case '2':
				if($check_div.is(':hidden'))
					$check_div.slideDown();
				break;
			case '3':
				if($amazon_div.is(':hidden'))
					$amazon_div.slideDown();
				break;
			default:
				break;
		}
	});
	$("#check-textarea").click(function(e){
		e.preventDefault();
		$(".loading").show();

		$("#text-content")
		.spellchecker({
			lang: "en",
			engine: "google",
			url: "/checkspelling.php",
			suggestBoxPosition: "below"
		})
		.spellchecker("check", function(result){

			// spell checker has finished checking words
			$("div.loading").hide();

			// if result is true then there are no badly spelt words
			if (result) {
				alert('There are no mispellings.');
			}
		});
	});

	var $itemsWithMenu = jQuery('ul.nav > li');

	var open = function(evt){
		var $heading = $(this).children('h2');
		$heading.data('oldcolor', $heading.css('color'));
		$heading.css('color', '#000');
		if(this.menuTimeout){
			clearTimeout(this.menuTimeout);
			this.menuTimeout = null;
		}

		$itemsWithMenu.not(this).find('ul.dropdown:visible').slideUp();
		if(!this.$menu){
			this.offsetSet = true;
			var offset = jQuery(this).offset();
			var width = jQuery(this).outerWidth();
			offset.top = offset.top + jQuery(this).outerHeight();
			this.$menu = jQuery('ul.dropdown', this).width(width);//.offset(offset);
		}
		if(this.$menu && this.$menu.length > 0)
			this.$menu.slideDown();
		evt.stopPropagation();
	};
	var close = function(){
		var $heading = $(this).children('h2');
		if($heading.data('oldcolor'))
			$heading.css('color', $heading.data('oldcolor'));
		if(this.$menu && this.$menu.length > 0){
			var $menu = this.$menu;
			this.menuTimeout = setTimeout(function(){
				$menu.slideUp();
			}, 800);
		}
	};
	var closeFromOutside = function() {
		var $node = $('ul.dropdown').closest('li');
		$node.each(function(){
			var $heading = $(this).children('h2');
			if($heading.data('oldcolor'))
				$heading.css('color', $heading.data('oldcolor'));
			if(this.$menu && this.$menu.length > 0){
				var $menu = this.$menu;
				$menu.slideUp();
			}
		});
		hideAllPopups();
	};
	$itemsWithMenu.live('mouseenter click', open).live('mouseleave dblclick', close);
	$('body').live('click', closeFromOutside);
	var hash = window.location.hash;
	if(hash.substring(1) == 'view-details') {
		$('a.view-details-slider').click();
	}
});

$.prototype.slideNewUp = function (speed,callBack) {
	var target = $(this);
	var h = target.height();
	var cssHeight=target.css('height');
	target.animate(
	{
		height: '1px'
	}, speed, function() {
		target.hide();
		target.height(h);
		target.css('height',cssHeight);
		if(typeof(callBack) == 'function')
			callBack();
	}
	);
};

function hideAllPopups() {
	$('div.product-square').each(function(){
		var $popup = $(this).data('$popup');
		if($popup && $popup.is(':visible')) {
			$popup.hide();
			$popup.unbind('mouseleave');
		}
	});
}
function copyAddressInfo(form) {
	var $form = $(form);
	$form.find('[name*=Billing]').each(function(){
		var $this = $(this);
		var label = $this.attr('name').split('[').pop().replace(']', '');
		var $result = $form.find('[name^=Shipping]').filter('[name*=' + label + ']');
		if($result) {
			$result.val($this.val());
		}
	});
}



