/*=============================================================*/
function addHandler(object, event, handler)
{
  if (typeof object.addEventListener != 'undefined')
    object.addEventListener(event, handler, false);
  else if (typeof object.attachEvent != 'undefined')
    object.attachEvent('on' + event, handler);
  else
    throw "Incompatible browser";
}

function removeHandler(object, event, handler)
{
  if (typeof object.removeEventListener != 'undefined')
    object.removeEventListener(event, handler, false);
  else if (typeof object.detachEvent != 'undefined')
    object.detachEvent('on' + event, handler);
  else
    throw "Incompatible browser";
}
/*-------------------------------------------------------------*/
if (!window.getComputedStyle) {
    window.getComputedStyle = function(el, pseudo) {
        this.el = el;
        this.getPropertyValue = function(prop) {
            var re = /(\-([a-z]){1})/g;
            if (prop == 'float') prop = 'styleFloat';
            if (re.test(prop)) {
                prop = prop.replace(re, function () {
                    return arguments[2].toUpperCase();
                });
            }
            return el.currentStyle[prop] ? el.currentStyle[prop] : null;
        }
        return this;
    }
}
function fixPNG(element)
{
  if (/MSIE (5\.5|6).+Win/.test(navigator.userAgent))
  {
    var src;
    
    if (element.tagName=='IMG')
    {
      if (/\.png$/.test(element.src))
      {
        src = element.src;
        element.src = path+"/images/blank.gif";
      }
    }
    else
    {
      src = element.currentStyle.backgroundImage.match(/url\("(.+\.png)"\)/i);
      if (src)
      {
        src = src[1];
        element.runtimeStyle.backgroundImage="none";
      }
    }
    if (src) element.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "',sizingMethod='scale')";
  }
}
/*=============================================================*/
function openw(res,title) {
   rand=Math.random().toString().substring(2,15);
   window.open(path + "/lib/show_pic.php?src="+res+"&title="+title, 'new_'+rand, "top=50, left=50, width=50, height=50, channelmode=no, toolbar=no, location=no, status=no, directories=no, menubar=no, resizable=yes, scrollbars=yes");
}

function __show_pic(url, alt) {
  if ($("#im").attr("src")) return;
  var w_div = 500;
  var h_div = 400;
  var w = screenSize().w;
  var h = screenSize().h;
  $("body").append("<div id='menu_pic'\
  	                style='position:absolute;\
  	                       left:"+(w/2-w_div/2)+"px; top:"+(h/2-h_div/2)+"px;\
  	                       width:auto; height:auto;\
  	                       border:2px solid #bb9500;\
  	                       background:#2d0101;\
  	                       display:none;\
  	                '>\
  	                <table id='im_table' cellpadding='0' cellspacing='0' border='0' width='"+"' style='color:#fbda81;'>\
  	                  <tr><td onclick=''><a href='javascript:void(0);' onclick='close_pic(this);' style='color:#fbda81; float:right; text-decoration:none;'>x&nbsp;</a></td></tr>\
  	                  <tr><td><img id='im' src='"+url+"' width='"+300+"' height='"+200+"' onclick='close_pic(this);' alt='"+alt+"' title='"+alt+"' style='border:2px solid #956b02;'></td></tr>\
  	                  <tr><td style='padding:5px;'>"+alt+"</td></tr>\
  	                </table>\
  	                </div>\
  ");
  var im = new Image();
  im.src = url;
  var im_w = im.width;
  var im_h = im.height;
  $("#im_table").attr("width",(im_w+2));
  $("#im").attr("width",im_w);
  $("#im").attr("height",im_h);
  $("#menu_pic").css("display","block");
}

