/**
 * 
 */

var appRoot = '';
var ajaxOnWork = false;
var ajaxAbort = false;
var lastScrollPosition = 0;
var cruiseTabPostion = 0;

function createAjaxDivs() 
{ 
    var _body = document.getElementsByTagName('body') [0];
    var _div = document.createElement('div');
    _div.setAttribute('id','formContainer');
    _div.setAttribute('name','formContainer');
    _div.style.zIndex = '10000';
    _body.appendChild(_div);
}

function loadDiv(divId, url, token, endFunc, endFuncParamsArr,inttimeout,noHistorySet) {


    //	if(!noHistorySet){
    //	$.history.loadDivCheck = true;
    //	$.history.load("param1="+divId+"&param2="+url+"&param3="+token);
    //	}
	
	
    /*    if (divId == 'cover') {

        divCoverScreen('cover');
        document.getElementById('cover').style.display = 'block';
        divAlignCenter('cover');
        document.getElementById('cover').focus();
        document.getElementById('cover').style.zIndex = '4000';
    }
     */
		
    var param = 'JIMET_TOKEN=' + token;

    var pageLoader = new JimetPageLoaderObj();
    if (endFunc)
        pageLoader.setEndFunc(endFunc);
    if (endFuncParamsArr)
        pageLoader.setEndFuncParams(endFuncParamsArr);

    if(inttimeout)
        pageLoader.setTimeout(inttimeout);


    pageLoader.doLoad(divId, url, param);

}

function wrapperEscKey(e) {
    keycode = 0;
    eventObj = null;

    if (!e)
        e = window.event;

    if (e.keyCode)
        keycode = (e.keyCode);
    else
        keycode = (e.charCode);

    var targ;
    if (e.target)
        targ = e.target;
    else if (e.srcElement)
        targ = e.srcElement;

    if (keycode == 27) {
        var but = document.getElementById('cancelBtn');
        if (but)
            but.click();
        else if (document.getElementById('wrapper'))
            document.getElementById('wrapper').style.overflowY = 'auto';

        document.getElementById('cover').style.display = 'none';
        document.getElementById('cover').innerHTML = '';

        try {
            tt_HideInit();
        } catch (e) {
        }

    }

}

function divCoverScreen(divId) {
    var pgMainDiv = document.getElementById(divId);
    var height = document.body.clientHeight;
    var width = document.body.clientWidth;


    if (document.getElementById('wrapper')
        && document.getElementById('wrapper').style.display != 'none')
        pgMainDiv.style.top = document.getElementById('wrapper').scrollTop;
    else
        pgMainDiv.style.top = 0 + 'px';

    // pgMainDiv.style.top = 0;
    pgMainDiv.style.left = 0 + 'px';
    pgMainDiv.style.height = height + 'px';
    pgMainDiv.style.width = width + 'px';
}
function hideCover(){

    document.getElementById('cover').style.display = 'none';
    document.getElementById('cover').innerHTML = '';
}
function divAlignCenter(divId) {
    var pgMainDiv = document.getElementById(divId);
    // pgMainDiv.style.position='absolute';
    // pgMainDiv.style.display = 'block';

    var height = pgMainDiv.offsetHeight;
    var width = pgMainDiv.offsetWidth;
    var top = (document.body.clientHeight - height) / 2;
    var left = (document.body.clientWidth - width) / 2;
    // alert ("DIV HEIGHT : " + height + " PG HEIGHT :" +
    // document.body.clientHeight + " TOP POS : " + top);
    // alert ("DIV WIDTH : " + width + " PG WIDTH :" + document.body.clientWidth
    // + " LEFT POS : " + left);
    pgMainDiv.style.top = top;
    pgMainDiv.style.left = left;
    pgMainDiv.style.height = height;
    pgMainDiv.style.width = width;
}
function selectAllForTable(selection, type) {
    var loop = 0;
    if (selection == null)
        return;

    if (selection.length) {
        selection.length;
        if (type == 1) {
            for (i = 0; i < selection.length; i++)
                selection[i].checked = true;
        } else {
            for (i = 0; i < selection.length; i++)
                selection[i].checked = false;
        }
    } else {
        if (type == 1) {
            selection.checked = true;
        } else {
            selection.checked = false;
        }
    }
}

