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

var Initialized = false;

function InitFilters(){
    if(Initialized) return;
    Initialized = true;
    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 flipBW(){ // това си е старата функция
    InitFilters();
    BWflag = !BWflag;

    document.all.mainpic.filters["Gray"].enabled = BWflag;

    if(BWflag) document.all.bw.src = "images/i_bw_on.gif";
    else document.all.bw.src = "images/i_bw.gif";
} //---------------------------------------------


function flipHM(){
    InitFilters();
    HMflag = !HMflag;
    document.all.mainpic.filters["FlipH"].enabled = HMflag;
    if(HMflag)
        document.all.hm.src = "images/x_hmirr_on.gif"
    else
        document.all.hm.src = "images/x_hmirr.gif"
}

function flipVM(){
    InitFilters();
    VMflag = !VMflag;
    document.all.mainpic.filters["FlipV"].enabled = VMflag;
    if(VMflag)
        document.all.vm.src = "images/x_vmirr_on.gif"
    else
        document.all.vm.src = "images/x_vmirr.gif"
}



var Cropflag = false;
var Gridflag = false;

var CurrentSide = null;
var x0 = 0;
var y0 = 0;

function MouseDown(side){
	CurrentSide = side;
	ev = window.event;
	x0 = ev.x;
	y0 = ev.y;
}

function MouseUp(){
	CurrentSide = null;
}

function MouseMove(){
	ev = window.event;
	if((CurrentSide == null) || (ev.button != 1)) return;
	dx = ev.x - x0;
	dy = ev.y - y0;

	style = document.all[CurrentSide].style;
	switch (CurrentSide){
		case "top":
			if(style.pixelHeight >= 8) style.pixelHeight += dy;
			if(style.pixelHeight < 8) style.pixelHeight = 8;
			else {x0 = ev.x; y0 = ev.y;}
		break;
		case "bottom":
			if(style.pixelHeight >= 8) style.pixelHeight -= dy;
			if(style.pixelHeight < 8) style.pixelHeight = 8;
			else {x0 = ev.x; y0 = ev.y;}
		break;
		case "left":
			if(style.pixelWidth >= 8) style.pixelWidth += dx;
			if(style.pixelWidth < 8) style.pixelWidth = 8;
			else {x0 = ev.x; y0 = ev.y;}
		break;
		case "right":
			if(style.pixelWidth >= 8) style.pixelWidth -= dx;
			if(style.pixelWidth < 8) style.pixelWidth = 8;
			else {x0 = ev.x; y0 = ev.y;}
		break;
	}
}

function SetCrop(left, top, right, bottom){
	if(!Cropflag) flipCrop();

	document.all.left.style.pixelWidth = left + 8;
	document.all.right.style.pixelWidth = right + 8;
	document.all.top.style.pixelHeight = top + 8;
	document.all.bottom.style.pixelHeight = bottom + 8;

	window.scrollTo(0,0);
}

function AddClipping(){
	x1 = document.all.left.style.pixelWidth - 8; if(x1 < 0) x1 = 0;
	y1 = document.all.top.style.pixelHeight - 8; if(y1 < 0) y1 = 0;
	x2 = document.all.right.style.pixelWidth - 8; if(x2 < 0) x2 = 0;
	y2 = document.all.bottom.style.pixelHeight - 8; if(y2 < 0) y2 = 0;

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

	document.comments.fldComment.value += s;
	document.comments.fldComment.focus();
}

function flipCrop(){
	Cropflag = !Cropflag;
	if(Cropflag){
		document.all.cropperimg.src = "images/i_crop_on.gif";
		document.all.cropper.style.display = "";
        document.getElementById("vmakni").style.display = "";
	}
	else{
		document.all.cropperimg.src = "images/i_crop.gif";
		document.all.cropper.style.display = "none";
        document.getElementById("vmakni").style.display = "none";
	}
}

function flipGrid(){
	Gridflag = !Gridflag;

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



function centerWindow(id) {
    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('vote.php?pic_id=' + id,'rating','width=300,height=130,screenX='+xOffset+',screenY='+yOffset+',top='+yOffset+',left='+xOffset+',copyhistory=no,directories=no, menubar=no,location=no,scrollbars=no,resizable=no');
}



function infoV(id) {
    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('info_votes.php?id='+id,'author','width=300,height=350,screenX='+xOffset+',screenY='+yOffset+',top='+yOffset+',left='+xOffset+',copyhistory=no,directories=no, menubar=no,location=no,scrollbars=yes,resizable=no') ;
}


/*
Image w/ description tooltip- By Dynamic Web Coding (www.dyn-web.com)
Copyright 2002 by Sharon Paine
Visit http://www.dynamicdrive.com for this script
*/

/* IMPORTANT: Put script after tooltip div or 
	 put tooltip div just before </BODY>. */

var dom = (document.getElementById) ? true : false;
var ns5 = ((navigator.userAgent.indexOf("Gecko")>-1) && dom) ? true: false;
var ie5 = ((navigator.userAgent.indexOf("MSIE")>-1) && dom) ? true : false;
var ns4 = (document.layers && !dom) ? true : false;
var ie4 = (document.all && !dom) ? true : false;
var nodyn = (!ns5 && !ns4 && !ie4 && !ie5) ? true : false;

// resize fix for ns4
var origWidth, origHeight;
if (ns4) {
	origWidth = window.innerWidth; origHeight = window.innerHeight;
	window.onresize = function() { if (window.innerWidth != origWidth || window.innerHeight != origHeight) history.go(0); }
}

// avoid error of passing event object in older browsers
if (nodyn) { event = "nope" }

///////////////////////  CUSTOMIZE HERE   ////////////////////
// settings for tooltip 
// Do you want tip to move when mouse moves over link?
var tipFollowMouse= true;	
// Be sure to set tipWidth wide enough for widest image
var tipWidth= 160;
var offX= 20;	// how far from mouse to show tip
var offY= 12; 
var tipFontFamily= "Verdana, arial, helvetica, sans-serif";
var tipFontSize= "8pt";
// set default text color and background color for tooltip here
// individual tooltips can have their own (set in messages arrays)
// but don't have to
var tipFontColor= "#000000";
var tipBgColor= "#DDECFF"; 
var tipBorderColor= "#999999";
var tipBorderWidth= 1;
var tipBorderStyle= "ridge";
var tipPadding= 4;

// tooltip content goes here (image, description, optional bgColor, optional textcolor)
var messages = new Array();
// multi-dimensional arrays containing: 
// image and text for tooltip
// optional: bgColor and color to be sent to tooltip


////////////////////  END OF CUSTOMIZATION AREA  ///////////////////

// preload images that are to appear in tooltip
// from arrays above
if (document.images) {
	var theImgs = new Array();
	for (var i=0; i<messages.length; i++) {
  	theImgs[i] = new Image();
		theImgs[i].src = messages[i][0];
  }
}


var mouseX, mouseY;
function trackMouse(evt) {
	mouseX = (ns4||ns5)? evt.pageX: window.event.clientX + document.body.scrollLeft;
	mouseY = (ns4||ns5)? evt.pageY: window.event.clientY + document.body.scrollTop;
	if (tipOn) positionTip(evt);
}

/////////////////////////////////////////////////////////////
//  positionTip function
//		If tipFollowMouse set false, so trackMouse function
//		not being used, get position of mouseover event.
//		Calculations use mouseover event position, 
//		offset amounts and tooltip width to position
//		tooltip within window.
/////////////////////////////////////////////////////////////
function positionTip(evt) {
	if (!tipFollowMouse) {
		mouseX = (ns4||ns5)? evt.pageX: window.event.clientX + document.body.scrollLeft;
		mouseY = (ns4||ns5)? evt.pageY: window.event.clientY + document.body.scrollTop;
	}
	// tooltip width and height
	var tpWd = (ns4)? tooltip.width: (ie4||ie5)? tooltip.clientWidth: tooltip.offsetWidth;
	var tpHt = (ns4)? tooltip.height: (ie4||ie5)? tooltip.clientHeight: tooltip.offsetHeight;
	// document area in view (subtract scrollbar width for ns)
	var winWd = (ns4||ns5)? window.innerWidth-20+window.pageXOffset: document.body.clientWidth+document.body.scrollLeft;
	var winHt = (ns4||ns5)? window.innerHeight-20+window.pageYOffset: document.body.clientHeight+document.body.scrollTop;
	// check mouse position against tip and window dimensions
	// and position the tooltip 
	if ((mouseX+offX+tpWd)>winWd) 
		tipcss.left = (ns4)? mouseX-(tpWd+offX): mouseX-(tpWd+offX)+"px";
	else tipcss.left = (ns4)? mouseX+offX: mouseX+offX+"px";
	if ((mouseY+offY+tpHt)>winHt) 
		tipcss.top = (ns4)? winHt-(tpHt+offY): winHt-(tpHt+offY)+"px";
	else tipcss.top = (ns4)? mouseY+offY: mouseY+offY+"px";
	if (!tipFollowMouse) t1=setTimeout("tipcss.visibility='visible'",100);
}

function hideTip() {
	if (!tooltip) return;
	t2=setTimeout("tipcss.visibility='hidden'",100);
	tipOn = false;
}

function favorites(fid, ftype) {
    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('favorites.php?fid=' + fid + '&ftype=' + ftype, 'favorites','width=300,height=130,screenX='+xOffset+',screenY='+yOffset+',top='+yOffset+',left='+xOffset+',copyhistory=no,directories=no, menubar=no,location=no,scrollbars=no,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=yes,fullscreen=yes');
	   sizer.location = website;
	} else {
	   window.open(website,'fotowindow','width=winwidth,height=winheight,menubar=no,status=no,location=no,fullscreen=yes,directories=no,resizable=yes');
	}
}

function setBackground(color){
	document.getElementById("tblMainPic").style.background = color;
}