$(function () {
  var tabContainers = $('div.tabs > div');
  tabContainers.hide().filter(':first').show();
  $('div.tabs ul.tabNavigation a').click(function () {
    tabContainers.hide();
    tabContainers.filter(this.hash).show();
    $('div.tabs ul.tabNavigation a').removeClass('selected');
    $(this).addClass('selected');
    return false;
  }).filter(':first').click();
});

$(function () {
  var tabContainersVertical = $('div.tabsVertical > div');
  tabContainersVertical.hide().filter(':first').show();
  $('div.tabsVertical ul.tabNavigationVertical a').click(function () {
    tabContainersVertical.hide();
    tabContainersVertical.filter(this.hash).show();
    $('div.tabsVertical ul.tabNavigationVertical a').removeClass('selected');
    $(this).addClass('selected');
    return false;
  }).filter(':first').click();
});

$(document).ready(function() {
    helper.init();
});

function activateMiniCart(){
    //empty stub function so it does not error
}

var productUtility = {
    imagePath: '/webresources/images/products/',

    init: function(){
        productUtility.activateGallery();
        productUtility.email();
    },

    activateGallery: function(){
        $('#images li a').click(function(){
            var thumb = $(this).attr('href');
            var largeImage = thumb.split('_')[1];
            $('#productImage').attr('src', productUtility.imagePath + largeImage);
            return false;
        })
    },

    email: function(){
        $('.email').click(function(){
            var location = window.location;
            var title = $(this).attr('title');
            location.href = "mailto:?subject="+title+"&body="+escape(location.href)
            return false;
        })
    }
}

var helper = {
    init: function(){
        helper.setFocus();
        helper.popupSearch();
        helper.print();
    },

    print: function(){
        $('.print').click(function(){
            window.print();
            return false;
        })
    },
    
    popupSearch: function(){
        $('#showSearch').click(function(){
            $('#searchDialog').toggleClass('hidden');
            if(!$('#searchDialog').hasClass('hidden')){
                $('.SearchQuery').focus().select();
            }
            return false;
        })
    },
    
    popupCreditCard: function(){
        $('#showCreditCard').click(function(){
            $("#dialog").dialog({
                closeOnEscape: true,
                height: 250,
                width: 350,
                title: 'Where to locate the security code?',
                resizable: false,
                bgiframe: true
            });
            $('#dialog').dialog('open');
            return false;
        })
    },

    setFocus: function(){
        $('.focus:eq(0)').focus().select();

    },

    teaser: function(div){
        var content = $(div);
        $('li:gt(4)', content).hide();
        var a = $('<a></a>').attr({
            'href': '#',
            'class': 'collapsed'
        }).html('View All Brands &raquo;');
        $(content).after(a);
        a.click(function(){
            if($(this).hasClass('collapsed')){
                $('li:gt(4)', content).slideDown();
                $(this).toggleClass('collapsed');
                $(this).html('&laquo; Collapse Brands');
            }
            else{
                $('li:gt(4)', content).slideUp();
                $(this).toggleClass('collapsed');
                $(this).html('View All Brands &raquo;');
            }
            return false;
        })
        
        
    }
}

