var buff1;
var buff2;
var buff3 = '';
var defaultImageThumb = '';
var imagesExtensions = new Array(".jpg", "jpeg", ".bmp", ".gif", ".png");

startList = function() {
	if (document.all&&document.getElementById) {
	navRoot = document.getElementById("nav");
	for (i=0; i<navRoot.childNodes.length; i++) {
	node = navRoot.childNodes[i];
	if (node.nodeName=="LI") {
	node.onmouseover=function() {
	this.className+=" over";
	  }
	  node.onmouseout=function() {
	  this.className=this.className.replace(" over", "");
	   }
	   }
	  }
	 }
	}

mySlider = function(){
	$("#slider").easySlider({
	continuous: true,
  vertical: true,
  auto: true,
  pause: 3000,
  speed: 1400,
  numeric: true
});
};

// Ajax functionality //

var runX = 1;

function pause(){
	return true;
}

function ReloadWait() {

        setTimeout( "ReloadWait();", 100);
}

function getHTTPObject(){
        if (window.ActiveXObject)
        return new ActiveXObject("Microsoft.XMLHTTP");
        else if (window.XMLHttpRequest)
        return new XMLHttpRequest();
        else {
                alert("Your browser does not support AJAX.");
                return null;
        }
}

function setContent() {
        /* táto podmienka je splnená, v prípade, že request dopadol dobre */
        if(httpObject.readyState == 4){
            if (httpObject.status == 200) {
                document.getElementById('contentReload').innerHTML = httpObject.responseText;
                $('#contentReload').fadeTo('slow', 1, function() {
                    // Animation complete
                });
            }
        }
        /* httpObject.responseText = výstup PHP funkcie. Čokoľvek čo PHP funkcia vráti, sa dostáva do tejto premennej */
}

function pageReload(link){
    httpObject = getHTTPObject();
    
        if (httpObject != null) {
            httpObject.open("GET", link, true);
            httpObject.onreadystatechange = setContent;
            httpObject.send(null);
        }
}

function contentReload(link){

    $('#contentReload').fadeTo('slow', 0, function() {
        pageReload(link);
    });

    

    return false;
}

jQuery.fn.fadeTo = function(speed,to,callback) {
    return this.animate({opacity: to}, speed, function() {
        if (to == 1 && jQuery.browser.msie)
          this.style.removeAttribute('filter');
        if (jQuery.isFunction(callback))
            callback();
    });
};

function autoFireLightbox() {
                        myLightbox = new Lightbox;
			//Check if location.hash matches a lightbox-anchor. If so, trigger popup of image.
			setTimeout(function() {
					myLightbox.start($('campaignHref'));
				},
				250
			);
		}

// Ajax functionality //

runOnLoad = function() {

	startList();
	externalLinks();
autoFireLightbox();
  // $('a.gallery').lightBox(); // Select all links with lightbox class

  $('#fileeditor_tree').fileTree({ script: '/filetree',
                                   root: './data/',
                                   folderclicked: function(node) { $('input.folder_path').val(node);}
                               }, function(file) {
                                 document.getElementById('full_file_path_input').value = file;
                                 updateThumbInFileTree( file )});

  // for action buttons in forms
  $('input[type=image]')
  .unbind('mousedown')
  .mousedown(function(){ $(this).after('<input type="hidden" name="'+$(this).attr('name')+'" value="'+$(this).attr('value')+'" />'); });

}

function sendFileName (s){
    var selvalue = s.value;
    if ( selvalue != '')
    {
        if ( window.opener.document.getElementById('src') != null )
            window.opener.document.getElementById('src').value = selvalue;
        if ( window.opener.document.getElementById('href') != null )
            window.opener.document.getElementById('href').value = selvalue;
        window.close();
    }
  }

function getWidthAndHeight() {
    buff1 = this.width;
    buff2 = this.height;

    var divider = 0;
    var maxDim = document.getElementById('file_thumb').clientWidth;

    var margSent = '';

    if ( buff1 >= buff2 ) //width is bigger then height
    {
        divider = maxDim / buff1;
        buff1 = Math.floor(buff1 * divider);
        buff2 = Math.floor(buff2 * divider);

        var topImgMargin = Math.floor((maxDim - buff2) / 2 );

        margSent = 'margin: ' + topImgMargin + 'px 0px ' + topImgMargin + 'px 0px;';
    }
    else if ( buff1 < buff2 )
    {
        divider = maxDim / buff2;
        buff1 = Math.floor(buff1 * divider);
        buff2 = Math.floor(buff2 * divider);

        var sizeImgMargin = Math.floor((maxDim - buff1) / 2 );

        margSent = 'margin: 0px ' + sizeImgMargin + 'px 0px ' + sizeImgMargin + 'px;';
    }

    buff3 = 'style=" width:' + buff1 + 'px; height:' + buff2 + 'px; ' + margSent + '"';

    if (defaultImageThumb.length == 0)
        defaultImageThumb = document.getElementById('file_thumb').innerHTML;

    document.getElementById('file_thumb').innerHTML = '<img src="' + this.name + '" ' + buff3 + ' />';
    return true;
}

function isFileImage( fileName )
{
    var pos = fileName.lastIndexOf(".");
    var ext = fileName.substr(pos);
    ext = ext.toLowerCase();

    for (var i = 0; i < imagesExtensions.length; i++)
    {
        //check to see if it's the proper extension
        if (imagesExtensions[i] == ext)
        {
            //it's the proper extension
            return true;
        }
    }

    return false;
}

function updateThumbInFileTree ( fileName )
{
    if ( isFileImage( fileName ) )
    {
        image_1 = new Image();
        image_1.name = fileName;
        image_1.onload = getWidthAndHeight;
        image_1.src = fileName;
    }
    else
    {
        if ( defaultImageThumb.length != 0 )
            document.getElementById('file_thumb').innerHTML = defaultImageThumb;
    }
}

window.onload=runOnLoad;

