/*
 * Fallback if console.log() does not exist - http://ajax-prototype.blogspot.com/2006/12/workaround-to-make-firebug-consolelog.html
 *
 */
try { console.log('init console... done'); } catch(e) { console = { log: function(str) {
	$('#debug').append(str + '<br/>');
} } }


$(document).ready(function() {

	/**
	* Image cycle
	*/
	if( $('#imgCycle a').length > 1 || $('#imgCycle img').length > 1) {
		
		//autoscroll only for frontpage
		if($('body').hasClass('frontpage')) {
			var tOut = 8000;
			var effect = 'scrollHorz';
		}
		else {
			var tOut = 0;
			var effect = 'fade';
		}
		
		$('#imgCycle').cycle({ 
			prev:   '#leftImgNav', 
			next:   '#rightImgNav', 
			speed:	1500, 
			timeout:tOut,
			fx:		effect
		});

		$('#imgCycle, #leftImgNav, #rightImgNav').mouseover(function() {
			$('#leftImgNav, #rightImgNav').css('display','block');
		}).mouseout(function(){
			$('#leftImgNav, #rightImgNav').css('display','none');
		});
	}


	//zoomable
	$('#imgCycle img.zoomable').loupe({
		width:	960,
		height:	465
	});
	
	//video
	if( $('.videos iframe').length > 0 ) {
		$('.videos iframe').each(function (index, domEle) {
			var srcValue = $(domEle).attr('src').split('?', 2);

			if(srcValue.length == 1) {
				 $(domEle).attr('src',  srcValue[0] + "?wmode=transparent");
			} else {
				var newSrc = srcValue[0] + "?wmode=transparent&" + srcValue[1];
				$(domEle).attr('src', newSrc);
			}
		});
	}
	
	$('.productPage #introBanner .videos iframe, .productPage #introBanner .videos object').each(function (index, domEle) {
		$(domEle).css({
			'width': '960px',
			'height': '465px'
		});
	});
	
	//to fix player keeping playing background. continues on line 76
	var videosHTML = $('.videos').html();
	
	$('#imgCycle .video').click(function(){
		var imageNo = $('#imgCycle .video').index(this);
		
		$('.videoObj:eq(' + imageNo + ')').show().css({
			'z-index':'1',
			'display':'block'
		});
		$('#imgCycle img').hide();

		$('.videoObj').parent().hover(function(){
			$('#leftImgNav, #rightImgNav').css('display', 'block');
		},function(){
			$('#leftImgNav, #rightImgNav').css('display', 'none');
		});
		
		$('#leftImgNav, #rightImgNav').click(function(){
			$('.videos').empty();
			$('.videos').html(videosHTML);
			
			$('#introBanner .videos iframe, #introBanner .videos object').addClass('videoObj');

			$('.videoObj').css('display', 'none');
			$('.videoObj').css('z-index', '0');
			$('.videoObj').parent().hover();
			$('#leftImgNav, #rightImgNav').click(function(){});
		});
		
		var videoSrc = $('.videoObj:eq(' + imageNo + ')').attr('src');
		var newSrc = videoSrc + "&autoplay=1";
		$('.videoObj:eq(' + imageNo + ')').attr('src', newSrc);
		
		return false;

	});

	$('#introBanner .videos iframe, #introBanner .videos object').each(function (index, domEle) {
		$(domEle).addClass('videoObj');
	});
	
	$('#imgCycle .video').each(function(){
		var imgWidth = $(this).width();
		
		var padding = (960 - imgWidth)/2;
		$(this).css('padding-left', padding);
		$(this).css('padding-right', padding);
	});	
	
	//start video link.
	$('.videoBox a').click(function() {
		$('#imgCycle .video').trigger('click');
		return false;
	});
	
	//navigation drop down
	$('#mainNavigation li').mouseover(function(){
		$(this).children('.subMenuBlock').css('display','block');
	}).mouseout(function(){
		$(this).children('.subMenuBlock').css('display','none');
	});
	
	$('#mainNavigation li.last').mouseover(function(){
		$(this).children('.subMenuBlock').css({
			'left'	:	'auto',
			'right'	:	'-10px'	
		});
	});


	// Fixed Top Bar
	$(window).scroll(function() {
		var topOffset = $(window).scrollTop();
		$("#header #topSection").css('top', topOffset);
	});
	
	
	/**
	* Font replacement
	*/
	Cufon.replace('ul#mainNavigation > li > a, ul#mainNavigation .subMenu span, ul#topNavigation, #shopByType, h1, h3, ul#footerNavigation span, #secondLevelNav span, .branding',{hover:true, hoverables:{a:true} });
	
	//form gfx elements
	//select boxes for qty box
	$(".sb").selectBox();
	
	
	/**
	* Blade redirection
	**/
	$("#colorsb").change(function() {
		var url = $("#colorsb").val();

		if(url != 'X') {
			window.location = url;
		}
		else {
			return false;
		}
	});


	/**
	* Color redirection
	**/
	$("#bladeedgesb").change(function() {
		var url = $("#bladeedgesb").val();

		if(url != 'X') {
			window.location = url;
		}
		else {
			return false;
		}

	});


	/**
	* Glossary
	*/
	$('ul#glossaryMenu li a').click(function(){
		var id = $(this).attr('class');

		$('ul#glossaryMenu li').removeClass('active');

		$(this).parent().addClass('active');

		$('#glossary .glossaryTerms').not('#' + id).hide();

		$('#glossary #' + id).show();
		
		Cufon.refresh();

		return false;
	});
	
	if(location.hash) { 
		$(location.hash.substring(1)).click();
		$('ul#glossaryMenu li a.' + location.hash.substring(1) ).trigger('click');
	}


	/**
	* Front promo rollover
	*/
	$('#bannerBlock ul li').mouseenter(function() {
		$('#bannerBlock ul li a.rollover').css('display', 'none');
		
		$(this).find('a').css('display', 'none');

		$(this).find('a.rollover').css('display', 'inline');

	});
	
	$('#bannerBlock ul li').mouseleave(function() {
		$(this).find('a.rollover').css('display', 'none');

		$(this).find('a').css('display', 'inline');
		
		$('#bannerBlock ul li a.rollover').css('display', 'none');
	});
	

	
	$(".tooltipContainer .close").click(function() {
		$(this).parent().parent().tooltip().hide();			
	});
	
	/**
	 * Product listing height adjustment
	 */
	$('.productListing').each(function(thisWrapper){
		var maxHeight = 0;
	
		$(this).find('.desc').each(function() {
			maxHeight = Math.max(maxHeight, $(this).height()); 
		});
		
		$(this).find('.desc').height(maxHeight + 14);	
	});


	/**
	* Pagination
	*/
	$('.pagenavigator #pageNumber').keydown(function(event) {
		if (event.keyCode == '13') {
			event.preventDefault();
			var pageNumber = ($('.pagenavigator #pageNumber').val() - 1) * 12;
			var currentUrl = $('.pagenavigator #url').val();
			var url = pageNumber;
			if(location.href.match('(offset)')) {
				$(location).attr('href',pageNumber);
			}
			else {
				$(location).attr('href',currentUrl + '/(offset)/' + pageNumber);
			}
			
		}
	});


	/**
	* Form validation
	**/
	$("form#warrantyForm, form#replacementPartsForm, form#becomeDealerForm, form#proDealerForm").submit(function() {
		var error = '<p class="error">';
		var notValid = 0

		if($('#firstName input').eq(0).val() == '' ) {
			error += 'Please fill the first name.<br />';
			notValid = 1;
		}

		if($('#lastName input').eq(0).val() == '' ) {
			error += 'Please fill the last name.<br />';
			notValid = 1;
		}
		
		if($('#streetAddress input').eq(0).val() == '' ) {
			error += 'Please fill the street address.<br />';
			notValid = 1;
		}

		if($('#city input').eq(0).val() == '' ) {
			error += 'Please fill the city.<br />';
			notValid = 1;
		}

		if( $('#state').length > 0 ) {
			if( $('#state select').val() == "---" ) {
				error += 'Please select a valid state.<br />';
				notValid = 1;
			}
		}
		
		if($('#zip input').eq(0).val() == '' ) {
			error += 'Please fill the zip.<br />';
			notValid = 1;
		}
		
		if($('#phone').length > 0 ) {
			if($('#phone input').eq(0).val() == '' ) {
				error += 'Please fill the phone number.<br />';
				notValid = 1;
			}
		}
		
		var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
			
		if(reg.test($("#email input").eq(0).val()) == false) {
			error += 'E-Mail address not valid.<br />';
			notValid = 1;
		}

		if($('#company').length > 0 ) {
			if($('#company input').eq(0).val() == '' ) {
				error += 'Please fill the company.<br />';
				notValid = 1;
			}
		}

		error += '</p>'

		$('#notifications').html(error);
		
		if(notValid == 1) {
			return false;
		} 
	});

	$("form#donationsForm").submit(function() {
		var error = '<p class="error">'; 
		var notValid = 0

		if($('#firstName input').eq(0).val() == '' ) {
			error += 'Please fill the first name.<br />';
			notValid = 1;
		}

		if($('#lastName input').eq(0).val() == '' ) {
			error += 'Please fill the last name.<br />';
			notValid = 1;
		}

		if($('#phone input').eq(0).val() == '' ) {
			error += 'Please fill the phone number.<br />';
			notValid = 1;
		}

		if($('#organization').length > 0 ) {
			if($('#organization input').eq(0).val() == '' ) {
				error += 'Please fill the organization.<br />';
				notValid = 1;
			}
		}
		
		var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
			
		if(reg.test($("#email input").eq(0).val()) == false) {
			error += 'E-Mail address not valid.<br />';
			notValid = 1;
		}

		if($('#request textarea').eq(0).val() == '' ) {
			error += 'Please insert your request info.<br />';
			notValid = 1;
		}

		error += '</p>'

		$('#notifications').html(error);
		
		if(notValid == 1) {
			return false;
		} 
	});


	/**
	* Form validation - Tell Us Your Story
	**/
	$("form#tellStoryForm").submit(function() {
		var error = '<p class="error">';
		var notValid = 0

		var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;

		if(reg.test($("#email input").eq(0).val()) == false) {
			error += 'E-Mail address not valid.<br />';
			notValid = 1;
		}

		if($('#story textarea').eq(0).val() == '' ) {
			error += 'Please insert your story.<br />';
			notValid = 1;
		}
		
		
		error += '</p>'

		$('#notifications').html(error);
		
		if(notValid == 1) {
			return false;
		} 
	});
	
	
	/**
	* Form validation - Video Contest Form
	**/
	$("form#videoContestForm").submit(function() {
		var error = '<p class="error">'; 
		var notValid = 0

		if($('#firstName input').eq(0).val() == '' ) {
			error += 'Please fill the first name.<br />';
			notValid = 1;
		}

		if($('#lastName input').eq(0).val() == '' ) {
			error += 'Please fill the last name.<br />';
			notValid = 1;
		}
		
		var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
			
		if(reg.test($("#email input").eq(0).val()) == false) {
			error += 'E-Mail address not valid.<br />';
			notValid = 1;
		}

		if($('#comments textarea').eq(0).val() == '' ) {
			error += 'Please insert your answer.<br />';
			notValid = 1;
		}

		error += '</p>'

		$('#notifications').html(error);
		
		if(notValid == 1) {
			return false;
		} 
	});


	/**
	* Form validation - Product Giveaway Form
	**/
	$("form#productGiveawayForm").submit(function() {
		var error = '<p class="error">'; 
		var notValid = 0
		
		var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
			
		if(reg.test($("#email input").eq(0).val()) == false) {
			error += 'E-Mail address not valid.<br />';
			notValid = 1;
		}

		if(!$('#age input').attr('checked')) {
			error += 'You need to be at least 18 years old.<br />';
		}
		
		if(!$('#privacy input').attr('checked')) {
			error += 'You need to accept the Privacy Policy and Terms of Use.<br />';
		}

		error += '</p>'

		$('#notifications').html(error);
		
		if(notValid == 1) {
			return false;
		} 
	});


	/**
	* Form validation
	**/
	$("form#oiaMemberForm").submit(function() {
		var error = '<p class="error">';
		var notValid = 0

		if($('#firstName input').eq(0).val() == '' ) {
			error += 'Please fill the first name.<br />';
			notValid = 1;
		}

		if($('#lastName input').eq(0).val() == '' ) {
			error += 'Please fill the last name.<br />';
			notValid = 1;
		}
		
		if($('#streetAddress input').eq(0).val() == '' ) {
			error += 'Please fill the street address.<br />';
			notValid = 1;
		}

		if($('#city input').eq(0).val() == '' ) {
			error += 'Please fill the city.<br />';
			notValid = 1;
		}
		
		if($('#zip input').eq(0).val() == '' ) {
			error += 'Please fill the zip.<br />';
			notValid = 1;
		}
		
		if($('#phone').length > 0 ) {
			if($('#phone input').eq(0).val() == '' ) {
				error += 'Please fill the phone number.<br />';
				notValid = 1;
			}
		}
		
		var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
			
		if(reg.test($("#email input").eq(0).val()) == false) {
			error += 'E-Mail address not valid.<br />';
			notValid = 1;
		}

		if($('#oiaMemberNumber input').eq(0).val() == '' ) {
			error += 'Please fill the OIA Member Number.<br />';
			notValid = 1;
		}

		error += '</p>'

		$('#notifications').html(error);
		
		if(notValid == 1) {
			return false;
		} 
	});


	/**
	* Warranty Info Form state ordering
	**/
	if($('#warrantyForm #state').length > 0) {
		var warrantyStates = new Object();
		warrantyStates.canada = new Array();
		warrantyStates.military = new Array();
		warrantyStates.us = new Array();
		var stateCountry = '';

		$('#warrantyForm #state select option').each(function() {
			stateCountry = '';
			stateCountry = this.text.split('(');
			if(stateCountry.length == 1) {
				this.text = stateCountry[0].trim();
				warrantyStates.us.push(this);
			} else {
				if(stateCountry[1] == "Canada)") {
					this.text = stateCountry[0].trim();
					warrantyStates.canada.push(this);
				}
				if(stateCountry[1] == "Military)") {
					this.text = stateCountry[0].trim();
					warrantyStates.military.push(this);
				}
			}
		});

		// sort the states
		warrantyStates.canada.sort(sortText);
		warrantyStates.military.sort(sortText);
		warrantyStates.us.sort(sortText);

		// insert the states in the selection
		$('#warrantyForm #state select').html('');
		$('#warrantyForm #state select').append(warrantyStates.military);
		$('#warrantyForm #state select').append('<option>---</option>');
		$('#warrantyForm #state select').append(warrantyStates.us);
		$('#warrantyForm #state select').append('<option>---</option>');
		$('#warrantyForm #state select').append(warrantyStates.canada);
	}


	/**
	* Misc
	*/

	// If we're doing checkout don't open new modal cart
	if($('body').hasClass('component-gerber_shop')) {
		$('#topNavigation #cartLink').removeAttr('id');
	}
	
	/**
	* Product reviews & news
	*/
	$('.toggleLinks span').click(function(){
		var id = $(this).attr('id');
		$('.productPage .innerWrapper').children().addClass('hidden');
		$('#' + id + 'Block').removeClass('hidden').addClass('activeBox');
		$('.toggleLinks span').addClass('nonActive');
		$(this).removeClass('nonActive');
		
		Cufon.refresh();
		oScrollPress.tinyscrollbar_update(); 
		oScrollReviews.tinyscrollbar_update(); 
	});
	
	$('[placeholder]').focus(function() {
		var input = $(this);
		if (input.val() == input.attr('placeholder')) {
			input.val('');
			input.removeClass('placeholder');
		}
	}).blur(function() {
		var input = $(this);
		if (input.val() == '' || input.val() == input.attr('placeholder')) {
			input.addClass('placeholder');
			input.val(input.attr('placeholder'));
	  }
	}).blur();
	
	/**
	* Product custom scroll bars
	*/
	var oScrollReviews = $('#reviewsBlock');
	var oScrollPress = $('#pressBlock');
	if(oScrollReviews.length > 0){
		oScrollReviews.tinyscrollbar({ sizethumb: 29 });
	}
	if(oScrollPress.length > 0){
		oScrollPress.tinyscrollbar({ sizethumb: 29 });
	}
	
	//checkboxes into starboxes
	$('#opinionForm input[type="checkbox"]').ezMark(); 
	//checking a start box checks all to the left, clicking the left most when it's the only one unchecks all.
	$('#opinionForm input[type="checkbox"]').click(function(){
		if($(this).parent().is(':first-child') && $(this).hasClass('ez-checked') && !$(this).parent().next().hasClass('ez-checked')){
			$(this).removeClass('ez-checked');
			$(this).parent().removeClass('ez-checked');
			$(this).attr("checked", false);
		} else {
			$(this).addClass('ez-checked');
			$(this).attr("checked", true);
			$(this).parent().addClass('ez-checked');
					
			$(this).parent().prevAll().addClass('ez-checked');
			$(this).parent().prevAll().find('input').attr("checked", true);
			$(this).parent().prevAll().find('input').addClass('ez-checked');	
		}
		$(this).parent().nextAll().removeClass('ez-checked');
		$(this).parent().nextAll().find('input').attr("checked", false);
		$(this).parent().nextAll().find('input').removeClass('ez-checked');
	});
	//hovering unchecked starts shows them checked
	$('#opinionForm input[type="checkbox"]').hover(function(){ 
		$(this).parent().addClass('ezh-checked');
		$(this).parent().prevAll().addClass('ezh-checked');	
	},function(){
		$(this).parent().removeClass('ezh-checked');
		$(this).parent().prevAll().removeClass('ezh-checked');	
	});
	
	// product review form ajax handling with jQuery.form plugin
	var opinionFormOptions = {
		url: _siteRootHttpS + '/product_review/add',
		//url: 'http://localhost/fiskars/ez/index.php/gerber-eng-us/product_review/add',
		type: 'post',
		dataType: 'json',
		beforeSubmit: validateOpinionForm,
		success: showOpinionFormResponse
	};
	
	$('#reviewForm').submit(function() {
		$('#opinionerror').css('display','none');
		$('#opinionerrorLong').css('display','none');
		
		$(this).ajaxSubmit(opinionFormOptions);
		return false;
	});
	
	// review removal links
	$('#reviewsBlock .overview .review .remove-review').live('click', function(){
		var reviewId = $(this).data('review-id');

		if(reviewId) {
			removeReview(reviewId);
		}
		
		return false;
	});
	
	// Limit product reviews to 1023 characters.
	$('#reviewForm textarea#review').bind('keydown keyup', function() {
		var maxLength = 2047;
		if($(this).val().length >= maxLength) {
			$(this).val($(this).val().substring(0, maxLength));
		}
	});
	
	/**
	* Product filtering
	**/

	// Tool filters are different for tool types, remove selects on change
	$("#toolTypeSelect").change(function(){		
		
		$(".toolFilterSelect").remove();
		
		return false;	
		
	});


	/**
	* Product glossary
	**/
	if( $("#glossary_definitions").length > 0) {
		 var text = $("#productDescription").eq(0).html();

		$(".definition").each(function() {
			var searchText = $(this).children("span.name").eq(0).html();

			var id = $(this).children('.content').eq(0).attr('id');

			text = text.replace(searchText, '<a rel="' + id + '" href="#" class="tooltip">' + searchText + '</a>');
		});


		$("#productDescription").eq(0).html(text);
		
		$('.tooltip').tooltip({
			tip: '.tooltipContainer',
			position: "bottom right",
			offset: [-10, -60],
			onBeforeShow: function() {
		       	var trigger = this.getTrigger().attr("rel");
		       	var ttContent = $('#' + trigger).clone();
		       	$('.tooltipContainer .middleContent').empty().html(ttContent);
		   	}
		});
	}


});
/* ------- end - document.ready ------- */

