// ---------------------------------------------------------------------------------------------

$(document).ready(function() {
	var is_IE = navigator.userAgent.indexOf('MSIE') != -1 ? true : false; 
	if (is_IE)
	{
		correctPNG();
	}
	
	menu_prepare();
	correct_gallery();
	
	so_init();
});

// ---------------------------------------------------------------------------------------------


function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}

function getBrowserSizeXY() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  return [ myWidth, myHeight ];
}

function correctPNG() // correctly handle PNG transparency in Win IE 5.5 or higher.
{
	for(var i=0; i<document.images.length; i++)
	{
		var img = document.images[i]
		var imgName = img.src.toUpperCase()
		if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
		{
			var imgID = (img.id) ? "id='" + img.id + "' " : ""
			var imgClass = (img.className) ? "class='" + img.className + "' " : ""
			var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
			var imgStyle = "display:inline-block;" + img.style.cssText
			if (img.align == "left") imgStyle = "float:left;" + imgStyle
			if (img.align == "right") imgStyle = "float:right;" + imgStyle
			if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
			var strNewHTML = "<span " + imgID + imgClass + imgTitle
				+ " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
				+ " filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
				+ "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>"
			img.outerHTML = strNewHTML
			i = i-1
		}
	}
}

function showPic(img_src) {
	
	$('#block_window_img').get(0).innerHTML = '<img src="' + img_src + '">';
	
	var screen_pos = getBrowserSizeXY();
	var scroll_pos = getScrollXY();
	var top_pos = scroll_pos[1] + (screen_pos[1] - $('#block_window').height())/2;
	var left_pos = ($(document).width()	- $('#block_window').width())/2;
	
	$.blockUI({ message: $('#block_window'), css: { cusor: 'pointer', backgroundColor: 'white', position: 'absolute', top: top_pos, left: left_pos } });
}

function menu_prepare() {
	var punkts = $('.top-menu-in > .punkts > .punkt, .punkt-e, .punkt-a');

	for (var i=0; i<punkts.length; i++) {

		$(punkts[i]).bind("mouseover", function() {
			$('.list', $(this)).show();
		});
		
		$(punkts[i]).bind("mouseout", function() {
			$('.list', $(this)).hide();
		});
	}
}

function correct_gallery() {
	var max_height = 0;
	var gal_boxes = jQuery('#catalog_id .block');

	for (var i=0; i<gal_boxes.length; i++) {
		max_height = Math.max(max_height, gal_boxes[i].clientHeight);
	}
 
	for (var i=0; i<gal_boxes.length; i++) {
		gal_boxes[i].style.height = max_height;
	}
}

function so_init()
{
	d=document, imgs = new Array(), zInterval = null, current=0, pause=false;
	if(!d.getElementById || !d.createElement)return;

	imgs = d.getElementById('imageContainer').getElementsByTagName('img');
	for(i=1;i<imgs.length;i++) 
	{
		imgs[i].xOpacity = 0;
	}
	imgs[0].style.display = 'block';
	imgs[0].xOpacity = .99;
	
	setTimeout(so_xfade, 3000);
}

function so_xfade()
{
	cOpacity = imgs[current].xOpacity;
	nIndex = imgs[current+1]?current+1:0;
	nOpacity = imgs[nIndex].xOpacity;

	cOpacity-=.05;
	nOpacity+=.05;

	imgs[nIndex].style.display = 'block';
	imgs[current].xOpacity = cOpacity;
	imgs[nIndex].xOpacity = nOpacity;

	setOpacity(imgs[current]);
	setOpacity(imgs[nIndex]);

	if(cOpacity<=0)
	{
		imgs[current].style.display = 'none';
		current = nIndex;
		setTimeout(so_xfade, 3000);
	}
	else
	{
		setTimeout(so_xfade,50);
	}

	function setOpacity(obj)
	{
		if(obj.xOpacity>.99)
		{
			obj.xOpacity = .99;
			return;
		}

		obj.style.opacity = obj.xOpacity;
		obj.style.MozOpacity = obj.xOpacity;
		obj.style.filter = 'alpha(opacity=' + (obj.xOpacity*100) + ')';
	}
}

function ShowBann (path, w, h)
{
	window.open(path,'ShowBann','width='+w+',height='+h+',status=no,toolbar=no,menubar=no,location=no,resizable=yes,scrollbars=yes,top='+ Math.round((screen.height/2)-(h/2)) +',left=' + Math.round((screen.width/2)-(w/2)));
} 
