// scripts.js - Java script kit
/*
CC
*/

/********************/
/* global variables */
/********************/

var imageDir       = "./img";       // current image directory (set by setImageDir
                                // and used in displayThumb)
var soundDir       = ".";       // current sound directory (set by setSoundDir
                                // and used in displayImage)
var endl           = "\n";      // end of line (used in: displayImage,
                                //   and displayThumb)
var html           = "";        // html string (used in: displayImage,
                                //   and displayThumb)
var maxThumbWidth  = 150;       // max width of thumbnails (set by
                                // setMaxThumbWidth and used by displayThumb)

/*************/
/* functions */
/*************/

/////////////////
// photo stuff //
/////////////////


function displayImageSimple(image, width, height, title, date)
{
   var newWin;
   var winName;
   var newTitle = title;

   html = "";

   // remove anything between "<" and ">"
   newTitle = newTitle.replace(/<(.*)>/g, " ");
   // make sure that there is no space at the end
   newTitle = newTitle.replace(/ $/g, "");

   html += "<html>" + endl;
   html += "<head>" + endl;
   html += "<title>" + newTitle + ": " + date + "</title>" + endl;
   html += "</head>" + endl;
   html += "<body bgcolor='#FFFFFF' topmargin='0' leftmargin='0'" +
           " marginheight='0' marginwidth='0'>" + endl;
   html += "<a href='javascript:window.close()'>" +
           "<img src='" + imageDir + "/" + image + "' width='" + width +
           "' height='" + height + "' alt='Click to Close Window' " +
           "border='0'></a>" + endl;
   html += "</body></html>";

   newWin = window.open("","Picture","status=no,resizable=yes,width=" + width + ",height=" + height + ",menubar=no")
   ;

   newWin.document.writeln(html);
   newWin.document.close();
   newWin.focus();
}


function displayImage(image, width, height, title, date)
{
   var newWin;
   var winName;
   var newTitle = title;

   html = "";

   // remove anything between "<" and ">"
   newTitle = newTitle.replace(/<(.*)>/g, " ");
   // make sure that there is no space at the end
   newTitle = newTitle.replace(/ $/g, "");
   html += "<html>" + endl;
   html += "<head>" + endl;
   html += "<title>" + newTitle + ": " + width + "х" + height + ": "+ date + "</title>" + endl;
   //html += "<bgsound src='" + soundDir + "/camera.wav' loop='1'>" + endl;
whratio = width / height;
height = (screen.height-80 < height ? screen.height-80 : height);
//width = (screen.width-40 < width ? screen.width-40 : width);
width = Math.floor(height * whratio);

   html += "</head>" + endl;
   html += "<body bgcolor='#FFFFFF' topmargin='0' leftmargin='0'" +
           " marginheight='0' marginwidth='0'>" + endl;
   html += "<a href='javascript:window.close()'>" +
           "<img src='" + imageDir + "/BIG_" + image + "' width='" + width +
           "' height='" + height + "' alt='Click to Close Window' " +
           "border='0'></a>" + endl;
   html += "</body></html>";

   // create name and prevent it from getting too long
/*
   winName = title.substr(0, 10) + date.substr(0, 10);
   winName = winName.replace(/(\W)/g, "");
+screen.width+'x'+screen.height+
*/

  /* newWin = window.open("", image, //winName,
                        "width=" + width +
                        ",height=" + height +
                        ",menubar=no" +
                        ",resizable=yes" +
                        ",scrollbars=yes",
                        true); */
	widthw = width+20;
//	heightw = Math.floor(height);
heightw = Math.floor(width/whratio+20);
   newWin = window.open("","Picture","status=no,resizable=yes,width=" + widthw + ",height=" + heightw + ",menubar=no,scrollbars")
   ;
   newWin.document.writeln(html);
   newWin.document.close();
   newWin.focus();
}

function setImageDir(ImageDir)
{
   imageDir = ImageDir;
}

////////////////////
// Bookmark Stuff //
////////////////////

function addBookmark(url, title) {
   if(window.external)
      external.AddFavorite(url, title)
   else
      alert("Sorry.  Your browser doesn't support this feature.");
}


/////////////////////
// Show/Hide Stuff //
/////////////////////

