/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
var lReloadPage = false;
document.onkeyup = KeyCheck;

function KeyCheck(KeyCode){
   var KeyID = (window.event) ? event.keyCode : KeyCode.keyCode;
   switch(KeyID)
   {
   case 27:
	   lShowSlideShow = false;
	   cancelFrm();
   }
}

function cancelFrm(){
	lFormOpened = false;
	$('#helpcnt').fadeOut('fast');
	$('#additional').html('');
	$('#errorpreview').html('');
        if (lReloadPage){
            window.location.reload();
        }
}

function showhide(cTagId){
    if (document.getElementById(cTagId).style.display=='block'){
        $('#'+cTagId).css({'display': 'none'}, 500);
    }else{
        $('#'+cTagId).css({'display': 'block'}, 500);
    }
}

function showcommentform(cTagId, lShowLine, lSideForm){
    if (document.getElementById(cTagId).style.display=='block'){
        $('#'+cTagId).css({'display': 'none'}, 500);
    }else{
        $('#'+cTagId).css({'display': 'block'}, 500);
        if (lSideForm){
            $('#'+cTagId+' #comment_textarea_side').focus();
        }else{
            $('#'+cTagId+' #comment_textarea').focus();
        }
    }
    if (lShowLine){
        if (!(document.getElementById('line_'+cTagId)==null)&&document.getElementById('line_'+cTagId).style.display=='none'){
            $('#line_'+cTagId).css({'display': 'block'}, 500);
        }else{
            $('#line_'+cTagId).css({'display': 'none'}, 500);
        }
    }
}

function setShadow(lTransparent){
	var nScroll = 0;
	if(!document.body.scrollTop)
    {
     nScroll = document.documentElement.scrollTop;
    } else {
     nScroll = document.body.scrollTop;
    }
	if (!lTransparent){
		$('#helpcntback').css({'background':'#000000'});
	}else{
		$('#helpcntback').css({'background':'none'});
	}
	document.getElementById('helpcnt').style.width=document.body.offsetWidth+"px";
	document.getElementById('helpcnt').style.height=document.getElementById('maincontainer').offsetHeight+"px";
	document.getElementById('helpcnt').style.top=nScroll+"px";
	document.getElementById('helpcnt').style.left="0px";
	document.getElementById('helpcntback').style.width=document.body.offsetWidth+"px";
	document.getElementById('helpcntback').style.height=document.getElementById('maincontainer').offsetHeight+"px";
	document.getElementById('helpcntback').style.left="0px";
	//document.body.style.overflow = 'hidden';
}

function photopreview(iPhotoId, lComment, user){
    $.post("system/modules/board/forms/photopreview.php", {iPhotoId:iPhotoId, lComment:lComment, user:user}, function(cHtml){
        setShadow();
        $('#helpcnt').fadeIn('fast');
        $('#additional').css({'top':'100px', 'width':'100%', 'left':'0px'});
        $('#additional').html(cHtml);
        $('#additional').css({'z-index':'203'});
    })
}

function cityphotopreview(iPhotoId, lComment, user){
    $.post("system/modules/board/forms/cityphotopreview.php", {iPhotoId:iPhotoId, lComment:lComment, user:user}, function(cHtml){
        setShadow();
        $('#helpcnt').fadeIn('fast');
        $('#additional').css({'top':'100px', 'width':'100%', 'left':'0px'});
        $('#additional').html(cHtml);
        $('#additional').css({'z-index':'203'});
    })
}

function addPhotoComment(iPhotoId, iUserId){
    cComment = $('#photo_comment_textarea_'+iPhotoId).val();
    $.post("system/modules/board/functions/addComment.php", {comment_textarea:cComment, photoid:iPhotoId, user:iUserId}, function(cHtml){
        if ($('#show_comments_all_photo_'+iPhotoId)!='null'){
            $('#show_comments_all_photo_'+iPhotoId).html(cHtml);
        }else{
            $('#comment_table').html(cHtml);
        }
        $('#photo_comment_textarea_'+iPhotoId).val('komentaras');
        if ($('#comment_count_'+iPhotoId)!=null){
            $('#comment_count_'+iPhotoId).html(parseInt($('#comment_count_'+iPhotoId).html())+1);
        }else{
            $('#comment_count').html(parseInt($('#comment_count').html())+1);
        }
        lReloadPage = true;
    });
}

