var doesImageExist = false;
var loggedUserID="";
var isInvited=false;
var iMousePosX;
var iMousePosY;

var personInd='-';
var newsYear=-1;
var newsMonth=-1;
var date=new Date();

function $el(id){
    var source = document.getElementById(id);
    if (!source){
        source = document.getElementsByName(id)[0];
    }
    return source;
}

function $clr(id){
    $el(id).innerHTML = '';
}

function $rst(id){
    $el(id).value = "";
}

var r={
    'notletters':/[^a-zA-Z]/g,
    'notlettersNSp':/[^a-zA-ZА-Яа-яЎўЁёІі\s]/g
};

function valid(o,w){
    o.value = o.value.replace(r[w],'');
}

function $noselect(id) {
    var el = $el(id);
    el.onselectstart = function() {return false;};
    el.onmousedown = function() { return false;}
}


function $curPos(e){
    e = e || window.event;
    var cursor = {x:0, y:0};
    if (e.pageX || e.pageY) {
        cursor.x = e.pageX;
        cursor.y = e.pageY;
    }
    else {
        var de = document.documentElement;
        var b = document.body;
        cursor.x = e.clientX +
                   (de.scrollLeft || b.scrollLeft) - (de.clientLeft || 0);
        cursor.y = e.clientY +
                   (de.scrollTop || b.scrollTop) - (de.clientTop || 0);
    }
    return cursor;
}

function getMouseXYPos(e) {
    if (!e) e = window.event;
    if (e){
  	    iMousePosX = document.all ? (e.clientX + document.body.scrollLeft) : e.pageX;
  	    iMousePosY = document.all ? (e.clientY + document.body.scrollTop) : e.pageY;
    }
}

function trim(str){
    return str.replace(/^\s+|\s+$/g, '').replace(/\s+/g, ' ') ;
}

function $key(e){
    var code;
    if (!e) e = window.event;
    if (e.keyCode) code = e.keyCode;
    else if (e.which) code = e.which;
    return code;
}

function onSubmit( func, e ) {
    if($key(e)=="13") eval(func);
}

function $over(el, over){
    if(over){
        el.style.opacity = 0.8;
        el.style.filter = "alpha(opacity=80)";
    }else{
        el.style.opacity = 1;
        el.style.filter = "alpha(opacity=100)";
    }
}

function setOnOverImages(){
    var imgs = document.getElementsByTagName("img");
    for(var i=0;i<imgs.length;i++){
        if(imgs[i].style.opacity == 'op'){
            imgs[i].onmouseover = function(){$over(this,true);};
            imgs[i].onmouseout = function(){$over(this,false);};
        }
    }
}

function hover(obj,clas){
    $(obj).each(function(){
        $(this).mouseenter(function(){
            $(this).toggleClass(clas);
        }).mouseleave(function(){
            $(this).toggleClass(clas);
        });
    });
}

function $select(url, cap){
   //doSend("/person.do", "authors", "letter="+cap, null, "page_div", null);
    window.location = "/" + url + (cap != null ? ("/"+cap+".dhtml") : "");
}

function select(item,ind){
    var oldInd;
    var itemdiv;
    switch(item){
        case "alfa":
            oldInd = personInd;
            itemdiv = item;
            break;
        case "year":
            oldInd = newsYear;
            itemdiv = "event";
            break;
        case "month":
            oldInd = newsMonth;
            itemdiv = "event";
            break;
    }

    if(oldInd==ind) return;

    var tab = $el(item+"_"+ind);
    if(!tab)return;
    tab.style.backgroundColor = 'transparent';
    tab.style.color = '#000';
    tab.style.borderColor = '#000';

    if(oldInd > -1 || oldInd != ''){
        var old_tab = $el(item+"_"+oldInd);
        old_tab.style.backgroundColor = '#000';
        old_tab.style.color = '#fff';
        old_tab.style.borderColor = '#fff';
        if(oldInd==''){
            hideAll("alfa_");
        }else{
            var old_div = $el(itemdiv+"_"+(item=="alfa"?oldInd:newsYear+"-"+newsMonth));
            old_div.style.display = 'none';
        }
    }

    switch(item){
        case "alfa":
            personInd = ind;
            break;
        case "year":
            newsYear = ind;
            var curYear = date.getFullYear();
            var curMonth = date.getMonth()+1;
            for(var m=1;m<13;m++){
                    $el("month_"+m).style.visibility = "visible";
                }
            if(curYear==ind){
                for(m=curMonth+1;m<13;m++){
                    $el("month_"+m).style.visibility = "hidden";
                }
            }
            if(ind==2009){
                $el("month_1").style.visibility = "hidden";
                $el("month_2").style.visibility = "hidden";
            }
            select("month",curYear==ind?curMonth:1);
            break;
        case "month":
            newsMonth = ind;
            break;
    }

    if(ind==''){
        showAll("alfa_");
    }else{
        var div = $el("div_"+itemdiv+"_"+(item=='alfa'?ind:newsYear+"-"+newsMonth));
        div.style.display = 'block';
    }
}

