Cufon.replace('#navigation ul li a, .entry h2, .product-gallery h2, .attachment-entry a, .orange-box h2, .top-content h1, .overview h2, .left-sidebar h2, .col h2, p.slogan');
Cufon.replace('.slider-navigation li a', {'hover' : true});

// Ride the carousel...
jQuery(document).ready(function($) {
	function mycarousel_initCallback(carousel) {
	    jQuery('.slider-navigation ul a').bind('click', function() {
	        carousel.scroll(jQuery.jcarousel.intval(jQuery(this).attr('rel')));
	        return false;
	    });
	
	    jQuery('.jcarousel-scroll select').bind('change', function() {
	        carousel.options.scroll = jQuery.jcarousel.intval(this.options[this.selectedIndex].value);
	        return false;
	    });
	
	};
	
	function mycarousel_itemFirstInCallback(carousel, item, idx, state) {
		idx = idx % $('.slider-navigation ul a').length;
		$('.slider-navigation ul a').removeClass('active');
		$('.slider-navigation ul a').eq(idx-1).addClass('active');
		
        Cufon.replace('.slider-navigation li a', {'hover' : true});
	};
	
	$("#navigation ul li").hover(function() {
		$(this).find(".dd").show();
		$(this).find("a:eq(0)").addClass('hover');
		}, function(e) {
		$(this).find(".dd").hide();
		$(this).find("a:eq(0)").removeClass('hover');
	});
	
	$("#navigation > ul > li").hover(function() {
			$(this).addClass('hover');
			Cufon.replace('#navigation ul li a');
		}, function(e) {
			$(this).removeClass('hover');
			Cufon.replace('#navigation ul li a');
	});
	
	var home_slider = jQuery("#mycarousel");
    home_slider.jcarousel({
        scroll: 1,
        itemFirstInCallback: mycarousel_itemFirstInCallback,
        initCallback: mycarousel_initCallback,
        buttonNextHTML: null,
        buttonPrevHTML: null,
        auto: 2,
        wrap: 'circular'
    });
    home_slider.hover(function() {
    	$(this).data('jcarousel').stopAuto();
	}, function() {
    	$(this).data('jcarousel').startAuto();
	});
    
    $('.thumbs .img:eq(0)').append('<span class="arr"></span>');
    
    $('.thumbs .img a').click(function(){
    	return false;
    });
    
    $('.thumbs .img a').hover(function(){
    	var index = $('.thumbs .img a').index($(this));
    	$(this).parent().parent().find('span.arr').remove();
    	$('.product-gallery .big-img img').hide();
    	$('.product-gallery .big-img img:eq(' + index + ')').show();
    	$(this).parent().append('<span class="arr"></span>');
    });
    
    function thumb_cicle_animate (target) {
    	var items = target.find('img').length;
		var left = -136*(items-1);
    	if ( parseInt(target.find('span').css('left')) == left ) {
    		left = 0;
    	};
    	target.find('span').animate({'left': left}, thumb_gallery_speed*items, 'linear', function() {
    		thumb_cicle_animate(target);
		});
    }
    
    var thumb_gallery_speed = 600;
    $('.overview-b:has(.thumb-gallery)').hover(function() {
    	$(this).find('span').stop();
    	thumb_cicle_animate($(this));
	}, function() {
    	$(this).find('span').stop().animate({'left': 0}, thumb_gallery_speed, 'linear');
	})
	
	$('.product-select .next-product').click(function() {
		if ($(this).parent().find('.thumb-slider:animated').length) {
			return false;
		};
		product_select_gallery_move(1);
		return false;
	});
	$('.product-select .prev-product').click(function() {
		product_select_gallery_move(-1);
		return false;
	});
	
	var thumb_gallery_position = 0;
	function product_select_gallery_move(step) {
		var slider = $('.product-select .thumb-slider');
		var new_index = thumb_gallery_position + step;
		var items = $('.product-select .thumb-slider img').length;
		new_index = (new_index < 0) ? items-1 : ( (new_index > items-1) ? 0 : new_index) ;
		
		var new_item = $('.product-select .thumb-slider img:eq(' + new_index + ')');
		var titles = $('.product-select').parent().find('.info');
		
		
		slider.animate({'left': -104*new_index}, thumb_gallery_speed);
		titles.filter(':visible').fadeOut(thumb_gallery_speed/2, function() {
			titles.eq(new_index).fadeIn(thumb_gallery_speed/2);
		});
		
		thumb_gallery_position = new_index;
	}
   
    $('.products-gallery .overview').hover(function(){
    	$(this).addClass('hovered');
    	Cufon.replace($(this).find('h2 a'), {"color":"#ffbf00"});
    }, function(){
    	$(this).removeClass('hovered');
    	Cufon.replace($(this).find('h2 a'), {"color":"#fff"});
    });
   
    $('.blink').focus(function () {
		if ($(this).val() == $(this).attr('title')) {
			$(this).css('color', '#000').val('');
		}
	});
	
	$('.blink').blur(function () {
		if ($(this).val() == '') {
			$(this).css('color', '#000').val($(this).attr('title'));
		}
	});
	
	$('a.go-top').click(function(){
		$.scrollTo('0', 1000);
		return false;
	});
	$('.left-sidebar ul li:last').addClass('last');
	
	
	if ( $('.main-img img').length > 1 ) {
		$('.main-img img:gt(0)').hide();
		index = 0;
		setInterval(function() {
			$('.main-img img:eq(' + index + ')').fadeOut();
			index ++;
			if ( index >= $('.main-img img').length) {
				index = 0;
			};
			$('.main-img img:eq(' + index + ')').fadeIn();
		}, 3000);
	};
	
	/* Contact Page */
	if ( $('.contact-area').length ) {
		var hash = window.location.hash.replace('#', '');
		if ( hash != '' ) {
			$('.contact-area input[value=' + hash + ']:not(:checked)').click();
		};
		
		$('.contact-area').submit(function() {
			var th = $(this);
			var error = false;
			
			jQuery.each(__contact_messages, function(name, err_msg) {
				var fld = th.find('[name=' + name + ']');
				
				if ( fld.val() == '' ) {
					alert(err_msg);
					fld.focus();
					error = true;
					return false;
				};
				
				if ( name == 'c_email' && !fld.val().match(/^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/) ) {
					alert(err_msg);
					fld.focus();
					error = true;
					return false;
				};
			});
			
			return !error;
		});
		
	};
});
