var myAgent   = navigator.userAgent.toLowerCase();
var myVersion = parseInt(navigator.appVersion);

var is_ie   = ((myAgent.indexOf("msie") != -1)  && (myAgent.indexOf("opera") == -1));
var is_nav  = ((myAgent.indexOf('mozilla')!=-1) && (myAgent.indexOf('spoofer')==-1)
                && (myAgent.indexOf('compatible') == -1) && (myAgent.indexOf('opera')==-1)
                && (myAgent.indexOf('webtv') ==-1)       && (myAgent.indexOf('hotjava')==-1));

var is_win   =  ((myAgent.indexOf("win")!=-1) || (myAgent.indexOf("16bit")!=-1));
var is_mac    = (myAgent.indexOf("mac")!=-1);


var grayflag = false;
var invertflag = false;
var alphaflag = false;

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

var width=0;
var height=0;

//! global variable to store lbutton flag
var g_button = 0;

function setAlpha() {
	alphaflag = !alphaflag;

	if(alphaflag==true) {
		document.getElementById('cropper').style.display = "";
	} else {
		document.getElementById('cropper').style.display = "none";
	}

///	if(alphaflag) document.getElementById('cropper').src = "/images/crop_b.gif";
///	else document.getElementById('cropper').src = "/images/crop.gif";
}

function AddCorp()
{
	x1 = parseInt(document.getElementById('left').style.width) - 8; if(x1 < 0) x1 = 0;
	y1 = parseInt(document.getElementById('top').style.height) - 8; if(y1 < 0) y1 = 0;
	x2 = parseInt(document.getElementById('right').style.width) - 8; if(x2 < 0) x2 = 0;
	y2 = parseInt(document.getElementById('down').style.height) - 8; if(y2 < 0) y2 = 0;

//	s = '{--VARIANT('+x1+','+y1+','+x2+','+y2+','+grayflag+','+invertflag+')--}';
    s = '[CROP('+x1+','+y1+','+x2+','+y2+')]';
    return s;
    /*
   document.getElementById('comment_box').style.display = '';
   fr = document.getElementById('editor_iframe');
   if (is_ie) {
    fr.contentWindow.document.getElementById('text_').value += s;
    fr.contentWindow.document.getElementById('text_').focus();
   } else {
    fr.contentDocument.getElementById('text_').value += s;
    fr.contentDocument.getElementById('text_').focus();
   }*/
}


function VARIANT(left,top,right,down,gflag,iflag)
{
	if(iflag) setInvert();
	if(gflag) setBW();

	if(!alphaflag) setAlpha();

	document.getElementById('left').style.width = left + 8 + 'px';
	document.getElementById('right').style.width = right + 8 + 'px';
	document.getElementById('top').style.height = top + 8 + 'px';
	document.getElementById('down').style.height = down + 8 + 'px';

	window.scrollTo(0,0);
}

function MouseDown(e,side)
{
    CurrentSide = side;

    if(!e)
        e = window.event;

    x0 = e.clientX;
    y0 = e.clientY;

    if (e.which)
        g_button = e.which == 1;
    else
        g_button = e.button == 1;

    return false;
}

function MouseUp()
{
	CurrentSide = null;
	g_button = 0;
}

function MouseMove(e,width,height)
{
	if(!e)
		e = window.event;

	if((CurrentSide == null) || !g_button)
		return;
	var ex = e.clientX;
	var ey = e.clientY;
	var dx = ex - x0;
	var dy = ey - y0;

	var style = document.getElementById(CurrentSide).style;
	var sho, sh, sw;

	if(style.height)
		sh = parseInt(style.height);

	if(style.width)
		sw = parseInt(style.width);

	switch (CurrentSide)
	{
		case "top":
            if(sh >= 8)
				sh += dy;
			if(sh < 8)
				sh = 8;
			else {
				x0 = ex;
				y0 = ey;
			}

			if(sh > (height-8))
				sh = height-8;
			else {
				x0 = ex;
				y0 = ey;
			}
			style.height = sh + 'px';

			// check total height including opposite side
			style = document.getElementById('down').style;
			sho = parseInt(style.height);
			if((sho + sh + 8) > height) {
				sho = height - sh - 8;
				style.height = sho + 'px';
			}
		break;
		case "down":
			if(sh >= 8)
				sh -= dy;
			if(sh < 8)
				sh = 8;
			else {
				x0 = ex;
				y0 = ey;
			}

			if(sh > (height-8))
				sh = height-8;
			else {
				x0 = ex;
				y0 = ey;
			}
			style.height = sh + 'px';

			// check total height including opposite side
			style = document.getElementById('top').style;
			sho = parseInt(style.height);
			if((sho + sh + 8) > height) {
				sho = height - sh - 8;
				style.height = sho + 'px';
			}

		break;
		case "left":
			if(sw >= 8)
				sw += dx;
			if(sw < 8)
				sw = 8;
			else {
				x0 = ex;
				y0 = ey;
			}
			style.width = sw + 'px';
		break;
		case "right":
			if(sw >= 8)
				sw -= dx;
			if(sw < 8)
				sw = 8;
			else {
				x0 = ex;
				y0 = ey;
			}
			style.width = sw + 'px';
		break;
	}
}

