$(function(){

    $('.accordionButton').click(function() {
        $('.accordionButton').removeClass('on');
        $('.accordionContent').slideUp('normal');
        if($(this).next().is(':hidden') == true) {
            $(this).addClass('on');
            $(this).next().slideDown('normal');
         } 
     });
     
     $('.pnlink').click(function() {
        $(this).addClass('pnlinkon');
     });
    
    
    $('.accordionButton').mouseover(function() {
        
        /*
        $('.accordionButton').removeClass('on');
        $('.accordionContent').slideUp('normal');
        if($(this).next().is(':hidden') == true) {
            $(this).addClass('on');
            $(this).next().slideDown('normal');
         }
        */
        $(this).addClass('over');
        
        
    }).mouseout(function() {
        $(this).removeClass('over');                                        
    });
    
    /*
    hoverConf = {
        sensitivity: 10, // number = sensitivity threshold (must be 1 or higher)
        interval: 1000, // number = milliseconds for onMouseOver polling interval
        timeout: 300, // number = milliseconds delay before onMouseOut
        over: function() {
            
            $('.accordionButton').removeClass('on');
            $('.accordionContent').slideUp('normal');
            if($(this).next().is(':hidden') == true) {
                $(this).addClass('on');
                $(this).next().slideDown('normal');
             }
            $(this).addClass('over');
             
        }, 
        out: function() { 
            
            $(this).removeClass('over'); 
        
        } 
        
    }
    
    
    $('.accordionButton').hoverIntent(hoverConf);
    */
    
    
    $('.pnlink').mouseover(function() {
        $(this).addClass('pnlinkover');
        }).mouseout(function() {
            $(this).removeClass('pnlinkover');
        });
    
   
    $('.accordionContent').hide();
    $('.on').trigger('click');
    $('.pnlinkon').trigger('click');
        
    $('div.detailsbutton').click(function () {
        if ($(this).parent('div.productcelldetailsnav').next().is(":hidden")){
            $(this).css("background-image", "url('/images/btn_details_up.png')");
            $(this).parent('div.productcelldetailsnav').next().slideDown();
        }
        else {
            $(this).css("background-image", "url('/images/btn_details_down.png')");
            $(this).parent('div.productcelldetailsnav').next().slideUp();
        }
    });
    
    
    
    $('#backroomBargains').qtip({
        content: 'TruAudio\'s "Back Room" is always a great way to pick up some extra profit.  Close-out items, damaged boxes, B-Stock, bulk deals. All items are backed by the famous TruAudio warranty unless otherwise noted. Items in the "Back Room" are subject to current availability.',
        position: {
            my: 'bottom center',
            at: 'top center'
        }
    });
    
    
    
  
/*  
  
    function switchText()
    {
        if ($(this).val() == $(this).attr('title'))
        	$(this).val('').removeClass('exampleText');
        else if ($.trim($(this).val()) == '')
        	$(this).addClass('exampleText').val($(this).attr('title'));
    }
    
    $('input[type=text][title!=""]').each(function() {
    if ($.trim($(this).val()) == '') $(this).val($(this).attr('title'));
    if ($(this).val() == $(this).attr('title')) $(this).addClass('exampleText');
    }).focus(switchText).blur(switchText);
    
    $('form').submit(function() {
    $(this).find('input[type=text][title!=""]').each(function() {
    	if ($(this).val() == $(this).attr('title')) $(this).val('');
    });
    });
    
    $('input[type=password][title!=""]').each(function() {
    if ($.trim($(this).val()) == '') $(this).val($(this).attr('title'));
    if ($(this).val() == $(this).attr('title')) $(this).addClass('exampleText');
    }).focus(switchText).blur(switchText);
    
    $('form').submit(function() {
    $(this).find('input[type=password][title!=""]').each(function() {
    	if ($(this).val() == $(this).attr('title')) $(this).val('');
    });
    });
*/  
});


function fill(thisValue) {
    $('#inputString').val(thisValue);
    $('#suggestions').hide();
}
function lookup(inputString) {
	/////alert(inputString);
		if(inputString.length == 0) {
			// Hide the suggestion box.
			$('#suggestions').hide();
		} else {
			$.post("/includes/ajaxProductSearch.php", {queryString: ""+inputString+""}, function(data){
				if(data.length >0) {
					$('#suggestions').show();
					$('#autoSuggestionsList').html(data);
				}
                else {
                    $('#suggestions').show();
					$('#autoSuggestionsList').html('<li>No Results Found</li>');
                }
			});
		}
	} // lookup
	function fill(thisValue) {
		$('#inputString').val(thisValue);
		setTimeout("$('#suggestions').hide();", 200);
	}
    
    
    
    
