$(document).ready(function() {
    $("a[href$='#track']").click(function(e) {
        var fullurl = $(this).attr("href");
        var filename = window.location.pathname + '/' + fullurl.substring(fullurl.lastIndexOf('/') + 1);
        pageTracker._trackPageview(filename);
        //alert(filename);
    });

    $("#slider").jCarouselLite({
        btnNext: ".next",
        btnPrev: ".prev",
        auto: 20000,
        speed: 1500,
        scroll: 3
    });
    $("#compare-slider").jCarouselLite({
        btnNext: ".next",
        btnPrev: ".prev",
        speed: 1500,
        scroll: 1,
        visible: 1
    });
    $("#product_showcase").removeClass('hide');
    $("#techmatch-showcase").removeClass('hide');
    $("#techmatch-showcase input.compare_products").click(function(e) {
        var selectedProductIDs = "";
        if ($(this).parent("div.product_item").find("input:checked").size() > 1) {
            $(this).parent("div.product_item").find("input:checked").each(function(i) { selectedProductIDs += $(this).val() + ","; });
            switch ($(this).attr("id")) {
                case 'compare_bi':
                    location.href = '/compareproducts.aspx?CurrentCat=9044&return=?&filterIds=&from=old&categoryId=9044&dirId=1&parentId=6710&productId=' + selectedProductIDs;
                    break;
                case 'compare_crm':
                    location.href = '/compareproducts.aspx?CurrentCat=8218&return=?&filterIds=&from=old&categoryId=8218&dirId=1&parentId=6710&productId=' + selectedProductIDs;
                    break;
                case 'compare_erp':
                    location.href = '/compareproducts.aspx?CurrentCat=7390&return=?&filterIds=&from=old&categoryId=7390&dirId=1&parentId=6710&productId=' + selectedProductIDs;
                    break;
                case 'compare_wireless':
                    location.href = '/compareproducts.aspx?CurrentCat=9898&return=?&filterIds=&from=old&categoryId=9898&dirId=1&parentId=9896&productId=' + selectedProductIDs;
                    break;
                case 'compare_sce':
                    location.href = '/compareproducts.aspx?CurrentCat=7296&return=?&filterIds=&from=old&categoryId=7296&dirId=1&parentId=6710&productId=' + selectedProductIDs;
                    break;                    
            }
        }
        else
            alert("Please select 2 or more products to compare.");
    });
    
    var validator = $("#aspnetForm").validate({
        rules: {
            channelnewsletter: { email: true }
        },
        messages: {
            channelnewsletter: "* please enter a valid email"
        },
        onsubmit: false,
        errorPlacement: function(error, element) {
            error.appendTo(element.parent());
        }
    });

    $("#channelnewsletter").keypress(function(e) {
        if (e.which == 13) {
            e.preventDefault();
            $("#subscribe_btn").click();
        }
    });
    $("#subscribe_btn").click(function() {
        if (validator.element("#channelnewsletter")) {
            $("#subscribe_btn").replaceWith("<span class='pleasewait'>Saving...<img class='loadingpic' src='/images/ajax-loader-s.gif' /></span>");
            $.get("/channel_ajax.aspx", { field: "newsletter", email: $("#channelnewsletter").val(), channel: channelName }, function(data) {
                $("#newsletterforms").html("<em>" + data + "</em>")
            });
        }
    });

    $("#expertbtn").click(function() {
        $("#expertbtn").replaceWith("<span class='pleasewait'>submitting question... <img class='loadingpic' src='/images/ajax-loader-s.gif' /></span>");
        $.get("/channel_ajax.aspx", { field: "askexpert", question: $("#askTextArea").val(), channel: channelName }, function(data) {
            $("#expertForm").html("<div id='expertsuccess'>" + data + "</div>")
        });
    });

    function initialize() {
        var feed = new google.feeds.Feed(feedurl);
        feed.setResultFormat(google.feeds.Feed.XML_FORMAT);

        feed.load(function(result) {
            if (!result.error) {
                var items = result.xmlDocument.getElementsByTagName("item");
                if (items.length > 0) {
                    $("#feed").append("<ul></ul>");
                    for (var i = 0; i < items.length; i++) {
                        var authorElement = google.feeds.getElementsByTagNameNS(items[i], "http://purl.org/dc/elements/1.1/", "creator")[0];
                        var author = authorElement.firstChild.nodeValue;

                        $("#feed ul").append("\
					    <li>\n\
						    <h2><a href='" + items[i].getElementsByTagName("link")[0].firstChild.nodeValue + "'>" + items[i].getElementsByTagName("title")[0].firstChild.nodeValue + "</a></h2>\n\
						    <span class='byline'>by " + author + "</span>\n\
						    <p>" + items[i].getElementsByTagName("description")[0].firstChild.nodeValue + "</p>\n\
						    <a class='readmore' href='" + items[i].getElementsByTagName("link")[0].firstChild.nodeValue + "'>Read Post</a>\n\
					    </li>\n");

                        var commentsElement = google.feeds.getElementsByTagNameNS(items[i], "http://purl.org/rss/1.0/modules/slash/", "comments")[0];
                        var comments = commentsElement.firstChild.nodeValue;
                        if (comments > 0)
                            $("#feed li:last").append("<span class='comments'> | Comments [ <a class='comments' href='" + items[i].getElementsByTagName("comments")[0].firstChild.nodeValue + "'>" + comments + "</a> ]</span>\n");
                    }
                }
            }
        });
    }
    google.setOnLoadCallback(initialize);

});		 