function setNewsYearAndMonth(year,month){
    selectNews('year', year, true);
    selectNews('month', month, true);
}

function selectLetter(url,cap,ind,nogo){
    var oldInd = personInd;
    $("#alfa_"+ind).addClass("letter-active").css("border-color","#000");
    if(oldInd != '-'){
        $("#alfa_"+oldInd).removeClass("letter-active");        
    }
    personInd = ind;
    if(!nogo){
        window.location = "/" + url + ((cap != null && cap != '') ? ("/"+cap+".dhtml") : "");
    }
}

function selectNews(item,ind,nogo){
    var oldInd;
    switch(item){
        case "year":
            oldInd = newsYear;
            break;
        case "month":
            oldInd = newsMonth;
    }

    if(oldInd==ind) return;

    var tab = $el(item+"_"+ind);
    tab.style.backgroundColor = 'transparent';
    tab.style.color = '#000';
    tab.style.borderColor = '#000';

    if(oldInd > -1 ){
        var old_tab = $el(item+"_"+oldInd);
        old_tab.style.backgroundColor = '#000';
        old_tab.style.color = '#fff';
        old_tab.style.borderColor = '#fff';
    }

    switch(item){
        case "year":
            newsYear = ind;
            var curYear = date.getFullYear();
            var curMonth = date.getMonth()+1;
            for(var m=1;m<13;m++){
                    $el("month_"+m).style.visibility = "visible";
                }
            if(curYear==ind){
                for(m=curMonth+1;m<13;m++){
                    $el("month_"+m).style.visibility = "hidden";
                }
            }
            if(ind==2009){
                $el("month_1").style.visibility = "hidden";
                $el("month_2").style.visibility = "hidden";
            }
            if(newsYear != ind){
                if(!nogo){
                    select("month",ind == 2009 ? 3 : 1);
                }
            }
            break;
        case "month":
            newsMonth = ind;
            if(!nogo){
                window.location = '/news.do?method=events&year=' + newsYear + '&month=' + newsMonth;
            }
            break;
    }
}

function over(item,ind,over){
    var tab = $el(item+"_"+ind);
    var oldInd;

    switch(item){
        case "alfa":
            oldInd = personInd;
            break;
        case "year":
            oldInd = newsYear;
            break;
        case "month":
            oldInd = newsMonth;
            break;
    }

    if(oldInd != ind){
        if(over){
            tab.style.backgroundColor = 'transparent';
            tab.style.color = '#000';
        }else{
            tab.style.backgroundColor = '#000';
            tab.style.color = '#fff';
        }
    }
}

function blockOver(item,fontel,over){
    if(over){
        item.style.backgroundColor='#000';
        $el(fontel).style.color='#fff';
    }else{
        item.style.backgroundColor='transparent';
        $el(fontel).style.color='#000';
    }
}

function showLabel(id,show){
    $el(id).style.visibility = show ? "visible" : "hidden";
}

function collapseEmptyPersons(divPattern){
    var div;
    for(var i=1; i<31; i++){
        div = $el(divPattern+i);
        if(trim(div.innerHTML)==""){
            div.style.paddingTop='0';
        }
    }
}
function showAll(divPattern){
    for(var i=1; i<31; i++){
        $el(divPattern+i).style.display = 'block';
    }
}

