// JavaScript Document

$(document).ready(function() {
	
	//Default Action
	$(".tab_content").hide(); //Hide all content
	$("ul.tabs li:first").addClass("active").show(); //Activate first tab
	$(".tab_content:first").show(); //Show first tab content
	
	//On Click Event
	$("ul.tabs li").click(function() {
		$("ul.tabs li").removeClass("active"); //Remove any "active" class
		$(this).addClass("active"); //Add "active" class to selected tab
		$(".tab_content").hide(); //Hide all tab content
		var activeTab = $(this).find("a").attr("href"); //Find the rel attribute value to identify the active tab + content
		$(activeTab).fadeIn(); //Fade in the active content
		return false;
	});
	
	/////////////////////////////////////////////////////////////////////////////////////////////////
	
	var position = $("#search-box").position();
	
	var menu_left = parseInt(position.left) + 1 ;
	
	$( "#search-box" ).autocomplete({
			open:function (){ $(".ui-autocomplete ").css( {"z-index":"100" , "left" : menu_left } )},						
			minLength: 3,
			source:  function( request, response ) {
				
/*				$.ajax({
				  type: 'POST',
				  url: base_url+"search/index/",
				  data: [term:$("#search-box").val()],
				  
				  dataType: 'json'
				});*/
				
				//$.getJSON( base_url+"search/index/"+$("#search-box").val(), "", response );
				
				$.post( base_url+"search/index/", { term : $("#search-box").val() } , response , "json" );
			},
			focus: function( event, ui ) {
				$( "#search-box" ).val( ui.item.value );
				return false;
			},
			select: function( event, ui ) {
				$( "#search-box" ).val( ui.item.value );
				window.location = base_url+"product/view/product/"+ui.item.value ;
				//$( "#product-id" ).val( ui.item.value );
				//$( "#product-description" ).html( ui.item.desc );
				//$( "#product-icon" ).attr( "src", "images/" + ui.item.icon );
				//$(ul).css();
				return false;
			}
		})
		
		.data( "autocomplete" )._renderItem = function( ul, item ) {
			return $( "<li></li>" )
				.data( "item.autocomplete", item )
				.append( "<a href='"+ base_url+"product/view/product/"+item.value +"'>" + item.label + "</a>" )
				.appendTo( ul );
				
		};

	/////////////////////////////////////////////////////////////////////////////////////////////////
	
/*	$("#hotOffers").faded({
		speed: 1000,
		crossfade: true,
		bigtarget: true,
		autoplay: 3000,
		autorestart: 3000,
		autopagination: false
	});
*/	
	
	
	

	/////////////////////////////////////////////////////////////////////////////////////////////////
	
	$("#update_order").click( function(){
		   
		$("#cart_form").attr("action" , base_url+"account/update_cart" );
		$("#cart_form").submit();

	});
	
	/////////////////////////////////////////////////////////////////////////////////////////////////
	
	$("#send_order").click( function(){
		   
		$("#cart_form").attr("action" , base_url+"account/send_order" );
		$("#cart_form").submit();

	});
	
	/////////////////////////////////////////////////////////////////////////////////////////////////
	
	$("#delete_order").click( function(){
		   
		$("#cart_form").attr("action" , base_url+"account/delete_from_cart" );
		$("#cart_form").submit();

	});
	
	/////////////////////////////////////////////////////////////////////////////////////////////////
	
	$("#update_d_order").click( function(){
		   
		$("#cart_form").attr("action" , base_url+"dealer/update_cart" );
		$("#cart_form").submit();

	});
	
	/////////////////////////////////////////////////////////////////////////////////////////////////
	
	$("#send_d_order").click( function(){
		   
		$("#cart_form").attr("action" , base_url+"dealer/send_order" );
		$("#cart_form").submit();

	});
	
	/////////////////////////////////////////////////////////////////////////////////////////////////
	
	$("#delete_d_order").click( function(){
	
		$("#cart_form").attr("action" , base_url+"dealer/delete_from_cart" );
		$("#cart_form").submit();

	});
	
	/////////////////////////////////////////////////////////////////////////////////////////////////
	
	$(".increase-qty").click( function(i){
		
		i.preventDefault();
		var index = $(".increase-qty").index(this);
		var input_qty = $(".input-qty:eq("+index+")").val();
		
		if(input_qty == "" || input_qty >=0 ){
			input_qty ++ ;
		}else{
			input_qty = 0;
		}
		
		$(".input-qty:eq("+index+")").val(input_qty);


	});
	
	/////////////////////////////////////////////////////////////////////////////////////////////////
	
	$(".decrease-qty").click( function(i){
		   
		i.preventDefault();
		var index = $(".decrease-qty").index(this);
		var input_qty = $(".input-qty:eq("+index+")").val();
		
		if(input_qty > 0 ){
			input_qty -- ;
		}else{
			input_qty = 0;
		}

		$(".input-qty:eq("+index+")").val(input_qty);
		
	});
	
	/////////////////////////////////////////////////////////////////////////////////////////////////
	
	$('#checkall').click(function () {
		$(this).parents('#cart_form:eq(0)').find(':checkbox').attr('checked', this.checked);
	});
	
	/////////////////////////////////////////////////////////////////////////////////////////////////
	
	var ajax_form_newsletter = { 
    target:       '#inputs-div',
    url:          base_url + 'email_list/action/',
	beforeSubmit: function(){
		
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
		var emailaddress = $("#email").val();
		
		if( !emailReg.test(emailaddress) || emailaddress == "" ) {
            alert("Invaled Email");
			return false;
        }
		
	},
	resetForm:    true
	};
	
	// pass options to ajaxForm 
	$('#add_email').ajaxForm(ajax_form_newsletter);
	
	
	
	
	/////////////////////////////////////////////////////////////////////////////////////////////////

	
	$(".scart-button").click( function(){
		
		var index = $(".scart-button").index(this);
		
		var product_id = $(".product-id:eq("+index+")").val();
		var qty = $(".input-qty:eq("+index+")").val();
		
		$.ajax({
		  beforeSend: function( ) { 
		  
		  		if( isNaN(qty) || qty <= 0  ){
					alert("Invalid Quantity");
				}else{
					$(".ajax-ctrl:eq("+index+")").css('display','none');
					$(".ajax-loader:eq("+index+")").css('display','block');
					$(".ajax-ctrl :input").attr('disabled', true);
					
				}
		  
		  } ,
		  type: 'POST',
		  url: base_url+"dealer/ajax_add_to_cart",
		  data: {"product_id":product_id,"qty":qty },
		  success:  function( data ){ 
		  				$(".ajax-loader:eq("+index+")").html(data);
						$(".ajax-ctrl :input").attr('disabled', false);
					} ,
		  dataType: "text/html"
		});
		
	});
	
	

});

