
var BWflag = false;
var HMflag = false;
var VMflag = false;


var Initialized = false;

var Cropflag = false;
var Gridflag = false;

var mousex = 0;
var mousey = 0;
var grabx = 0;
var graby = 0;
var orix = 0;
var oriy = 0;
var elex = 0;
var eley = 0;
var algor = 0;
var drag_boty = 0;
var drag_rightx = 0;

var dragobj = null;


var GridH=0;
var Top=0;
var Bottom=0;



function falsefunc() { 
	return false; 
}

function init() {
  document.onmousemove = update; // update(event) implied on NS, update(null) implied on IE
  update();
}

function getMouseXY(e) { 
	if (!e) e = window.event; // works on IE, but not NS (we rely on NS passing us the event)

	if (e) { 

		if (e.pageX || e.pageY) { // this doesn't work on IE6!! (works on FF,Moz,Opera7)
			mousex = e.pageX;
			mousey = e.pageY;
		}
		else if (e.clientX || e.clientY){ // works on IE6,FF,Moz,Opera7
			mousex = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
			mousey = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
		}
	}
}

function update(e) {
	getMouseXY(e); // NS is passing (event), while IE is passing (null)
}

function grab(context) {
	
	
	document.onmousedown = falsefunc; // in NS this prevents cascading of events, thus disabling text selection
	dragobj = context;
	document.onmousemove = drag;
	document.onmouseup = drop;

	grabx = mousex;
	graby = mousey;
	
	if(dragobj.id=="m_right"){orix=dragobj.parentNode.offsetLeft;}
	else{orix=dragobj.offsetLeft;}
	if(dragobj.id=="m_bottom"){oriy=dragobj.parentNode.offsetTop;}
	else{oriy=dragobj.offsetTop;}

    elex = orix;
	eley = oriy;

	
	update();
}

function drag(e) {
	if (dragobj) {
	
		elex = orix + (mousex-grabx);
		eley = oriy + (mousey-graby);
	 
	 var Max = document.getElementById('mainpic').offsetHeight;    
     var GridH = document.getElementById('grid').offsetHeight;
	 
	 
		switch(dragobj.id) {
			case 'm_top' : if(eley < 8) { eley = 8;}
			
				document.getElementById('top').style.height = eley + 'px';
               
				if((eley+75)>Max){document.getElementById('top').style.height=(Max-75);}
				   if(GridH<=60){ return false;	}
				
				//em = document.getElementById("brumhop");
				//em.firstChild.nodeValue="TOP: X: "+elex+" Y: "+eley+" | ORIX:"+orix+" ORIY:"+oriy;
				break;
			case 'm_left' : if(elex < 8){ elex = 8;}
		
				document.getElementById('left').style.width = elex + 'px';
			
				//em = document.getElementById("brumhop");
				//em.firstChild.nodeValue="LEFT: X: "+elex+" Y: "+eley+" | ORIX:"+orix+" ORIY:"+oriy;
				break;
			case 'm_right' : if(elex > drag_rightx-8) { elex = drag_rightx-8; }
			
				document.getElementById('right').style.width = (drag_rightx-elex) + 'px';
		
				//em = document.getElementById("brumhop");
				//em.firstChild.nodeValue="RIGHT: X: "+elex+" Y: "+eley+" | ORIX:"+orix+" ORIY:"+oriy;
				break;
			case 'm_bottom' : if(eley > drag_boty-8) {eley = drag_boty-8;}
			
				document.getElementById('bottom').style.height = (drag_boty-eley) + 'px';
		
		
		 if((drag_boty-eley)+75>Max){document.getElementById('bottom').style.height=(Max-75);}
	    if(GridH<=70){ return false; }
	
				//em = document.getElementById("brumhop");
				//em.firstChild.nodeValue="BOTTOM X: "+elex+" Y: "+eley+" | ORIX:"+orix+" ORIY:"+oriy;
				break;
		}
	}

	update(e);
	return false; // in IE this prevents cascading of events, thus text selection is disabled
}

