﻿/// <reference path="jquery-1.3.2-vsdoc2.js" />

jQuery.noConflict();

var clientPC = navigator.userAgent.toLowerCase();
var is_gecko = ((clientPC.indexOf('gecko') != -1) && (clientPC.indexOf('spoofer') == -1) &&
                (clientPC.indexOf('khtml') == -1) && (clientPC.indexOf('netscape/7.0') == -1));
var is_safari = ((clientPC.indexOf('AppleWebKit') != -1) && (clientPC.indexOf('spoofer') == -1));
var is_khtml = (navigator.vendor == 'KDE' || (document.childNodes && !document.all && !navigator.taintEnabled));
if (clientPC.indexOf('opera') != -1) {
    var is_opera = true;
    var is_opera_preseven = (window.opera && !document.childNodes);
    var is_opera_seven = (window.opera && document.childNodes);
}

function CaretPosition() {
    var start = null;
    var end = null;
}

function getCaretPosition(oField) {
    var oCaretPos = new CaretPosition();

    if (oField.selectionStart || oField.selectionStart == '0') {
        oCaretPos.start = oField.selectionStart;
        oCaretPos.end = oField.selectionEnd;
    }
    else if (document.selection) {
        oField.focus();

        var range = document.selection.createRange();
        var stored_range = range.duplicate();

        stored_range.moveToElementText(oField);
        stored_range.setEndPoint('EndToEnd', range);
        oCaretPos.start = stored_range.text.length - range.text.length;
        oCaretPos.end = oCaretPos.start + range.text.length;
    }

    return (oCaretPos);
}

function insertAtCaret(obj, text, block) {
    var caret = getCaretPosition(obj);

    if (document.selection) {
        obj.focus();

        var range = document.selection.createRange();

        if (range.parentElement() != obj)
            return false;

        if (block && caret.start != 0)
            text = "\n" + text;

        range.text = text;
    }
    else if (obj.selectionStart >= 0) {
        if (block && caret.start != 0)
            text = "\n" + text;

        obj.value = obj.value.substr(0, caret.start) + text + obj.value.substr(caret.end, obj.value.length);
    }

    if (caret != null) {
        if (navigator.appName == "Opera") {
            var arr = Array();
            arr = text.split('\n');
            caret.start += arr.length;
        }

        setCaretTo(obj, caret.start + text.length);
    }
    else
        obj.value += text;
}

function setCaretTo(obj, pos) {
    if (obj.createTextRange) {
        var range = obj.createTextRange();
        range.move('character', pos);
        range.select();
    }
    else if (obj.selectionStart) {
        obj.focus();
        obj.setSelectionRange(pos, pos);
    }
}

