﻿/// <reference path="jquery-1.4.1-vsdoc.js" />
var IsUserAuthenticatedToWebinar = false;
(function ($) {


$(document).ready(function () {

    jQuery.each(jQuery.browser, function (i) {
        if ($.browser.msie) {
            $(".formular .row .inputHolder").corner("5px");
        } else {
            $(".formular .row .inputHolder").corner("2px");
        }
    });

    $("a:not(a[href])").each(function (i, e) {
        var $e = $(e);
        var span = $("<span></span>");
        var a = $("<a name=\"" + $e.attr("name") + "\"></a>");
        span.html($e.html());
        span.insertAfter($e);
        a.insertBefore(span);
        $e.remove();
    });


    $(".addAttendee a").click(function (e) {
        e.preventDefault();
        addAttendee();
    });


    function addAttendee(i) {
        var addAttendee = $(".addAttendee a").parent(".addAttendee");
        var attendees = addAttendee.parent(".section").find(".attendee");


        var attendee = $("<div>" + $(attendees.get(0)).html() + "</div>");


        if (i != 0) {
            attendee.find("strong:first").html(attendee.find("strong:first").html().substring(0, attendee.find("strong:first").html().lastIndexOf(" ")) + " " + (attendees.size() + 1));
            attendee.find(".attendeeName").attr("name", "attendee" + (attendees.size() + 1) + "Name");
            attendee.find(".attendeeMail").attr("name", "attendee" + (attendees.size() + 1) + "Mail");
            attendee.find(".attendeeCompany").attr("name", "attendee" + (attendees.size() + 1) + "Company");
            var a = $("<div class=\"attendee\">" + attendee.html() + "</div>");
        }

        $("#attendeeCount").val(attendees.size() + 1);
       
        if (i == undefined)
            a.css({ opacity: 0, height: 0 }).animate({ opacity: 1, height: $(attendees.get(0)).outerHeight() + 4 }, 300, "easeout");

        if (i != 0) {
            $(addAttendee).before(a);
        }
        if (attendeeData == null) {

            a.find(".attendeeName").get(0).setAttribute('value', "");
            a.find(".attendeeMail").get(0).setAttribute('value', "");
            a.find(".attendeeCompany").get(0).setAttribute('value', $(attendees.get(0)).find(".attendeeCompany").attr("value"));
        } else {
            if (attendeeData.length >= attendees.size() + 1) {
               
                a.find(".attendeeName").get(0).setAttribute('value', attendeeData[attendees.size()].Name);
                a.find(".attendeeMail").get(0).setAttribute('value', attendeeData[attendees.size()].Mail);
                a.find(".attendeeCompany").get(0).setAttribute('value', attendeeData[attendees.size()].Company);
            } else {
                a.find(".attendeeName").get(0).setAttribute('value', "");
                a.find(".attendeeMail").get(0).setAttribute('value', "");
                a.find(".attendeeCompany").get(0).setAttribute('value', $(attendees.get(0)).find(".attendeeCompany").attr("value"));
            }
        }

        if (attendees.size() >= 7)
            addAttendee.hide();
    }

    var ToolTip = $("#ToolTip");
    $(".generateLinks a:not(.noGenerate):not(:has(img))").each(function (index, elm) {
        var $this = $(elm);
        $this.addClass("generatedLink");
        var span = $("<span></span>").html($this.html())
        $this.html(span);
        $this.mouseover(function () {
            span.addClass("hover");
        });
        $this.mouseout(function () {
            span.removeClass("hover");
        });
    });
    $("#BoxRight .box:first").addClass("first");
    $("#BoxRight .box:last").addClass("last");

    $("#Webinars .webinarHolder .webianarRow .info .login .button").live("mouseover mouseout", function () { $(this).toggleClass("hover") });

    $("#Webinars .showForgotPassword").live("click", function (e) {
        e.preventDefault();
        var $this = $(this);
        $this.parent().next(".forgotPassword").slideToggle("fast");
    });

    $("#Webinars .forgotPassword .btnGray").live("click", function (e) {
        e.preventDefault();
        var $this = $(this);
        var parent = $this.parents(".forgotPassword");
        $.ajax({
            url: '/Ajax/Targit/ForgotPassword.ashx',
            data: { username: parent.find(".mail").val() },
            dataType: "json",
            type: "POST",
            success: function (data) {
                if (data) {
                    parent.find(".mailSent").show();
                    parent.find(".mailNotSent").hide();
                } else {
                    parent.find(".mailNotSent").show();
                    parent.find(".mailSent").hide();
                }
            }
        });

    });

    $("#FooterSections a, #ContentSubPageContent a, #HeaderMenu a").live("mousemove", function (e) {
        ToolTip.css({ top: e.pageY, left: e.pageX });
    });
    var tooltipelm;
    $("#FooterSections a, #ContentSubPageContent a, #HeaderMenu a").live("mouseover", function (e) {

        if ($(this).attr("title") == "")
            return;

        tooltipelm = this;
        e.preventDefault();
        ToolTip.show();
        ToolTip.children(".middle").html($(this).attr("title"));
        $(this).attr("title", "");

    });
    $("#FooterSections a, #ContentSubPageContent a, #HeaderMenu a").live("mouseout", function (e) {

        $(tooltipelm).attr("title", ToolTip.children(".middle").html());

        ToolTip.hide();
    });

    $(".button").mouseover(function () {
        $(this).addClass("hover");
    });
    $(".button").mouseout(function () {
        $(this).removeClass("hover");
    });

    $("#ContentFrontPageBottomLinks div.linkContainer").mouseover(function () 
    {
        $(this).data("hover", $(this).css("background-image"));

        $(this).css("background-image", "url('" + $(this).find(".hoverimage").attr("src") + "')");
    });
    $("#ContentFrontPageBottomLinks div.linkContainer").mouseout(function () {

        $(this).css({ "background-image": $(this).data("hover") });
    });

    $("#ContentSubPageMenu ul li a").mouseover(function () {
        $(this).parent().addClass("hover");
    });
    $("#ContentSubPageMenu ul li a").mouseout(function () {
        $(this).parent().removeClass("hover");
    });

    $(".navigationpos").each(function (index, elm) {
        var count = $(this).find("a").size();
        $(this).width((count * 9) + ((count) * 10));
    });
    $("#FooterNewsletterForm input.inputButton").mouseover(function () {
        $(this).addClass("hover");
    });
    $("#FooterNewsletterForm input.inputButton").mouseout(function () {
        $(this).removeClass("hover");
    });

    $(".btnGray").live("mouseover", function () {

        $(this).addClass("hover");
    });
    $(".btnGray").live("mouseout", function () {
        $(this).removeClass("hover");
    });

    $(".imageBorder").each(function (index, elm) {
        var $this = $(this);
        var img = $(this).find("img");
        if (img.size() == 0)
            return;
        if (img.get(0).getAttribute("height") != img.height() || img.get(0).getAttribute("width") != img.width()) {
            $(this).addClass("zoom");
            $(this).find(".btnZoom").css("marginTop", img.height() / 2 - 10);
        }

        if (img.height() != 0 && img.width() != 0)
            $(this).height(img.height() + 10).width(img.width() + 10);

        img.load(function (e) {
            if (img.height() != 0 && img.width() != 0)
                $this.height(img.height() + 10).width(img.width() + 10);
        });

        if (img.attr("align") != "") {
            $(this).addClass(img.attr("align"));
            img.removeAttr("align");
        }
    });


    $("img").each(function (index, elm) {
        var $this = $(this);
        if ($this.attr("align") != "") {
            $this.addClass($this.attr("align"));
            $this.removeAttr("align");
        }
    });

    var timer = null;
    $("#SelectLanguage").click(function (e) {
        e.preventDefault();
        clearTimeout(timer);
        $("#LanguageSelector").show();
    });
    $("#SelectLanguage").mouseover(function (e) {
        clearTimeout(timer);
        $("#LanguageSelector").show();
    });
    $("#SelectLanguage").mouseout(function (e) {

        timer = setTimeout('$("#LanguageSelector").hide();', 1000);
    });
    $("#LanguageSelector").mouseover(function (e) {

        clearTimeout(timer);

    });
    $("#LanguageSelector").mouseout(function (e) {

        timer = setTimeout('$("#LanguageSelector").hide();', 1000);

    });
    var imageOverlay = $("#ImageOverlay");
    var imageOverlayHolder = $("#ImageOverlayHolder");
    $(".zoom .btnZoom").click(function () {

        imageOverlayHolder.css({ height: 100, width: 100, top: $(window).height() / 2 - 50, left: $(window).width() / 2 - 50, opacity: 0 }).show();
        imageOverlay.css({ opacity: 0 }).show();
        imageOverlay.animate({ opacity: 0.3 }, 300)
        imageOverlayHolder.animate({ opacity: 1 }, 200);
        var originalImage = $(this).parent().find("img");

        var newImage = $("<img />");
        newImage.hide();

        var newImageSrc = String(originalImage.attr("src")).substr(0, String(originalImage.attr("src")).indexOf("?"));

        newImage.attr("src", newImageSrc);

        imageOverlayHolder.append(newImage);
        newImage.load(function () {

            var newWidth = newImage.width() + 10;
            var newHeight = newImage.height() + 10;
            imageOverlayHolder.animate({ height: newHeight,
                width: newWidth,
                top: $(window).height() / 2 - newHeight / 2,
                left: $(window).width() / 2 - newWidth / 2
            }, 300, function () {
                newImage.css({ opacity: 0 }).show();
                newImage.animate({ opacity : 1 }, 200);
            });
        });
    });
    $("#ImageOverlay, #ImageOverlayHolder, #ImageOverlayHolder img").click(function () {
        imageOverlay.stop();
        imageOverlayHolder.stop();
        imageOverlayHolder.find("img").stop();
        imageOverlayHolder.html("");
        $("#ImageOverlay, #ImageOverlayHolder").animate({ opacity: 0 }, 300, function () {
            $(this).hide();
        });
    });

    $.fn.WebinarViewed = function(id)
    {
        $.ajax({
            url : '/Ajax/Targit/WebinarViewed.ashx',
            data : { id : id },
            dataType : "json",
            type : "GET"
        });
    };

    var videoOverlay = $("#VideoOverlay");
    var videoOverlayHolder = $("#VideoOverlayHolder");
    var videoOverlayClose = $("#VideoOverlayClose");
    
    
    $.fn.ShowVideo = function (video, width, height, image, shareDescription, shareLink) {
     
        shareLink = shareLink + "";
        shareLink  = shareLink.replace("&", "%26");

        videoOverlayHolder.css({ height: 100, width: 100, top: $(window).height() / 2 - 50, left: $(window).width() / 2 - 50, opacity: 0 }).show();
        videoOverlay.css({ opacity: 0 }).show();
        videoOverlay.animate({ opacity: 0.3 }, 300);
        videoOverlayHolder.animate({ opacity: 1 }, 200);

        //        // max width and height
        //        var newWidth = width == "" || width == undefined ? 1280 + 10 : width + 10;
        //        var newHeight = height == "" || height == undefined ? 720 + 10 : height + 10;

        //        if ($(window).width() < newWidth + 40) {
        //            var width2 = $(window).width() - 40;
        //            newHeight = (width2 / newWidth) * newHeight;
        //            newWidth = width2;
        //        }

        //        if ($(window).height() < newHeight + 40) {
        //            var height2 = $(window).height() - 40;
        //            newWidth = (height2 / newHeight) * newWidth;
        //            newHeight = height2;
        //        }

        var newWidth = 522;
        var newHeight = 298;
        videoOverlayHolder.animate({ height: newHeight,
            width: newWidth,
            top: $(window).height() / 2 - newHeight / 2,
            left: $(window).width() / 2 - newWidth / 2
        }, 300, function () {
            videoOverlayClose.css({ top: videoOverlayHolder.css("top"), left: ($(window).width() / 2 - newWidth / 2) + newWidth });
            videoOverlayClose.css({ opacity: 0 }).show().animate({ opacity: 1 }, 300)
            $("#VideoLightBox").css({ height: (newHeight - 10), width: (newWidth - 10) });
            if (image != null)
                $("#VideoLightBox").css({ backgroundImage: image });

            flowplayer("VideoLightBox", { src: "/Resources/Targit/Flash/Player/flowplayer.commercial-3.2.5.swf", wmode: "opaque" }, {
                key: "#$db55a9ea7f485c801ef",
                clip: {
               
                    autoPlay: true,
                    autoBuffering: true,
                    url: "/" + video + "?ext=.flv",
                    scaling: 'fit'
                },
                play: {
                    replayLabel: replayLabelText
                },
                onFinish: function () {
                    this.getPlugin("play").hide();
                    var videoLightBox = $("#VideoLightBox");
                   
                    $("#VideoFormForm").show();
                     $("#VideoFormSuccess").hide();
                    videoLightBox.animate({ opacity: 0 }, 300, function () {
                        videoLightBox.hide().css({ opacity: 1 });

                        var w = 310;
                        var h = 150;
                        videoOverlayClose.hide()
                        videoOverlayHolder.animate({ height: h,
                            width: w,
                            top: $(window).height() / 2 - h / 2,
                            left: $(window).width() / 2 - w / 2
                        }, 300, function () {
                            $("#VideoForm").css({ opacity: 0 }).show().animate({ opacity: 1 }, 300);
                            videoOverlayClose.css({ top: videoOverlayHolder.css("top"), left: ($(window).width() / 2 - w / 2) + w });
                            videoOverlayClose.css({ opacity: 0 }).show().animate({ opacity: 1 }, 300);
                        });

                    });
                },
                onStart: function () {


                },
                plugins: {
                    viral: {
                        url: '/Resources/Targit/Flash/Player/flowplayer.viralvideos-3.2.3.swf',
                     
                        share: {
                            description: shareDescription,
                            shareUrl: shareLink
                        }
                    },
                    gatracker: {
                        url: "/Resources/Targit/Flash/Player/flowplayer.analytics-3.2.1.swf",
                        labels: {
                            start: "Start",
                            play: "Play",
                            pause: "Pause",
                            resume: "Resume",
                            seek: "Seek",
                            stop: "Stop",
                            finish: "Finish",
                            mute: "Mute",
                            unmute: "Unmute",
                            fullscreen: "Full Screen",
                            fullscreenexit: "Full Screen Exit"
                        },
                        debug: false,
                        trackingMode: "AS3",
                        googleId: "UA-4941988-1" // your Google Analytics id here
                    }
                }
            });
        });


    }

    $.fn.ShowExternalVideo = function (id, image) {
    
        $.ajax({
            url: '/Ajax/Targit/ExternalVideo.ashx',
            data: { id: id },
            dataType: 'text',
            cache: false,
            success: function (data) {

                var video = $(data);
                var width = parseInt(video.attr('width'));
                var height = parseInt(video.attr('height'));

                videoOverlayHolder.css({ height: 100, width: 100, top: $(window).height() / 2 - 50, left: $(window).width() / 2 - 50, opacity: 0 }).show();
                videoOverlay.css({ opacity: 0 }).show();
                videoOverlay.animate({ opacity: 0.3 }, 300);
                videoOverlayHolder.animate({ opacity: 1 }, 200);
                
                var newWidth = width + 10;
                var newHeight = height + 10;

                videoOverlayHolder.animate({ height: newHeight,
                    width: newWidth,
                    top: $(window).height() / 2 - newHeight / 2,
                    left: $(window).width() / 2 - newWidth / 2
                }, 300, function () {
                    videoOverlayClose.css({ top: videoOverlayHolder.css("top"), left: ($(window).width() / 2 - newWidth / 2) + newWidth });
                    videoOverlayClose.css({ opacity: 0 }).show().animate({ opacity: 1 }, 300)
                    $("#VideoLightBox").html(data).css({ height: (newHeight - 10), width: (newWidth - 10), backgroundImage: image });
                });
            }
        });
    }

    $.fn.CloseVideo = function () {
   
        videoOverlay.stop();
        videoOverlayHolder.stop();
        videoOverlayClose.stop();
        //     videoOverlayHolder.html("<div id=\"VideoLightBox\"></div>");
        $("#VideoLightBox").remove();
        videoOverlayHolder.prepend("<div id=\"VideoLightBox\"></div>");
        $("#VideoOverlay, #VideoOverlayHolder, #VideoOverlayClose, #VideoForm").animate({ opacity: 0 }, 300, function () {
            $(this).hide();
        });

    }

    $("#VideoOverlay, #VideoOverlayClose").click(function () {
        $.fn.CloseVideo();
    });


    if ($("#Tabs").size() != 0) {
        var width = $("#Tabs ul li:first").offset().left - $("#Tabs ul li:last").offset().left;
        width = width + $("#Tabs ul li:first").width();
      
      $("#Tabs ul").css('cssText', 'width: ' + width + 'px !important; margin-left: -2px;');
        if($("#Tabs").hasClass("resellerTabs") == false){

       
                if ( jQuery.browser.safari ) {
                    $("#Tabs ul").css('cssText', 'width: ' + width + 'px !important; margin-left: -3px;');
                }
         }
        
    }
    $("#TabsTabs a").click(function (e) {
        e.preventDefault();
        $("#TabsTabs li").removeClass("active");
        var li = $(this).parent().addClass("active");
        $("#TabsTabs li").removeClass("prev");
        $(this).parent().next().addClass("prev");

        $("#TabsContent .content").hide();
        $("#TabsContent " + $(this).attr("href")).show();

        replaceArrow();
    });
    $("#TabsTabs a").hover(function (e) {
        e.preventDefault();
        $("#TabsTabs li").removeClass("hover");
        var li = $(this).parent().addClass("hover");
        $("#TabsTabs li").removeClass("prevHover");
        $(this).parent().next().addClass("prevHover");
    }, function (e) {
        e.preventDefault();
        $("#TabsTabs li").removeClass("prevHover");
        $("#TabsTabs li").removeClass("hover");
    });

    $("#TabsTabs li:last").addClass("first active");
    $("#TabsTabs li:first").addClass("last");
    $("#TabsContent .content:last").show();


    $(".webianarRow .readmore a").live("click", function (e) 
    {
        e.preventDefault();

        var $this = $(this);
        var webianarRow = $this.parents(".webianarRow");
        var info = webianarRow.find(".info");
        
        if (!$this.hasClass("active")) 
        {
            showHideMovie(info);

            var rightInfo = info.find(".infoRight");
            var infoPaddingBottom = 20;

            $this.addClass("active");
            webianarRow.addClass("showinfo");
            info.css("opacity", 0).show();

            rightInfo.css("min-height", info.height() - infoPaddingBottom);
            webianarRow.find(".thumbPlay").hide();
            
//            var h = info.height();
//            info.css("height", 0);
//            info.animate({ opacity: 1, height: h }, 300, function () {
//                info.css("height", "auto");
//                webianarRow.find(".thumbPlay").show();
//            });
//            
//            info.find(".infoRight").find("img").load(function()
//            {
//                info.css("height", "auto");
//                info.find(".infoRight").find('.link').css("height", "auto");
//                info.find(".infoRight").height(info.height());
//            });
            
            var h = info.height();
            info.css("height", 0);

            info.animate({ opacity: 1, height: h }, 300, function () 
            {
                info.css("height", "auto");
                rightInfo.css("min-height", info.height() - infoPaddingBottom);

                var imageHeight = rightInfo.find("img").height();
                var rightInfoHtml = rightInfo.html();

                if(imageHeight == 0)
                {
                    rightInfo.find("img").load(function() 
                    {
                        rightInfo.html(rightInfoHtml);
                        
                        imageHeight = $(this).height();

                        info.css("min-height", info.height() + imageHeight);
                        rightInfo.css("min-height", info.height() - infoPaddingBottom);
                    });
                }
            });
        } 
        else 
        {
            webianarRow.find(".thumbPlay").hide();
            info.animate({ opacity: 0, height: 0 }, 300, function () 
            {
                webianarRow.removeClass("showinfo");
                $this.removeClass("active");
                info.hide().css("height", "auto");
            });
        }
    });


    $(".thumbPlay").each(function (i, e) 
    {
        var $e = $(e);
        var img = $e.next("img");
        
        if(img.height() != 0 && img.width() != 0)
        {
            $e.css({ marginTop : (img.height() / 2) - $e.height() / 2, marginLeft : (img.width() / 2) - $e.width() / 2 });
        }
        img.load(function() 
        {
            $e.css({ marginTop : (img.height() / 2) - $e.height() / 2, marginLeft : (img.width() / 2) - $e.width() / 2 });
        });
    });

   


    $(".newsletterForm .inputButton").bind("click", function (e) {

        e.preventDefault();
        var form = document.createElement("form");
        var $form = $(form).hide();
        form.method = "POST";
        form.action = "http://visitor.constantcontact.com/d.jsp";
        form.target = "_blank";
        $form.append($(this).parents(".newsletterForm").html());

        $form.find(".inputText").get(0).value = $(this).siblings(".inputText").val();
        $("body").append(form);
        form.submit();
        $(form).remove();


    });

    if ($("#Slider:not(.noAutoStart)").size() != 0) {

        var sliderInterval = setInterval("$.fn.nextSlider()", 10000);
    }
    var sliders = $("#Sliders .sliderHolder .slider ");
    sliders.mouseover(function () {
        $(this).addClass("hover");
        clearTimeout(this.timeout);
    });
    sliders.mouseout(function () {
        if (this.id == "") {
            this.id = (new Date()).getTime();
        }
        this.timeout = setTimeout('$.fn.hideSlider("' + this.id + '")', 1);
    });
    $.fn.hideSlider = function(id)
    {
        $("#" + id).removeClass("hover");
    };
    sliders.click(function (e) {
        clearInterval(sliderInterval);
        var $this = $(this);
        sliders.removeClass("active");
        $this.addClass("active");
        var pos = $this.parent().prevAll(".sliderHolder").size();
        var content = $("#SliderContent .sliderContent").hide().get(pos);

        $(content).show();
        $.fn.placeImageInSlider($(content).find("img"));
        if (pos == 0)
            $("#SlidersTop").addClass("active");
        else
            $("#SlidersTop").removeClass("active");
    });

    var sliderContent = $($("#SliderContent .sliderContent").get(0)).show();
     $.fn.placeImageInSlider(sliderContent.find("img"));

    $.fn.nextSlider = function () {

        var active = $("#Sliders .sliderHolder .slider.active");
        active.removeClass("active");
        var pos = active.parent().prevAll(".sliderHolder").size() + 1;
        if (pos >= sliders.size())
            pos = 0;
        $(sliders.get(pos)).addClass("active");
        if (pos == 0)
            $("#SlidersTop").addClass("active");
        else
            $("#SlidersTop").removeClass("active");
        var content = $("#SliderContent .sliderContent").hide().get(pos);
         $(content).show();
         $.fn.placeImageInSlider($(content).find("img"));
       
    };




    $(".webianarRow .login .username, .webianarRow .login .password").live("keydown", function (e) {
        if (e.keyCode == 13) {
            var login = $(this).parents(".login");
            login.find(".button").click();
        }
    });

    $(".webianarRow .login .button:button ").live("click", function () {
        var $this = $(this);
        var login = $this.parents(".login");
        var username = login.find(".username:text").val();
        var password = login.find(".password:password").val();
        var remember = login.find("input:checkbox:checked").size();
        var wrongUser = login.find(".wrongUser");
        $.ajax({
            url: '/Ajax/Targit/WebinarLogin.ashx',
            data: { username: username, password: password, remember: remember },
            dataType: "json",
            type: "POST",
            success: function (data) {
                IsUserAuthenticatedToWebinar = data;
                if (IsUserAuthenticatedToWebinar == false)
                    wrongUser.show();
                showHideMovie($this.parents(".webianarRow").find(".info"));
            }
        });
    });


    $(".webinars .link a").click(function (e) 
    {
        e.preventDefault();
        var $this = $(this);
        var webinars = $this.parents(".webinars");
        $this.hide();

        var holder = webinars.find(".webinarHolder .holder");
        var stdHolder = webinars.find(".stdHolder").html();
        
        //webinars.find("select option:selected").removeAttr("selected");
        webinars.find("select").attr('selectedIndex', 0);


        var skip = "";

        if (stdHolder == "") 
        {
            skip = "&skip=0";
        }

        var currentLanguage = $("#CurrentLanguage").text();

        $.ajax({
            url: '/Ajax/Targit/Webinar.ashx?CurrentLanguage=' + currentLanguage + skip,
            data: { id: $this.attr("href").substring(1) },
            dataType: "json",
            success: function (data) 
            {
                //alert(data[2].Language);
                processList(holder.attr("id"), data, "#webinarTemplate");
            }
        });
    });

    $(".courses .link a").click(function (e) 
    {
        var location = $("#CourseCalendar .filter .location option:selected").val();
        var language = $("#CourseCalendar .filter .language option:selected").val();
        var country = $("#CourseCalendar .filter .country option:selected").val();

        e.preventDefault();
        var $this = $(this);
        var webinars = $this.parents(".courses");
        $this.hide();

        var holder = webinars.find(".courseHolder .holder");
        var stdHolder = webinars.find(".stdHolder").html();

        //webinars.find("select option:selected").removeAttr("selected");
        webinars.find("select").attr('selectedIndex', 0);

        var skip = "";

        if (stdHolder == "") 
        {
            skip = "?skip=0";
        }

        $.ajax({
            url: '/Ajax/Targit/Courses.ashx' + skip,
            data: { id: $this.attr("href").substring(1), sort: "", location: location, language: language, country: country },
            dataType: "json",
            success: function (data) 
            {
                processList(holder.attr("id"), data, "#CourseTemplate");
            }
        });
    });

    $(".news .link a").click(function (e) 
    {
        e.preventDefault();
        var $this = $(this);
        var news = $this.parents(".news");
        $this.hide();

        var holder = news.find(".newsHolder .holder");
        var stdHolder = news.find(".stdHolder").html();

        var skip = "";

        if (stdHolder == "") 
        {
            skip = "?skip=0";
        }

        $.ajax({
            url: '/Ajax/Targit/News.ashx' + skip,
            data: { id: $this.attr("href").substring(1) },
            dataType: "json",
            success: function (data) 
            {
                processList(holder.attr("id"), data, "#NewsTemplate");
            }
        });
    });


    $(".webinars .sort .language, .webinars .sort .usertype, .webinars .sort .region").change(function (e) 
    {
        var selected = $(this).find("option:selected");

        $(this).parents(".sort").find("option:selected").each(function (inde, elm) 
        {
            if ($(elm).val() != selected.val())
                $(elm).parent().attr("selectedIndex", 0);
            //$(elm).removeAttr("selected");
        });

        var $this = $(this);
        var webinars = $this.parents(".webinars");
        var region = webinars.find(".region option:selected").val();
        var usertype = webinars.find(".usertype option:selected").val();
        var language = webinars.find(".language option:selected").val();

        var link = webinars.find(".link a");


        if (region == "" && usertype == "" && language == "")
        {
            link.hide();
        } 
        else
        {
            link.show();
        }

        var holder = webinars.find(".webinarHolder .holder");

        var currentLanguage = $("#CurrentLanguage").text();

        $.ajax({
            url: '/Ajax/Targit/Webinar.ashx?CurrentLanguage=' + currentLanguage + '&skip=0',
            data: { id: link.attr("href").substring(1), region: region, language: language, usertype: usertype },
            dataType: "json",
            success: function (data) 
            {
                webinars.find(".stdHolder").html("");

                processList(holder.attr("id"), data, "#webinarTemplate");
            }
        });
    });


    $(".events .sort .location").change(function (e) 
    {
        var selected = $(this).find("option:selected");

        $(this).parents(".sort").find("option:selected").each(function (inde, elm) 
        {
            if ($(elm).val() != selected.val())
                $(elm).removeAttr("selected");
        });

        var $this = $(this);
        var events = $this.parents(".events");
        var location = events.find(".location option:selected").val();

        var link = events.find(".showhidelink a");


        if (location == "")
        {
            link.hide();
        } 
        else
        {
            link.show();
        }

        var holder = events.find(".eventHolder .holder");

        $.ajax({
            url: '/Ajax/Targit/Events.ashx?skip=0',
            data: { id: link.attr("href").substring(1), location: location },
            dataType: "json",
            success: function (data) {
                events.find(".stdHolder").html("");
                processList(holder.attr("id"), data, "#EventTemplate");
            }
        });
    });
    
    $(".courses .sort .sortOrder").change(function (e) 
    {
        var selected = $(this).find("option:selected");

        var $this = $(this);
        var courses = $this.parents(".courses");
        courses.find(".link").hide();
        var sort = courses.find(".sortOrder option:selected").val();

        var holder = courses.find(".courseHolder .holder");
        $.ajax({
            url: '/Ajax/Targit/Courses.ashx?skip=0',
            data: { id: courses.find(".id").val(), sort: sort, location: "Netherlands" },
            dataType: "json",
            success: function (data) 
            {
                courses.find(".stdHolder").html("");
                processList(holder.attr("id"), data, "#CourseTemplate");
            }
        });
    });

    $("#CourseCalendar .filter select.type").change(function (e) 
    {
        var type = $("#CourseCalendar .filter .type option:selected").val();
        var update = false;
        
        $("#CourseCalendar .courseSection").each(function (index, elm) 
        {
            var id = $(elm).find(".id").val();

            if (type != "") 
            {
                if (id != type)
                {
                    $(elm).hide();
                } 
                else 
                {
                    if (!$(elm).is(':visible')) 
                    {
                        $(elm).show();
                        update = true;
                    }
                }
            } 
            else 
            {
                $(elm).show();
                update = true;
            }

        });

        if (update) 
        {
            $.fn.updateCourses();
        }
    });
    $.fn.updateCourses = function()
    {
        var location = $("#CourseCalendar .filter .location option:selected").val();
        var language = $("#CourseCalendar .filter .language option:selected").val();
        var country = $("#CourseCalendar .filter .country option:selected").val();

        var type = $("#CourseCalendar .filter .type option:selected").val();

        $(".courses .link a").show();
        $("#CourseCalendar .courseSection:visible").each(function(index, elm) 
        {
            var id = $(elm).find(".id").val();
            var link = $(elm).find(".link a");
            var take = "showamount";
            if (link.is(":hidden"))
            {
                take = "";
            }
            
            var holder = $(elm).find(".courseHolder .holder");
            
            $.ajax({
                url : '/Ajax/Targit/Courses.ashx?skip=0',
                data : { id : id, take : take, sort : "date", location : location, language : language, country : country },
                dataType : "json",
                success : function(data)
                {
                    $(elm).find(".stdHolder").html("");
                    processList(holder.attr("id"), data, "#CourseTemplate");
                }
            });
        });
    };
    
    $("#CourseCalendar .filter select:not(.type)").change(function (e) 
    {
        $.fn.updateCourses();
    });

    $(".eventRow .readmore a").live("click", function (e) 
    {
        e.preventDefault();

        var $this = $(this);
        var eventRow = $this.parents(".eventRow");
        var info = eventRow.find(".info");
        if (!$this.hasClass("active")) 
        {
            showHideMovie(info);

            var rightInfo = eventRow.find(".infoRight");
            var infoPaddingBottom = 20;

            $this.addClass("active");
            eventRow.addClass("showinfo");
            info.css("opacity", 0).show();
            rightInfo.height(info.height() - infoPaddingBottom);
            //rightInfo.find('.link').height(info.height() - 74);

            //eventRow.find(".infoRight").find('.link').height(info.height() - 74);
            

            var h = info.height();
            info.css("height", 0);
            info.animate({ opacity: 1, height: h }, 300, function () 
            {
                rightInfo.height(info.height() - infoPaddingBottom);

                var imageHeight = rightInfo.find("img").height();
                var rightInfoHtml = rightInfo.html();

                if(imageHeight == 0)
                {
                    rightInfo.find("img").load(function() 
                    {
                        imageHeight = $(this).height();

                        rightInfo.html(rightInfoHtml);
                        info.height(info.height() + imageHeight);
                        rightInfo.height(info.height() - infoPaddingBottom);
                    });
                }
            });
        } 
        else 
        {
            info.animate({ opacity: 0, height: 0 }, 300, function () 
            {
                eventRow.removeClass("showinfo");
                $this.removeClass("active");
                info.hide().css("height", "auto");
            });
        }
    });

    $(".events .link a").click(function (e) 
    {
        e.preventDefault();
        var $this = $(this);
        var events = $this.parents(".events");
        $this.hide();

        var holder = events.find(".eventHolder .holder");
        var stdHolder = events.find(".stdHolder").html();

        events.find("select option:selected").removeAttr("selected");
        var skip = "";

        if (stdHolder == "") {
            skip = "?skip=0";
        }

        $.ajax({
            url: '/Ajax/Targit/Events.ashx' + skip,
            data: { id: $this.attr("href").substring(1) },
            dataType: "json",
            success: function (data) {
                processList(holder.attr("id"), data, "#EventTemplate");
            }
        });
    });
    
    $.fn.adjustHeightRightBox = function () 
    {
        if($("#BoxRight").size() != 0)
        {
            var top = $("#BoxRight").offset().top - $("#ContentSubPageContent").offset().top;
            
            if ($("#ContentSubPageContent").height() < top + $("#BoxRight").height()) 
            {
                $("#ContentSubPageContent").height(top + $("#BoxRight").height());
            }
        }
    };
    $.fn.adjustHeightRightBox();
});


    $.fn.placeImageInSlider = function(images)
    {
        images.each(function(index, elm) 
        {
            var img = $(elm);
            if (img.size() == 0)
                return;

            if (img.height() != 0 && img.width() != 0)
            {
                img.css("margin-top", (img.height() / 2) * -1);
            }

            img.load(function() 
            {
                if (img.height() != 0 && img.width() != 0)
                {
                    img.css("margin-top", (img.height() / 2) * -1);
                }
            });
        });
    };
})(jQuery);

