﻿
var cookieid = "techdaysOnline_rating";
var newMemebr = 0;
var isSignIn = 0;
var signInType = "";
$(function () {

    $(".ratingId").each(function () {
        var ratingInfo = $.trim($(this).text()).split(",");
        var id = ratingInfo[0];

        var rating = 0;
        if (ratingInfo.length > 1) {

            rating = ratingInfo[1];
        }

        setupRating(id, rating);
        if (can_rating(id)) {
            $('.rating_' + id).addClass("canRate");

            var fullBoxClass = "full-box-blue";

            if ($('.rating_' + id).hasClass("white-bg")) {
                fullBoxClass = "full-box-white";
            }


            $('.rating_' + id).hover(function () {

                //On hover: make all div empty
                $('.rating_' + id + ' .div1').addClass("empty-box");
                $('.rating_' + id + ' .div2').addClass("empty-box");
                $('.rating_' + id + ' .div3').addClass("empty-box");
                $('.rating_' + id + ' .div4').addClass("empty-box");
                $('.rating_' + id + ' .div5').addClass("empty-box");


                //Add hover to all five divs
                $('.rating_' + id + ' .div1').hover(function () {
                    $('.rating_' + id + ' .div1').removeClass("empty-box").addClass(fullBoxClass);
                    $('.rating_' + id + ' .div2').removeClass(fullBoxClass).addClass("empty-box"); ;
                    $('.rating_' + id + ' .div3').removeClass(fullBoxClass).addClass("empty-box"); ;
                    $('.rating_' + id + ' .div4').removeClass(fullBoxClass).addClass("empty-box"); ;
                    $('.rating_' + id + ' .div5').removeClass(fullBoxClass).addClass("empty-box"); ;
                    //on hover of the div fill everything to the left

                });
                $('.rating_' + id + ' .div2').hover(function () {
                    $('.rating_' + id + ' .div1').removeClass("empty-box").addClass(fullBoxClass);
                    $('.rating_' + id + ' .div2').removeClass("empty-box").addClass(fullBoxClass);
                    $('.rating_' + id + ' .div3').removeClass(fullBoxClass).addClass("empty-box"); ;
                    $('.rating_' + id + ' .div4').removeClass(fullBoxClass).addClass("empty-box"); ;
                    $('.rating_' + id + ' .div5').removeClass(fullBoxClass).addClass("empty-box"); ;
                    //on hover of the div fill everything to the left

                });
                $('.rating_' + id + ' .div3').hover(function () {
                    $('.rating_' + id + ' .div1').removeClass("empty-box").addClass(fullBoxClass);
                    $('.rating_' + id + ' .div2').removeClass("empty-box").addClass(fullBoxClass);
                    $('.rating_' + id + ' .div3').removeClass("empty-box").addClass(fullBoxClass);
                    $('.rating_' + id + ' .div4').removeClass(fullBoxClass).addClass("empty-box"); ;
                    $('.rating_' + id + ' .div5').removeClass(fullBoxClass).addClass("empty-box"); ;
                    //on hover of the div fill everything to the left

                });
                $('.rating_' + id + ' .div4').hover(function () {
                    $('.rating_' + id + ' .div1').removeClass("empty-box").addClass(fullBoxClass);
                    $('.rating_' + id + ' .div2').removeClass("empty-box").addClass(fullBoxClass);
                    $('.rating_' + id + ' .div3').removeClass("empty-box").addClass(fullBoxClass);
                    $('.rating_' + id + ' .div4').removeClass("empty-box").addClass(fullBoxClass);
                    $('.rating_' + id + ' .div5').removeClass(fullBoxClass).addClass("empty-box"); ;
                    //on hover of the div fill everything to the left

                });
                $('.rating_' + id + ' .div5').hover(function () {
                    $('.rating_' + id + ' .div1').removeClass("empty-box").addClass(fullBoxClass);
                    $('.rating_' + id + ' .div2').removeClass("empty-box").addClass(fullBoxClass);
                    $('.rating_' + id + ' .div3').removeClass("empty-box").addClass(fullBoxClass);
                    $('.rating_' + id + ' .div4').removeClass("empty-box").addClass(fullBoxClass);
                    $('.rating_' + id + ' .div5').removeClass("empty-box").addClass(fullBoxClass);
                    //on hover of the div fill everything to the left

                });
            },
                function () {

                    //On hover: make all div empty
                    $('.rating_' + id + ' .div1').removeClass("empty-box full-box-blue full-box-white");
                    $('.rating_' + id + ' .div2').removeClass("empty-box full-box-blue full-box-white");
                    $('.rating_' + id + ' .div3').removeClass("empty-box full-box-blue full-box-white");
                    $('.rating_' + id + ' .div4').removeClass("empty-box full-box-blue full-box-white");
                    $('.rating_' + id + ' .div5').removeClass("empty-box full-box-blue full-box-white");
                }
            );



        }
        else {

            removeOnClickBinding(id);
        }
    });
});