function show_pic(url, alt, hrest) {
  if ($("#menu_pic").css("display") != "none") return;
  //var w_div = 500;
  //var h_div = 400;
  //var w = screenSize().w;
  //var h = screenSize().h;

  var im = new Image();
  im.src = url;
  if (im.width == 0 || (im.fileSize ? (im.fileSize == -1) : false)) return;
  var im_w = im.width;
  var im_h = im.height;
  $("#im_table").attr("width",(im_w+2));
  $("#caption").text(alt);
  $("#im").attr("src",url);
  if ( hrest ) var h_restrict = hrest;
  else var h_restrict = 500;
  if (parseInt(im_h) > h_restrict) {
  	$("#im_table").attr("width","");
  	$("#im").attr("width",im_w*h_restrict/im_h);
  	$("#im").attr("height",h_restrict);
  } else {
  	$("#im").attr("width",im_w);
  	$("#im").attr("height",im_h);  
  }
  $("#im").attr("alt",alt);
  $("#im").attr("title",alt);
  if (/Microsoft/.test(navigator.appName))
  {
  	$("#menu_pic").css("position","absolute");
    $("#menu_pic").css("top",(document.body.scrollTop + 50));
    //$("#menu_pic").css("left",(10));
  }
  $("#menu_pic").css("display","block");
}

function close_pic(this_) {
  $(this_).parents("#menu_pic").css("display","none");
}

function screenSize() {
      var w, h; // Объявляем переменные, w - длина, h - высота
      w = (window.innerWidth ? window.innerWidth : (document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body.offsetWidth));
      h = (window.innerHeight ? window.innerHeight : (document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.offsetHeight));
      return {w:w, h:h};
}

function scrolling_pic () {
  //if ($("#menu_pic").css("display") == "none") return;
  if (/Microsoft/.test(navigator.appName)) {
    $("#menu_pic").css("top",document.body.scrollTop + 50);
  }
}

window.onscroll = scrolling_pic;

/*--- begin vote -----------------------------*/
var num_mark_div = 5;
var mark_style = new Array();
var mark_style_flag = true;
function foto_vote_move(_this, foto_id) {
	var mark = _this.id.substr(_this.id.length-1,1);
	if (mark_style_flag) {
		mark_style = new Array();
		for (var i=1; i<=num_mark_div; i++) {
			var mark_div = document.getElementById("foto_vote_"+foto_id+"_"+i);
			mark_style[i] = mark_div.className;
			//mark_style[i] = mark_div.style.background;
			//alert(mark_style[i]);
		}
		mark_style_flag = false;
	}
	for (var i=1; i<=num_mark_div; i++) {
		var mark_div = document.getElementById("foto_vote_"+foto_id+"_"+i);
		mark_div.className = "foto_vote foto_vote_off";
		//mark_div.style.background = "transparent url("+path+"/images/vote_asterisk.gif) scroll 0px 0px no-repeat";
	}
	for (var i=1; i<=mark; i++) {
		var mark_div = document.getElementById("foto_vote_"+foto_id+"_"+i);
		mark_div.className = "foto_vote foto_vote_on";
		//mark_div.style.background = "transparent url("+path+"/images/vote_asterisk.gif) scroll -14px 0px no-repeat";
	}
}
function foto_vote_out(_this, foto_id) {
	//for (var i=1; i<=num_mark_div; i++) {
	//	var mark_div = document.getElementById("foto_vote_"+foto_id+"_"+i);
	//	mark_div.style.background = "transparent url("+path+"/images/vote_asterisk.gif) scroll 0px 0px no-repeat";
	//}
	if (!mark_style_flag) {
		for (var i=1; i<=num_mark_div; i++) {
			var mark_div = document.getElementById("foto_vote_"+foto_id+"_"+i);
			mark_div.className = mark_style[i];
			//alert(mark_style[i]);
		}
		mark_style_flag = true;
		//alert(mark_style);
	}
}
function foto_vote_click(_this, foto_id) {
	foto_vote_callback = function (resp) {
		//alert(resp);
		if (resp != "0") {
			var votes_count = /\[votes:(\d+)\]/.exec(resp);
			var votes_count_div = document.getElementById("foto_vote_count_"+foto_id);
			votes_count_div.innerHTML = "голосов:&nbsp;"+votes_count[1];
			//var mark = /\[mark:([^\]]+)\]/.exec(resp);
			alert("Вы только что проголосовали.");
		} else {
			alert("Ваш голос уже учтен.");
		}
		//alert(resp);
		//alert(navigator.cookieEnabled);
	}
	var mark = _this.id.substr(_this.id.length-1,1);
	var _xmlHttp = $.post(
		foto_vote_url,
		{foto_id:foto_id, mark:mark},
		foto_vote_callback
	);
	//alert(_xmlHttp.raquestText);
}
/*--- end vote -----------------------------*/