/**
 * @author jude solis
 */


// DOM Ready Function
// Put all executing code in here
jQuery(function($){
   

	function setCookie(c_name,value,expiredays) {
		var exdate=new Date();
		exdate.setDate(exdate.getDate()+expiredays);
		document.cookie=c_name+ "=" +escape(value)+
		((expiredays==null) ? "" : ";expires="+exdate.toUTCString()+
		";path=/");
	}
   
   //////////////////////////
	// Theme Picker functions
	
	var hideDrawer = function() {
		$("#theme-drawer #theme-container").slideToggle("normal", function () {
			if ($("#theme-drawer #theme-container").is(':visible')) {
				$("#footer").css("margin-bottom", "180px");
			} else {
				$("#footer").css("margin-bottom", "30px");
			}
		});
	};
	
	var switchTheme = function(event) {
		event.preventDefault();
		
		var newtheme = $(this).attr('href');
		var themesheet = $('#sushi-custom-theme');
		
		themesheet.attr({href: "http://sushiart.org/wp-content/themes/tempura/custom-theme-css.php" + newtheme});
		hideDrawer();
		setCookie('user_theme', newtheme, 7);
	};
	
	var notSelectThumb = function() {
		$(this).find('span').fadeTo(0,0);
		$(this).find('p').hide();
	};
	
	var doSelectThumb = function() {
		$(this).find('span').fadeTo(0,0.7);
		$(this).find('p').show();
	};
	
	
   // Set up Theme Picker
   $('#theme-browser .theme-toggle').click(hideDrawer);
   
   $('#theme-browser #theme-carousel li a.theme-thumb').each(notSelectThumb)
   .hover(doSelectThumb, notSelectThumb);
   
   
   $('#theme-browser #theme-carousel li a.theme-thumb').click(switchTheme);
   
   
   ///////////////////////////////
   // Hero Rota (aka. Image Gallery)
   
   
   var initializeRota = function() {
	   // get rota
	   var rota = $('#hero-rota');
	   if (!rota.length) return;
	   var slots = rota.find('li').length;
	   if (slots < 2) return;
	   
	   // set up manager
	   window.rman = {
		   count : 0,
		   gsize : slots,
		   next : function() {
			  return ++this.count % this.gsize;
		   },
		   timer : 0
	   };
	   
	   // common functions
/*	   var cycleHeroRota = function() {
		   var strip = rota.find('ul');
		   strip.css('margin-left', function() {
			   return (window.rman.next() * -615) + 'px';
		   });
	   };
*/
	   
	   var cycleHeroRota = function() {
		   var strip = rota.find('ul');
		   var newpos = window.rman.next(); // * -615 + 'px';
		   // do animation
		   strip.animate({marginLeft: (newpos * -615) + 'px'}, 400, function() {
			   // adjust if at end of rota
			   if (newpos == (window.rman.gsize - 1)) {
				   window.rman.next();
				   strip.css('margin-left', '0px');
			   }
		   });
	   };
	   
	   var setRotaInterval = function() {
		   window.rman.timer = setInterval(cycleHeroRota, 7000);
	   };
	   
	   
	   
	   // bind events
	   setRotaInterval();
	   
	   var nextbtn = rota.find('.rota-next-btn');
	   nextbtn.click(function() {
		   clearInterval(window.rman.timer);
		   cycleHeroRota();
		   setRotaInterval();
	   });
   };
   
   initializeRota();
   
   
   
   ///////////////////////////////
   // Search Form Styled Behavior
   
   (function() {
		var txtfields = $('#searchform #s');
		
		// Set text field behavior
		txtfields.each(function() {
			if ($(this).val()) {
				$(this).siblings('label').hide();
			}
		});
		txtfields.load(function() {
			if ($(this).val()) {
				$(this).siblings('label').hide();
			}
		});
		txtfields.focus(function() {
			if (!$(this).val()) {
				$(this).siblings('label').hide();
			}
		});
		txtfields.keydown(function(ev) {
			$(this).siblings('label').hide();
		});
		txtfields.keyup(function() {
			if (!$(this).val()) {
				$(this).siblings('label').show();
			}
		});
		txtfields.blur(function() {
			if (!$(this).val()) {
				$(this).siblings('label').fadeTo(0, 1);
				$(this).siblings('label').show();
			}
		});
	
	})();
	
	
	////////////////////////////
	// The Fly
	
	$('#the-fly').draggable();
	
	
	
   
});