function drop() {
	
	if (dragobj) {	
		dragobj.style.zIndex = 0;
		dragobj = null;
	}
	
	update();
	document.onmousemove = update;
	document.onmouseup = null;
	document.onmousedown = null;   // re-enables text selection on NS
}


function InitFilters(){
    if(Initialized) return;
    Initialized = true;
	if(document.all) {
		document.all.mainpic.style.filter = "FlipH FlipV Gray";
		document.all.mainpic.filters["Gray"].enabled = BWflag;
		document.all.mainpic.filters["FlipH"].enabled = HMflag;
		document.all.mainpic.filters["FlipV"].enabled = VMflag;
	}
}
function flags() {
	rf = '?';
	if(BWflag) {
		rf += 'bw&';
	}
	if(HMflag) {
		rf += 'hf&';
	}
	if(VMflag) {
		rf += 'vf&';
	}
	rf += 's='
	return rf;
}
function flipBW(p){
	InitFilters();
	BWflag = !BWflag;
	if(document.all) document.all.mainpic.filters["Gray"].enabled = BWflag;
	else {
		document.getElementById("mainpic").src = "image.php" + flags() +p;
	}
	if(BWflag) document.getElementById("bw").src = "components/gallery/theme/default/images/i_bw_on.gif";
	else {
		document.getElementById("bw").src = "components/gallery/theme/default/images/i_bw.gif";
	}

	return false;
} //---------------------------------------------

function flipHM(p){
    InitFilters();
    HMflag = !HMflag;

    if(document.all) document.all.mainpic.filters["FlipH"].enabled = HMflag;
	else {
		document.getElementById("mainpic").src = "image.php" + flags() + p;
	}
    if(HMflag) document.getElementById("hm").src = "components/gallery/theme/default/images/x_hmirr_on.gif"
    else {
		document.getElementById("hm").src = "components/gallery/theme/default/images/x_hmirr.gif";
	}

	return false;
}

function flipVM(p){
    InitFilters();
    VMflag = !VMflag;

    if(document.all) document.all.mainpic.filters["FlipV"].enabled = VMflag;
	else {
		document.getElementById("mainpic").src = "image.php" + flags() + p;
	}
    if(VMflag) document.getElementById('vm').src = "components/gallery/theme/default/images/x_vmirr_on.gif"
    else {
        document.getElementById('vm').src = "components/gallery/theme/default/images/x_vmirr.gif"
	}
	return false;
}



function flipCrop(){
	Cropflag = !Cropflag;
		

	if(Cropflag){
		if(!document.all) {
		//za mozilla
			document.getElementById("cropper").style.left = document.getElementById("mainpic").offsetLeft;
			document.getElementById("cropper").style.top = document.getElementById("mainpic").offsetTop;
			document.getElementById("grid").style.left = document.getElementById("mainpic").offsetLeft;
			document.getElementById("grid").style.top = document.getElementById("mainpic").offsetTop;
	
		}
		
		document.getElementById("cropperimg").src = "components/gallery/theme/default/images/i_crop_on.gif";
		document.getElementById("cropper").style.display = "";
        //document.getElementById("vmakni").style.display = "";
	}
	else{
		document.getElementById("cropperimg").src = "components/gallery/theme/default/images/i_crop.gif";
		document.getElementById("cropper").style.display = "none";
        //document.getElementById("vmakni").style.display = "none";
			
	}
	
	drag_boty = document.getElementById("m_bottom").parentNode.offsetTop;
	drag_rightx = document.getElementById("m_right").parentNode.offsetLeft;
	
	return false;
}

function SetCrop(left, top, right, bottom){
	document.getElementById("left").style.width = left + 8;
	document.getElementById("right").style.width = right + 8;
	document.getElementById("top").style.height = top + 8;
	document.getElementById("bottom").style.height = bottom + 8;
	
	drag_boty = document.getElementById("m_bottom").parentNode.offsetTop;
	drag_rightx = document.getElementById("m_right").parentNode.offsetLeft;

	if(!Cropflag) flipCrop();
	window.scrollTo(0,0);
}