function addCityPhotoComment(iPhotoId, iUserId){
    cComment = $('#photo_comment_textarea_'+iPhotoId).val();
    $.post("system/modules/board/functions/addCityComment.php", {comment_textarea:cComment, photoid:iPhotoId, user:iUserId}, function(cHtml){
        if ($('#show_comments_all_photo_'+iPhotoId)!='null'){
            $('#show_comments_all_photo_'+iPhotoId).html(cHtml);
        }else{
            $('#comment_table').html(cHtml);
        }
        $('#photo_comment_textarea_'+iPhotoId).val('komentaras');
        if ($('#comment_count_'+iPhotoId)!=null){
            $('#comment_count_'+iPhotoId).html(parseInt($('#comment_count_'+iPhotoId).html())+1);
        }else{
            $('#comment_count').html(parseInt($('#comment_count').html())+1);
        }
        lReloadPage = true;
    });
}

function addUserComment(toUser, iUserId){
    cComment = $('#user_comment_textarea_'+toUser).val();
    $.post("system/modules/board/functions/addComment.php", {lAddUserComment: true, text:cComment, userTo:toUser, userFrom:iUserId}, function(cHtml){
        $('#comment_table').html(cHtml);
        $('#user_comment_textarea_'+toUser).val('komentaras');
        $('#comment_count').html(parseInt($('#comment_count').html())+1);
        lReloadPage = true;
    });
}

function limitText(limitField, limitNum) {
	if (limitField.value.length > limitNum) {
		limitField.value = limitField.value.substring(0, limitNum);
	}
}

function checkMusicFile(form, file){
    extArray = new Array("mp3");
    allowSubmit = false;
    if (!file) return;
//    while (file.indexOf("\\") != -1)
//    file = file.slice(file.indexOf("\\") + 1);
//    ext = file.slice(file.indexOf(".")).toLowerCase();
    ext = getFileExtension(file);
    ext = ext.toLowerCase();
    for (var i = 0; i < extArray.length; i++) {
    if (extArray[i] == ext) { allowSubmit = true; break; }
    }
    if (allowSubmit) return true;
    else
    alert("Netinkamas failo tipas. Kolkas galimi muzikos failo tipai yra:  "
    + (extArray.join("  ")) + "\nPasirinkit naują "
    + "failą įkėlimui.");
    return false;
}

function checkPhotoFile(form, file){
    extArray = new Array("jpg", "gif", "png");
    allowSubmit = false;
    if (!file) return;
//    while (file.indexOf("\\") != -1)
//    file = file.slice(file.indexOf("\\") + 1);
//    ext = file.slice(file.indexOf(".")).toLowerCase();
    ext = getFileExtension(file);
    ext = ext.toLowerCase();
    for (var i = 0; i < extArray.length; i++) {
    if (extArray[i] == ext) { allowSubmit = true; break; }
    }
    if (allowSubmit) return true;
    else
    alert("Netinkamas failo tipas. Kolkas galimi nuotraukos failo tipai yra:  "
    + (extArray.join("  ")) + "\nPasirinkit naują "
    + "failą įkėlimui.");
    return false;
}

function getFileExtension(filename)
{
  var ext = /^.+\.([^.]+)$/.exec(filename);
  return ext == null ? "" : ext[1];
}

function setVotePictures(cImg, cImg2, nCount, cTag){
    for (var i = 1; i < 6; i++) {
        if (i<=nCount){
            $('#'+cTag+'_'+i).attr('src', cImg);
        }else{
            $('#'+cTag+'_'+i).attr('src', cImg2);
        }
    }
}

