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);


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

function comment_to(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.comm.comment.focus();
	return;
}


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

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

var width=0;
var height=0;

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.pix.style.filter = "Gray";
			document.all.pix.filters["Gray"].enabled = grayflag;
			invertflag = false;
//			document.all.invert.src = "/images/invert.gif";
			break;
		case 2: document.all.pix.style.filter = "Invert";
			document.all.pix.filters["Invert"].enabled = invertflag;
			grayflag=false;
			document.all.bw.src = "./templates/default/images/bw.gif";
			break;
	}
}

function faq(url,name,weite,hoehe){
         window.open(url, name, 'width='+weite+',height='+hoehe+',scrollbars=0,status=0,menubar=0,resizable=0,location=0,toolbar=0,top=100,left=200');
}

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

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

}

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

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

function setAlpha() {
	alphaflag = !alphaflag;

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

	if(alphaflag) document.all.corp.src = "./templates/default/images/crop_b.gif";
	else document.all.corp.src = "./templates/default/images/crop.gif";
}

function AddCorp()
{
	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.down.style.pixelHeight - 8; if(y2 < 0) y2 = 0;

	s = '[FRAGFOTO]FRAGFOTO('+x1+','+y1+','+x2+','+y2+','+grayflag+','+invertflag+')[/FRAGFOTO]';

	document.commentform.comment_text.value += s;
	document.commentform.comment_text.focus();
}

function add_smile(smile) {
//	doInsert(" " + smilie + " ", "", false)
	document.forum.msg.value += smile;
	document.forum.msg.focus();
}

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

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

	if(!alphaflag) setAlpha();

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

	window.scrollTo(0,0);
}

function MouseUp()
{
	CurrentSide = null;
}

function MouseMove(width,height)
{
	thisevent = window.event;
	if((CurrentSide == null) || (thisevent.button != 1)) return;
	dx = thisevent.x - x0;
	dy = thisevent.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 = thisevent.x; y0 = thisevent.y;}

			if(style.pixelHeight > (height-8)) style.pixelHeight = height-8;
			else {x0 = thisevent.x; y0 = thisevent.y;}
		break;
		case "down":
			if(style.pixelHeight >= 8) style.pixelHeight -= dy;
			if(style.pixelHeight < 8) style.pixelHeight = 8;
			else {x0 = thisevent.x; y0 = thisevent.y;}

			if(style.pixelHeight > (height-8)) style.pixelHeight = height-8;
			else {x0 = thisevent.x; y0 = thisevent.y;}
		break;
		case "left":
			if(style.pixelWidth >= 8) style.pixelWidth += dx;
			if(style.pixelWidth < 8) style.pixelWidth = 8;
			else {x0 = thisevent.x; y0 = thisevent.y;}
		break;
		case "right":
			if(style.pixelWidth >= 8) style.pixelWidth -= dx;
			if(style.pixelWidth < 8) style.pixelWidth = 8;
			else {x0 = thisevent.x; y0 = thisevent.y;}
		break;
	}
}

//для голосования
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/popup/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_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/popup/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/popup/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/popup/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 moderator_popup(query) {
    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/popup/moderator.php'+query,'moderator','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/popup/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/popup/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 open_messanger(to_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/popup/messanger.php?id='+to_id,'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/popup/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/popup/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;
}
