/*
 * jQuery custom code for Three Tablespoons
 * Copyright (c) 2009 Roberto Garcia - http://www.rgdesign.org
 * Dual licensed under the MIT and GPL licenses.
 * http://docs.jquery.com/License
 *
 * Date: 2009-05
 * Version: 1.0
 * Notes: This code works only for: http://threetablespoons.com/
 * Legal: You can reuse this code or part/s of it while keeping credits intact. ;)
 */

/* -------- */
/* "window" means, once all is loaded! */
$(function(){
						 
	/* */
	var anatomy = $('#treatAnatomy .nav a');
	var loader = $('#treatAnatomy #imageLoader img');
	anatomy.each(function() {
		$(this).click(function(){
			img = $(this).attr('href');
			loader.attr('src', img);
			return false;
		})		
	});
						 
	/* random image for home */
	var randomList = $("#randomImage ul li");
	if(randomList.length > 0){
		randomList.hide();
		var randomImages = new Array(); 
		randomList.each(function() {
			randomImages.push($(this));
		});
		var imgLength = randomList.length; 
		var myRandom = Math.floor(Math.random()*imgLength);
		randomImages[myRandom].show();
	}
	
	/* png fix for all images on body */
	/* be carefull with this once you move your images folder */
	$('body').pngFix({blankgif:'images/blank.gif'});
	
	/* menu hovers */
	var menuItems = $("#menu li a");
	menuItems.each(function() {
		$(this).children('.hidden').hide();
		$(this).children('.visible').show();
		$(this).hover(function() {
			$(this).children('.visible').hide();
			$(this).children('.hidden').show();
		  }, function() {
			$(this).children('.hidden').hide();
			$(this).children('.visible').show();
		  });
	});
	
	/* general forms focus/blur */
	$('#newsletter_email, .qty').focus(function() {
		if (this.value == this.defaultValue){ 
			this.value = '';
		}
		if(this.value != this.defaultValue){
			this.select();
		}
	});
	$('#newsletter_email, .qty').blur(function() {
		if ($.trim(this.value) == ''){
			this.value = (this.defaultValue ? this.defaultValue : '');
		}
		//$(this).animate({"opacity":0.4}, 150);
	});
	
	/* fade effects */
	jQuery.fn.fadeMe = function(start,end){
		return this.each(function() {			
			$(this).hover(
				function(){$(this).css({"opacity":end});},
				function(){$(this).css({"opacity":start});}
			);
			$(this).css({"opacity":start});
		});
	}
	/* this is reusable, just add class to any element, also you can create more instances for the fadeMe function. */
	/* valuies are in decimals, ex. 0.8 menans 80% alpha */
	$('.fadeMeOut').fadeMe(1, 0.8);
	$('.fadeMeIn').fadeMe(0.8, 1);
	
	/* rollovers x gif images */
	jQuery.fn.rollOver = function($ext, $format){
		return this.each(function() {	
			parentClass = $(this).parent().attr('class');
			
			if ($format) {
				$(this).click(function() {
					$oldlink = $('#treatAnatomy .nav a.selected');
					oldsrc = $oldlink.removeClass().children('img').attr('src');
					if (oldsrc.match(/_over/)) {
						if ($ext == 'gif')
							oldsrcOFF = oldsrc.replace(/_over.gif$/ig, '.gif');
						else
							oldsrcOFF = oldsrc.replace(/_over.png$/ig, '.png');
						$oldlink.children('img').attr('src', oldsrcOFF);
					}
					imgsrc = $(this).attr("src");
					matches = imgsrc.match(/_over/);
					if (!matches) {
						if ($ext == 'gif')
							imgsrcON = imgsrc.replace(/.gif$/ig,"_over.gif");
						else
							imgsrcON = imgsrc.replace(/.png$/ig,"_over.png");
						$(this).attr("src", imgsrcON);
					}
					$(this).parent().addClass('selected');
				});
			}
			
			if(parentClass == 'selected'){
					imgsrc = $(this).attr("src");
					matches = imgsrc.match(/_over/);
					if (!matches) {
						if ($ext == 'gif')
							imgsrcON = imgsrc.replace(/.gif$/ig,"_over.gif");
						else
							imgsrcON = imgsrc.replace(/.png$/ig,"_over.png");
						$(this).attr("src", imgsrcON);
					}
				}else{
				$(this).hover(
					function(){
						imgsrc = $(this).attr("src");
						matches = imgsrc.match(/_over/);
						if (!matches) {
							if ($ext == 'gif')
								imgsrcON = imgsrc.replace(/.gif$/ig,"_over.gif");
							else
								imgsrcON = imgsrc.replace(/.png$/ig,"_over.png");
							$(this).attr("src", imgsrcON);
						}
					},
					function(){
						$(this).attr("src", imgsrc);
					}
				);
				rollsrc = $(this).attr("src");
				rollON = ($ext == 'gif') ? rollsrc.replace(/.gif$/ig,"_over.gif") : rollsrc.replace(/.png$/ig,"_over.png");
				$("<img>").attr("src", rollON);
			}
		});
	}
	/* the images inside a subnav div will roll over, remember you will need two images "image.gif" and "image_over.gif", the rest is done here */
	$('.subnav a img').rollOver('gif');
	$('#treatAnatomy .nav a img').rollOver('gif', true);
	$('.rollOver img').rollOver('png');
	
	$('.radio_list input').unbind('click').click(function() {
		if ($(this).val() == true)
			$('#gift-inputs').show();
		else
			$('#gift-inputs').hide();
	});
});



/* -------- */