/*
function doAlert(s) {
   alert(s);
   }
function jumpTo(tag) {
   self.location=tag;
   }

function comment_to(s){
    if (document.comm.comment.value.length == 0) {
           document.comm.comment.value=s+': ';
       } else {
            document.comm.comment.value = document.comm.comment.value+' '+s+': ';
    }
//    document.comm.to_id.value = to_id;
    document.comm.comment.focus();
    return;
}

function add_answer(s,to_id) {
    if (document.comm.comment.value.length == 0) {
           document.comm.comment.value=s+': ';
       } else {
            document.comm.comment.value = document.comm.comment.value+' '+s+': ';
    }
    document.comm.to_id.value = to_id;
    document.all.answer_block.innerHTML = '??????????? ????? ???????? ??? ????? ?? <b>' + s + '</a></b> | <a href=javascript:delete_answer(); class=list_link>????????</a>';
    document.comm.comment.focus();
    return;
}
function delete_answer() {
    document.comm.to_id.value = '';
    document.all.answer_block.innerHTML = '';
    return;
}
*/

/*
function detectResolution() {

    if(self.screen) {
        width = screen.width;
        height = screen.height;
    } else if (self.java) {
        var javaToolKit = java.awt.Toolkit.getDefaultToolkit();
        var screenSize = javaToolKit.getScreenSize();
        width = screenSize.width;
        height = screenSize.height;
    }

}

function getRes() {
    document.write("???? ?????????? ??????: <b>", width, "x", height, '</b><br>', "???????????");
    if (width < 1600) {
        document.write(" ?????? ?? ");
        if (width >= 1152)
            rec = "<b>1024</b>";
        else if (width >= 1024)
            rec="<b>800</b>";
        else if (width >= 800)
            rec="<b>640</b>";
        document.write(rec);
    }
    else if (width >= 1600)
        document.write(" ?? ??????? ??????????");
}


function InitFilters(filtername)
{
    switch(filtername) {
        case 1: document.all.photo_id.style.filter = "Gray";
            document.all.photo_id.filters["Gray"].enabled = grayflag;
            invertflag = false;
//            document.all.invert.src = "/images/invert.gif";
            break;
        case 2: document.all.photo_id.style.filter = "Invert";
            document.all.photo_id.filters["Invert"].enabled = invertflag;
            grayflag=false;
            document.all.bw.src = "/images/bw.gif";
            break;
    }
}

function setBW() {
    InitFilters(1);
    grayflag = !grayflag;
    document.all.photo_id.filters["Gray"].enabled = grayflag;

    if(grayflag) document.all.bw.src = "/images/bw_b.gif";
    else document.all.bw.src = "/images/bw.gif";

}

function setInvert() {
    InitFilters(2);
    invertflag = !invertflag;
    document.all.photo_id.filters["Invert"].enabled = invertflag;

    if(invertflag) document.all.invert.src = "/images/invert_b.gif";
    else document.all.invert.src = "/images/invert.gif";
}
*/