function setupRating(docId, rating) {
    if (!$('.rating_' + docId).hasClass("rating-setup")) {

        var rateBoxClass = "rating-box-blue";

        if ($('.rating_' + docId).hasClass("white-bg")) {
            rateBoxClass = "rating-box-white";
        }

        var regBox = "rating-box";
        var mydivs = "";

        var boxes = new Array();

        boxes[0] = (rating > 0) ? rateBoxClass : regBox;
        boxes[1] = (rating > 1) ? rateBoxClass : regBox;
        boxes[2] = (rating > 2) ? rateBoxClass : regBox;
        boxes[3] = (rating > 3) ? rateBoxClass : regBox;
        boxes[4] = (rating > 4) ? rateBoxClass : regBox;

        mydivs = "<div class='div1 " + boxes[0] + "'" + " onclick='javascript:rateThis" + '("' + docId + '","1");' + "'>" + "</div> ";
        mydivs += "<div class='div2 " + boxes[1] + "'" + " onclick='javascript:rateThis" + '("' + docId + '","2");' + "'>" + "</div> ";
        mydivs += "<div class='div3 " + boxes[2] + "'" + " onclick='javascript:rateThis" + '("' + docId + '","3");' + "'>" + "</div> ";
        mydivs += "<div class='div4 " + boxes[3] + "'" + " onclick='javascript:rateThis" + '("' + docId + '","4");' + "'>" + "</div> ";
        mydivs += "<div class='div5 " + boxes[4] + "'" + " onclick='javascript:rateThis" + '("' + docId + '","5");' + "'>" + "</div> ";

        $('.rating_' + docId).html(mydivs);

        $('.rating_' + docId).addClass("rating-setup");
    }
}

function removeOnClickBinding(docId) {
    $('.rating_' + docId + ' .div1').attr('onclick', '').unbind('click');
    $('.rating_' + docId + ' .div2').attr('onclick', '').unbind('click');
    $('.rating_' + docId + ' .div3').attr('onclick', '').unbind('click');
    $('.rating_' + docId + ' .div4').attr('onclick', '').unbind('click');
    $('.rating_' + docId + ' .div5').attr('onclick', '').unbind('click');

}

function removeOnHoverBinding(docId) {
    $('.rating_' + docId + ' .div1').unbind('mouseenter mouseleave');
    $('.rating_' + docId + ' .div2').unbind('mouseenter mouseleave');
    $('.rating_' + docId + ' .div3').unbind('mouseenter mouseleave');
    $('.rating_' + docId + ' .div4').unbind('mouseenter mouseleave');
    $('.rating_' + docId + ' .div5').unbind('mouseenter mouseleave');
}

