function posted () {
	//setTimeout(function(){
		document.location.pathname = '/store';	
	//}, 10000);
}
function flashPost (authenticity_token, ssize, flavour1, flavour2, flavour3) {
	//alert('authenticity_token: ' + authenticity_token + 'size: ' + ssize + ' flavour1: ' + flavour1 + ' flavour2: ' + flavour2 + ' flavour3: ' + flavour3);
	
	if (flavour2 == 'undefined') {
		delete flavour2;
	};
	
	if (flavour3 == 'undefined') {
		delete flavour3;
	};
	
	jQuery.post( "/store/add_to_cart/ice-cream-box", 
	{
		"authenticity_token": authenticity_token,
		"size": ssize,
		"flavour1": flavour1,
		"flavour2": flavour2,
		"flavour3": flavour3
	}
	, function(data) {
		if (data =="OK") {
			document.location.pathname = '/store';
		}
		else{
			document.location.pathname = '500';
		}
	},
	'text'
	);
}
//Jquery ajax stuff
jQuery.ajaxSetup({ 
  'beforeSend': function(xhr) {xhr.setRequestHeader("Accept", "text/javascript")}
})

jQuery.fn.submitWithAjax = function() {
  this.submit(function() {
    $.post(this.action, $(this).serialize(), null, "script");
    return false;
  })
  return this;
};

function remove_fields(link) {
  $(link).prev("input[type=hidden]").val("1");
  $(link).closest(".fields").hide();
}

function add_fields(link, association, content) {
  var new_id = new Date().getTime();
  var regexp = new RegExp("new_" + association, "g")
  $(link).parent().before(content.replace(regexp, new_id));
}

function show_order_form(orderDiv, animDuration) {
	/*$(orderDiv).addClass('expanded').removeClass('contracted').animate({'width' : "708px", 'height' : '144px'}, {queue : false}).children().children('.col1').css({
		'width': 0,
		'height' : 144,
		'overflow' : 'hidden'
	}).show().animate({'width' : "520px"}, {queue : false}).next().animate({'padding-top' : "57px"}, {queue : false}).children('input').attr('src', '/images/store/order-final.png');*/
	$(orderDiv).addClass('expanded').removeClass('contracted').animate({'width' : "708px"}, {
		duration: animDuration,
		complete: function() {
			$(this).children().children('.col1, .errorExplanation').slideDown(animDuration, function(){
				$('.col2').children('input').attr('src', '/images/store/order-final.png');
			});
			$(this).children().children('.col2').animate({'padding-top' : '99px'}, {duration : animDuration})
		}
	});
}
function hide_order_form(){
	
}

Cufon.replace('h1', { fontFamily: 'Rockwell' });
Cufon.replace('p.subtitle', { fontFamily: 'Rockwell' });
Cufon.replace('.blackboard h2', { fontFamily: 'Rockwell' });
Cufon.replace('html#news h2', { fontFamily: 'Rockwell' });
Cufon.replace('html#admin-index h2', { fontFamily: 'Rockwell' });
Cufon.replace('html.store h3', { fontFamily: 'Rockwell' });
Cufon.replace('html.store div.price', { fontFamily: 'Rockwell' });
Cufon.replace('html.store div.sum', { fontFamily: 'Rockwell' });
Cufon.replace('html.store span.total', { fontFamily: 'Rockwell' });
Cufon.replace('html#index #social-media a', { fontFamily: 'Rockwell' });
Cufon.replace('html.store h2', { fontFamily: 'Kasimir' });


jQuery(window).load(function(){
	$('div.imagerotator').alphaRotate();
	
	$('html.store div.cake div.image').prepend('<img class="image-clip" src="/images/store/image-clip.png" />');
	$('html.store div.cake img.image-zoom').css('cursor', 'pointer').click(function(){
		$(this).next().click();
	});
	
	
	
	$('select option').each(function(index, option){
		if ($(option).attr('value') == '') {
			$(option).css('color', '#4AB3EF');
		}
		else{
			$(option).css('color', '#000');
		}
	});
	$("select").change(function() {
		$('select').css('color', $("select option:selected").css('color'))
	}).change();
	
	
	$("#cart-item-count").submitWithAjax();
	
	//item quantity ajax update
	$("form#cart-item-count #item_quantity").focus(function(){
		$(this).attr('oldvalue', $(this).val());
	}).change(function(event){
		value = parseInt($(this).val());
		old_value = parseInt($(this).attr('oldvalue'));

		if (isNaN(value)){
			$(this).val(old_value);
		}
		else if (value == '0') {
			$(this).parent().parent().siblings('.buttons').children('a').click();
		}
		else if(value != old_value){
			$(this).submit();
		}
	}).submit(function(){
		//Maybe some indication of ajax activity
	});
	
	//don't do this in IE6
	if (window.ie6 != true) {
		
		$('html#store-index div#cart-btn').filter('.updated').parent().tipTip({maxWidth: "200px", defaultPosition: 'left', activation: 'focus', inital_display: 5000});
		
		//order form
		$('html#store-cart div#order').addClass("contracted");
		$('html#store-cart div#order input[type=image]').click(function(event){
			orderDiv = $('#order');
			if (orderDiv.hasClass('contracted')) {
				show_order_form(orderDiv, 400);
				return false
			};
		});
		
		if ($('html#store-cart div#order .errorExplanation').size() != 0) {
			show_order_form($('#order'), 0)
		};
		
		
		
		//fancybox for choosing gift-card value
		if ($('html').attr('id') == 'store-index') {
			$('<div id="ajax-content" style="display: none"><div id="store-choose-gift-card"></div></div>').appendTo('body');
			$('#store-choose-gift-card').load('/store/choose_gift_card #form');
			$('#add_gift_card').attr("href", "#store-choose-gift-card");
			$('#add_gift_card').fancybox({
				href: '#store-choose-gift-card',
				titleShow: false,
				overlayOpacity: 0.5,
				overlayColor: '#000',
				onComplete: function(){
					$('#store-choose-gift-card input[type=text]').focus(function(){
						this.select();
					});
					$('#store-choose-gift-card input[type=text]').focus();
				}
			});
			$('.lightbox').fancybox({
				overlayOpacity: 0.5,
				overlayColor: '#000',
				hideOnContentClick: true
			});
		};
	};
	
});