/*

function gebi(id) {return document.getElementById(id);}

function countSubstrings(_str, _sub) {
    var count = 0;
    while (_str.indexOf(_sub) != -1) {
        _str = _str.substring(_str.indexOf(_sub) + _sub.length, _str.length);
        count++;
    }
    return count;
}

function flipvFilter() {

    if(is_ie) {
    alert('!-1');
        var cmd = gebi("cmd");
    alert('!-2');
        var steps = countSubstrings(cmd.value, "v") % 2;
    alert('!-3');
        gebi("mainpic").style.filter = (steps) ? "" : "FlipV";
    alert('!-4');
        gebi("cmd").value += "v";
    alert('!-5');
        return false;
    } else return true;
}

function fliphFilter() {
     alert('@@@');
   if(is_ie) {
      alert('!-1');
      var cmd = gebi("cmd");
     alert('!-2');
       var steps = countSubstrings(cmd.value, "h") % 2;
     alert('!-3');
       gebi("pic_container").style.filter = (steps) ? "" : "FlipH";
     alert('!-4');
       gebi("cmd").value += "h";
        return false;
    } else return true;
}

var rotateArray = [0, 90, 180, 270];
var _rotateArray = [0, -90, -180, -270];
function rotate(side) {
    var cmd = document.forms["crop"].cmd;
    var flipCount = (countSubstrings(cmd.value, "h") + countSubstrings(cmd.value, "v")) % 2;
    var side_count = countSubstrings(cmd.value, side) + 1;
    var unside_count = countSubstrings(cmd.value, ((side == "r") ? "l" : "r"));
    var steps = (side_count - unside_count) % 4;
    if (flipCount) {
        var flag = (side == "l" && steps > 0) || (side == "r" && steps < 0);
    }
    else var flag = ((side == "r" && steps > 0) || (side == "l" && steps < 0));
    var arr = (flag) ? rotateArray : _rotateArray;
    steps = Math.abs(steps);
    gebi("mainpic").rotation = arr[steps];
    cmd.value += side;
    clearCrop();
    Cropflag = false;
    Filterflag = false;
    gebi("FilterDiv").style.display = "none";
    AddInComment.style.display="none";
    FiltresProp.style.display="none";
    return false;
}
 */

