// Title: Toffee Apple Tummies - javascript
// Author: Kalon Edser, http://www.kalonedser.com/
// Updated: 11 March 2011

jQuery(document).ready(function() {
	
	$('body').addClass('js');

	// fix png images with alpha channels
	$.ifixpng('../img/pixel.gif');
	$('.png').ifixpng();
	
	// scroll effect
	$.localScroll({ margin: true });
	$.localScroll.hash({ margin: true });

	// remove active class from active subcategory parent
	$('ul#nav-products li.active').parents('li.active').removeClass('active').addClass('expand');

	// add active class to products category
	if($('.index a').length > 0) {
		var $thiscat = $('.index a').attr('href');
		var $thiscat = $.url.setUrl($thiscat).param("c");
		$("#cat-" + $thiscat).addClass('active').parents('#nav-products-cat li').addClass('expand');
	}
	
	// last
	$('#home .article:last').addClass('last');
	$('#home .article:first').addClass('first');
	
	// mini-cart notices
	if($('.cart-msg').length > 0) {
		$('.cart-msg').fadeIn().delay(3000).fadeOut();
	}
	
	var $paypal = 'Purchase via PayPal';
	
	// sort payment options
	$('#payment').sortOptions(false).selectOptions($paypal);

	// show extra checkout fields when not using PayPal
	$('.yab-shop-text').hide();
	
	$('select#payment').change(function(){
		switch ($(this).val()){
			case $paypal:
				$(".yab-shop-text").hide();
			break;
			default:
				$(".yab-shop-text").show();
		}
	});
	
	// toggle message
	$('a[rel^="message"]').toggle(
		function () { // toggle show
			$('#message-box').show("slide", {direction: 'up'}, 'normal', function() {
				$.scrollTo( '#message-box', 800, {margin:true} )
			});
		},
		function () { // toggle hide
			$('#message-box').hide("slide", {direction: 'up'}, 'normal');
		}
	);

	// photo viewer
	$("a[rel^='photo-viewer']").prettyPhoto({
		animation_speed: 'normal', /* fast/slow/normal */
		slideshow: false, /* false OR interval time in ms */
		autoplay_slideshow: false, /* true/false */
		opacity: 0.35, /* Value between 0 and 1 */
		show_title: false, /* true/false */
		allow_resize: true, /* Resize the photos bigger than viewport. true/false */
		counter_separator_label: '/', /* The separator for the gallery counter 1 "of" 2 */
		theme: 'light_rounded', /* light_rounded / dark_rounded / light_square / dark_square / facebook */
		modal: false, /* If set to true, only the close button will close the window */
		overlay_gallery: false, /* If set to true, a gallery will overlay the fullscreen image on mouse over */
		keyboard_shortcuts: true /* Set to false if you open forms inside prettyPhoto */
	});

	// clear default input values
	$.fn.clearDefault = function(){
		return this.each(function(){
			var default_value = $(this).val();
			$(this).focus(function(){
				if ($(this).val() == default_value) $(this).val("");
			});
			$(this).blur(function(){
				if ($(this).val() == "") $(this).val(default_value);
			});
		});
	};

	$('input.clear-default').clearDefault();
	
	// custom submit button
	$('.button-replacement').click(function() {
		$(this).parents('form').submit();
	});

	// open new window for external links
	$('A[rel="external"]').click( function() {
        window.open( $(this).attr('href') );
        return false;
    });

});