var locator = {
    getClosestDistributorByPostalCode: function(postalCode, radius) {

        $.ajax({
            type: "GET",
            url: "/aform/locateResellers.php?postalCode=" + postalCode + "&radius=" + radius,
            //data: "{'postalCode':'" + postalCode + "', 'radius':'" + radius + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            beforeSend: function() {
                $('#loader').removeClass('hidden');
            },
            success: function(result) {
            
			items = result;
			
		 $('#myDealers tbody tr').remove();

        if (items.length > 0) {
            var html = '';

            for (var i = 0; i < items.length; i++) {
                html += '<tr>';
                html += '<td><strong>' + items[i].Name + '</strong></td>';
                //				html += '<td><a href="#" class="map" lat="' + items[i].Latitude + '" long="' + items[i].Longitude + '" name="' + items[i].Name + '" dest="' + items[i].Destination + '" phone="' + items[i].Phone + '">' + items[i].Name + '</a></td>';
                html += '<td>' + items[i].Address1 + '</td>';
                html += '<td>' + items[i].City + '</td>';
                html += '<td>' + items[i].Region + '</td>';
                html += '<td>' + items[i].PostalCode + '</td>';
                html += '</tr>';
            }
            $('#myDealers').append(html);
            $('#loader').html('');
        }
        else {
            $('#loader').html('No dealers found.');
        }

        return false;
			
			
			
            },
            error: function(xhr, status, error) {
				
                alert(xhr.responseText);
                var err = eval("(" + xhr.responseText + ")");
                alert(err.Message)
            }
        });
    },


    getClosestDistributorByState: function(state) {

        $.ajax({
            type: "GET",
            //data: "{'state':'" + state + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            cache: false,
            beforeSend: function() {
                $('#loader').removeClass('hidden');
            },
            url: "/aform/locateResellers.php?state=" + state,
            success: function(result) {
            
			items = result;
				

        $('#myDealers tbody tr').remove();

        if (items.length > 0) {
            var html = '';

            for (var i = 0; i < items.length; i++) {
                html += '<tr>';
                html += '<td><strong>' + items[i].Name + '</strong></td>';
                //				html += '<td><a href="#" class="map" lat="' + items[i].Latitude + '" long="' + items[i].Longitude + '" name="' + items[i].Name + '" dest="' + items[i].Destination + '" phone="' + items[i].Phone + '">' + items[i].Name + '</a></td>';
                html += '<td>' + items[i].Address1 + '</td>';
                html += '<td>' + items[i].City + '</td>';
                html += '<td>' + items[i].Region + '</td>';
                html += '<td>' + items[i].PostalCode + '</td>';
                html += '</tr>';
            }
            $('#myDealers').append(html);
            $('#loader').html('');
        }
        else {
            $('#loader').html('No dealers found.');
        }

        return false;				
				
				
            },
            error: function(xhr, status, error) {
                alert(xhr.responseText);
                var err = eval( xhr.responseText );
                alert(status);
            }
        });
    },

    populateClosestDistributorByState: function(items) {
        html += '<thead>';
        html += '<tr>';
        html += '	<th>Store Name</th>';
        html += '	<th>City</th>';
        html += '	<th>State</th>';
        html += '   <th>Zip</th>';
        html += '</tr>';
        html += '</thead>';



        //alert(items.length);
        $('#myDealers tbody tr').remove();

        if (items.length > 0) {
            var html = '';

            for (var i = 0; i < items.length; i++) {
                html += '<tr>';
                html += '<td><strong>' + items[i].Name + '</strong></td>';
                //				html += '<td><a href="#" class="map" lat="' + items[i].Latitude + '" long="' + items[i].Longitude + '" name="' + items[i].Name + '" dest="' + items[i].Destination + '" phone="' + items[i].Phone + '">' + items[i].Name + '</a></td>';
                html += '<td>' + items[i].Address1 + '</td>';
                html += '<td>' + items[i].City + '</td>';
                html += '<td>' + items[i].Region + '</td>';
                html += '<td>' + items[i].PostalCode + '</td>';
                html += '</tr>';
            }
            $('#myDealers').append(html);
            $('#loader').html('');
        }
        else {
            $('#loader').html('No dealers found.');
        }

        return false;
    },

    populateClosestDistributors: function(items) {
        html += '<thead>';
        html += '<tr>';
        html += '	<th>Store Name</th>';
        html += '	<th>City</th>';
        html += '	<th>State</th>';
        html += '   <th>Zip</th>';
        html += '</tr>';
        html += '</thead>';

        //alert(items.length);
        $('#myDealers tbody tr').remove();

        if (items.length > 0) {
            var html = '';

            for (var i = 0; i < items.length; i++) {
                html += '<tr>';
                html += '<td><strong>' + items[i].Name + '</strong></td>';
                //              html += '<td><a href="#" class="map" lat="' + items[i].Latitude + '" long="' + items[i].Longitude + '" name="' + items[i].Name + '" dest="' + items[i].Destination + '" phone="' + items[i].Phone + '">' + items[i].Name + '</a></td>';
                html += '<td>' + items[i].Address1 + '</td>';
                html += '<td>' + items[i].City + '</td>';
                html += '<td>' + items[i].Region + '</td>';
                html += '<td>' + items[i].PostalCode + '</td>';
                html += '</tr>';
            }
            $('#myDealers').append(html);
            $('#loader').html('');
        }
        else {
            $('#loader').html('No dealers found.');
        }

        return false;
    }
}

	function setCartTotal()
	{
		


		if (window.XMLHttpRequest)
		{// code for IE7+, Firefox, Chrome, Opera, Safari
			xmlhttp=new XMLHttpRequest();
		}

		else

		{// code for IE6, IE5
			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		}

		xmlhttp.open("POST","/aform/cartTotal.php",true);
		xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlhttp.setRequestHeader("Content-length", 0);
		xmlhttp.setRequestHeader("Connection", "close");

		xmlhttp.onreadystatechange=function()
		{
			if (xmlhttp.readyState==4 && xmlhttp.status==200)
			{
				var resp = xmlhttp.responseText;
				//alert(resp);
				
				document.getElementById("cartquantity").innerHTML = resp + "&nbsp;";
				
				
		//document.getElementById("inputform").style.display = "none";
   		//document.getElementById("thankyouform").style.display = "block";
		
			}
		}

		xmlhttp.send();
		return false;
	}

