/**
 * SiteClick CMS website
 * javascript initialisation
 */

if($ == undefined)
  alert('JavaScript error: JQuery not installed');

function StatsObj () {

  var _this = this;


  /**
   * Create random visitor ID
   */
  this.createId = function() {
    var s = '', aChars = new Array(0,1,2,3,4,5,6,7,8,9,'a','b','c','d','e','f');
    for(var i=0; i<32; i++) {
      s = s + aChars[Math.round(Math.random()*15)];
    }
    return s;
  }


  /**
   * Get screen dimensions
   */
  this.screenDimensions =       screen.width + 'x' + screen.height;

  /**
   * Get browser and version
   */
  this.browser          =       'Other';
  if($.browser.mozilla)
    this.browser        =       'Firefox';
  if($.browser.safari)
    this.browser        =       'Safari';
  if($.browser.msie)
    this.browser        =       'IE';
  if($.browser.opera)
    this.browser        =       'Opera';

  this.version          =       $.browser.version;

  /**
   * Get operating system
   */
  this.OS               =       'Other';
  if(navigator.platform.match(/Win/i) != null)
    this.OS             =       'Windows';
  if(navigator.platform.match(/Mac/i) != null)
    this.OS             =       'Mac';
  if(navigator.platform.match(/Linux/i) !=null)
    this.OS             =       'Linux';

  /**
   * Check for returning user
   */
  if($.cookie('user_id'))
    this.user           =       $.cookie('user_id');
  else {
    this.user           =       this.createId();
    $.cookie('user_id', this.user, {expires : 1000});
  }

  /**
   * Get referring page
   */
  this.referrer         =       document.referrer;
  this.location         =       location.href;

  /**
    * Get search engine info
    */
  if(this.referrer.match(/^http:\/\/www\.google\./i) != null) {
        this.engine   = 'Google';
        this.keywords = this.referrer.split('q=')[1].split('&')[0];
  }
  else if(this.referrer.match(/^http:\/\/search\.msn\./i) != null) {
        this.engine   = 'MSN';
        this.keywords = this.referrer.split('q=')[1].split('&')[0];
  }
  else if(this.referrer.match(/search\.yahoo\./i) != null) {
        this.engine   = 'Yahoo';
        this.keywords = this.referrer.split('p=')[1].split('&')[0];
  }
  else if(this.referrer.match(/ask\.com/i) != null) {
        this.engine   = 'Ask';
        this.keywords = this.referrer.split('q=')[1].split('&')[0];
  }

  /**
   * Display results
   */
  this.serialise = function() {
    var s = '';
    $.each(this, function(i, item) {
      if(typeof(item) == 'function')
        return;
      s += i + '=' + encodeURIComponent(item) + '&';
    })
    return s;
  }

  
  /**
   * Send results
   */
  this.send = function() {
    s           =       _this.serialise();
    var img     =       $('<img src="track.php?' + s + '" />');
    $(document.body).append(img);
    $(img).hide();
  }
  
  this.send();
}


function basket() {

	/**
	 * Make checkout button set action parameter and submit
	 */
	$('button.checkout').click(function() {
		if(confirm('You will now be redirected to pay via a secure server')) {
			$('form.shoppingBasket').find('input.action').val('checkout');
			$('form.shoppingBasket').submit();
		}
	})
	
	
	/**
	 * Make remove product click button submit basket form
	 */
	$('table.shoppingBasket').find('a.removeProduct').click(function() {

		if(confirm('Are you sure you wish to remove this item from your basket?')) {
			$('form.shoppingBasket').find('input.line').val($(this).attr('href').replace('#', ''));
			$('form.shoppingBasket').find('input.action').val('remove');
			$('form.shoppingBasket').submit();
		}
		return false;
	})

	/**
	 * Change basket quantity
	 */
	 $('table.shoppingBasket').find('input.qty').change(function() {
    	// Get line #
    	var line = $(this).attr('name').split('line_')[1];
		$('form.shoppingBasket').find('input.line').val(line);
		$('form.shoppingBasket').find('input.action').val('quantity');
		$('form.shoppingBasket').find('input.qty').val($(this).val());
		$('form.shoppingBasket').submit();
	 })


	/**
	 * Make PSP button autosubmit form
	 */
	 window.setTimeout( function() { $('form.psp').trigger('submit') }, 1000);
}


$(document).ready(function() {


  // Add target="_blank" attribute to links
  $('a.sc-new-window').attr('target', '_blank');

  // Stats tracking
  var Stats = new StatsObj();
  
  // E-Commerce basket
  basket();
  
  // Gallery
  $('ul.light-gallery').lightGallery();
})