$(document).ready(function() {
	
	$('.thumb_popup').css('bottom','106px')
	
	// Featured Slider
	var $featured_area = $('#featured-slider'),
	    $feature_thumb = $('#featured-thumbs img'),
		$active_arrow = $('div#active_item');
		ordernum = 1,
		pause_scroll = false,
		$slider_control = $('#featured-thumbs'), //div#featured-thumbs
		$slider_control_tab = $feature_thumb.parent('a');
	
	if (!($featured_area.length == 0)) {
		$featured_area.cycle({
			timeout: 0,
			speed: 300,
			cleartypeNoBg: true,
			fx: 'fade'
		});
	};
	
	
	function gonext(this_element){
		$slider_control.find("img.active").removeClass('active');
		this_element.find("img").addClass('active');
					
		$active_arrow.animate({"left": this_element.find("img").position().left+55}, "slow");
		
		ordernum = this_element.prevAll('a').length+1;
		$featured_area.cycle(ordernum - 1);
	};
	
	
	$slider_control_tab.click(function() {
		clearInterval(interval);
		gonext($(this));
		return false;
	});
	
	$('a#prevlink, a#nextlink').click(function() {
		clearInterval(interval);
		
		if ($(this).attr("id") === 'nextlink') {
						
			auto_number = $slider_control.find("img.active").parent().prevAll('a').length+1;
			if (auto_number === $slider_control_tab.length) auto_number = 0;
			
		} else {
			auto_number = $slider_control.find("img.active").parent().prevAll('a').length-1;
			if (auto_number === -1) auto_number = $slider_control_tab.length-1;
		};
		
		gonext($slider_control_tab.eq(auto_number));
		return false;
	});
				
	$feature_thumb.hover(function(){         
	   $next_div = $(this).parent('a').next('div');
	   
	   $next_div.css('bottom','106px')
	   $next_div.css({'left':jQuery(this).position().left-10});
	   
	   $(this).addClass('hover').fadeTo('fast',0.5);
	   $next_div.css('display','block').animate({"bottom": "96px", "opacity": "1"}, "fast");
	},function(){
	   $(this).removeClass('hover').fadeTo('fast',1);
	   $next_div.animate({"bottom": "106px", "opacity": "0"}, "fast");
	});
	
	var auto_number;
	var interval;
	
	$slider_control_tab.bind('autonext', function autonext(){
		if (!pause_scroll) gonext($(this)); 
		return false;
	});
	
	interval = setInterval(function () {
		auto_number = $slider_control.find("img.active").parent().prevAll('a').length+1;
		if (auto_number === $slider_control_tab.length) auto_number = 0;
		$slider_control_tab.eq(auto_number).trigger('autonext');
	}, 5000);
					
	
	// Menu 
	$('ul.superfish').superfish({ 
		delay: 200,
		animation: {'marginLeft':'0px', opacity:'show', height:'show'},
		speed: 'fast',
		autoArrows: true,
		onBeforeShow: function(){ this.css('marginLeft','20px'); },
		dropShadows: false
	});	
	$('ul.nav > li > a.sf-with-ul').parent('li').addClass('sf-ul');

	// Search Form
	var $searchform = $('#header div#search-form');
	var $searchinput = $searchform.find("input#searchinput");
	var $searchvalue = $searchinput.val();
	
	$searchform.css("right", "25px");
	
	$("#header a#search-icon").click(function() {
		if ($searchform.filter(':hidden').length == 1)	
			$searchform.animate({"right": "-1", "opacity": "toggle"}, "slow")
		else
			$searchform.animate({"right": "25", "opacity": "toggle"}, "slow");
		return false;
	});
		
	$searchinput.focus(function() {
		if ($(this).val() == $searchvalue) jQuery(this).val("");
	}).blur(function(){
		if ($(this).val() == "") jQuery(this).val($searchvalue);
	});

});