function AddClipping(){
	
	x1 = parseInt(document.getElementById("left").style.width) - 8; 
	if((x1 < 0) || (!x1)) x1 = 0;
	y1 = parseInt(document.getElementById("top").style.height) - 8; 
	if((y1 < 0) || (!y1)) y1 = 0;
	x2 = parseInt(document.getElementById("right").style.width) - 8; 
	if((x2 < 0) || (!x2)) x2 = 0;
	y2 = parseInt(document.getElementById("bottom").style.height) - 8; 
	if((y2 < 0) || (!y2)) y2 = 0;

	s = '[CROP('+x1+','+y1+','+x2+','+y2+')]';

	document.inputform.comment_message.value += s;
	document.inputform.comment_message.value.focus();
}

function flipGrid(){
	Gridflag = !Gridflag;

	if(Gridflag){
		if(!Cropflag) flipCrop();
		document.all.gridimg.src = "components/gallery/theme/default/images/i_grid_on.gif";
		document.all.grid.style.visibility = "visible";
	}
	else{
		document.all.gridimg.src = "components/gallery/theme/default/images/i_grid.gif";
		document.all.grid.style.visibility = "hidden";
	}
}


function showPopUp(url,name,w,h) {
    if (document.all)
        var xMax = screen.width, yMax = screen.height;
    else
        if (document.layers)
            var xMax = window.outerWidth, yMax = window.outerHeight;
        else
            var xMax = 640, yMax=480;

    var xOffset = (xMax - 200)/2, yOffset = (yMax - 200)/2;

    window.open(url,name,'width='+w+',height='+h+',screenX='+xOffset+',screenY='+yOffset+',top='+yOffset+',left='+xOffset+',copyhistory=no,directories=no, menubar=no,location=no,scrollbars=yes,resizable=no');
}


function expandingWindow(website) {
	var winwidth = screen.availWidth;
	var winheight = screen.availHeight;

	if (document.all) {
	   var sizer = window.open("","",'left=0,top=0,width='+winwidth+',height='+winheight+',scrollbars=auto,fullscreen=yes');
	   sizer.location = website;
	} else {
	   window.open(website,'fotowindow','width=winwidth,height=winheight,menubar=no,status=no,location=no,scrollbars=auto,fullscreen=yes,directories=no,resizable=yes');
	}
}

function setBackground(color,vId){
	
	document.getElementById(vId).style.background = color;
	
	if(document.getElementById("top")){
	document.getElementById("top").style.background = color;
	}
	if(document.getElementById("bottom")){
	document.getElementById("bottom").style.background = color;
	}
	if(document.getElementById("left")){
	document.getElementById("left").style.background = color;
	}
	if(document.getElementById("right")){
	document.getElementById("right").style.background = color;
	}

	switch (color){
		case "#808080":
		case "#666666":
		case "#4d4d4d":
		case "#333333":
		case "#1A1A1A":
		case "#000000":
			if (document.getElementById("lnkSeriesName")){
				document.getElementById("lnkSeriesName").style.color = "#FFFFFF";
			}
			if (document.getElementById("LinkPhoto")){
				document.getElementById("LinkPhoto").style.color = "#FFFFFF";
			}
			document.getElementById(vId).style.color = "#FFFFFF";
		
			break;
		case "#FFFFFF":
		case "#E5E5E5":			   
		case "#CCCCCC":
		case "#B3B3B3":
		case "#999999":
		default:
			if (document.getElementById("lnkSeriesName")){
				document.getElementById("lnkSeriesName").style.color = "#000000";
			}
			if (document.getElementById("LinkPhoto")){
				document.getElementById("LinkPhoto").style.color = "#000000";
			}
			document.getElementById(vId).style.color = "#000000";
			
			break;
	}		   
}




var nick_set_attempts	= 0;
function set_kredor_BP_by_Nick(val)
{
	if( document.cookie.match("kV=") ) {
		return;
	}
	if( document.getElementById("kbs").firstChild.value == val ) {
		return;
	}
	if( nick_set_attempts ++ >= 5 ) {
		return;
	}
	konc();
	set_kredor_BP_by_Nick(val);
}

/*
window.onload = function(){ 
	try{ 
		set_kredor_BP_by_Nick("BP"); 
	} 
	catch(e) {} 
} 
*/