function processTableAction(frmId, token) {
    if (frmId == 'adminListForm') {
        var url = '/admin/common/process/errorTable.jsp';
        var params = token + '&process=ADMIN_LIST_ACTION';
        var tableAction = document.getElementById('tableAction').options[document
        .getElementById('tableAction').selectedIndex].value;
        params += '&tableAction=' + tableAction;
        var selectedIds = '';
        var selection = document.adminListForm.actionSB;

        if (selection.length) {
            for (i = 0; i < selection.length; i++) {
                if (selection[i].checked)
                    selectedIds += selection[i].value + ',';
            }
        } else {
            if (selection.checked)
                selectedIds += selection.value;
        }

        params += '&objIds=' + selectedIds;
        loadDiv('cover', url, params);
    } else if (frmId == 'tourListForm') {//example
        url = '/admin/common/process/errorTable.jsp';
        params = token + '&process=TOUR_DELETE';
        tableAction = document.getElementById('tableAction').options[document
        .getElementById('tableAction').selectedIndex].value;
        params += '&tableAction=' + tableAction;
        selectedIds = '';
        selection = document.tourListForm.actionSB;

        if (selection.length) {
            for (i = 0; i < selection.length; i++) {
                if (selection[i].checked)
                    selectedIds += selection[i].value + ',';
            }
        } else {
            if (selection.checked)
                selectedIds += selection.value;
        }
        params += '&objIds=' + selectedIds;
        loadDiv('cover', url, params);
    }
}

/*
Developed by Robert Nyman, http://www.robertnyman.com
Code/licensing: http://code.google.com/p/getelementsbyclassname/
 */	
var getElementsByClassName = function (className, tag, elm){
    if (document.getElementsByClassName) {
        getElementsByClassName = function (className, tag, elm) {
            elm = elm || document;
            var elements = elm.getElementsByClassName(className),
            nodeName = (tag)? new RegExp("\\b" + tag + "\\b", "i") : null,
            returnElements = [],
            current;
            for(var i=0, il=elements.length; i<il; i+=1){
                current = elements[i];
                if(!nodeName || nodeName.test(current.nodeName)) {
                    returnElements.push(current);
                }
            }
            return returnElements;
        };
    }
    else if (document.evaluate) {
        getElementsByClassName = function (className, tag, elm) {
            tag = tag || "*";
            elm = elm || document;
            var classes = className.split(" "),
            classesToCheck = "",
            xhtmlNamespace = "http://www.w3.org/1999/xhtml",
            namespaceResolver = (document.documentElement.namespaceURI === xhtmlNamespace)? xhtmlNamespace : null,
            returnElements = [],
            elements,
            node;
            for(var j=0, jl=classes.length; j<jl; j+=1){
                classesToCheck += "[contains(concat(' ', @class, ' '), ' " + classes[j] + " ')]";
            }
            try	{
                elements = document.evaluate(".//" + tag + classesToCheck, elm, namespaceResolver, 0, null);
            }
            catch (e) {
                elements = document.evaluate(".//" + tag + classesToCheck, elm, null, 0, null);
            }
            while ((node = elements.iterateNext())) {
                returnElements.push(node);
            }
            return returnElements;
        };
    }
    else {
        getElementsByClassName = function (className, tag, elm) {
            tag = tag || "*";
            elm = elm || document;
            var classes = className.split(" "),
            classesToCheck = [],
            elements = (tag === "*" && elm.all)? elm.all : elm.getElementsByTagName(tag),
            current,
            returnElements = [],
            match;
            for(var k=0, kl=classes.length; k<kl; k+=1){
                classesToCheck.push(new RegExp("(^|\\s)" + classes[k] + "(\\s|$)"));
            }
            for(var l=0, ll=elements.length; l<ll; l+=1){
                current = elements[l];
                match = false;
                for(var m=0, ml=classesToCheck.length; m<ml; m+=1){
                    match = classesToCheck[m].test(current.className);
                    if (!match) {
                        break;
                    }
                }
                if (match) {
                    returnElements.push(current);
                }
            }
            return returnElements;
        };
    }
    return getElementsByClassName(className, tag, elm);
};
var activeTabIdx = 0;

