$(document).ready(function() {
	/* Hide all images in news image slider */
	$('#product-detail-image-wrapper.news-detail-image-wrapper').find('img').hide();
	
	/* Remove text in newsletter input */
	$('#news-newsletter-email').each(function() {
		var $this = $(this);
		var originalValue = $this.val();
		$this.focus(function() {
			if ($this.val() == originalValue)
				$this.val('');
		}).blur(function() {
			if ($this.val() == '')
				$this.val(originalValue);
		});
	});
	
	/* Disable check out button */
	$('a.table-cell-150-button.inactive').click(function() {
		return false;
	});
	
	/* Sort dropdown */
	$('#dropdown-sort-header').click(function() {
		if ($('#dropdown-sort-list').is(':visible')) {
			$('#dropdown-sort-list').slideUp(200, function() { $('#dropdown-sort-header').removeClass('active'); });
			$('body').unbind('click');
		} else {
			$('#dropdown-sort-list').slideDown(200);
			$('#dropdown-sort-header').addClass('active');
			$('body').bind('click', function() {
				$('#dropdown-sort-list').slideUp(200, function() { $('#dropdown-sort-header').removeClass('active'); });
				$(this).unbind('click');
			});
		}
		return false;
	});
	/* Sort dropdown2 */
	$('#dropdown-sort-header-2').click(function() {
		if ($('#dropdown-sort-list-2').is(':visible')) {
			$('#dropdown-sort-list-2').slideUp(200, function() { $('#dropdown-sort-header-2').removeClass('active'); });
			$('body').unbind('click');
		} else {
			$('#dropdown-sort-list-2').slideDown(200);
			$('#dropdown-sort-header-2').addClass('active');
			$('body').bind('click', function() {
				$('#dropdown-sort-list-2').slideUp(200, function() { $('#dropdown-sort-header-2').removeClass('active'); });
				$(this).unbind('click');
			});
		}
		return false;
	});	
	/* Sort dropdown3 */
	$('#dropdown-sort-header-3').click(function() {
		if ($('#dropdown-sort-list-3').is(':visible')) {
			$('#dropdown-sort-list-3').slideUp(200, function() { $('#dropdown-sort-header-3').removeClass('active'); });
			$('body').unbind('click');
		} else {
			$('#dropdown-sort-list-3').slideDown(200);
			$('#dropdown-sort-header-3').addClass('active');
			$('body').bind('click', function() {
				$('#dropdown-sort-list-3').slideUp(200, function() { $('#dropdown-sort-header-3').removeClass('active'); });
				$(this).unbind('click');
			});
		}
		return false;
	});
	/* Replace checkboxes */
	$('input.replace').each(function() {
		var thisCheckbox = $(this);
		var newCheckbox = $('<div class="checkbox" id="checkbox-replaced-' + thisCheckbox.attr('id') + '">');
		if (thisCheckbox.is(':checked')) {
			newCheckbox.addClass('active');
		}
		newCheckbox.bind('click', function() {
			if (thisCheckbox.is(':checked')) {
				thisCheckbox.removeAttr('checked');
				newCheckbox.removeClass('active');
			} else {
				thisCheckbox.attr('checked', true);
				newCheckbox.addClass('active');
				newCheckbox.removeClass('error');
			}
		});
		thisCheckbox.next('label').css({float: 'left'}).after('<div style="clear: both"></div>').click(function() {
			newCheckbox.trigger('click');
			return false;
		}).find('a').click(function() {
			if ($(this).attr('target') == '_blank')
				window.open($(this).attr('href'));
			else
				window.location = $(this).attr('href');
			return false;
		});
		thisCheckbox.after(newCheckbox);
		thisCheckbox.hide();
	});
	
	/* Check out - Fold out delivery fields */
	if ($('#check-out-delivery-wrapper').length > 0) {
		var checkOutDeliveryWrapper = $('#check-out-delivery-wrapper');
		checkOutDeliveryWrapper.hide();
		$('#checkbox-replaced-check-out-delivery-checkbox').bind('click', function() {
			if ($('#check-out-delivery-checkbox').is(':checked')) {
			checkOutDeliveryWrapper.slideDown(200);
			checkOutDeliveryWrapper.find('input.to-be-required').addClass('required');
		} else {
			checkOutDeliveryWrapper.slideUp(200);
			checkOutDeliveryWrapper.find('input.to-be-required').removeClass('required');
		}
		});
	}
	$('a.inactive:[href=checkout/]').click(function() {
		return false;
	});
	
	/* Article Detail - Share */
	$('#button-share-fold-out').hide();
	$('#button-share-link').toggle(function() {
			$('#button-share-fold-out').slideDown(300);
		return false;
	}, function() {
		$('#button-share-fold-out').slideUp(300);
		return false;
	});
	
	
	
	/* Check Out Form */
	$('#check-out-form').submit(function() {
		if (validateForm($(this)))
			return true;
		else
			return false;
	});
	/* Form Validation */
	function validateForm(form) {
		var returnBool = true;
		form.find('.required').each(function() {
			$this = $(this);
			$this.tagName = $this.get(0).tagName.toLowerCase();
			if ($this.tagName == 'input') { // This is a input
				if ($this.attr('type') == 'checkbox') { // This is a checkbox
					if (!$this.attr('checked')) { // Checkbox is not checked
						$this.next('div.checkbox').addClass('error');
						returnBool = false;
					}
				} else { // This is a text field
					if (!$this.val()) { // Check if the field is empty
						$this.addClass('error');
						returnBool = false;
					} else if ($this.hasClass('email')) { // Check if the field is supposed to be a email
						//var emailFilter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
						var emailFilter = /^([a-zA-Z0-9_\.\-+])+@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/;
						if (!emailFilter.test($this.val())) {
							$this.addClass('error');
							returnBool = false;
						} else { // The email is valid
							$this.removeClass('error');
						}
					} else { // This field is valid
						$this.removeClass('error');
					}
				}
			}
		});
		return returnBool;
	}
	
	/* Lightbox triggers */
	$('.open-lightbox').click(function() {
		openLightbox('templates/lightbox/login.html', '203', '230');
		return false;
	});
	$('.lightbox-close').live('click', function() {
		closeLightbox();
		return false;
	});
	$('#lightbox-ok-button').live('click', function() {
		closeLightbox();
		return false;
	});
	$('a.table-cell-150-button.show-popup').click(function() {
		openLightbox('templates/lightbox/country.html', '544', '70');
		return false;
	});
	$('#terms-conditions-trigger').click(function() {
		openLightbox('templates/lightbox/terms-condition.html', '544', '500');
		return false;
	});
	
});

