﻿var stamp_type = "";
var stampTimer = null;
$("document").ready(function() {
    function getStamp() {
        if (stampTimer) {
            clearTimeout(stampTimer);
            stampTimer = null;
        }
        $.get("/handler/processStampSavingStream.ashx", { lastSeq: lastStampReq, stamp_type: stamp_type }, function(data) {
            try {
                eval('var ret = ' + data);
                if (ret.ret == 0) {
                    lastStampReq = ret.lastReq;
                    if (ret.data.length < 10) {
                        //If < 10, No more content available
                        $("#moreContentDiv").hide();
                    }
                    for (var i = 0; i < ret.data.length; i++) {
                        var stampObj = ret.data[i];
                        var stampDiv = GetStampObjDisplay(stampObj);
                        var stampWrapper = $('<div class="wboxnorm stampboxcover"><div class="wboxnormcont"></div></div>');
                        stampWrapper.find('div.wboxnormcont').append(stampDiv);
                        $("#stampStartDiv").after(stampWrapper);
                        stampWrapper.after($('<div class="ie6clearFix">&nbsp;</div>'));
                    }
                } else {
                    //ret = 2
                    if (lastStampReq == "") {
                        var divNoContent = $('.nomorecontent');
                        var spanLabel = divNoContent.find("span:eq(0)");
                        var aLink = divNoContent.find("a:eq(0)");
                        switch (stamp_type) {
                            case "stamp_salefind":
                                {
                                    spanLabel.text("sales");
                                    aLink.text("Submit your sale");
                                    aLink.attr('href', "/submit_sale.html");
                                    break;
                                }
                            case "stamp_coupon":
                                {
                                    spanLabel.text("coupons");
                                    aLink.text("Submit your coupon");
                                    aLink.attr('href', "/submit_coupon.html");
                                    break;
                                }
                            case "stamp_personalsale":
                                {
                                    spanLabel.text("personal sales");
                                    aLink.text("Submit your personal sale");
                                    aLink.attr('href', "/submit_personalsale.html");
                                    break;
                                }
                            case "stamp_freestuff":
                                {
                                    spanLabel.text("free stuff");
                                    aLink.text("Submit your free stuff");
                                    aLink.attr('href', "/submit_freestuff.html");
                                    break;
                                }
                            case "stamp_grocery":
                                {
                                    spanLabel.text("grocery deals");
                                    aLink.text("Submit your grocery deal");
                                    aLink.attr('href', "/submit_grocerydeal.html");
                                    break;
                                }
                            case "stamp_fsdeal":
                                {
                                    spanLabel.text("free shipping");
                                    aLink.text("Submit your free shipping");
                                    aLink.attr('href', "/submit_freeshipping.html");
                                    break;
                                }
                            case "stamp_savingtip":
                                {
                                    spanLabel.text("savings tip");
                                    aLink.text("Submit your saving tip");
                                    aLink.attr('href', "/submit_savingtip.html");
                                    break;
                                }
                            default:
                                {
                                    spanLabel.text("savings");
                                    aLink.text("Submit your savings");
                                    aLink.attr('href', "/submit_savings.html");
                                    break;
                                }
                        }
                        divNoContent.show();
                        $("#moreContentDiv").hide();
                    }
                    
                }
            } catch (e) {
            }
            stampTimer = setTimeout(getStamp, 5000);
        });
    };

    //$('#scoll').serialScroll({
    //    items:'ul',
    //    duration:2000, 
    //    force:true,
    //    axis:'y',
    //    easing:'linear',
    //    lazy:true,
    //    start: 0, // On which element (index) to begin ( 0 is the default, redundant in this case )
    //    cycle:true,
    //    interval:1000, // yeah! I now added auto-scrolling
    //    step:1, // scroll 1 news each time
    //    constant:true
    // }); 




    $("#showMore").click(function() {
        if (OldestStampId != 0) {
            $("#showMore").hide();
            $("#showMore").next().show();
            $.get("/handler/processStampSavingStream.ashx", { "load": "old", oldestId: OldestStampId, stamp_type: stamp_type }, function(data) {
                try {
                    eval('var ret = ' + data);
                    $("#showMore").next().hide();
                    if (ret.ret == 0) {
                        OldestStampId = ret.lastReq;
                        for (var i = 0; i < ret.data.length; i++) {
                            var stampObj = ret.data[i];
                            var stampDiv = GetStampObjDisplay(stampObj);
                            var stampWrapper = $('<div class="wboxnorm stampboxcover"><div class="wboxnormcont"></div></div>');
                            stampWrapper.find('div.wboxnormcont').append(stampDiv);
                            $("#moreContentDiv").before(stampWrapper);
                            stampWrapper.after($('<div class="ie6clearFix">&nbsp;</div>'));
                        }
                        $("#showMore").show();
                    } else {
                        //ret = 1
                        $("#moreContentDiv").hide();
                    }

                } catch (e) {
                }
            });
        }
    });

    for (var i = InitSavingStamp.length - 1; i >= 0; i--) {
        var stampObj = InitSavingStamp[i];
        var stampDiv = GetStampObjDisplay(stampObj);
        var stampWrapper = $('<div class="wboxnorm stampboxcover"><div class="wboxnormcont"></div></div>');
        stampWrapper.find('div.wboxnormcont').append(stampDiv);
        $("#stampStartDiv").after(stampWrapper);
        //stampWrapper.after($('<div class="ie6clearFix">&nbsp;</div>'));
    }

    stampTimer = setTimeout(getStamp, 5000);

    //For these small reload fuction
    $("#ulFreshStamp>li").click(function() {
        $("#stampStartDiv").nextAll().not("#moreContentDiv,.nomorecontent").remove();
        stamp_type = $(this).attr('stype');
        $(this).addClass('active');
        $(this).siblings().removeClass('active');

        $("#showMore").next().hide();
        $("#showMore").show();
        $("#moreContentDiv").show();
        $('.nomorecontent').hide();

        lastStampReq = "";
        getStamp();
    });


});
