﻿$(document).ready(function() {
	externalLinks();
	replaceFonts();
	bannerDisplay();
	intPayPal();
	currentPage();
	$('#quantity').customStyle();
});
function externalLinks(){
	$('a[rel = external]').attr('target','blank');
}
function replaceFonts(){
	Cufon.replace('h1,.download-flash h5');
}
function bannerDisplay(){
  var flashvars = {};
  var params = {wmode:"transparent", quality:'high'};
  var attributes = {};
  swfobject.embedSWF(flash, "flash-holder", "940", "290", "9.0.0", "/flash/expressInstall.swf", flashvars, params, attributes);
}

function intPayPal(){
  $('fieldset.hide').show();
  //$('input[name=postage]').show().val('£' + parseFloat($('input[name=shipping]').val()).toFixed(2));
  $('input[name=subtotal]').show().val('£' + parseFloat($('input[name=amount]').val()).toFixed(2));
  
  $('select[name=quantity]').change(function() {
	  var p = parseFloat($('input[name=amount]').val());
	  var q = parseInt($('select[name=quantity]').val());
	  //var s = parseFloat($('input[name=shipping]').val());
	  //var as = parseFloat($('input[name=shipping2]').val());		
	  if(q > 0){
		  if(q == 1){
			  //$('input[name=postage]').val('£' + s.toFixed(2));
			  $('input[name=subtotal]').val('£' + p.toFixed(2));
		  }else{
			  //$('input[name=postage]').val('£' + (s + (as * (q- 1))).toFixed(2));
			  $('input[name=subtotal]').val('£' + (p * q).toFixed(2));
		  }
	  }else{
		  //$('input[name=postage]').val('£0.00');
		  $('input[name=subtotal]').val('£0.00');
	  }
  });
}

function currentPage(){
	var set = false;
	var curUrl = window.location.pathname;
	var extension = curUrl.substr( (curUrl.lastIndexOf('/') ) ); //live line
	//var extension = "/staging" + curUrl.substr( (curUrl.lastIndexOf('/') ) );//staging line
	if(extension != "" && extension != "/"){
	  $('.menu li a').each(
		  function(){
			//alert(extension);  
		  	if($(this).attr('href') == extension){
			  $(this).attr('class', 'active');
			  set = true;
		  	}
	  });	
	}
}
	

(function($) {
    $.fn.extend({
        customStyle: function(options) {
			if($.browser.opera){
				$('#quantity').attr('class','opera-select');
			}else{
			  if (!$.browser.msie || ($.browser.msie && $.browser.version > 6)) {
				  return this.each(function() {
					  var currentSelected = $(this).find(':selected');
					  $(this).after('<span class="customStyleSelectBox"><span class="customStyleSelectBoxInner">' + currentSelected.text() + '</span></span>').css({ position: 'absolute', opacity: 0, fontSize: $(this).next().css('font-size') });
					  var selectBoxSpan = $(this).next();
					  var selectBoxWidth = parseInt($(this).width()) - parseInt(selectBoxSpan.css('padding-left')) - parseInt(selectBoxSpan.css('padding-right'));
					  var selectBoxSpanInner = selectBoxSpan.find(':first-child');
					  selectBoxSpan.css({ display: 'inline-block' });
					  selectBoxSpanInner.css({ width: selectBoxWidth, display: 'inline-block' });
					  var selectBoxHeight = parseInt(selectBoxSpan.height()) + parseInt(selectBoxSpan.css('padding-top')) + parseInt(selectBoxSpan.css('padding-bottom'));
					  $(this).height(selectBoxHeight).change(function() {
						  selectBoxSpanInner.text($(this).find(':selected').text()).parent().addClass('changed');
					  });
  
				  });
			  }	
			}
            
        }
    });
})(jQuery);