var isOverlayOpen = false;

function openOverlay() {
	if(isOverlayOpen === false) {
		$('body').append('<div id="overlay"></div>');
		setOverlaySize();
		if (!$.browser.msie ) {
			$("#overlay").css({
				opacity : 0
			}).animate({
				opacity : 1
			});
		}
	}
	isOverlayOpen = true;
	return false;
}

function closeOverlay() {
	$("#overlay").animate({
		opacity : 0
	}, null, null, function() {
		$(this).remove();
	});
	$('#overlay').remove();
	isOverlayOpen = false;
}

function setOverlaySize() {
	$("#overlay").css({
		display : "block",
		width : $(document).width(),
		height : $(document).height()
	});
}

$(window).resize(setOverlaySize);





/**
* Remove a review.
**/
function removeReview(reviewId) {
	
	$.post(_siteRootHttpS + '/product_review/remove',
		{
			reviewId: reviewId
		},
		function(data) {
			if(data.result == "success") {
				$('#product-review-'+reviewId).remove();
			}
		},
		"json"
	);
}


/**
* Success callback function for opinion form submit
**/
function showOpinionFormResponse(responseJson, statusText, xhr, $form) {

	if(responseJson.result == 'success') {

		// clear form
		$form.clearForm();
		
		// set thankyou message
		$('#reviewForm').css('display','none');
		$('#opinionerror').css('display','none');
		$('#opinionerrorLong').css('display','none');

		if(responseJson.moderated == 1) {
			$('#opinionthankyou2').css('display','block');	
		}
		else {
		
			$('#opinionthankyou').css('display','block');
			
			
			// activate reviews tab
			$('.productPage .innerWrapper').children().addClass('hidden');
			$('#reviewsBlock').removeClass('hidden').addClass('activeBox');
			$('.toggleLinks span').addClass('nonActive');
			$('.toggleLinks #reviews').removeClass('nonActive');
					
			//alert(responseJson.content);
	
			// insert returned HTML
			$('#reviewsBlock .viewport .overview').prepend(responseJson.content);
			
			Cufon.refresh();
			//oScrollPress.tinyscrollbar_update(); 
			var oScrollReviews = $('#reviewsBlock');
			oScrollReviews.tinyscrollbar_update(); 
		}
		
	}
	else {
		
		// show error	
	}
}


