/**
 * SiteClick CMS website
 * javascript initialisation
 */

if($ == undefined)
  alert('JavaScript error: JQuery not installed');


/**
 * Ajax shopping cart
 */
function AjaxBasket() {
	var _this 		 = this;
	this.ajaxDiv   = null;
	this.overlay   = null;
	this.root			 = document.getElementsByTagName('base')[0].href;

	/**
	 * Adds the overlay and ajax container div to the document
	 * Accepts the form to submit once the divs are created
	 */
	this.addAjaxDiv = function(cb) {
		_this.overlay = $('<div class="overlay"></div>');
		// Prevent click on overlay - makes cart modal
		$(_this.overlay).mousedown(function() {
			return false;
		})
		$('body').append(_this.overlay);
		$(_this.overlay).fadeTo('normal', 0.7, function() {
			_this.ajaxDiv = $('<div class="basketAjaxContainer"></div>');
			$('body').append(_this.ajaxDiv);
			$(_this.ajaxDiv).css({	left:			($('body').width() - $(_this.ajaxDiv).width())/2,
															zIndex:		'100'});

			_this.postForm(cb);
		})
	}


	/**
	 * Post form callback
	 */
	this.postForm_cb = function(html) {
		$(_this.ajaxDiv).html(html);
		// Register this function as the form handler for contained basket form
		$(_this.ajaxDiv).find('form').not('#pspForm').submit(_this.formSubmit);

		// Continue shopping button
		$(_this.ajaxDiv).find('button.basketContinue').mousedown(function() {
			$(_this.ajaxDiv).remove();
			_this.ajaxDiv = null;
			$(_this.overlay).fadeOut('normal', function() {
				$(this).remove();
				_this.overlay = null;
			})
			return false;
		})
	}

	/**
	 * Post the actual form data
	 * accepts the form to submit
	 */
	this.postForm = function(ele) {
		
		switch($(ele).get(0).tagName.toLowerCase()) {
			case 'form':
				$.post(_this.root + $(ele).attr('action'), $(ele).serialize(), _this.postForm_cb , 'html');
				break;
				
			case 'a':
				$.post(_this.root + $(ele).attr('href'), $(ele).serialize(), _this.postForm_cb , 'html');
				break;
		}
	}


	/**
	 * Form submit callback
	 */
	this.formSubmit = function() {
		if(!_this.ajaxDiv) {
			_this.addAjaxDiv(this);
		} else {
			_this.postForm(this);
		}
		return false;
	}


	/**
	 * Initialise function - attaches ajax behaviour to basket forms
	 */
	this.initialise = function() {
		$('form.basketAddProduct').submit(_this.formSubmit);
		$('a.basketOpen,a.basketAutoOpen').click(_this.formSubmit);
		$('a.basketAutoOpen').trigger('click');
	}

	_this.initialise();
}


function AjaxSearch() {

	var _this 		 = this;
	this.root			 = document.getElementsByTagName('base')[0].href;
	this.ajaxDiv   = null;
	this.overlay   = null;
	
	/**
	 * Destroy
	 */
	this.destroy = function() {
		$(_this.ajaxDiv).remove();
		_this.ajaxDiv = null;
		$(_this.overlay).fadeOut('normal', function() {
			$(this).remove();
			_this.overlay = null;
		})
	}

	/**
	 * Post form callback
	 */
	this.postForm_cb = function(html) {
		$(_this.ajaxDiv).html(html);

		// Register this function as the form handler for contained basket form
		$(_this.ajaxDiv).find('form').submit(_this.formSubmit);

		// Add close button
		var clo = $('<img src="js/gallery_close.png" title="Close window" style="float: right; cursor: pointer" />');
		$(_this.ajaxDiv).prepend(clo);
		$(clo).click(_this.destroy);
	}

	/**
	 * Post the actual form data
	 * accepts the form to submit
	 */
	this.postForm = function(ele) {
		$.get(_this.root + $(ele).attr('action'), $(ele).serialize(), _this.postForm_cb , 'html');
	}

	/**
	 * Adds the overlay and ajax container div to the document
	 * Accepts the form to submit once the divs are created
	 */
	this.addAjaxDiv = function(cb) {
		_this.overlay = $('<div class="overlay"></div>');

		// Prevent click on overlay - makes cart modal
		$(_this.overlay).mousedown(function() {
			return false;
		})
		$('body').append(_this.overlay);
		$(_this.overlay).fadeTo('normal', 0.7, function() {
			_this.ajaxDiv = $('<div class="ajaxContainer"></div>');



			$('body').append(_this.ajaxDiv);
			$(_this.ajaxDiv).css({	left:			($('body').width() - $(_this.ajaxDiv).width())/2,
															zIndex:		'100'});

			_this.postForm(cb);
		})
	}

	/**
	 * Form submit callback
	 */
	this.formSubmit = function() {

		if(!_this.ajaxDiv) {
			_this.addAjaxDiv(this);
		} else {
			_this.postForm(this);
		}
		return false;
	}


	/**
	 * Initialise function - attaches ajax behaviour to basket forms
	 */
	this.initialise = function() {
		$('form.search').submit(_this.formSubmit);
	}

	_this.initialise();
}

$(document).ready(function() {

  // Add target="_blank" attribute to links
  $('a.sc-new-window').attr('target', '_blank');

  // E-Commerce basket
	var myBasket = new AjaxBasket();

  // Search
	var mySearch = new AjaxSearch();

  // Gallery
  $('ul.light-gallery').each(function() {
		$(this).lightGallery({showTitles	:	$(this).hasClass('with-titles')});
	})
})