function addToCart(){
    var ID = parseInt($(this).find('input[name="ID"]').val());
    var qty = parseInt($(this).find('input[name="quantity"]').val());
    if (!$(this).find('input[name="DealID"]').val()){
        var dealid = 0;
    }
    else {
        var dealid = $(this).find('input[name="DealID"]').val();
    }
    
    var clickedForm = $(this);
    var clicked = $(this).attr("name");
    
    if ($(this).attr("name") == 'addDealCart'){
        if ($('#qtyOrdered').length != 0){
            var qtyOrdered = parseInt($('#qtyOrdered').html());
            var qtyLimit = parseInt($('#qtyLimit').html());
            var qtyInCart = parseInt($('#dddInCart').html());
            
            if (qty > qtyLimit){
                alert("The quantity limit for this deal is " + qtyLimit + " per dealer, please select a smaller quantity.");
                return false;
            }
            
            if ((qtyInCart+qty) > qtyLimit){
                alert ("You already have " + qtyInCart + " in your cart.  The limit for this deal is " + qtyLimit + ".");
                return false;
            }
            
            if ((qtyOrdered+qty) > qtyLimit){
                alert ("You have aleady ordered " + qtyOrdered + ".  You can only add " + (qtyLimit-(qtyOrdered)) + " more of this item to your cart.");
                return false;
            }
        }
    }
    if ($(this).attr("name") == 'addFeaturedCart'){
            var qtyOrdered = parseInt($(this).find('input[name="featuredOrdered"]').val());
            var qtyLimit = parseInt($(this).find('input[name="featuredLimit"]').val());
            var qtyInCart = parseInt($(this).find('input[name="featuredInCart"]').val());
            
            if (qty > qtyLimit){
                alert("The quantity limit for this featured item is " + qtyLimit + " per dealer, please select a smaller quantity.");
                return false;
            }
            
            if ((qtyInCart+qty) > qtyLimit){
                alert ("You already have " + qtyInCart + " in your cart.  The limit for this featured item is " + qtyLimit + ".");
                return false;
            }
            
            if ((qtyOrdered+qty+qtyInCart) > qtyLimit){
                alert ("You have aleady ordered " + qtyOrdered + ".  You can only add " + (qtyLimit-(qtyOrdered)) + " more of this item to your cart.");
                return false;
            }
    }

    $.ajax({
        type: 'POST',
        url: '/cart.php',
        data: 'ID=' + ID +'& quantity=' + qty + '& DealID=' + dealid,
        dataType: 'html',
        success: function(response) {
            newCount = $(response).find('#cartCount').html();
            $('#cartCount').html(newCount);
            if (dealid != 0){
                var newCount = qty + qtyInCart;
                
                if (clicked == 'addFeaturedCart'){
                    $(clickedForm).find('input[name="featuredInCart"]').val(""+newCount+"");
                }
                if (clicked == 'addDealCart'){
                    $('#dddInCart').html(""+newCount+"");
                }
            }
            
        }
    });
    return false;
}
    
    
function sendPass(PurchaserLoginID){
    
    $.ajax({
        type: 'POST',
        url: '/includes/sendPass.php',
        data: 'PurchaserLoginID=' + PurchaserLoginID,
        dataType: 'html',
        success: function(response) {
            if (response == 'OK'){
                alert("Password Sent");
            }
            else {
                alert("There was error, please contact your sales rep.");
            }
        }
    });
}
    
    
  
// index slide show
	/*** 
		    Simple jQuery Slideshow Script
		    Released by Jon Raasch (jonraasch.com) under FreeBSD license: free to use or modify, not responsible for anything, etc.  Please link out to me if you like it :)
		***/
		function slideSwitch() {
    var $active = $('#slideshow DIV.active');
    if ( $active.length == 0 ) $active = $('#slideshow DIV:last');
    // use this to pull the divs in the order they appear in the markup
    var $next =  $active.next().length ? $active.next()
        : $('#slideshow DIV:first');
    // uncomment below to pull the divs randomly
    // var $sibs  = $active.siblings();
    // var rndNum = Math.floor(Math.random() * $sibs.length );
    // var $next  = $( $sibs[ rndNum ] );
    $active.addClass('last-active');
    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active last-active');
        });
		}
		$(function() {
		    setInterval( "slideSwitch()", 10000 );
		});
// end index slide show