/**
* Validation callback function for opinion form submit
**/
function validateOpinionForm(formData, jqForm, options) {
	
	var validInput = true;
	var tooLong = false;
	var form = jqForm[0];
	
	if(!form.reviewname.value || !form.reviewtitle.value || !form.review.value) {
		validInput = false;
	}
	
	var isRated = false;

	for(var i = 0; i < formData.length; i++) {
		for(var s = 10; s > 0; s--) {
			if(formData[i].name == 'review_star'+s) {
				isRated = true;
				break;		
			}
		}
	}
	
	if(!isRated || $('#opinionForm textarea#review').hasClass('placeholder')) {
		validInput = false;
	}
	
	if($('#opinionForm textarea#review').hasClass('placeholder') === false && form.review.value.length > 2047) {
		// too long input
		validInput = false;
		tooLong = true;
	}
	
	if(!validInput) {
		
		$('#opinionerror').css('display','none');
		$('#opinionerrorLong').css('display','none');
		
		if(tooLong) {
			$('#opinionerrorLong').css('display', 'block');
		}
		else {
			$('#opinionerror').css('display', 'block');
		}
	}
		
	return validInput;
		
}


/**
 * Runs the GA tracking code. This function is called after the order is
 * successfully placed.
 *
 */
function gerberTracking() {
	// From checkout.php
	var order_id = $('#gerberCartNewOrderId').html();
	
	var gerberTrans = new Object;
	gerberTrans.order_id = order_id;
	gerberTrans.name = 'Gerbergear.com';
	gerberTrans.total = $('#gerberCartTotals #grand_total').html().replace('$', '');
	gerberTrans.tax = $('#gerberCartTotals #sales_tax').html().replace('$', '');
	gerberTrans.shipping = $('#gerberCartTotals #shipping').html().replace('$', '');
	gerberTrans.city = $('#shippingCity').val();
	gerberTrans.state = $('#shippingState').val();
	gerberTrans.country = $('#shippingCountry').val();
	
	_gaq.push(['_addTrans',
		gerberTrans.order_id, // required
		gerberTrans.name,
		gerberTrans.total,
		gerberTrans.tax,
		gerberTrans.shipping,
		gerberTrans.city,
		gerberTrans.state,
		gerberTrans.country
	]); 
	
	$('#gerberCartOverview tbody tr').each(function(){
		var item = new Object;
		
		item.order_id = order_id;
		
		item.SKU = $(this).find('.SKU').html();

		var temp = $(this).find('.itemDescription');
		$(temp).find('span').remove();
		item.product_name = $(temp).html();
		
		item.product_category = '--';
		
		var rowTotal = $(this).find('.rowTotal').html().replace('$', '');
		var quantity = $(this).find('.quantityAmount').html();
		item.unit_price = (0 + rowTotal) / (0 + quantity);
		item.quantity = quantity;
	
		_gaq.push(['_addItem',
			item.order_id, // required
			item.SKU,
			item.product_name,
			item.product_category,
			item.unit_price,  // required
			item.quantity  //required
		]); 
	});
	
	_gaq.push(['_trackTrans']);
	
	(function() {
		var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
		ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
		var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
	})();
	
	return false;
}


/**
* Sort function using text object property
**/
function sortText(a,b) {
	var stateA = a.text.toLowerCase();
	var stateB = b.text.toLowerCase();

	if(stateA < stateB) {
		return -1; 
	}

	if(stateA > stateB) {
		return 1;
	}

	return 0;
};

