// Title: FJ Flowers - javascript
// Author: Kalon Edser, http://www.kalonedser.com/
// Updated: 1 March 2010

jQuery(document).ready(function() {

	$('body').addClass('js');
	
	// remove active class from active subcategory parent
	$('ul#products-nav li.active').parents('li.active').removeClass('active');
	
	// mini cart animation
	$('#mini-cart, #featured').animate({
	   opacity: 0.9
	 }, 'fast');
	
	$('#mini-cart').hover(
	      function () {
	        $(this).animate({ 
			        opacity: 1
			      }, 100 );
	      }, 
	      function () {
	        $(this).animate({ 
			        opacity: 0.9
			      }, 100 );
	      }
	);
	
	$.localScroll();
	$.localScroll.hash();

	// round corners
	if ($.browser.name != 'msie') {
		$('.rounded-box, .promote, .promote a, .keywords a, .show-info a, #pagination a').corners('7px');
	}
	// rounded courners for internet explorer
	if ($.browser.name == 'msie') { 
		// && $.browser.versionX > 6
		$('.rounded-box').corners('5px transparent');
		$('.promote, .promote a').corners('2px transparent'); // .keywords a
	}

	// preload images
	$.preloadImages('/img/prettyPhoto/light_rounded/sprite.png','/img/prettyPhoto/light_rounded/loader.gif');

	// inline picture viewer
	var bindBehaviours = function(scope) {

		$("a[rel^='prettyPhoto']").prettyPhoto({
			animationSpeed: 'normal', /* fast/slow/normal */
			padding: 40, /* padding for each side of the picture */
			opacity: 0.5, /* Value betwee 0 and 1 */
			showTitle: false, /* true/false */
			allowresize: true, /* 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 */
		});
	};

	bindBehaviours(this);

	// more information ajax toggle
	$('.more-link').toggle(
		function () { // toggle show
			var $thislink = $(this);
			var moreinfo = $thislink.parents('.product-info').find('.more-info');
			var moredata = $thislink.attr("href")+'?type=more';

			// loading feedback
			moreinfo.after('<p class="more-loading"><img src="/img/prettyPhoto/light_rounded/loader.gif" alt="Loading..." /></p>');
			$('.more-loading').ajaxStart(function() {
				$(this).slideDown('fast');
			}).ajaxStop(function() {
				$(this).slideUp('fast',function(){
					$(this).remove();
					$.scrollTo($thislink.parents('.product-list'), 800, {margin:true} )
				});
			});

			// load data
			moreinfo.hide().load(moredata, function () {
				bindBehaviours(this);
				moreinfo.slideDown('normal');
				$thislink.addClass('hide-link').text('hide information');
			});
		},
		function () { // toggle hide
 			$(this).removeClass('hide-link').text('show information').parents('.product-info').find('.more-info').slideUp('normal');
		}
	);
	
	// toggle messages
	var messages = $('#messages');
	$('a[rel^="message"]').toggle(
		function () { // toggle show
			var $thislink = $(this);
			var content = $thislink.attr("href")+'?type=text';
			
			$thislink.parents('p').append('<span class="more-loading"><img src="/img/prettyPhoto/light_rounded/loader.gif" alt="Loading..." /></span>');

			// load data
			messages.hide().load(content, function () {
				messages.slideDown('normal');
				$.scrollTo( '#messages', 800, {margin:true} );
				$('.more-loading').fadeOut('slow');
			});
		},
		function () { // toggle hide
			messages.slideUp('normal');
		}
	);

	// hide cart message field
	$('p.cart-message').hide();
    
	// show cart message field when not using PayPal
	$('select#payment').after('<span class="paypal-icons" title="Pay by credit card with PayPal"></span>').change(function(){
		switch ($(this).val()){
			case 'Purchase via PayPal':
				$("p.cart-message").hide();
				$(".paypal-icons").show();
			break;
			default:
				$("p.cart-message").show();
				$(".paypal-icons").hide();
		}
	});

	// main nav hover
	$('#nav li:not(.active) a').hover(
	      function () {
	        $(this).css('background-position','0 0').animate({ 
			        opacity: 0
			      }, 100 );
	      }, 
	      function () {
	        $(this).animate({ 
			        opacity: 1
			      }, 100 );
	      }
	);
	
	// active main nav
	$('#nav .active a').animate({ 
	        opacity: .25
	      }, 100 );

	// load after all resources are loaded
	$(window).load(function() {
	
		// highlight mini-cart update
		$('p#yab-shop-add-message').hide().slideDown('slow').animate({opacity: 1.0}, 3000,function(){ 
				$(this).slideUp('slow');
			});
			
		// rotate side images
		$('.side-images').cycle({ 
			timeout:  10000, 
			speed:  3000,
			pause: 1
		});
	});

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

	// Search Box for Safari
	if ($.layout.name == 'webkit') {
		var $searchvalue = $('#search-input input').attr('value');
		$('<input type="search" name="q" placeholder="Search" autosave="fjflowers" results="10" size="15" />').attr({ value: $searchvalue }).insertAfter('#search-input p');
		$('#search-input p:eq(0)').remove();
	}

});