function tabClick(idx)
{
    clicked = document.getElementById('tabHeaders').getElementsByTagName('div')[idx];
    clickedContent = getElementsByClassName('tabContent','div',document.getElementById('tabBoxMiddle'))[idx];
	
    activeContent = null;
    activeTab = null;
    if(activeTabIdx!=null){
        activeTab = document.getElementById('tabHeaders').getElementsByTagName('div')[activeTabIdx];
        activeContent = getElementsByClassName('tabContent','div',document.getElementById('tabBoxMiddle'))[activeTabIdx];
    }
	
    if(activeTab!=null&&activeTab!=clicked)
    {
        activeTab.style.marginTop = '0px';
        activeContent.style.display = 'none';
    }
    clicked.style.marginTop = '-6px';
    clickedContent.style.display = 'block';
    activeTabIdx = idx;
}

function next(elem) {
    do {
        elem = elem.nextSibling;
    } while (elem && elem.nodeType != 1);
    return elem;                
}

function ShowFileInfo( fileUrl, data )
{
    var msg = 'The selected file URL is: ' + fileUrl + '\n\n';
    // Display additional information available in the "data" object.
    // For example, the size of a file (in KB) is available in the data["fileSize"] variable.
    msg += 'File size: ' + data['fileSize'] + 'KB\n';
    msg += 'Last modifed: ' + data['fileDate'];

    alert( msg );
}

function startFileManager()
{
    // This is a sample function which is called when a file is selected in CKFinder.

    var finder = new CKFinder();
    finder.SelectFunction = ShowFileInfo;
    // It is not required to use the "v1" skin.
    finder.Skin = 'v1';
    finder.popup();
}
function clearSelectOptions(selectId){
    var sel = document.getElementById(selectId);
    for (var i=sel.length-1;i>=0;i--){
        if(sel.options[i].value!='-1')
        sel.options[i] = null;
    }
}
function loadSelect(selectId,url,params,required,empty,endFunc){
    var inputLoader = new JimetInputLoaderObj();
    if (empty == true){
        var sel = document.getElementById(selectId);
        for (var i=sel.length-1;i>=0;i--){
            sel.options[i] = null;
        }
    }
    if (endFunc)
        inputLoader.setEndFunc(endFunc);

    inputLoader.doLoad(selectId,url,params,required);
}

function cleanIllegalCharacters(string){
    var turk = new Array ("ç","ğ","ı","ö","ş","ü");
    var ing = new Array ("c","g","i","o","s","u");  
    var clean = string.toLowerCase();
    for (j=0; j < clean.length; j++) {
        for (i=0;i<turk.length;i++) {
            clean = clean.replace(turk[i],ing[i]);
        }
    }
    clean = clean.replace(/\W/g, "_"); 
    return clean + '.html';
}

function reloadImg(id) {
    var obj = document.getElementById(id);
    var src = obj.src;
    var newsrc = src.split('&preventcache=')[0];
    var date = new Date();
    obj.src = newsrc + '&preventcache=' + date.getTime();
    return false;
}

function openWindow(url){
	window.open(url);
}


$(document).ready(function(){
	$('.colorbox').click(function() {
		$(this).colorbox({href: $(this).attr('src')});
});});