/* Lightbox */
function openLightbox(url, width, height) {
    var overlay = $('<div id="lightbox-overlay"></div>');
    var content = $('<div id="lightbox-content"><p id="lightbox-content-loading">Loading...</p></div>');
    overlay.css({
    	opacity: '0'
    });
    content.css({
    	opacity: '0',
    	height: height + 'px',
    	width: width + 'px',
    	marginTop: - (height / 2) - 40 + 'px',
    	marginLeft: - (width / 2) - 40 + 'px'
    });
    $('body').append(overlay).append(content).css({overflow: 'hidden'});
    overlay.animate({opacity: '0.7'}, 500).click(function() {
    	closeLightbox();
    });
    content.animate({opacity: '1'}, 500, function() {
    	content.css('filter','');
    });
    $.ajax({
    	url: url,
    	success: function(data) {
    		content.html(data);
    	}
    });
}

function closeLightbox() {
    $('#lightbox-overlay').animate({opacity: '0'}, 500, function() {
    	$(this).remove();
    });
    $('#lightbox-content').animate({opacity: '0'}, 500, function() {
    	$(this).remove();
    });
    $('body').css({overflow: 'auto'});
}

$(window).load(function() {
	/* Move header arrow on load */
	var headerArrowTimeout;
	var headerArrow = $('#header-arrow');
	var activeMenuItem = $('#header-menu a.active');
	var arrowPos = Math.floor(activeMenuItem.position().left + (activeMenuItem.width() / 2) - 22);
	headerArrow.css('marginLeft', arrowPos + 'px');
	
	/* Move header arrow on menu item hover */
	$('#header-menu a').hover(function() {
		clearTimeout(headerArrowTimeout);
		var thisItem = $(this);
		headerArrow.stop().animate({marginLeft: Math.floor(thisItem.position().left + (thisItem.width() / 2) - 22) + 'px'}, 200);
	}, function() {
		headerArrowTimeout = setTimeout(function() {
			headerArrow.stop().animate({marginLeft: arrowPos + 'px'}, 300);
		}, 150)
	});

    /* Product detail image slider */
	if ($('#news-detail-wrapper').length) {
		var isWrapper = $('#product-detail-image-wrapper');
		var isTotal = isWrapper.find('img').length;
		var isCurrent = 1;
		isWrapper.find('img').show();
		isWrapper.find('img:not(:eq(0))').hide();
		isImage = isWrapper.find('img:visible');
		isImage = isWrapper.find('img:eq(0)');
		
		isImage.addClass('current-image');
		
		isImageNext = isImage.next('img');
		isImageNext.show().css({
		    height: '100px',
		    position: 'absolute',
		    top: (isImage.height() / 2) - 50 + 'px'
		});
		isImageNext.css({
		    left: isImage.position().left + isImage.width() + 80 + 'px'
		});
		isImageNext.next('img').hide();
				
		// Position arrows
		$('#paging-arrow-next').css({
		    left: isImage.position().left + isImage.width() + 'px',
		    top: (isImage.height() / 2) - 20 + 'px',
		    display: 'block'
		});
		if (isTotal == isCurrent) {
			$('#paging-arrow-next').addClass('inactive');
		}
		$('#paging-arrow-prev').css({
		    left: isImage.position().left - 40 + 'px',
		    top: (isImage.height() / 2) - 20 + 'px',
		    display: 'block'
		}).addClass('inactive');
		
		if (isTotal == 1) {
			$('#paging-arrow-next').hide();
			$('#paging-arrow-prev').hide();
		}
		
		
		$(window).resize(function() { // Reposition on resize
			
			//isWrapper.find('img:not(:eq(' + (isCurrent - 1) + '))').hide();
			isImage = isWrapper.find('img[class="current-image"]');
			//isImage = isWrapper.find('img:eq(0)');
			
			isImageNext = isImage.next('img');
			isImageNext.show().css({
			    height: '100px',
			    position: 'absolute',
			    top: (isImage.height() / 2) - 50 + 'px'
			});
			isImageNext.css({
			    left: isImage.position().left + isImage.width() + 80 + 'px'
			});
			
			isImagePrev = isImage.prev('img');
			isImagePrev.css({
			    height: '100px',
			    position: 'absolute',
			    top: (isImage.height() / 2) - 50 + 'px'
			});
			isImagePrev.css({
			    left: isImage.position().left - isImagePrev.width() - 80 + 'px'
			});
					
			// Position arrows
			$('#paging-arrow-next').css({
			    left: isImage.position().left + isImage.width() + 'px',
			    top: (isImage.height() / 2) - 20 + 'px'
			});

			$('#paging-arrow-prev').css({
			    left: isImage.position().left - 40 + 'px',
			    top: (isImage.height() / 2) - 20 + 'px'
			});
			
		});
		
		$('#paging-arrow-next').bind('click', function() {
			if ($(this).hasClass('inactive'))
				return false;
			if (isCurrent == isTotal-1) {
				$(this).addClass('inactive');
			} else if (isCurrent == 1) {
				
			}
			$('#paging-arrow-prev').removeClass('inactive');
			if (isCurrent < isTotal) {
				isImage = isWrapper.find('img:eq(' + isCurrent + ')');
				//isImage.removeAttr('style').css('display', 'inline');
				$('img.current-image').removeClass('current-image');
				isImage.removeAttr('style').addClass('current-image');
				
				isImagePrev = isImage.prev('img');
				isImagePrev.css({
					height: '100px',
					position: 'absolute',
					top: (isImage.height() / 2) - 50 + 'px'
				});
				isImagePrev.css({
					left: isImage.position().left - isImagePrev.width() - 80 + 'px'
				});
				isImagePrev.prev('img').hide();
				
				isImageNext = isImage.next('img');
				isImageNext.show().css({
					height: '100px',
					position: 'absolute',
					top: (isImage.height() / 2) - 50 + 'px'
				});
				isImageNext.css({
					left: isImage.position().left + isImage.width() + 80 + 'px'
				});
				isImageNext.next('img').hide();
				
				isCurrent++;
				// Position arrows
				$(this).css({
					left: isImage.position().left + isImage.width() + 'px',
					top: (isImage.height() / 2) - 20 + 'px'
				});
				$('#paging-arrow-prev').css({
					left: isImage.position().left - 40 + 'px',
					top: (isImage.height() / 2) - 20 + 'px'
				});
			}
			
			return false;
		});
		$('#paging-arrow-prev').bind('click', function() {
			if ($(this).hasClass('inactive'))
				return false;
			if (isCurrent == 2) {
				$(this).addClass('inactive');
			}
			$('#paging-arrow-next').removeClass('inactive');
			if (isCurrent > 1) {
				isImage = isWrapper.find('img:eq(' + (isCurrent - 2) + ')');
				//isImage.removeAttr('style').css('display', 'inline');
				$('img.current-image').removeClass('current-image');
				isImage.removeAttr('style').addClass('current-image');
				
				isImagePrev = isImage.prev('img');
				isImageNext = isImage.next('img');
				
				isImagePrev.prev('img').hide();
				isImageNext.next('img').hide();
				
				isImageNext.css({
					height: '100px',
					position: 'absolute',
					top: (isImage.height() / 2) - 50 + 'px'
				});
				isImageNext.css({
					left: isImage.position().left + isImage.width() + 80 + 'px'
				});
				
				isImagePrev.show().css({
					height: '100px',
					position: 'absolute',
					top: (isImage.height() / 2) - 50 + 'px'
				});

				isImagePrev.css({
					left: isImage.position().left - isImagePrev.width() - 80 + 'px'
				});	
				
				isCurrent--;
				// Position arrows
				$('#paging-arrow-next').css({
					left: isImage.position().left + isImage.width() + 'px',
					top: (isImage.height() / 2) - 20 + 'px'
				});
				$(this).css({
					left: isImage.position().left - 40 + 'px',
					top: (isImage.height() / 2) - 20 + 'px'
				});
			}
			return false;
		});
	}
});