function showHideMovie(info) 
{
    (function($)
    {
        var now = (new Date()).getTime();
        var timestamp = Number($(info).children(".timestamp").html());
        var date = new Date(timestamp).getTime();

        if (now <= date)
        {
            //if (now <= date || ((new Date(now).toDateString() == new Date(timestamp).toDateString()) && (new Date(now).getHours() >= new Date(timestamp).getHours()) && (new Date(now).getMinutes() >= new Date(timestamp).getMinutes()))) {

            info.find(".live").show();
            info.find(".movie").hide();
            info.find(".login").hide();
        }
        else
        {
            info.find(".live").hide();
            if (IsUserAuthenticatedToWebinar)
            {
                var id = "movieHolder" + now;
                var movieholder = info.find(".movieHolder");

                if (movieholder.find("embed").size() == 0 && movieholder.find("object").size() == 0)
                {
                    movieholder.attr("id", id);
                    //var so = new SWFObject('/Resources/Targit/Flash/player2.swf', 'ply', '275', '179', '9');
                    //so.addParam('allowfullscreen', 'true');
                    //so.addParam('allowscriptaccess', 'always');

                    //if (location.hash != "") 
                    //{
                    //    so.addVariable('autostart', 'true');
                    //}

                    //so.addParam('wmode', 'opaque');
                    //so.addVariable('menu', 'false');
                    //so.addVariable('file', movieholder.html().replace(".ashx", ".flv"));
                    //so.write(id);
                }

                info.find(".movie").show();

                var countImages = info.find(".rate").find("img").size();
                
                if(countImages <= 0)
                {
                    AddRater(info.find(".rate"));
                }

                info.find(".login").hide();
                
                setTimeout(function() 
                {
                    info.find(".thumbPlay").each(function(i, e) 
                    {
                        var $e = $(e);
                        var img = $e.next("img");
                        $e.css({ marginTop : (img.height() / 2) - $e.height() / 2, marginLeft : (img.width() / 2) - $e.width() / 2 });
                    });

                }, 100);
            }
            else
            {
                info.find(".movie").hide();
                info.find(".login").show();
            }
        }
    })(jQuery);
}

