var galleryPreviewPos = 1;

function getGalleryPreview (n) {
	if (n > galleryPreviewLast) {
		return 1;
	} 
	else if (n < 1) {
		return galleryPreviewLast;
	}
	else return n;
}

function galleryPreviewScroll (n) {
	galleryPreviewPos = getGalleryPreview(galleryPreviewPos + n);	

	document.getElementById('gallery_preview_' + getGalleryPreview(galleryPreviewPos - n)).style.display = 'none';
	document.getElementById('gallery_preview_' + getGalleryPreview(galleryPreviewPos)).style.display = 'block';
	return false;
}

function popup (linkObj,w_w,w_h,ext) {
	var ext = ext || 0;		
  window.open(linkObj.href,linkObj.target,'width='+w_w+',height='+w_h+
    ',resizable='+ext+',scrollbars='+ext+',location=0,toolbar=0,menubar=0,status=0');
  return false;
}

function imgOpen(imgLink,Title,width,height) {
	var doResize;
	if (width == null) {width = 100;height = 100;doResize=1}
  if (typeof(window.resizeBy)=='undefined') return true;
  imgWndw=window.open('',imgLink.target,'width='+width+',height='+height+
    ',toolbar=no,menubar=no,location=no,status=no,'+
    'resizable=yes,scrollbars=no,left='+(screen.width>>>2)+
    ',top='+(screen.height>>>4));
  if (doResize) self.focus();
  var imgTitle=(Title)?Title:imgLink.href;
  with (imgWndw.document){
    open();
    write('<ht'+'ml><he'+'ad><ti'+'tle>'+imgTitle+'</ti'+'tle>'+
    '</he'+'ad><bo'+'dy leftmargin="0" topmargin="0" '+
    'rightmargin="0" bottommargin="0" marginwidth="0" '+
    'style="margin:0;padding:0;position:fixed;overflow:none;" '+
    'marginheight="0"><img src="'+imgLink.href+'" border="0" '+
    ' alt="'+imgTitle+'" title="'+imgTitle+'"/></bo'+
    'dy></ht'+'ml>');
    close();
  }
  if (doResize==1) {resId=setInterval('imgResize()',500);}
  return false;
}

function imgResize() {
  var w=imgWndw.document.images[0].width;
  if (w>screen.availWidth) w=screen.availWidth;
  var h=imgWndw.document.images[0].height;
  if (h>screen.availHeight) h=screen.availHeight;
  if (w>50 && h>50) {
    var ww=(imgWndw.innerWidth)?imgWndw.innerWidth:((document.body)?
      imgWndw.document.body.clientWidth:null);
    var wh=(imgWndw.innerHeight)?imgWndw.innerHeight:((document.body)?
      imgWndw.document.body.clientHeight:null);
		if (/gecko/i.exec(navigator.userAgent)) ww-=50;
    if (ww && wh) {
      imgWndw.resizeBy(w-ww,h-wh);
    }
    imgWndw.focus();
    clearInterval(resId)
  }
}

// Функция установки значения cookie.
// name      - имя cookie
// value     - значение cookie
// [path]    - путь, для которого cookie действительно (по умолчанию - /)
// [expires] - дата окончания действия cookie (по умолчанию - до конца сессии)
// [domain]  - домен, для которого cookie действительно (по умолчанию - домен,
//             в котором значение было установлено)
// [secure]  - логическое значение, показывающее требуется ли защищенная
//             передача значения cookie
function setCookie(name, value, path, expires, domain, secure) {
  var curCookie = name + "=" + escape(value) +
    ((expires) ? "; expires=" + expires.toGMTString() : "") +
    ((path) ? "; path=" + path : "; path=/") +
    ((domain) ? "; domain=" + domain : "") +
    ((secure) ? "; secure" : "");
  document.cookie = curCookie;
}

// Функция чтения значения cookie.
// name - имя считываемого cookie
function getCookie(name) {
  var prefix = name + "=";
  var cookieStartIndex = document.cookie.indexOf(prefix);
  if(cookieStartIndex == -1) return null;
  var cookieEndIndex = document.cookie.indexOf(";", cookieStartIndex + prefix.length);
  if(cookieEndIndex == -1) cookieEndIndex = document.cookie.length;
  return unescape(document.cookie.substring(cookieStartIndex + prefix.length, cookieEndIndex));
}

// Функция удаления значения cookie
// name - имя cookie
// [path] - путь, для которого cookie действительно
// [domain] - домен, для которого cookie действительно
function delCookie(name, path, domain) {
  if(getCookie(name)) {
    document.cookie = name + "=" +
    ((path) ? "; path=" + path : "; path=/") +
    ((domain) ? "; domain=" + domain : "") +
    "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}

// Устанавливает Cookie сложной структуры.
function setCookieEx(name, value, path, expires, domain, secure)
{ value=Serialize(value);
//  alert(value);
  return setCookie(name, value, path, expires, domain, secure);
}

// Читает Cookie сложной структуры.
function getCookieEx(name)
{ var v=getCookie(name);
  return Unserialize(v);
}

function formArchiveUrl (form) {
	document.location.href = '/archive/' +  form.year.value + '/' + form.month.value + '/';
	return false;
}

function ToggleCheckboxesInBox (box, value) {
	var box = document.getElementById(box);
	var elements = box.getElementsByTagName("input");
	for (var i = 0; i < elements.length; i++) {
		elements[i].checked = value;
	}
}