jQuery(document).ready(function($){

	//Banner Click
	$('.product img, .product .title, .product .more-info').click(function(){
		window.location = $(this).parent('.product').find('span.more-btn > a').attr('href');
		return false;
	});
	
	//Chart Tabs
	$(".chart-wrap").hide().filter(':first').show();
	$("ul#chart-tabs li:first").addClass("active").show();
	
	//On Click Event
	$("ul#chart-tabs li").click(function() {
		$("ul#chart-tabs li").removeClass("active");
		$(this).addClass("active");
		$(".chart-wrap").hide();
		var activeTab = $(this).find("a").attr("href"); 		
		$(activeTab).fadeIn(1000);
		return false;
	});
	
	//Content Block Animations
	$('.home-content-block .go-to').css({bottom: '17px', left: '-210px'});
	$('.home-content-block').hover(function() {
		$(this).find('h3, p').animate({marginTop: '-30px'}, 250);
		$(this).find('.go-to').delay(175).animate({left: '0px'}, 150);
	}, function() {
		$(this).find('.go-to').animate({left: '-210px'}, 150);
		$(this).find('h3, p').delay(125).animate({marginTop: '0px'}, 120);
	});
	/*go to the desired location on click*/
	$('.home-content-block').click(function(){
		url = $(this).find("a").attr("href");
		if (url==undefined)
			url = $(this).find("form").attr("action");
		if (url!=undefined)
			window.location=url;
		
	})
	
	var win = $(window).width();
	
	// Create function for cycle plugin so we can reuse in AJAX callback
	function runCycle() {
		if(win > 1024){
			$('#prodWrap').before(
				'<span id="prodPrev">Prev</span>'+
				'<span id="prodNext">Next</span>');
		}
		$('#pagesWrap').append('<div id="pages">');
		$('.prodRow').cycle({
		fx: 'scrollHorz',
		speed: 600,
		next:   '#prodNext', 
    	prev:   '#prodPrev',
    	pager: '#pages'
		}).cycle('pause');
	}
	
	// Run it on load
	runCycle();
		
	// AJAX function for product grid
	function getProd(val) {
		$('.tabContent div').html('<img src="ajax-loader.gif" style="margin: 230px 470px;" />');
		var data = 'cat='+val;
    	$.ajax({
    		url: "productgrid.php",
    		type: "GET",
    		data: data,
    		cache: false,
    		success: function (html) {
    			$('.tabContent div').html(html);
    			// Apply cycle plugin to the new content
    			runCycle();
    			var num = $('.prodRow li').length;
    			if(num <= 12) { $('#pages,#prodPrev,#prodNext').remove(); }
    			$('.tabContent').hide().fadeIn(500);
    			$('.shortDesc').each(function () {
    				$(this).hide();
    			});
    			$('.prodRow li').hover(function() {
    				$(this).find('.shortDesc').show();
    			}, function() {
    				$(this).find('.shortDesc').hide();
    			})
    		}
    	});
	}
	
	// Control active tabs and trigger the AJAX function
	$('.tabContent').hide().filter(':first').show();
	$('ul#prodTabs li:first a').addClass('active');
	$('ul#prodTabs li a').click(function() {
		$('#pages,#prodPrev,#prodNext').remove();
		$('ul#prodTabs li a').removeClass('active');
		$(this).addClass('active');
		var cat = $(this).data('cat');
		getProd(cat);
		return false;
	});
	
	// Activate Manufacturer Logos Slideshow
	$('#logos').cycle({
		timeout: 4000
	});
	
	$('img').each(function(){
  		$(this).attr('src', $(this).attr('dsrc'));
	});
	
	//Signup Form
	$("a#inline").fancybox({
		'overlayOpacity': 0.85,
		'overlayColor': '#000',
		'type': 'inline'
	});
		
});