function openItem(id)
{
    (function($)
    {
        if (typeof selectedItem === 'undefined')
            return;

        if (selectedItem == "")
            return;

        if ($(id + " #" + selectedItem + " .info").size() != 0)
        {
            $(id + " #" + selectedItem + " .readmore a").trigger("click", [false]);

            ScrollToHolder($(id + " #" + selectedItem));
        }
    })(jQuery);
}

function webinarReadmore(webinar, holder)
{
    (function($)
    {
        selectedItem = webinar;

        $(".link a[href='#" + holder + "']").trigger("click");

        openItem("");
    })(jQuery);
}


function openReadmore(holder)
{
    (function ($)
    {
        $('.showhidelink a').each(function ()
        {
            $(this).trigger("click");
        });

        setTimeout(function ()
        {
            var $link = $("#" + holder + " .readmore a");
            $link.trigger("click", [false]);

            ScrollToHolder($("#" + holder));
        }, 200);

    })(jQuery);
}

function ScrollToHolder($holder) 
{
    var targetOffset = $holder.offset();
    var targetTop = targetOffset.top;
   
    $('html, body').animate({ scrollTop: targetTop }, 200);
}

function processList(HolderID, data, templateSelector) 
{
    (function ($)
    {
        $(document).ready(function ()
        {
            var Holder = $("#" + HolderID);
            if (Holder.prev(".stdHolder").size() != 0)
            {
                if (Holder.prev(".stdHolder").html() == "")
                {
                    if (data.length > 0)
                    {
                        Holder.siblings(".noFound").hide();
                        Holder.siblings(".row").show();
                    } 
                    else
                    {
                        Holder.siblings(".noFound").show();
                        Holder.siblings(".row").hide();
                    }
                }
            }
            else
            {
                if (data.length > 0)
                {
                    Holder.siblings(".noFound").hide();
                    Holder.siblings(".row").show();
                }
                else
                {
                    Holder.siblings(".noFound").show();
                    Holder.siblings(".row").hide();
                }
            };
            Holder.setTemplate($(templateSelector).html(), [], { filter_data: false });
            Holder.processTemplate(data);

            if (Holder.hasClass("holder"))
            {
                if (templateSelector == "#webinarTemplate")
                {
                    if ($(".webinarHolder:last .holder").attr("id") == HolderID)
                    {
                        Holder.find(".row .region, .row .date").hide();

                        Holder.find(".row .topic").width(350);
                    }
                }
                Holder.find(".row").each(function (index, elm)
                {
                    $(this).css({ opacity: 0 }).show().delay(index * 100).animate({ opacity: 1 }, 150);
                });
            }
            openItem("#" + HolderID);
        });
    })(jQuery);
}

function escapeHTML(str) 
{
    var div = document.createElement('div');
    var text = document.createTextNode(str);
    div.appendChild(text);
    return div.innerHTML;
}