/*
//??? ???????????
function show_votes(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 - 300)/2, yOffset = (yMax - 350)/2;

    window.open('/content/nwnd/votes.php?photo_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') ;
}

//??? ???????????
function show_vote(id,at) {
    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 - 300)/2, yOffset = (yMax - 350)/2;

    window.open('/content/nwnd/vote.php?photo_id='+id+'&account_type='+at,'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') ;
}


function show_views(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 - 300)/2, yOffset = (yMax - 350)/2;

    window.open('/content/nwnd/views.php?photo_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') ;
}

function add_vote(id,vote) {
    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 - 250)/2, yOffset = (yMax - 100)/2;

    window.open('/content/nwnd/votes.php?photo_id='+id+'&vote='+vote,'author','width=250,height=200,screenX='+xOffset+',screenY='+yOffset+',top='+yOffset+',left='+xOffset+',copyhistory=no,directories=no, menubar=no,location=no,scrollbars=yes,resizable=no') ;
}


//??? information
function show_info(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 - 350)/2, yOffset = (yMax - 450)/2;

    window.open('/content/nwnd/photo.php?photo_id='+id,'info','width=350,height=450,screenX='+xOffset+',screenY='+yOffset+',top='+yOffset+',left='+xOffset+',copyhistory=no,directories=no, menubar=no,location=no,scrollbars=yes,resizable=no') ;
}

//??? information
function complaint_popup(query,page) {
    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 - 450)/2, yOffset = (yMax - 450)/2;

    window.open('/content/nwnd/toadministration.php'+query+'&page='+page,'toadministration','width=450,height=450,screenX='+xOffset+',screenY='+yOffset+',top='+yOffset+',left='+xOffset+',copyhistory=no,directories=no, menubar=no,location=no,scrollbars=yes,resizable=no') ;
}


function show_recomendations(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 - 350)/2;

    window.open('/content/nwnd/recomendations.php?photo_id='+id,'recomendations','width=200,height=350,screenX='+xOffset+',screenY='+yOffset+',top='+yOffset+',left='+xOffset+',copyhistory=no,directories=no, menubar=no,location=no,scrollbars=yes,resizable=no') ;
}

function add_recomendations(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 - 250)/2, yOffset = (yMax - 100)/2;

    window.open('/content/nwnd/recomendations.php?action=add&photo_id='+id,'recomendations','width=250,height=300,screenX='+xOffset+',screenY='+yOffset+',top='+yOffset+',left='+xOffset+',copyhistory=no,directories=no, menubar=no,location=no,scrollbars=yes,resizable=no') ;
}

function add_anti_recomendations(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 - 250)/2, yOffset = (yMax - 100)/2;

    window.open('/content/nwnd/anti_recomendations.php?action=add&photo_id='+id,'antirecomendations','width=250,height=300,screenX='+xOffset+',screenY='+yOffset+',top='+yOffset+',left='+xOffset+',copyhistory=no,directories=no, menubar=no,location=no,scrollbars=yes,resizable=no') ;
}

function show_recs(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 - 350)/2;

    window.open('/content/nwnd/recs.php?photo_id='+id,'recomendations','width=200,height=350,screenX='+xOffset+',screenY='+yOffset+',top='+yOffset+',left='+xOffset+',copyhistory=no,directories=no, menubar=no,location=no,scrollbars=yes,resizable=no') ;
}

function add_recs(id,rec_type) {
    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 - 250)/2, yOffset = (yMax - 100)/2;

    window.open('/content/nwnd/recs.php?action=add&type='+rec_type+'&photo_id='+id,'recomendations','width=250,height=300,screenX='+xOffset+',screenY='+yOffset+',top='+yOffset+',left='+xOffset+',copyhistory=no,directories=no, menubar=no,location=no,scrollbars=yes,resizable=no') ;
}

function add_votes(id,vote_type,vote) {
    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 - 250)/2, yOffset = (yMax - 100)/2;

    window.open('/content/nwnd/vote.php?action=add&vote='+vote+'&type='+vote_type+'&photo_id='+id,'votes','width=250,height=300,screenX='+xOffset+',screenY='+yOffset+',top='+yOffset+',left='+xOffset+',copyhistory=no,directories=no, menubar=no,location=no,scrollbars=yes,resizable=no') ;
}

function sms_votes(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 - 400)/2, yOffset = (yMax - 300)/2;

    window.open('/content/nwnd/sms_vote.php?photo_id='+id,'votes','width=400,height=300,screenX='+xOffset+',screenY='+yOffset+',top='+yOffset+',left='+xOffset+',copyhistory=no,directories=no, menubar=no,location=no,scrollbars=yes,resizable=no') ;
}

function open_special_actions(target,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 - 500)/2, yOffset = (yMax - 550)/2;

    window.open('/content/nwnd/special_actions.php?target='+target+'&id='+id,'special_action','width=500,height=550,screenX='+xOffset+',screenY='+yOffset+',top='+yOffset+',left='+xOffset+',copyhistory=no,directories=no, menubar=no,location=no,scrollbars=yes,resizable=no') ;
}


function open_messanger(to_id,type) {
    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 - 500)/2, yOffset = (yMax - 550)/2;

    window.open('/content/nwnd/messanger.php?id='+to_id+'&type='+type,'messanger','width=500,height=550,screenX='+xOffset+',screenY='+yOffset+',top='+yOffset+',left='+xOffset+',copyhistory=no,directories=no, menubar=no,location=no,scrollbars=yes,resizable=no') ;
}

function open_favs(add_id,favs_type) {
    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 - 300)/2, yOffset = (yMax - 150)/2;

    window.open('/content/nwnd/favs.php?id='+add_id+'&favs_type='+favs_type,'favorites','width=300,height=150,screenX='+xOffset+',screenY='+yOffset+',top='+yOffset+',left='+xOffset+',copyhistory=no,directories=no, menubar=no,location=no,scrollbars=yes,resizable=no') ;
}

function open_report(type,photo_id,coment_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 - 300)/2, yOffset = (yMax - 150)/2;

    window.open('/content/nwnd/report.php?type='+type+'&photo_id='+photo_id+'&coment_id='+coment_id,'report','width=300,height=150,screenX='+xOffset+',screenY='+yOffset+',top='+yOffset+',left='+xOffset+',copyhistory=no,directories=no, menubar=no,location=no,scrollbars=yes,resizable=no') ;
}

function doInsert(ibTag, ibClsTag, isSingle)
{
	var isClose = false;
	var obj_ta = document.forum.msg;
	if ( (myVersion >= 4) && is_ie && is_win) {
		if(obj_ta.isTextEdit) { // this doesn't work for NS, but it works for IE 4+ and compatible browsers
			obj_ta.focus();
			var sel = document.selection;
			var rng = sel.createRange();
			rng.colapse;
			if((sel.type == "Text" || sel.type == "None") && rng != null){
				if(ibClsTag != "" && rng.text.length > 0)
					ibTag += rng.text + ibClsTag;
				else if(isSingle)
					isClose = true;

				rng.text = ibTag;
			}
		} else {
			if(isSingle) isClose = true;
			obj_ta.value += ibTag;
		}
	} else {
		if(isSingle) isClose = true;
		obj_ta.value += ibTag;
	}

	obj_ta.focus();

	return isClose;
}

function newPopUnder()
{
};
*/
