﻿$(function () {
    $('.action-list ul>li a, .action-link, .news-reader-bbar a').click(function () {
        var wh = $(this).attr('hash').substring(1).split(',');
        $.colorbox({
            iframe: true,
            href: $(this).attr('href'),
            initialWidth: 200,
            initialHeight: 100,
            width: wh[0],
            height: wh[1],
            overlayClose: false
        });
        return false;
    });

    // forces colorbox to stay in the middle when scrolling
    $(document).bind('cbox_open', function () {
        $(window).bind('scroll', function () {
            var cb = $('#colorbox');
            var top = 0;
            var cbHeight = cb.outerHeight();
            var winHeight = $(this).height();
            if (winHeight > cbHeight)
                top = (winHeight - cbHeight) / 2 + $(this).scrollTop();

            cb.animate({
                top: top
            }, 40);
        });

    }).bind('cbox_closed', function () {
        $(window).unbind('scroll');
    });

    // home page news box
    var switchTopNews = function (num) {
        var box = $('.news-box');
        var news = box.find('.news-box-item');
        var activeNews = news.filter('.active');
        var index = 0
        if (activeNews.length > 0) {
            index = activeNews.index() + 1;
            index = (index == news.length) ? 0 : index;
            activeNews.fadeOut('slow', function () { $(this).removeClass('active'); });
        } else if (box.find('ul').length == 0) {
            var ul = $('<ul></ul>');
            news.each(function (ix) { ul.append('<li>&nbsp;</li>'); });
            box.append(ul);
            var lst = box.find('li');
            lst.hover(function () {
                if (newsSwitchTimer != null) { clearInterval(newsSwitchTimer); }
                var lix = $(this).index();
                switchTopNews(lix);
            }, function () {
                startNewsSwitch();
            });
        }
        index = (num != null) ? num : index;
        news.eq(index).fadeIn('slow', function () { $(this).addClass('active'); });
        box.find('li').removeClass('active');
        box.find('li').eq(index).addClass('active');
    };
    var newsSwitchTimer = null;
    var startNewsSwitch = function () {
        newsSwitchTimer = setInterval(function () {
            switchTopNews();
        }, 10000);
    };
    if ($('.news-box').length > 0) {
        switchTopNews();
        startNewsSwitch();
    }

    // home news tabs
    $(".news-tabs ul").tabs("div.news-pane", {
        effect: 'fade'
    });
});

function closePopup(tabId) {
    $.fn.colorbox.close();
    if (tabId == null)
        window.location.reload();
    else
        window.location = "/Default.aspx?TabID=" + tabId;
}


function moveItem(slcFrom, slcTo, slcFromtxt, slcTotxt) {
    slcFromSelectedIndex = document.all[slcFrom].selectedIndex;
    slcToLenght = document.all[slcTo].length;
    if (slcFromSelectedIndex != -1 && document.all[slcFrom].options[slcFromSelectedIndex].value > "") {
        fromText = document.all[slcFrom].options[slcFromSelectedIndex].text;
        fromValue = document.all[slcFrom].options[slcFromSelectedIndex].value;
        for (i = 0; i < document.all[slcTo].length; i++) {
            if (document.all[slcTo].options[i].value == fromValue)
                return;
        }
        document.all[slcFrom].options[slcFromSelectedIndex] = null;
        document.all[slcTo].options[slcToLenght] = new Option(fromText, fromValue, false, true);
        // populate hidden fileds for postback
        document.all[slcFromtxt].value = "";
        for (i = 0; i < document.all[slcFrom].length; i++) {
            document.all[slcFromtxt].value += document.all[slcFrom].options[i].value;
            document.all[slcFromtxt].value += "^";
            document.all[slcFromtxt].value += document.all[slcFrom].options[i].text;
            document.all[slcFromtxt].value += "&";
        }

        document.all[slcTotxt].value = "";
        for (i = 0; i < document.all[slcTo].length; i++) {
            document.all[slcTotxt].value += document.all[slcTo].options[i].value;
            document.all[slcTotxt].value += "^";
            document.all[slcTotxt].value += document.all[slcTo].options[i].text;
            document.all[slcTotxt].value += "&";
        }

    } else {
        alert("Please select a module first");
    }
} 

//File Browser Scripts
function gotoFolder(lnk) {
	window.location.replace(lnk);
}

function DisplayFileManagerPropertyPanes(insertType) {
	if (insertType == "none")
		$("#sml_property").css('display', "none");		
	else
		$("#sml_property").css('display', "block");	
}

function displayImage(filename, insertType) {	
	DisplayFileManagerPropertyPanes(insertType);	
	$("#img_previw").html("<img name='img_prv' src='" + filename + "' />");
}

function displayFlash(filename, insertType){
	DisplayFileManagerPropertyPanes(insertType);	
	
	document.all["img_previw"].html("<object classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0' id='Adver' align='middle' width='450' height='300'>"
				+ "<param name='allowScriptAccess' value='sameDomain' />"
				+ "<param name='movie' value='" + filename + "' />"
				+ "<param name='menu' value='false' />"
				+ "<param name='salign' value='t' />"
				+ "<embed src='" + filename + "' menu='false' name='Adver' salign='t' allowScriptAccess='sameDomain' type='application/x-shockwave-flash' pluginspage='http://www.macromedia.com/go/getflashplayer' width='550' height='400' />"
				+ "</object>");
}

function displayOther(filename, insertType) {
	DisplayFileManagerPropertyPanes(insertType);	
	$("#img_previw").html("");
}

function displayNone() {
	$("#sml_property").css('display', "none");
}
