﻿function getNavCookie(name) {
    if (document.cookie.length > 0) {
        start = document.cookie.indexOf(name + "=");
        if (start != -1) {
            start += name.length + 1;
            end = document.cookie.indexOf(";", start);
            if (end == -1) end = document.cookie.length;
            return unescape(document.cookie.substring(start, end));
        }
    }
    return "";
}

function DeactivateAll() {
    $('#ProjectDetailDiscussionSwitch').removeClass('ol-DiscussionSummaryHeadline');
    $('#ProjectDetailBlogSwitch').removeClass('ol-DiscussionSummaryHeadline');
    $('#ProjectDetailFAQSwitch').removeClass('ol-DiscussionSummaryHeadline');
    
    $('#ProjectDetailDiscussionSwitch').addClass('ol-DiscussionSummaryHeadlineInactive');
    $('#ProjectDetailBlogSwitch').addClass('ol-DiscussionSummaryHeadlineInactive');
    $('#ProjectDetailFAQSwitch').addClass('ol-DiscussionSummaryHeadlineInactive');
}

function SetupProjectDetailNavigation() {
    // set discussion visiable by default
    $('#ProjectDetailLowerLeftZoneWrapper').show();
    
    $('#ProjectDetailLowerLeftZoneWrapper table div:eq(0)').hide();
    $('#projectBlogList').hide();
    $('#projectDiscussionList').hide();

    // show one of the three
    if ($('#ProjectDetailDiscussionSwitch').length > 0) {
        $('#projectDiscussionList').show();
    }
    else if ($('#ProjectDetailFAQSwitch').length > 0) {
        $('#ProjectDetailLowerLeftZoneWrapper table div:eq(0)').show();
    }
    else if ($('#ProjectDetailBlogSwitch').length > 0) {
        $('#projectBlogList').show();
    }

    $('#ProjectDetailDiscussionSwitch').click(function() {

        // show the discussions
        DeactivateAll();
        $('#ProjectDetailDiscussionSwitch').removeClass('ol-DiscussionSummaryHeadlineInactive');
        $('#ProjectDetailDiscussionSwitch').addClass('ol-DiscussionSummaryHeadline');

        $('#ProjectDetailLowerLeftZoneWrapper table div:eq(0)').hide();
        $('#projectBlogList').hide();
        $('#projectDiscussionList').show();

        // force silverlight to recalc the size for the html scroller
        recalculateSize();
    });

    $('#ProjectDetailBlogSwitch').click(function() {
        // show the blogs
        DeactivateAll();
        $('#ProjectDetailBlogSwitch').removeClass('ol-DiscussionSummaryHeadlineInactive');
        $('#ProjectDetailBlogSwitch').addClass('ol-DiscussionSummaryHeadline');

        $('#ProjectDetailLowerLeftZoneWrapper table div:eq(0)').hide();
        $('#projectDiscussionList').hide();
        $('#projectBlogList').show();

        // force silverlight to recalc the size for the html scroller
        recalculateSize();        
    });

    $('#ProjectDetailFAQSwitch').click(function() {
        // show the faq
        DeactivateAll();
        $('#ProjectDetailFAQSwitch').removeClass('ol-DiscussionSummaryHeadlineInactive');
        $('#ProjectDetailFAQSwitch').addClass('ol-DiscussionSummaryHeadline');

        $('#ProjectDetailLowerLeftZoneWrapper table div:eq(0)').show();
        $('#projectDiscussionList').hide();
        $('#projectBlogList').hide();

        // force silverlight to recalc the size for the html scroller
        recalculateSize();        
    });

    // remove the vertical spacing from the webpart zone
    $('#ProjectDetailLowerLeftZoneWrapper div.ms-PartSpacingVertical').remove();
}

function SetSelectedCategory(name) {
    $('.ol-CategoryNavSubSelected').addClass('ol-CategoryNavSub');
    $('.ol-CategoryNavSubSelected').removeClass('ol-CategoryNavSubSelected');
    $('.ol-CategoryNavSub[id="category_' + name + '"]').addClass('ol-CategoryNavSubSelected');
}

function recalculateSize() {
    try {
        top.frameLoaded($(document).height());
    }
    catch (e) { }
}

/* Feedback Functions */

