﻿// Silver Pride Software Popup window
// Copyright 2008 - All rights reserved
// Silver Pride Software Limited http://www,silver-pride.com
//
/// <reference path="jquery.intellisense.js"/>

$(document).ready(function() {
    popupInit();
});


function popupInit() {
    // Create the div popup

    $('#cms-popup').dialog({
        autoOpen: false,
        width: 900,
        height: 600,
        modal: true,
        bgiframe: true,
        stack: true,
        resizable: false,
        open: initIframe,
        hide: 'clip',
        close: function() { finalise(); }
    });
        
}

jQuery.fn.ShowPopup = function(width, height) {

    $('#cms-popup').append("<div class='loading'><div class='animated'></div></div>");

    $('#cms-popup').dialog('option', 'width', width);
    $('#cms-popup').dialog('option', 'height', height + 30); // add control button height
    $('#cms-popup').dialog('open');

    // $(this).blur();

};

function initIframe() {
    $('#popupdialogframe').remove();
    $('#cms-popup').append("<iframe frameborder='0' hspace='0' src='" + _SPPurl + "' id='popupdialogframe' onload='popup_removeloading()' width='100%' height='100%' scrolling='no' > </iframe>");  
}

function popup_removeloading() {
    $('#cms-popup .loading').remove();
}

var _SPPcallback;
var _SPPurl;

function showpopup(url, width, height, callback) {
    showpopup(url, width, height, callback, "");
}

function showpopup(url, width, height, callback, title) {
    _SPPcallback = callback;
    _SPPurl = url;

    $('#cms-popup').dialog('option', 'title', title);
    $('#cms-popup').ShowPopup(width, height);

}

var refreshOnUpdate;

function hidepopup() {
    refreshOnUpdate = false;
    animateclosepopup();
}

function hidepopupandrefresh() {
    refreshOnUpdate = true;
    animateclosepopup();
}

function hidedialog_and_redirect(url) {
    var cmd = "window.location = '" + url + "'; ";
    eval(cmd);
}

function animateclosepopup() {
    $('#cms-popup').dialog('close');
    $('#cms-popup iframe').remove();
}


function finalise() {
    
    if (refreshOnUpdate == true) {
       
        // tell parent to refresh and rebind data
        if (_SPPcallback != '')
            eval("window." + _SPPcallback);

        if (RequestDataRefresh)
            RequestDataRefresh();
                  
        refreshOnUpdate = false;
    }
}

function filechooserselected(file) {
    if (SetParam)
        SetFileParam(file);        
}

function linkchooserselected(link) {
    if (SetParam)
        SetLinkParam(link);
}

/* -------------------- Admin content popup ---- */

function initialise_admindropdown() {

    // Find all the a tags with a specified href and turn them into a click handler.
    // $('a.admin-dropdown-default[href]').click(function() { window.location = $(this).attr('href'); return false; });

    // For all editable areas add the double handler. When double click occurs traverse up through the tree to the dropdown menu, then down in the menu.
    // When found fire the click event.
    $('.admin-dropdown').parent().children().dblclick(function(e) {
        var $toplevel = $(e.target).closest('div:has(.admin-dropdown)');
        $toplevel.effect('highlight', {}, 500);
        $toplevel.find('.admin-dropdown-default').click();
    });

    var $admindropdown_click = $('.admin-dropdown-click');

    // Add alpha of 50% when not under the mouse.
    $admindropdown_click.fadeTo('fast', .75).hover(function(e) { $(e.target).fadeTo('fast', 1.0); }, function(e) { $(e.target).fadeTo('fast', 0.75); });

    $admindropdown_click.click(function() {
        $(this).siblings('.admin-dropdown-bg').show("drop", {}, 500).mouseleave(function() {
            $(this).hide("drop", {}, 300); $(this).unbind('mouseleave');
            $(this).closest('.admin-dropdown').css('z-index', '96');
        });

        $(this).parent().css('z-index', '98');
        $(this).closest('div:has(.admin-dropdown)').effect('highlight', {}, 500);
    });

    if ($.cookie('sparkcms-showcontrols') == 'no') {
        HideAdminControls();
    }
    
    if ($.cookie('sparkcms-showcontrols') == 'no') {
        $('#chkDisplayContentControls').attr('checked', false);
    }
    else {
        $('#chkDisplayContentControls').attr('checked', true);
    }        
    
}

function ShowAdminControls() {
    $('.admin-dropdown').fadeIn('slow');
    $.cookie('sparkcms-showcontrols', 'yes');
}

function HideAdminControls() {
    $('.admin-dropdown').fadeOut('slow');
    $.cookie('sparkcms-showcontrols', 'no');
}

function HideAdminControlsIfVisiblilityIsOn() {
    if ($.cookie('sparkcms-showcontrols') == null || $.cookie('sparkcms-showcontrols') == 'yes')
        $('.admin-dropdown').hide();
}

function ShowAdminControlsIfVisibilityIsOn() {
    if ($.cookie('sparkcms-showcontrols') == null || $.cookie('sparkcms-showcontrols') == 'yes')
        $('.admin-dropdown').show();
}

// Set the admin dropdown check status and respond
$(document).ready(function() {

    $('#chkDisplayContentControls').click(function() {
        if ($(this).is(':checked')) {
            // Display the controls
            ShowAdminControls();
        }
        else {
            HideAdminControls();
        }
    });
});