﻿var j$ = jQuery.noConflict();


function load_ViewDetail(obj)
{
    var eventID = j$("input.inputEventID").val();  
    
    
    function createCookie(name, value, expiryDays)
    {
        var expireDate = new Date(new Date().getTime() + (expiryDays * 86400000));
        document.cookie = name + "=" + value + "; expires=" + expireDate.toGMTString() + "; path=/;";
    }


    function cookieExists(name)
    {
        if (document.cookie.indexOf(name + "=") >= 0) { return true; }
        else { return false; }
    }
    
    
    if (cookieExists('EventDigg' + eventID))
    {
        var diggCountLink = j$("a.lnkDiggCount");
        diggCountLink.parent().append("<span class='goingToEvent'>You are going.</span>");
        diggCountLink.remove();
    }
    else
    {    
        j$("a.lnkDiggCount").click(function(event) {
            event.preventDefault();
            
            var link = j$(this);
            
            if (typeof(obj) === "undefined") { obj = new ViewDetail(); }            
            
            obj.IncrementDiggCount(eventID, false, function(result, context, methodName) {
                link.parent().parent().effect("highlight", {}, 1000);
                
                // Check for the 'EventDigg' cookie.
                if (!cookieExists('EventDigg' + eventID))
                {
                    createCookie("EventDigg" + eventID, "true", 10);   
                    
                    var digCountLabel = j$("span.lbldiggCount");
                    var currentCount = parseInt(digCountLabel.text());
                    digCountLabel.text(currentCount + 1);
                    
                    link.parent().append("<span class='goingToEvent'>You are going.</span>");
                    link.remove();
                }
            }, function(result) { alert(result); });
        });
    }
    
    
    var addToItinerary = function(event) {
        event.preventDefault();         
        var link = j$(this);
        
        if (typeof(obj) === "undefined") { obj = new ViewDetail(); }
        
        obj.AddEventToItinerary(eventID, false, function(result, context, methodName) {
            link.parent().effect("highlight", {}, 1000);
            link.parent().append("<span class='lblItineraryAdded'></span>");
            link.remove();
        }, function(result) { link.click(addToItinerary); });
        
        link.unbind("click", addToItinerary).click(function(event) { event.preventDefault(); });
    };
    
    j$("a.lnkItineraryAdd").click(addToItinerary);
}