function ShowFeedbackWindow() {
    // Check browser version and determine correct "display" css value
    var sDisplayCSSValue = "table";
    if (navigator.appName == "Microsoft Internet Explorer") {
        sDisplayCSSValue = "block";
    }
    var FeedbackTable = document.getElementById("FeedbackTable");

    // unhide table
    FeedbackTable.style.display = sDisplayCSSValue;
    // Position box so it lands in center of scrolled window
    FeedbackTable.style.top = (document.body.scrollTop);

    $('#FeedbackDiv1').css("margin-top", "40px");
    $('#FeedbackFrameDiv').html('');
    $('#FeedbackFrameDiv').append('<iframe frameborder="0" width="700" height="400" style="border: 0;" src="/_layouts/olsite/Feedback.aspx"></iframe>');

    //Show semi-transparent white background
    var backgroundWhite = document.getElementById("backgroundWhite");
    backgroundWhite.style.display = "block";
    backgroundWhite.style.height = document.body.scrollHeight;
    // Show Slide Show window
    document.getElementById("FeedbackDiv1").style.display = sDisplayCSSValue;
}

function CloseFeedbackWindow() {
    //Hide semi-transparent white background
    backgroundWhite.style.display = "none";
    // Hide Span
    FeedbackDiv1.style.display = "none";
    // Hide table
    FeedbackTable.style.display = "none";
}


/* License Functions */

function ShowLicenseWindow(fileurl) {
    // Check browser version and determine correct "display" css value
    var sDisplayCSSValue = "table";
    if (navigator.appName == "Microsoft Internet Explorer") {
        sDisplayCSSValue = "block";
    }
    var LicenseTable = document.getElementById("LicenseTable");
    var filepath = fileurl.substring(fileurl.indexOf('?'));
    filepath = filepath.replace('?license=1', '');

    // unhide table
    LicenseTable.style.display = sDisplayCSSValue;
    // Position box so it lands in center of scrolled window
    //LicenseTable.style.top = (document.body.scrollTop);
    
    $('#LicenseDiv1').css("margin-top", "40px");
    $('#LicenseFrameDiv').html('');
    $('#LicenseFrameDiv').append('<iframe frameborder="0" width="100%" height="450" scrolling="no" style="border: 0;" src="/_layouts/olsite/License.aspx' + filepath + '"></iframe>');

    //Show semi-transparent white background
    var backgroundWhite = document.getElementById("backgroundWhite");
    backgroundWhite.style.display = "block";
    backgroundWhite.style.height = document.body.scrollHeight;
    // Show Slide Show window
    document.getElementById("LicenseDiv1").style.display = sDisplayCSSValue;
}

function CloseLicenseWindow() {
    //Hide semi-transparent white background
    backgroundWhite.style.display = "none";
    // Hide Span
    LicenseDiv1.style.display = "none";
    // Hide table
    LicenseTable.style.display = "none";
}

/* File Download Licensing Function */

function lic_FileDownload(fileurl) {

  if(fileurl.toLowerCase().indexOf("?license=1") != -1) {

      $('#yes').click(function() {

          //Close the license
          CloseLicenseWindow();

          //Webtrends monitoring
          dcsMultiTrack('DCS.dcssip', 'www.educationlabs.com', 'DCS.dcsuri', fileurl.replace('http://www.educationlabs.com', '').replace('/_layouts/olsite/TryIt.ashx?downloadUrl=', '').replace('?license=1', ''), 'WT.ti', 'Moodle%20Plugin', 'WT.dl', '1');

          //Download the file
          location.href = fileurl;
      }); 

    $('#no').click(function() { 
        //Close the license
        CloseLicenseWindow();
        return false; 
    });
   
    ShowLicenseWindow(fileurl);
  }  
  else {

      //Webtrends monitoring
      dcsMultiTrack('DCS.dcssip', 'www.educationlabs.com', 'DCS.dcsuri', fileurl.replace('http://www.educationlabs.com', '').replace('/_layouts/olsite/TryIt.ashx?downloadUrl=', ''), 'WT.ti', fileurl.substring(fileurl.indexOf('project=')).replace('project=', '') + '%20Download', 'WT.dl', '1');
     
    //download the file - no license acceptance required
    location.href = fileurl;
  }
  return false;
}