var define_file_name_popup_img = "../popup_img.html";
var define_get_param_popup_img = "id";
//==============================================================================
//==============================================================================
function SetPageTitle(str)
{
	if(str.length > 0)
	{
		document.title = str;
	}
}
//==============================================================================
//==============================================================================
function LoadImg(element_id, img_prefix)
{
    if(document.getElementById(element_id) == null)
    {
    	return;
    }
    
    var big_img_src = GetBigImg(window.location.search.substring(1), img_prefix);
    
	if(big_img_src.length > 0)
	{
		var my_big_img  = '<img src="'+big_img_src+'" style="visibility:hidden;" onload="FitWindowSizeByPictSize();" onError="ImgError(\''+element_id+'\');">';
	
		document.getElementById(element_id).innerHTML = my_big_img;
	}
}
//==============================================================================
function ImgError(element_id)
{
	if(document.getElementById(element_id) != null)
    {
    	var error = '<img src="images/loading_no.gif">';
    	document.getElementById(element_id).innerHTML = error;
    }
}
//==============================================================================
function FitWindowSizeByPictSize()
{	
	window.moveTo(0, 0); 
	
	var arr_wh = getViewPortWH();
	
	var viewportwidth  = arr_wh[0];
 	var viewportheight = arr_wh[1];
 	
	var imgHeight = 0;
	var imgWidth  = 0;
	
	if(document.images[0])
	{
	    imgWidth  = document.images[0].width;
	    imgHeight = document.images[0].height;
	}
	
	var arr_img_new_size = GetResizeImageSizeProportionally(imgWidth, imgHeight, (viewportwidth), (viewportheight));
	
	if(document.images[0])
	{
	    document.images[0].width  = arr_img_new_size[0];
	    document.images[0].height = arr_img_new_size[1];
	    document.images[0].style.visibility = "visible";
	}
	
	if(document.images[0])
	{
	    imgWidth  = document.images[0].width;
	    imgHeight = document.images[0].height;
	}
	
	self.focus();
}
//==============================================================================
//==============================================================================
function GetResizeImageSizeProportionally(img_w, img_h, max_w, max_h)
{
	var new_img_w = 0;
	var new_img_h = 0;
	
	var percent_w = Math.floor((max_w*100)/img_w);
	var percent_h = Math.floor((max_h*100)/img_h);
	
	var dev_w = (100 - percent_w);
	var dev_h = (100 - percent_h);
	
	var dev = 0;
	
	if(dev_w < 0) //uvelichenie
	{
		if(dev_h < 0) //uvelichenie
		{
			dev = 0;
		}
		else 
		{
			dev = dev_h;
		}
	}
	else //namalenie
	{
		if(dev_h < 0) //uvelichenie
		{
			dev = dev_w;
		}
		else 
		{
			if(dev_w > dev_h)
				dev = dev_w;
			else 
				dev = dev_h;
		}
	}
	
	new_img_w = Math.floor(img_w - ((dev*img_w)/100));
	new_img_h = Math.floor(img_h - ((dev*img_h)/100));

	return [new_img_w, new_img_h];
}
//==============================================================================
//==============================================================================
function GetBigImg(query, img_prefix)
{
	var i = 0;
	var img_num = "";
    var img_dir_id = "";
    
	var vars = query.split("&");
    
	for(i in vars) 
	{ 
    	var pair = vars[i].split("=");
    	if(pair.length == 2)
    	{
    		if(pair[0] == define_get_param_popup_img)
    		{
    			var sub_pair = pair[1].split("|");
    			if(sub_pair.length == 2)
    			{
    				img_num    = sub_pair[0];
    				img_dir_id = sub_pair[1];
    				break;
    			}
    		}
    		break;
    	}
	}
	
	SetPageTitle(img_num);
	
	var big_img_src = "";
	
	if(img_num.length > 0 && img_dir_id.length > 0)
	{
		switch(img_dir_id)
		{
			case "1":
			big_img_src = img_prefix+"images/"+img_num+"_big.jpg";
			break;
			
			case "2":
			big_img_src = img_prefix+"images_product/"+img_num+"_big.jpg";
			break;
		}
	}
	
	return big_img_src;
}
//==============================================================================
//==============================================================================
function SwapImageSrc(img_name, img_src)
{
	if(document.images[img_name] != null)
	{
		document.images[img_name].src=img_src;
	}
}
//==============================================================================
//==============================================================================
var popy;
function PopUpOpen(my_new_url,winName,features)
{
	if(!window.focus)
		return true;

	if(popy!=null)
	{
		if(false == popy.closed)
		{
			popy.window.close();
		}
    }
	popy = window.open(my_new_url,winName,features);
	popy.focus();
	
    return false;
}
//==============================================================================
//==============================================================================
function HelpOpenNewSmallWin1(param)
{
	var my_width  = (screen.width);
	var my_height = (screen.height);
	
	var my_new_url = define_file_name_popup_img+'?'+define_get_param_popup_img+'='+param;
	var winName  = 'small_win_1';
	var features = 'width='+my_width+',height='+my_height+',toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,copyhistory=no';

	PopUpOpen(my_new_url,winName,features);
}
//==============================================================================
//==============================================================================
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 getViewPortWH()
{
	var viewportwidth  = 0;
 	var viewportheight = 0;
 
	// the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
	if (typeof window.innerWidth != 'undefined')
	{
		viewportwidth  = window.innerWidth;
		viewportheight = window.innerHeight;
	}
	
	// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
	else if (typeof document.documentElement != 'undefined'
			&& typeof document.documentElement.clientWidth !=
			'undefined' && document.documentElement.clientWidth != 0)
	{
		viewportwidth  = document.documentElement.clientWidth;
		viewportheight = document.documentElement.clientHeight;
	}
	
	// older versions of IE
	else
	{
		viewportwidth  = document.getElementsByTagName('body')[0].clientWidth;
		viewportheight = document.getElementsByTagName('body')[0].clientHeight;
	}
	
	return [viewportwidth, viewportheight];
}
//==============================================================================
//==============================================================================
function CreateSWF(file_path, file_id, width, height)
{
	str_one  = '';
	str_one += '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="'+width+'" height="'+height+'" name="'+file_id+'" align="middle">';
	str_one += '<param name="allowScriptAccess" value="sameDomain">';
	str_one += '<param name="movie" value="'+file_path+'">';
	str_one += '<param name="quality" value="high">';
	str_one += '<param name="wmode" value="transparent">';
	str_one += '<param name="bgcolor" value="#ede6dc">';
	str_one += '<embed src="'+file_path+'" quality="high" wmode="transparent" bgcolor="#ede6dc" width="'+width+'" height="'+height+'" name="'+file_id+'" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer">';
	str_one += '</object>';
	
	document.write(str_one);
}
//==============================================================================
//==============================================================================