function insertTags(txtarea, tagOpen, tagClose, sampleText) {
    // IE
    if (document.selection && !txtarea.selectionStart) {
        var theSelection = document.selection.createRange().text;
        var replaced = true;

        if (!theSelection) {
            replaced = false;
            theSelection = sampleText;
        }
        txtarea.focus();

        // This has change
        var text = theSelection;
        if (theSelection.charAt(theSelection.length - 1) == " ") {// exclude ending space char, if any
            theSelection = theSelection.substring(0, theSelection.length - 1);
            r = document.selection.createRange();
            r.text = tagOpen + theSelection + tagClose + " ";
        }
        else {
            r = document.selection.createRange();
            r.text = tagOpen + theSelection + tagClose;
        }

        if (!replaced) {
            r.moveStart('character', -text.length - tagClose.length);
            r.moveEnd('character', -tagClose.length);
        }

        r.select();
        // Mozilla
    }
    else if (txtarea.selectionStart || txtarea.selectionStart == '0') {
        replaced = false;

        var startPos = txtarea.selectionStart;
        var endPos = txtarea.selectionEnd;

        if (endPos - startPos)
            replaced = true;

        var scrollTop = txtarea.scrollTop;
        var myText = (txtarea.value).substring(startPos, endPos);

        if (!myText)
            myText = sampleText;

        if (myText.charAt(myText.length - 1) == " ") { // exclude ending space char, if any
            subst = tagOpen + myText.substring(0, (myText.length - 1)) + tagClose + " ";
        } else {
            subst = tagOpen + myText + tagClose;
        }
        txtarea.value = txtarea.value.substring(0, startPos) + subst +
                    txtarea.value.substring(endPos, txtarea.value.length);
        txtarea.focus();

        //set new selection
        if (replaced) {
            var cPos = startPos + (tagOpen.length + myText.length + tagClose.length);
            txtarea.selectionStart = cPos;
            txtarea.selectionEnd = cPos;
        } else {
            txtarea.selectionStart = startPos + tagOpen.length;
            txtarea.selectionEnd = startPos + tagOpen.length + myText.length;
        }
        txtarea.scrollTop = scrollTop;
        // All others
    } else {
        var copy_alertText = alertText;
        var re1 = new RegExp("\\$1", "g");
        var re2 = new RegExp("\\$2", "g");
        copy_alertText = copy_alertText.replace(re1, sampleText);
        copy_alertText = copy_alertText.replace(re2, tagOpen + sampleText + tagClose);

        if (sampleText) {
            text = prompt(copy_alertText);
        } else {
            text = "";
        }
        if (!text) { text = sampleText; }
        text = tagOpen + text + tagClose;
        //append to the end
        txtarea.value += "\n" + text;

        // in Safari this causes scrolling
        if (!is_safari) {
            txtarea.focus();
        }

    }
    // reposition cursor if possible
    if (txtarea.createTextRange) {
        txtarea.caretPos = document.selection.createRange().duplicate();
    }
}

function getObjTopPos(obj) {
    var top = obj.offsetTop;

    while ((obj = obj.offsetParent) != null)
        top += obj.offsetTop;

    return top;
}

function DetectNEnter(e, btn) {
    var characterCode;

    if (e && e.which) // NN4 specific code
    {
        e = e;
        characterCode = e.which;
    }
    else {
        e = event
        characterCode = e.keyCode; // IE specific code
    }

    if (characterCode == 13) //// Enter key is 13
    {
        e.returnValue = false;
        e.cancelBubble = true;
        document.getElementById(btn).click();
    }
    else
        return false;
}

function gotoUrl(url) {
    window.location.href = url;
}

function tr_sethover(obj) {
    obj.oldClassName = obj.className == undefined ? "" : obj.className;
    obj.className = obj.oldClassName + " hover";
}

function tr_setblur(obj) {
    obj.className = obj.oldClassName;
}

function ToggleVisibility(controlId, display) {

    var cnt = document.getElementById(controlId);
    var tbl = document.getElementById("table_league");

    if (cnt != null)
        cnt.style.display = display;

    if (tbl != null && tbl.className.indexOf("table_league_toggle", 0) == -1)
        tbl.className += " table_league_toggle";

    return true;
}

function BindThirdMenuItems() {
    var itemsContainer = document.getElementById("horizontal_third_menu");
    var currentPageName = GetPageName();

    if (itemsContainer == null)
        return;

    var items = itemsContainer.getElementsByTagName("a");

    for (i = 0; i <= items.length - 1; i++) {
        var item = items[i];

        if (item.getAttribute("id") == currentPageName) {
            if (item.getAttribute("id") == "progress")
                item.className = "menu_last_item selected";
            else
                item.className = "selected";
        }
    }
}

function Alphabet_Click(litera) {
    var i = document.URL.indexOf("#", 0);
    if (i > 0) {
        var url = document.URL.substr(0, i);
        location = url + "#" + litera.toLowerCase();
    }
    else
        location = document.URL + "#" + litera.toLowerCase();
}

var uppanels = new Array();
var uppanelsForWaitint = new Array();
var pbargs = new Array(); 