function saveVotes(cType, nVote, rec, user){
    if (cType=='book'){
        $.post("system/modules/board/functions/AddVote.php", {cType:cType, nVote:nVote, book:rec, user:user}, function(cHtml){
           window.location.reload();
        });
    }else if (cType=='game'){
        $.post("system/modules/board/functions/AddVote.php", {cType:cType, nVote:nVote, game:rec, user:user}, function(cHtml){
           window.location.reload();
        });
    }else if (cType=='event'){
        $.post("system/modules/board/functions/AddVote.php", {cType:cType, nVote:nVote, event:rec, user:user}, function(cHtml){
           window.location.reload();
        });
    }else if (cType=='movie'){
        $.post("system/modules/board/functions/AddVote.php", {cType:cType, nVote:nVote, movie:rec, user:user}, function(cHtml){
           window.location.reload();
        });
    }else if (cType=='music'){
        $.post("system/modules/board/functions/AddVote.php", {cType:cType, nVote:nVote, music:rec, user:user}, function(cHtml){
           window.location.reload();
        });
    }else if (cType=='joke'){
        $.post("system/modules/board/functions/AddVote.php", {cType:cType, nVote:nVote, joke:rec, user:user}, function(cHtml){
           window.location.reload();
        });
     }else if (cType=='test'){
        $.post("system/modules/board/functions/AddVote.php", {cType:cType, nVote:nVote, test:rec, user:user}, function(cHtml){
           window.location.reload();
        });
    }
}

function saveCityVotes(cType, nVote, rec, user){
    if (cType=='event')
    {
        $.post("system/modules/board/functions/AddCityVote.php", {cType:cType, nVote:nVote, event:rec, user:user}, function(cHtml){
           window.location.reload();
        });
    }
    else if (cType=='imones')
    {
        $.post("system/modules/board/functions/AddCityVote.php", {cType:cType, nVote:nVote, imones:rec, user:user}, function(cHtml){
           window.location.reload();
        });
    }
    else if (cType=='veikla')
    {
        $.post("system/modules/board/functions/AddCityVote.php", {cType:cType, nVote:nVote, veikla:rec, user:user}, function(cHtml){
           window.location.reload();
        });
    }
    else if (cType=='skelbimai')
    {
        $.post("system/modules/board/functions/AddCityVote.php", {cType:cType, nVote:nVote, skelbimai:rec, user:user}, function(cHtml){
           window.location.reload();
        });
    }
}


function checkSearchForm(form){
    if (empty(form.search_email.value)){
        alert('Neįvestas ieškomas el. pašto adresas');
        form.search_email.focus();
        return false;
    }
    if (form.search_email.value.length<3){
        alert('Paieškos užklausą turi sudaryti bent 3 simboliai');
        form.search_email.focus();
        return false;
    }
    return true;
}

function empty(cValue){
    if (cValue=='') return true;
    if (cValue==0) return true;
    if (typeof(cValue)=='undefined') return true;
    if (cValue==null) return true;
    return false;
}

function removePart(cType, iRec, user){
    lConfirm = confirm('Ar tikrai norite pašalinti įrašą?');
    if (!lConfirm){ return;}
    $.post('system/modules/board/functions/delRec.php', {cType:cType, iRec:iRec, user:user}, function(){
       window.location.reload();
    });
}

function removeCityPart(cType, iRec, user){
    lConfirm = confirm('Ar tikrai norite pašalinti įrašą?');
    if (!lConfirm){ return;}
    $.post('system/modules/board/functions/delCityRec.php', {cType:cType, iRec:iRec, user:user}, function(){
       window.location.reload();
    });
}

function delPhotoComment(cType, iRec, user, iPhotoId){
    lConfirm = confirm('Ar tikrai norite pašalinti įrašą?');
    if (!lConfirm){ return;}
    $.post('system/modules/board/functions/delRec.php', {cType:cType, iRec:iRec, user:user}, function(){
       lReloadPage = false;
       cancelFrm();
       photopreview(iPhotoId, true, user);
       lReloadPage = true;
    });
}

function delCityPhotoComment(cType, iRec, user, iPhotoId){
    lConfirm = confirm('Ar tikrai norite pašalinti įrašą?');
    if (!lConfirm){ return;}
    $.post('system/modules/board/functions/delCityRec.php', {cType:cType, iRec:iRec, user:user}, function(){
       lReloadPage = false;
       cancelFrm();
       cityphotopreview(iPhotoId, true, user);
       lReloadPage = true;
    });
}

function ShowTestForm(test, user, id){
    var xyobj = $('#' + id).position();
    var objleft = xyobj.left;
    var objtop = xyobj.top;
    $.post("system/modules/board/forms/test.php", {test:test, user:user}, function(textHtml){
       ShowForm(textHtml, objleft, objtop);
    });
}