$(document).ready(function() {
	

	function megaHoverOver(){
		$(this).find(".sub").stop().fadeTo('fast', 1).show();
			
		//Calculate width of all ul's
		/*(function($) { 
			jQuery.fn.calcSubWidth = function() {
				rowWidth = 0;
				//Calculate row
				$(this).find("ul").each(function() {					
					rowWidth += $(this).width(); 
				});	
			};
		})(jQuery); 
		
		if ( $(this).find(".row").length > 0 ) { //If row exists...
			var biggestRow = 0;	
			//Calculate each row
			$(this).find(".row").each(function() {							   
				$(this).calcSubWidth();
				//Find biggest row
				if(rowWidth > biggestRow) {
					//biggestRow = rowWidth;
				}
			});
			//Set width
			//$(this).find(".sub").css({'width' :biggestRow});
			//$(this).find(".row:last").css({'margin':'0'});
			
		} else { //If row does not exist...
			
			//$(this).calcSubWidth();
			//Set Width
			//$(this).find(".sub").css({'width' : rowWidth});
			
		}*/
	}
	
	function megaHoverOut(){ 
	  $(this).find(".sub").stop().fadeTo('fast', 0, function() {
		  $(this).hide(); 
	  });
	}


	var config = {    
		 sensitivity: 2, // number = sensitivity threshold (must be 1 or higher)    
		 interval: 100, // number = milliseconds for onMouseOver polling interval    
		 over: megaHoverOver, // function = onMouseOver callback (REQUIRED)    
		 timeout: 500, // number = milliseconds delay before onMouseOut    
		 out: megaHoverOut // function = onMouseOut callback (REQUIRED)    
	};

	$("#BlueMenu #BlueInnerMenu ul li .sub").css({'opacity':'0'});
	$("#BlueMenu #BlueInnerMenu ul li").hoverIntent(config);



});

$(window).load(function(){
	
	$('img.image-center').each(function(index) {
										
		var parent_height = $(this).parent().height();
		var image_height = $(this).height();
		
		var top_margin = (parent_height - image_height)/2;
		//center it
		$(this).css( 'margin-top' , top_margin);
	});

});	

/////////////////////////////////////////////////////////////////////

function slideSwitch() {
	
	var img_len = $('.content li').length;
		
	if( img_len > 1 )
	{

		if( $('.content li.active').index() == $('.content li:last').index() ){
			
			$('.content li').removeClass("active");
			
			$('.content li:first').css({opacity: 0.0}).addClass('active').animate({opacity: 1.0}, 500 );
			//$('#images li').css( "opacity", "0.0");
			$('.pagination li').removeClass("active");
			$('.pagination li:first').addClass("active");
			
		
		}else{
		
			var index = $('.content li.active').index() + 1;
			$('.content li').removeClass("active");
			
			
			$('.content li').eq(index).css({opacity: 0.0}).addClass('active').animate({opacity: 1.0}, 500 );
			//$('#images li').css( "opacity", "0.0");
			$('.pagination li').removeClass("active");
			$('.pagination li').eq(index).addClass("active");
		
		}
		
	}

}

$(function() {
    var timer = setInterval( "slideSwitch()", 5000 );
	
	$(".pagination li a").click(function(){
								   
		clearInterval( timer );
		
		var index = $(".pagination li a").index(this);
		
		$('.content li').removeClass("active");	
		$('.content li').eq(index).css({opacity: 0.0}).addClass('active').animate({opacity: 1.0}, 500 );
		//$('#images li').css( "opacity", "0.0");
		$('.content li').removeClass("active");
		$('.content li').eq(index).addClass("active");
		
		
		timer = setInterval( "slideSwitch()", 5000 );
		
		return false;
	});
	
});
