// "use strict";
/*jslint browser: true, newcap: true, nomen: false, onevar: true, eqeqeq: true, plusplus: true, bitwise: true, immed: true, evil: true */
/*global jQuery: true, window: true */
(function($, _) {
  // Because MS IE sucks ass
  if(typeof(String.prototype.trim) === 'undefined') {
    String.prototype.trim = function() {
      return String(this).replace(/^\s+|\s+$/g, '');
    };
  }

  function apply_toggles () {
    $('.toggler').click(
      function() {
        $(this).parent().find('.toggleable').toggle('fast');
        return false;
      }).
      attr('title', 'Click to toggle extra information').
      parent().find('.toggleable').hide();
  }

  function setup_video () {
    if ($('#test_video').length) {
      _.flowplayer('test_video', '/flash/flowplayer.swf', {
        clip: {
          autoPlay: false,
          autoBuffering: true
        }
      });
    }
  }
  
  // Ensure files are filled in
  function validate_contact_form () 
  {
    if('' === $.map([ 
      '#name', 
      '#email', 
      '#phone', 
      '#query' ], 
      function (item) { return $(item).val(); }).join().trim())
    {
      _.alert('Please complete all of the form fields');
      return false;
    }
    $('#dummy').val('Test123');
    return false;
  }
  
  function quick_order (country, product_set) {
    // These site must be handled separately for some reason
    // See: http://www.waiora.com/global
    var
      url,
      product = '',
      frame_target,
      distributor = '596367',
      separate_sites = { 
        'HK': 'http://www.waiora.com.hk', 
        'TW': 'http://shop.waiorahk.com', 
        'JP': 'http://www.waiora.co.jp' },
      // NFR (Not for resale) is outside US from what I see
      domain = ('US' === country) ? 'waiora.com' : 'waioranfr.com';
    if (separate_sites[country]) {
      location.href = separate_sites[country];
      return false;
    }
    // Single product hack
    // Note, see: https://shop.waioranfr.com/pls/ngsipsp/ngs_instore.show_product_listing?fn_category_id=2126&fn_product_id=6000
    if (6000 === product_set) {
      product = '6000';
      product_set = '';
    }
    url = 'https://shop.' + domain + '/pls/ngsipsp/ngs_instore_check_out.campaign_order?fv_organization_code=NFR&fv_product_number=' + product + '&fv_product_set=' + product_set + '&fv_build_own_pack=NULL&fv_dm_promo=&fn_clear_cookies=1';
    frame_target = 'http://' + domain + '/index.php?c=' + country + '&d=' + distributor;
    $('#hidden_frame').html('<iframe id="cookie_cutter" src="' + frame_target + '" onload="setTimeout(function () { location.href=\'' + url + '\'; }, 2500);"></iframe>');
    $('.country_modal .content').html('<h3>Loading ...</h3>');
    if ($.browser.msie) {
      var cookie_cutting = _.open(frame_target, 'cookie_cutting', 'width=640, height=400');
      _.focus();
      setTimeout(cookie_cutting.close, 3000);
      url = frame_target; // In case pop up blocker turds things up
    }
    setTimeout(function () {
      location.href = url;
    }, 8000);

  }

  // Modal for country
  function show_country (product_set) {
    $('.country_modal').
      dialog({
        // hide:       'fade',
        // show:       'fade',
        width:      640, 
        height:     460, 
        modal:      true, 
        autoOpen:   true, 
        resizable:  false, 
        draggable:  false }).
      find('li a').unbind().click(function (event) {
        var country = $(event.target).attr('class').toUpperCase();
        return quick_order (country, product_set);
      });
  }

  // Set up order button click event
  function setup_product_ordering () {
    // Need a look up for different promo codes (see: https://shop.waioranfr.com/join)
    // Quantity   Code    Volume    ProductSet    Price
    // 14x3       21520   1000 CV   378           $1450 ($1650)
    // 6x3        21062   500 CV    385           $650 ($775)
    // 1x3        26010   110 CV    382           $130 ($155)
    var product_info = {
      'bottle_42':  378,
      'bottle_18':  385,
      'bottle_3':   382,
      'bottle':     6000,
    };
    $('.buy .external').click(function (event) {
      var 
        item        = $(event.target).closest('a'),
        product_set = product_info[item.attr('id')];
      show_country(product_set);
      return false;
    });
  }

  $(document).ready(function () {
    setup_product_ordering();
    apply_toggles();
    setup_video();
    $('.open_in_new_window').click(function (event) {
      _.open(event.target.href, 'external').focus();
      return false;
    });
    $('.section').attr('title', 'Click here and find out more!');
    $('form.enquiry').submit(validate_contact_form);
    $('.sections').tabs({ fx: { opacity: 'toggle', duration: 'slow' } }).tabs('rotate', 5000);
    $('.section').live('click', function(event) { _.location = $(event.target).attr('href'); });
  });
  
}(jQuery, window));

