// init the page for js and set tabs
$(function(){
  $("body").removeClass('no-script');
  $('#tabs').tabs();
});

$(function(){
  var selection_description = $('#selections_show #content .description')
  selection_description.hide();
  
  // $('#selections_show #content .banner').click(function(){
  //   selection_description.slideToggle('slow');
  // });
  
  $('#selections_show #content .toggle').toggle(function() {
        $(this).children('.toggle-indicator').html('Hide Details');
        selection_description.slideToggle('slow');

    },function(){
        $(this).children('.toggle-indicator').html('Show Details');
        selection_description.slideToggle('slow');
  });
  $("#quick-cart").submit(function() {
	$.get($(this).attr("action"), {"id":  $(this).find("select[name=id]").val() }, function(data) {
		$.facebox(data);
	});
	return false;
  });
});





// $(document).ready(setupZoom);
function highlight_stars(n) {
  for(var i=1;i<=n;i++)
    $('#rate_this_' + i).find("img").attr('src', "/images/star_filled.gif");
  for(var i=n+1;i<=5;i++)
    $('#rate_this_' + i).find("img").attr('src', "/images/star_unfilled.gif");
}

function swap_product_image(el, fullsize_url, thumb_url, can_enlarge) {
	$('#hero_pic').attr('src', fullsize_url);
	$('.more_pics a').removeClass('selected');
	$(el).addClass('selected');
	if(can_enlarge) {
		$('.embiggen').slideDown(100);
		$('.embiggen').attr('href', thumb_url);
		$('.hero a').attr('rel', '');
		$('.hero a').attr('href', thumb_url);
		$('.hero a').removeClass('cant_zoom');
	} else {
		$('.embiggen').slideUp(100);
		$('.hero a').attr('rel', 'disable');
		$('.hero a').addClass('cant_zoom');
	}
	el.blur();
}

function show_panel(id, scope) {
	$(scope + ' .tab_panel').hide();
	$(scope + ' .tab').removeClass('selected');
	$(scope + ' #' + id + '_tab').addClass('selected');
	$(scope + ' #' + id).show();
	document.location.hash = '#' + id + '_panel';	
}


function cc_type(num) {
	num = $.trim(num);
	
	if(num[0] == '4')
		return 'visa';
		
	if(num.slice(0,4) == '6011' || num.slice(0,2) == '65')
		return 'discover';
		
	first_two = parseInt(num.slice(0,2));
	if(first_two >= 51 && first_two <= 55)
		return 'master';
	
	if(first_two == 34 || first_two == 37)
		return 'american_express'
		
	return null;
}

var CountDownTimer = {
    init: function() {
        this.end_sec = $("span:first-child",this).hide().html()
    },
    update: function(e) {
        // use utc times?
        // getUTCHours, etc.
        var secs = this.end_sec - Math.floor((new Date()).getTime()/1000);
        if (secs < 0) {
            $(this).stopTime().hide();
            return;
        }
        $(".days",this).text(this.digits(Math.floor(secs/86400)));
        $(".hours",this).text(this.digits(Math.floor(secs/3600)%24));
        $(".minutes",this).text(this.digits(Math.floor(secs/60)%60));
        $(".seconds",this).text(this.digits(secs%60));
    },

    digits: function(n) {
        return n.toString();
    },

    start: function() {
        this.update();
        $(this).everyTime(1000,function(){ this.update()});
    }
};

$(document).ready(function() {
        // set all the coupon fields to the same when one is changed.
        $("#cart_wrapper form").bind("submit",function(e) {
                var code = $("#add_coupon .coupon_code_input").val();
                $("#cart_wrapper .coupon_code_input").val(code);
            });

		$('.global-flash').each(function() {
			$(this).append('<a href="#" class="close-flash" style="position:absolute;display:block;right:10px;top:0.6em"><image src="/images/close.gif" /></a>');
		});
		$(".close-flash").live("click", function() {
			$(this).closest('.global-flash').slideToggle();
		});
		
		$(".search-box").focus(function() {	
			if ($(this).attr("placeholder") == $(this).val()) {
				$(this).val("");
				$(this).css({"color":"black"});
			}
		}).blur(function() {
			if ($(this).val() == "") {
				$(this).val($(this).attr("placeholder"));
				$(this).css({"color":"#aaa"});
			} else if ($(this).val() == $(this).attr("placeholder")) {
				$(this).css({"color":"#aaa"});
			}
		}).each(function() {
			if ($(this).attr("placeholder")  == $(this).val()) {
				$(this).css({"color":"#aaa"});
			} 
		});
		
		if (RegExp(" AppleWebKit/").test(navigator.userAgent)) {
			box = $("#header .search-box").hide();
			box.replaceWith($('<input type="search" name="q" autosave="perfume" results="0" placeholder="Search" class="pretty-search" size="30" />'));
		}
});

function increment_cart_count() {
	var arr = $(".cart_status a").html().match(/([0-9]+) items?/);
	count = parseInt(arr[1]) + 1;
	str = count + " " + (count == 1 ? "item" : "items");
	$(".cart_status a").html($(".cart_status a").html().replace(/[0-9]+ items?/, str));
}
