Panagora.Site = new (Panagora.Class.create(
/** @lends Panagora.Site */
function Site() {

	/* Private variables
	 ********************/

	var self = this;
	var validator;
	var submitting = false;
	
	this.mouseX = 0;
	this.mouseY = 0;
	
	function init() {

		// validation
		$j.validator.addMethod('emailFix', function (value) {
			return /\.[a-z]{2,}$/.test(value);
		}, '');

		validator = $j('form#newsletter-signup-form').validate({
			submitHandler: onNewsletterFormSubmit,
			onkeyup: function (element) {
				if (element.id == 'email') {
					validator.element(element);
				}
			},
			rules: {
				email: {
					required: true,
					email: true,
					emailFix: true
				}
			},
			errorPlacement: function (error, element) {
				error.insertAfter(element);
			},
			messages: {
				email: { email: '*' }
			}
		}); 

		$j(document).mousedown(getMouseCoords);

		$j().ajaxStart(function () { $j('body').addClass('waiting'); } )
			.ajaxStop(function () { $j('body').removeClass('waiting'); } );

		$j('#header-navigation ul li').hover(
			function(){ $j(this).addClass('hover'); },
			function(){ $j(this).removeClass('hover'); }
		);

		$j('#doc-newsletter a').click(function(event){
			event.preventDefault();
			$j('#newsletter-signup').toggle();
		});
		$j('#header-navigation ul .brands-overview a').click(function(event){
			event.preventDefault();
			toggleBrands();
		});

		$j('#newsletter-email').focus(function(){
			if (this.value == this.defaultValue || this.value == '')
				this.value = '';
		});
		$j('#newsletter-email').blur(function(){
			if (this.value == this.defaultValue || this.value == '')
				this.value = this.defaultValue;
		});

		//fix IE PNG issues
		$j(document).pngFix();
		
		//billboard gallery
		$j('#billboard-top').innerfade({ 
            type: 'random',
			speed: 1000,
			timeout: 5000, 
			containerheight: '59px'
		});

	}
	
	function toggleBrands() {
		if ($j('#brands-overview').is(':visible')) {
			$j(document).unbind('click', onDocClick);
		} else {
			$j(document).click(onDocClick);
		}
		
		$j('#brands-overview').slideToggle('slow');
	}

	function onNewsletterFormSubmit(form) {
		Panagora.post({
			url: form.action,
			data: {
				id: form.elements.id.value,
				email: form.elements.email.value,
				partial: 'newslettersignup'
			},
			dataType: 'text',
			success: function(text) {
				$j('#newsletter-signup').html(text);
			},
			failure: function() {
			}
		});
		return false;
	}
	
	function onDocClick(e) {
		if (!$j(e.target).closest('#brands-overview').length
			&& !$j(e.target).closest('li.brands-overview').length) {
			toggleBrands();
		}
	}

	function getMouseCoords(e) {
		self.mouseX = e.pageX;
		self.mouseY = e.pageY;
	}

	this.updateCartSubmitButton = function _updateCartSubmitButton(willRedirect) {
		if (willRedirect) {
			$j('#process-order').addClass('redirect');
		} else {
			$j('#process-order').removeClass('redirect');
		}
	}

	Panagora.ready(init);

}))();

Panagora.errorAlert = function _errorAlert(message) {
	Panagora.alert(message, true);
};

Panagora.alert = function _alert(message, error) {
	var str = Panagora.applyConstants(message);
	
	var noticeTimeout;
	
	var className = error ? "popup-notice error" : "popup-notice";
	
	var notice = $j('<div class="' + className + '"></div>')
		.text(str)
		.css({position: 'absolute', left: Panagora.Site.mouseX + 'px', top: Panagora.Site.mouseY + 'px'})
		.appendTo($j(document.body));

	function startHideNotice() {
		noticeTimeout = setTimeout(hideNotice, 3000);
		$j(document).unbind('mousemove', startHideNotice);
	}
	
	function hideNotice() {
		notice.fadeOut('fast', onNoticeHidden);
	}
	
	function onNoticeHidden() {
		notice.remove();
	}
		
	$j(document).mousemove(startHideNotice);
};

(function ($) {
  var originalVal = $.fn.val;
  $.fn.val = function(value) {
    if (typeof value != 'undefined') {
      if (value === null) { value = ''; }
    }
    return originalVal.call(this, value);
  };
})(jQuery);