function hideAll(divPattern){
    for(var i=1; i<31; i++){
        $el(divPattern+i).style.display = 'none';
    }
}

function updateTitle(title) {
    //var te = document.getElementsByTagName("title")[0];
    //te.innerHTML = te.innerHTML + title;
	document.title += " " + title;

	var de = document.getElementsByTagName("meta")[1];
    de.setAttribute('content', title + ' - www.prajdzisvet.org');
}


function updateKeywords(keywords) {
    var ke = document.getElementsByTagName("meta")[0];
    var content = ke.getAttribute('content');
    ke.setAttribute('content', keywords + ', ' + content);
}


function toggle(btn,id){    
    var div = $("#"+id);
    div.stop(1,1).toggle(700);
//    div.slideToggle("slow");
    var head = $("dt", btn);
    if(head.html().indexOf("+") > -1){
        head.html(head.html().replace("+","-").replace("разгарнуць", "згарнуць"));
    }else{
        head.html(head.html().replace("-","+").replace("згарнуць", "разгарнуць"));
    }

}

function setDatesCalendar(selector, date){    
    $(function() {
        $(selector).datepicker({
            dateFormat: 'dd-mm',
            defaultDate: date+"2011",
            changeYear:true,
            hideIfNoPrevNext: true,
            maxDate:'31-12-2011',
            minDate:'01-01-2011',
            option: $.datepicker.regional['by'],

            onSelect: function(dateText, inst) { loadDatesForDate(dateText); }
        });        
    });
}






/////////////// TRAVEL  //////////////////////////

var objNames = new Array();
objNames['resort'] = 'курорт';
objNames['tour'] = 'тур';
objNames['hotel'] = 'отель';
objNames['accommodation'] = 'номер';
objNames['meals'] = 'питание';
objNames['attribute'] = 'дополнительные сведения';


if (!Array.prototype.indexOf) {
  Array.prototype.indexOf = function (obj, fromIndex) {
    if (fromIndex == null) {
        fromIndex = 0;
    } else if (fromIndex < 0) {
        fromIndex = Math.max(0, this.length + fromIndex);
    }
    for (var i = fromIndex, j = this.length; i < j; i++) {
        if (this[i] === obj)
            return i;
    }
    return -1;
  };
}

String.prototype.ReplaceAll = function(stringToFind,stringToReplace){
    var temp = this;
    var index = temp.indexOf(stringToFind);
    while(index != -1){
        temp = temp.replace(stringToFind,stringToReplace);
        index = temp.indexOf(stringToFind);
    }
    return temp;
}

function isOptionPresent(sbox,name){
    for(var i=0; i<sbox.options.length; i++){
        if(sbox.options[i].text == name) return true;
    }
    return false;
}

function addOption(sbox,text,value){
    var optn = document.createElement("option");
    optn.text = text;
    optn.value = value;
    sbox.options.add(optn);
}

function removeSelected(sbox){    
    for(var i=0; i<sbox.options.length; i++){
        if(sbox.options[i].selected){
            sbox.remove(i);
            break;
        }
    }
}

function countSelected(sbox){
    var count = 0;
    for (var i=0; i<sbox.options.length; i++) {
        if (sbox.options[i].selected) {
            count++;
        }
    }
    return count;
}

function capitalize(string){
    return string.charAt(0).toUpperCase() + string.slice(1);
}

function setDatepicker(selector){    
    $(function() {
        $(selector).datepicker({
            showOn: 'button',
            buttonImage: '/img/calendar.gif',
            buttonImageOnly: true,
            dateFormat: 'dd-mm',
            option: $.datepicker.regional['ru']
        });
    });
}


function setDatepickerWithFormat(selector,format){
    $(function() {
        $(selector).datepicker({
            showOn: 'button',
            buttonImage: '/img/calendar.gif',
            buttonImageOnly: true,
            dateFormat: format,
            option: $.datepicker.regional['ru']
        });
    });
}



