//pagetracker for pdfs
$('a[href*=pdf]').click(function() {
    _gaq.push(['_trackPageview', '/pdf/' + $(this).text()]);

});


// initialize cart
var path = window.location.pathname;
var carttype = 1;
var cart = $('div.fullcart');
function initCart() {
    var carth = $(cart).height();
    $(cart).css({ top: -(carth + 200) + 'px' });
    // re-apply click function to newly added cart items
    $('div.action a.remove').unbind('click');
    $('div.action a.remove').click(function() {
        $(this).parent().parent().fadeOut('slow', function() {
            $(this).remove();
            $('tr').removeClass('even');
            $('tr:even').addClass('even');
            $('tr:last').addClass('last');
            $('span.qty').text(parseInt($('span.qty').text()) - 1);
        });
        // need to do ajax remove from actual cart and update cart total
        $.ajax({
            type: "POST",
            url: "/handlers/removeitem.ashx?t=" + carttype + "&p=" + this.pathname.replace(/^\//,''),
            success: function(xml) {
                $(cart).find('span.total').text(xml);
            }
        });

        return false;
    });
    $('div.action a.update').unbind('click');
    $('div.action a.update').click(function() {
    var qty = $(this).parent().parent().find('.qty-text').val();
    $.ajax({
        type: "POST",
        url: "/handlers/updateitem.ashx?t=" + carttype + "&p=" + this.pathname.replace(/^\//,'') + "&q=" + qty,
        success: function(xml) {
            $(cart).find('span.total').text(xml);
        }
    });
    return false;
});
}



// view cart
$('div.cart a.view').click(function() {
    if ($(this).hasClass('c-close')) {
        var carth = $(cart).height();
        $(cart).animate({ top: -(carth + 100) + 'px' }, 500);
        $(this).removeClass('c-close');
    } else {
        $(cart).animate({ top: '0px' }, 500);
        $(this).addClass('c-close');
    }
});
// continue shopping and hide cart
$('div.fullcart a.shop').click(function() {
    var carth = $(cart).height();
    $(cart).animate({ top: -(carth + 100) + 'px' }, 500);
    $(cart).removeClass('c-close');
});
// add item to cart
$('a.add').click(function() {
    $.ajax({
        type: "POST",
        url: "/handlers/additem.ashx?t=" + carttype + "&p=" + this.pathname.replace(/^\//,''),
        success: function(xml) {
        $('span.qty').text(parseInt($('span.qty').text()) + 1);
            // get new total here
            $.ajax({
                type: "POST",
                url: "/handlers/total.ashx",
                success: function(txml) {
                    $('.total').text(txml);
                }
            });
            // update carts here
			// check to see if item already exists in cart!
			$(xml).stop().appendTo('div.fullcart ul');
			$(cart).animate({ top: '0px' }, 500);
		
            initCart();
			//Added on August 6
			_gaq.push(['_trackPageview', '/addtocart/' +  $(this).attr('href')]);
        }
    });
    return false;
});


// filter 
var filters = $('#tabs li ul li a');
if ($('#tabs li.f1').children('ul').children('li').length < 1) { $('#tabs li.f1').hide(); }
if ($('#tabs li.f2').children('ul').children('li').length < 1) { $('#tabs li.f2').hide(); }
$(filters).click(function() {
    if ($(this).hasClass('selected')) {
        $(this).removeClass('selected')
    } else {
        $(this).parent().parent().find('a').removeClass('selected');
        $(this).addClass('selected')
    }
    var f1 = $('#tabs li.f1 ul li a.selected');
    var f2 = $('#tabs li.f2 ul li a.selected');
    $('.products').empty().addClass('loading');
    $('.products').load("/handlers/filter.ashx?fp=" + $(this).parent().parent().parent().attr('rel') + "&f1=" + $(f1).attr('rel') + "&f2=" + $(f2).attr('rel'), function() {
        styles();
        wrap('.c-pr .wbox');
		//Added on August 6
		_gaq.push(['_trackPageview', '/f1/' + $(f1).attr('rel') + '/f2/' + $(f2).attr('rel')]);


		$('.products').removeClass('loading');
    });

});
function styles() {
	// color price column
    $('.products tr').find('td:eq(1)').addClass('grey');
	// superscript cents
	var tags = $('.c-price > p:contains($), .price > p:contains($), .pcol > p:contains($)');
		$.each($(tags), function() {
		$(this).html($(this).text().replace(/\.+([0-9][0-9])/, '.<span class=sup>$1</span>').replace(/\./, '<span>.</span>'));
	});
}
// toggle content
$(".c-more").click(function() {
    $('.intro').slideToggle();
    $(this).toggleClass('c-less');
});
// product detail accordian
$(".p-more").click(function() {
    if ($(".cont").height() < $(".pr-cont").height()) {
        $(".cont").animate({ height: $('.pr-cont').height() + 40 + 'px' }, 500 );
        $(this).animate({ width: '678px' }, 500, function() {$(this).css({ backgroundPosition: 'right -53px' });  });
        
    } else {
        $(".cont").animate({ height: '260px' }, 500);
        $(this).css({ width: '394px' });
        $(this).css({ backgroundPosition: 'right top' });
    }
});
// tabbed i-face
function Tabs(etab, epnl) {
    var tabs = $(etab).find("> li");
    var panels = $(epnl).find("> div");
    $(tabs.get(0)).addClass("sel");
    $(panels.get(0)).addClass("sel");

    $(tabs).click(function() {
        var index = $(tabs).index(this);
        $(tabs).removeClass("sel");
        $(panels).removeClass("sel");
        $(tabs.get(index)).addClass("sel");
        $(panels.get(index)).addClass("sel");
    }, function() { });
}
// menu
function mainmenu() {
    $("#nav li").hover(function() {
        $(this).find('ul:first').css({ visibility: "visible", display: "none" }).show();
    }, function() {
        $(this).find('ul:first').css({ visibility: "hidden" });
    });
}
// wrap all divs with wbox class
function wrap(el) {
    $(el).wrap('<div class="wrap">' + '<div class="bd">' + '<div class="c">' + '<div class="s">' + '</div>' + '</div>' + '</div>' + '</div>');
    $(el).parent().parent().parent().parent().prepend('<div class="hd">' + '<div class="c"></div>' + '</div>').append('<div class="ft">' + '<div class="c"></div>' + '</div>');
}
$(document).ready(function() {
    $.ajaxSettings.cache = false;
    mainmenu();
    wrap('.wbox');
    initCart();
    styles();
});



function CheckSearchBox(theForm) {

    if (theForm.Search.value == "") {
        alert("Please enter a search term.");
        theForm.Search.focus();
        return false;
    }

    return true
}




// Added on Dec 09, 2010
//alert ($('#ctl00_content_lblProductDescription').html());
if ($('#ctl00_content_lblProductDescription').html() == "") {  
                $('a.p-more').hide();   //hide the plus minus control
                $('div.price').addClass('moveprice');  // add class to reposition price
				$('div.stock').addClass('movestock');   // add class to reposition price
				$('div.cont').addClass('movecont');   // add class to reposition cont
}
else{
				$('div.price').css({ visibility: 'visible' });   // add class to reposition cont
				$('div.stock').css({ visibility: 'visible' });   // add class to reposition cont

}