function ShowForm(htmlText, left, top){
    var newdiv = document.createElement("div");
    var backdiv = document.createElement("div");
    var appendTo = document.getElementById("maincontainer");
    newdiv.setAttribute("id", "new_form");
    backdiv.setAttribute("id", "form_back");
    appendTo.appendChild(backdiv);
    appendTo.appendChild(newdiv);
    $("#form_back").css({'top':top+'px'})
    $("#new_form").css({'left':'41%', 'top':top+200+'px'});
    $("#new_form").html(htmlText);
    document.body.style.overflow = "hidden";
}

function CloseForm(Id){
    var removeFrom = document.getElementById("maincontainer");
    remNode = document.getElementById(Id);
    removeFrom.removeChild(remNode);
    remNode = document.getElementById("form_back");
    removeFrom.removeChild(remNode);
    document.body.style.overflow = "auto";
}

function changeWallType(_type)
{
        $.post("system/modules/board/functions/ChangeWallType.php", {type:_type}, function(cHtml){
                //window.location.reload();
                window.location.href = '/';
        });
}

function postCityChatMessage(msg)
{
        $.post("system/modules/board/functions/AddCityChat.php", {citychat:msg}, function(cHtml){
                //window.location.reload();
                getNewCityChatMessages();
                $('#citychat').val('');
        });
}

function handleChatKeyPress(e,form)
{
        var key=e.keyCode || e.which;
        if (key==13)
        {
                postCityChatMessage($('#citychat').val());
                return false;
        }
        return true;
}

var lastCityChatId;
function getNewCityChatMessages()
{
        $.post("system/modules/board/functions/GetNewChat.php", {lastchatid:lastCityChatId, page: 1}, function(cHtml){
                //alert('Posting on '+'#citychat'+lastCityChatId);
                $('#citychat_box1').html(cHtml);
                //$('#citychat'+lastCityChatId).before(cHtml);
        });
        $.post("system/modules/board/functions/GetNewChat.php", {lastchatid:lastCityChatId, page: 2}, function(cHtml){
                $('#citychat_box2').html(cHtml);
        });
        $.post("system/modules/board/functions/GetNewChat.php", {lastchatid:lastCityChatId, page: 3}, function(cHtml){
                $('#citychat_box3').html(cHtml);
        });
}

function autoGetNewCityChatMessages (notfirst)
{
        if (typeof notfirst == "undefined")
        {

        }
        else
        {
                getNewCityChatMessages();
        }

        setTimeout(function(){ autoGetNewCityChatMessages(true); }, 10000);
}

$(function()
{
        if ($('#citychat').length > 0)
        {
                autoGetNewCityChatMessages();
        }
});

function youtubeIDextract(url) 
{ 
    var youtube_id; 
    youtube_id = url.replace(/^[^v]+v.(.{11}).*/,"$1"); 
    return youtube_id; 
}

var currentImgNo = 0;
function getMindImages()
{
    _videoUrl = $('#input_mind_share').val();
    if (_videoUrl.match(/watch\?v=([a-zA-Z0-9\-_]+)/))
    {
        youtubeId = youtubeIDextract(_videoUrl);
        $('#share_form #youtube-video').remove();
        $('#share_form').addClass('share_form_bigger');
        $('#share_form').append("<div id=\"youtube-video\"><iframe width=\"177\" height=\"150\" src=\"http://www.youtube.com/embed/" + youtubeId + "\" frameborder=\"0\" allowfullscreen></iframe></div>");
        $('#imgpreferno').val(youtubeId);
    }
    else
    {
        $.post("system/modules/board/functions/GetMindImages.php", {share_mind:$('#input_mind_share').val()}, function(cHtml){
                if (cHtml.msg == 'ok')
                {
                        var size = cHtml.size;
                        //var aImg;
                        for (var key in cHtml.images)
                        {
                                if (!cHtml.images.hasOwnProperty(key)) { continue; }
                                //alert(key + " -> " + cHtml.images[key]);
                                var iImgNo = key;
                                var cImgLink = cHtml.images[iImgNo];

                                //aImg[iImgNo] = cImgLink;
                        }

                        $('#imgpreferno').val(currentImgNo);
                        $('#imgprefer').attr('src', cHtml.images[currentImgNo]);
                        var imgNoStr = (currentImgNo * 1 + 1);
                        $('#mind_images_count').html(imgNoStr+' iš '+size);
                        $('#mind_title').html(cHtml.title);
                        $('#img_mind_table').show();
        		$('#share_form').addClass('share_form_bigger');
        	}
        	else
        	{
                        $('#img_mind_table').hide();
        		$('#share_form').removeClass('share_form_bigger');
                        $('#share_form #youtube-video').remove();
		}
        }, 'json');
    }
}