function rateThis(documentId, ratingCount) {
    if (can_rating(documentId)) {


        var param = { 'docId': documentId, 'rating': ratingCount };
        var serviceUrl = "/umbraco/webservices/techdaysonlinewebservice.asmx/AddRating";
        $.ajax({
            type: "POST",
            url: serviceUrl,
            data: JSON.stringify(param),
            contentType: "application/json;charset=utf-8",
            dataType: "json",
            success: function (data) {
                var newRating = parseInt(data.d);
                //remove canRate class and unbind the hover
                $('.rating_' + documentId).removeClass("canRate");
                $('.rating_' + documentId).unbind('mouseenter mouseleave')

                var rateBoxClass = "rating-box-blue";

                if ($('.rating_' + documentId).hasClass("white-bg")) {
                    rateBoxClass = "rating-box-white";
                }

                $('.rating_' + documentId + ' .div1').removeClass().addClass("rating-box div1");
                $('.rating_' + documentId + ' .div2').removeClass().addClass("rating-box div2");
                $('.rating_' + documentId + ' .div3').removeClass().addClass("rating-box div3");
                $('.rating_' + documentId + ' .div4').removeClass().addClass("rating-box div4");
                $('.rating_' + documentId + ' .div5').removeClass().addClass("rating-box div5");
                removeOnClickBinding(documentId);
                removeOnHoverBinding(documentId);

                switch (newRating) {
                    case 5:
                        $('.rating_' + documentId + ' .div5').removeClass().addClass(rateBoxClass + " div5");
                    case 4:
                        $('.rating_' + documentId + ' .div4').removeClass().addClass(rateBoxClass + " div4");
                    case 3:
                        $('.rating_' + documentId + ' .div3').removeClass().addClass(rateBoxClass + " div3");
                    case 2:
                        $('.rating_' + documentId + ' .div2').removeClass().addClass(rateBoxClass + " div2");
                    case 1:
                        $('.rating_' + documentId + ' .div1').removeClass().addClass(rateBoxClass + " div1");
                        break;

                }

                set_rating_cookie(documentId);
            },
            error: function (e) {

            }
        });
    }
}
function can_rating(documentId) {
    var val = "";
    //if($.cookie(cookieid)!=null){
    if (GetCookie(cookieid) != null) {
        //val = $.cookie(cookieid);
        val = GetCookie(cookieid);
    }
    return $.inArray(documentId, val.split(',')) < 0;
}
function set_rating_cookie(documentId) {
    var val = "";
    //   if($.cookie(cookieid)!=null){
    //    val = $.cookie(cookieid);
    //   }
    //    $.cookie(cookieid, (val==""?"" : val + ",") + documentId, { expires: 9000});
    if (GetCookie(cookieid) != null) {
        //val = $.cookie(cookieid);
        val = GetCookie(cookieid);
    }
    SetCookie(cookieid, (val == "" ? "" : val + ",") + documentId);
}





function GetCookie(sName) {
    // cookies are separated by semicolons
    var aCookie = document.cookie.split("; ");
    for (var i = 0; i < aCookie.length; i++) {
        // a name/value pair (a crumb) is separated by an equal sign
        var aCrumb = aCookie[i].split("=");
        if (sName == aCrumb[0])
            return unescape(aCrumb[1]);
    }
    // a cookie with the requested name does not exist
    return null;
}

// Create a cookie with the specified name and value.
function SetCookie(sName, sValue, sExpire) {
    document.cookie = sName + "=" + escape(sValue);
    // Expires the cookie in one month
    var date;
    if (!sExpire) {
        date = new Date();
        date.setMonth(date.getMonth() + 1);
    }
    else {
        date = new Date(sExpire);
    }
    document.cookie += ("; expires=" + date.toUTCString());
}

// Delete the cookie with the specified name.
function DelCookie(sName) {
    document.cookie = sName + "=; expires=Fri, 31 Dec 1999 23:59:59 GMT;";
}


// HOMEPAGE TWITTER CONTROL
function TwitterControl(title, searchTwit) {
    TwitterControlDetailed(title, '', searchTwit, 'FFFFFF', 'e9632d', 'FFFFFF', '02aedb', 'e9632d', 1, 1);
}

function TwitterSpeakerControl(title, searchTwit) {
    TwitterControlDetailed(title, '', searchTwit, 'FFFFFF', 'e9632d', 'FFFFFF', '02aedb', 'e9632d', 1, 1);
}

