$(document).ready(function() {

	var updatePrice = function() {
	   var shipping = $('#shipping').val();
	   
	   if(shipping != undefined) {
	       shipping = shipping.split('|');
    	   var cost = Number($('#cost').text());
    	   var quantity = Number($('#quantity').val());
    	   var price = (cost + Number(shipping[1])) * quantity;
    
    	   $('#price').html(price.toFixed(2));
	   }
	}
	
	updatePrice();
	
	$('#nav, div.fire-navigation-main ul.fire-navigation').localScroll();
	var $tabs = $('#preview').tabs({ fx: { opacity: 'toggle' } });
	
	function attachBuyExpand() {
		$('a.buy, li.fire-navLink-buy a, a[href=#buy]').unbind('click');
		if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || $(window).width() < 959 ) { 
			$('a.buy, li.fire-navLink-buy a, a[href=#buy]').click(function() {
				$('#checkout').slideToggle();
				$('li.fire-navLink-buy a').toggleClass('active');
				$('li.fire-navLink-buy a').css({'width' : 'auto', 'display' : 'inline'});
				return false;
			});
		} else {
			$('a.buy, li.fire-navLink-buy a, a[href=#buy]').click(function() {
				$('li.fire-navLink-buy a').toggleClass('active');
				
				if ( $('li.fire-navLink-buy a').hasClass('active') ) {
					$('#checkout').slideDown(300);
				} else {
					$('#checkout').hide();
				}
				
				return false;
			});
		}
	}
	attachBuyExpand();
	$(window).resize(function() {
		attachBuyExpand();
	});
	
	$('#preview div img').click(function() {
        var next = $tabs.tabs('option', 'selected') +1;
        if(next == $tabs.tabs('length')) {
            next = 0;
        }
        
        $tabs.tabs('select', next);
	});
	$('#shipping,#quantity').change(function() {
        updatePrice();
	});
});