function flip(id, sel)
{
   if(DOM) {
      document.getElementById(id).style.display = (sel.checked ? "block" : "none");
   }
   else if(IE4) {
      document.all(id).style.display = (sel.checked ? "block" : "none");
   }
}


function initCSS()
{
   DOM   = (document.getElementById ? true : false);
   Ver4  = parseInt(navigator.appVersion) == 4;
   IE4   = (navigator.userAgent.indexOf("MSIE") != -1) && Ver4;
   Opera = (window.opera ? true : false);
   
   html = "";

   html += "<style type='text/css'>" + endl;
   if((DOM || IE4) && !Opera) {
      html += ".checkbox {}" + endl;
      html += ".details {display:none}" + endl;
   }
   else {
      html += ".checkbox {display:none}" + endl;
      html += ".details {}" + endl;
   }
   html += "</style>"

   document.writeln(html);
}


function MM_findObj(n, d) { //v3.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document); return x;
}
/* Functions that swaps images. */
function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

/* Functions that handle preload. */
function MM_preloadImages() { //v3.0
 var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
   var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
   if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

/* Functions that open pictures in new window. */
function openPict(u) {
if (u) {
p=open("","Picture","status=no,resizable=yes,width=440,height=440");
p.document.open();
var s="<p align=center><img src=\"http://www.october.ru/img/"+u+"\"></p>";
p.document.write(s);
p.document.close();
} else {
alert("No file selected!");
}
}

/**
 * Sets/unsets the pointer in browse mode
 *
 * @param   object   the table row
 * @param   object   the color to use for this row
 *
 * @return  boolean  whether pointer is set or not
 */
function setPointer(theRow, thePointerColor)
{
//    if (thePointerColor == '' || typeof(theRow.style) == 'undefined') {
//        return false;
//    }
    if (typeof(document.getElementsByTagName) != 'undefined') {
        var theCells = theRow.getElementsByTagName('td');
    }
    else if (typeof(theRow.cells) != 'undefined') {
        var theCells = theRow.cells;
    }
    else {
        return false;
    }

    var rowCellsCnt  = theCells.length;
    for (var c = 0; c < rowCellsCnt; c++) {
        theCells[c].style.backgroundColor = thePointerColor;
    }

    return true;
} // end of the 'setPointer()' function

/*function checkunauthuser() {//check requred fields

		CF = document.forms.order;

        if (CF.elements.fromname.value == "") { alert("Укажите Ф.И.О. контактного лица."); }

        else if (CF.elements.formfield2.value == "") { alert("Укажите Ваш телефон для связи."); }

		else if (CF.elements.fromadr.value == "") { alert("Укажите Ваш e-mail для связи."); }

        else CF.submit();

}*/
/*
*/
function flip(id, sel)//hide-show stuff
{
	DOM   = (document.getElementById ? true : false);
    Ver4  = parseInt(navigator.appVersion) == 4;
	IE4   = (navigator.userAgent.indexOf("MSIE") != -1) && Ver4;
	Opera = (window.opera ? true : false);
	if(DOM) {
      document.getElementById(id).style.display = (sel.checked ? "block" : "none");
   }
   else if(IE4) {
      document.all(id).style.display = (sel.checked ? "block" : "none");
   }
}
/*  Random number generator. If max=3 then function returns 1,2 or 3
===================================================================*/
function getRandom(max) {return (Math.floor(Math.random()*max))+1;}

/*  Place random banner into HTML

<P ALIGN="center">
<SCRIPT language="JavaScript">postAd("1","6");</SCRIPT>
<BR>
</P>

*/
function postAd(id, numAd)//hide-show stuff
{
id = "http://www.lada.nizhny.ru/Advertising/";
numAd = 6;
AdNumber = getRandom(numAd); 
log_report=""+Math.random()+"&wh="+screen.width+'x'+screen.height+"&px="+
(((navigator.appName.substring(0,3)=="Mic"))?
screen.colorDepth:screen.pixelDepth)+"&r="+escape(document.referrer)+"&pg="+
escape(window.location.href);
BannerAd = "<A HREF=\"http://127.0.0.1/redirect?host=" + log_report + "&b=" + AdNumber + "\" TARGET=\"_blank\"><IMG BORDER=\"0\" ALT=\"Image rotate\" SRC=\"" + id + AdNumber + ".jpg\"></A>";

document.writeln(BannerAd);
return BannerAd;
}