function addNewPostback(panelId, args)
{
    var panel = document.getElementById(panelId);
    
    jQuery(panel).toggleClass("updatedPanel");
    
    uppanels.push(panelId);
    pbargs.push(args);
    uppanelsForWaitint.push(panelId);
}

function runPostBacks()
{
    //for (var i = 0; i < uppanels.length; i++)
    //{
        //__doPostBack(uppanels[i], pbargs[i]);
      //  jQuery.delay(3400);
    //}
        
//    uppanels = new Array();
//    pbargs = new Array();
    
    if (uppanels.length > 0)
    {
   
        __doPostBack(uppanels.pop(), pbargs.pop());
        
    }
}

function closeUpdateWaiting()
{
    var panelId = uppanelsForWaitint.pop();
        var panel = document.getElementById(panelId);
        jQuery(panel).toggleClass("updatedPanel");
    }


///* Позиционирование нижнего правого баннера. */
//function ShowProgressPanel() {
//    if (navigator.userAgent.indexOf('MSIE') != -1) {
//        $$('select').each(function(item) {
//            item.oldVisibility = item.style.visibility == undefined ? null : item.style.visibility;
//            item.style.visibility = 'hidden';
//        });
//    }

//    // устанавливаем позицию вывода всплывающего окна для update panel
//    var myWidth = 0, myHeight = 0;
//    if (typeof (window.innerWidth) == 'number') {
//        //Non-IE
//        myWidth = window.innerWidth;
//        myHeight = window.innerHeight;
//    } else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
//        //IE 6+ in 'standards compliant mode'
//        myWidth = document.documentElement.clientWidth;
//        myHeight = document.documentElement.clientHeight;
//    } else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
//        //IE 4 compatible
//        myWidth = document.body.clientWidth;
//        myHeight = document.body.clientHeight;
//    }

//    var form = $("aspnetForm");
//    form.clientH = myHeight;

//    var progress = $("upd_progress");
//    progress.style.height = form.offsetHeight + "px";
//    progress.style.width = form.offsetWidth + "px";
//    progress.style.display = "block";
//    var waitPanel = $("wait");

//    if (progress != null && waitPanel != null) {
//        waitPanel.style.left = parseInt((myWidth - waitPanel.offsetWidth) / 2) + "px";

//        if (navigator.userAgent.indexOf('MSIE') != -1)
//            waitPanel.style.left = parseInt((form.offsetWidth - waitPanel.offsetWidth) / 2) + "px";
//        else
//            waitPanel.style.top = parseInt(myHeight / 2) + "px";
//    }
//}

//function InitializeRequest(sender, args) {
//    if (prm.get_isInAsyncPostBack())
//        args.set_cancel(true);

//    ShowProgressPanel();
//}

//function EndRequest(sender, args) {
//    if (navigator.userAgent.indexOf('MSIE') != -1) {
//        $$('select').each(function(item) {
//            item.style.visibility = item.oldVisibility == null ? '' : item.oldVisibility;
//        });
//    }

//    $("upd_progress").style.display = "none";
//}

//function AlignRightBottomBanner(contentContainerId, rightContainerId, rightBottomBannerContainerId) {
//    var contentArea = document.getElementById(contentContainerId);
//    var rightArea = document.getElementById(rightContainerId);
//     
//    if (contentArea != null && rightArea != null)
//    {
//        var centerHeight = contentArea.offsetHeight;
//        var rightHeight = rightArea.offsetHeight;
//        
//        //если нужно опустить баннер вниз
//        if (centerHeight > rightHeight)
//        {

//            var bannerContainer = document.getElementById(rightBottomBannerContainerId);

//            if (bannerContainer != null)
//            {
//                var padding = centerHeight - rightHeight;
//                bannerContainer.style.paddingTop = padding + "px";    
//                bannerContainer.style.display = "block";
//            }
//        }                            
//    }
//}