function getNextMindImage()
{
        ccurrentImgNo = currentImgNo * 1 + 1;
        $.post("system/modules/board/functions/GetMindImages.php", {share_mind:$('#input_mind_share').val(), imgpreferno: ccurrentImgNo}, function(cHtml){
                if (cHtml.msg == 'ok')
                {
                        var size = cHtml.size;
                        currentImgNo = cHtml.imgno;

                        $('#imgpreferno').val(currentImgNo);
                        $('#imgprefer').attr('src', cHtml.imglink);
                        var imgNoStr = (currentImgNo * 1 + 1);
                        $('#mind_images_count').html(imgNoStr+' iš '+size);
                        $('#mind_title').html(cHtml.title);
                        $('#img_mind_table').show();
        		$('#share_form').addClass('share_form_bigger');
        	}
        	else
        	{
                        $('#img_mind_table').hide();
        		$('#share_form').removeClass('share_form_bigger');
		}
        }, 'json');
}

function getPrevMindImage()
{
        ccurrentImgNo = currentImgNo * 1 - 1;
        $.post("system/modules/board/functions/GetMindImages.php", {share_mind:$('#input_mind_share').val(), imgpreferno: ccurrentImgNo}, function(cHtml){
                if (cHtml.msg == 'ok')
                {
                        var size = cHtml.size;
                        currentImgNo = cHtml.imgno;

                        $('#imgpreferno').val(currentImgNo);
                        $('#imgprefer').attr('src', cHtml.imglink);
                        var imgNoStr = (currentImgNo * 1 + 1);
                        $('#mind_images_count').html(imgNoStr+' iš '+size);
                        $('#mind_title').html(cHtml.title);
                        $('#img_mind_table').show();
        		$('#share_form').addClass('share_form_bigger');
        	}
        	else
        	{
                        $('#img_mind_table').hide();
        		$('#share_form').removeClass('share_form_bigger');
		}
        }, 'json');
}

var lastCityChatIdFirstPage;
function getNewCityChatMessagesFirstPage()
{
        $.post("system/modules/board/functions/GetNewChatFirstPage.php", {lastchatid:lastCityChatIdFirstPage}, function(cHtml){
                if (cHtml.msg == 'ok')
                {
                        $('#citychatfirstpage').prepend(cHtml.html);
                        lastCityChatIdFirstPage = cHtml.newId;
                        $('#chat_'+lastCityChatIdFirstPage).slideDown();
                }
        }, 'json');
}

function autoGetNewCityChatMessagesFirstPage (notfirst)
{
        if (typeof notfirst == "undefined")
        {

        }
        else
        {
                getNewCityChatMessagesFirstPage();
        }

        setTimeout(function(){ autoGetNewCityChatMessagesFirstPage(true); }, 5000);
}

$(function()
{
        if ($('#citychatfirstpage').length > 0)
        {
                autoGetNewCityChatMessagesFirstPage();
        }
});

function refreshBoardByPage(option, page)
{
    link = "index.php";
    $.get(link, {SHOW_SHARE_FORM:"1", walloption:option}, function(respond){
       $("#share_form").html(respond); 
    });
    refreshContainer(link, option, page, "board");
}

function refreshBoard(option)
{
    link = "index.php";
    $.get(link, {SHOW_SHARE_FORM:"1", walloption:option}, function(respond){
       $("#share_form").html(respond); 
    });
    refreshContainer(link, option, 1, "board");
}

function refreshContainer(link, option, page, tabId)
{
    $.get(link, {walloption:option, showPage:page}, function(respond){
       $("#"+tabId).html(respond); 
    });
}