function TwitterControlDetailed(title, caption, searchTwit, shell_background, shell_color, tweets_background, tweets_color, tweets_link, allow_scrollbar,
    allow_looping) {

    searchTwit = searchTwit.replace(' ', ' OR ');
    var scroll = false;
    if (allow_scrollbar != 0) {
        scroll = true;
    }
    var looping = false;
    if (allow_looping != 0) {
        looping = true;
    }

    new TWTR.Widget({
        version: 2,
        type: 'search',
        search: searchTwit,
        interval: 30000,
        title: title,
        subject: caption,
        width: 'auto',
        height: 300,
        theme: {
            shell: {
                background: '#' + shell_background,
                color: '#' + shell_color
            },
            tweets: {
                background: '#' + tweets_background,
                color: '#' + tweets_color,
                links: '#' + tweets_link
            }
        },
        features: {
            scrollbar: scroll,
            loop: looping,
            live: true,
            behavior: 'default'
        }
    }).render().start();


}
function stopVideo() {
    $('div.videoPlayer').empty();
    $('div.videoPlayer').hide();
}

function ChangeVideo(videoUrl) {
    $('div.videoPlayer').empty();
    var playerStr = "<div class='videoPlayer'> <a href=" + '"' + "javascript:stopVideo();" + '"' + "><div class='closeButton flt-right'>CLOSE</div></a><object data='data:application/x-silverlight-2' width='600' type='application/x-silverlight-2' height='338'>";
    playerStr = playerStr + "<param name='source' value='/ClientBin/VideoPlayer.xap'>";
    playerStr = playerStr + "<param name='background' value='Transparent'>";
    playerStr = playerStr + "<param id='initParams' name='initParams' value='m=" + videoUrl + ",autostart=true'>";
    playerStr = playerStr + "<param name='minruntimeversion' value='2.0.31005.0'>";
    playerStr = playerStr + "<param name='windowless' value='true'>";
    playerStr = playerStr + "<a style='text-decoration: none;' href='http://go.microsoft.com/fwlink/?LinkId=124807'>";
    playerStr = playerStr + "<img style='' alt='Get Microsoft Silverlight' src='http://go.microsoft.com/fwlink/?LinkId=108181'>";
    playerStr = playerStr + "</a></div>";

    $('div.videoPlayer').replaceWith(playerStr);

}



$(document).ready(function () {
    $('.chkTechnologyTypes input').click(function () {
        CheckBoxFilters($(this),'chkTechnologyTypes');
    });

    $('.chkProductTypes input').click(function () {
        CheckBoxFilters($(this), 'chkProductTypes');
    });


    $('.chkContentType input').click(function () {
        CheckBoxFilters($(this), 'chkContentType');
    });

    $('.chkAudienceTypes input').click(function () {
        CheckBoxFilters($(this), 'chkAudienceTypes');
    });


    $('.chkLocation input').click(function () {
        CheckBoxFilters($(this), 'chkLocation');
    });


    $('.chkYear input').click(function () {
        CheckBoxFilters($(this), 'chkYear');
    });


    $('.chkTags input').click(function () {
        CheckBoxFilters($(this), 'chkTags');
    }); 

    

});

function CheckBoxFilters(options, checkboxClass) {
    if (options.is(':checked')) {
        var whatWasChecked = options[0].id;
        var oldValue = checkboxClass;
        var newValue = 'hdnName';
        var newHiddenValue = '';

        newHiddenValue = whatWasChecked.replace(oldValue, newValue)

        if ($('#' + newHiddenValue).val() == 'all') {
            $('.'+checkboxClass+' input').attr('checked', false);
            $('#' + whatWasChecked).attr('checked', true);
        } else {
            $('.' + checkboxClass + ' input:checked').each(function () {
                if (this.checked) {
                    if ($('#' + $(this)[0].id.replace(oldValue, newValue)).val() == 'all') {
                         $(this).attr('checked',false);                        
                        return false;
                    }
                }
            });
        }
    }
}

