/*------------------------------------------------------------------------------
|	DELPHINE SITE MANAGEMENT SYSTEM v.5
+-------------------------------------------------------------------------------
|	This file	: kernel.js
|	Author		: Dmitri Delphine
|	Note		: Основные функции
+-------------------------------------------------------------------------------
|	Copyright (c) 2005-2008 Dmitri Delphine. All Rights Reserved.
+-----------------------------------------------------------------------------*/

var uagent    = navigator.userAgent.toLowerCase();

var is_safari = ( (uagent.indexOf('safari') != -1) || (navigator.vendor == "Apple Computer, Inc.") );
var is_opera  = (uagent.indexOf('opera') != -1);
var is_webtv  = (uagent.indexOf('webtv') != -1);
var is_ie     = ( (uagent.indexOf('msie') != -1) && (!is_opera) && (!is_safari) && (!is_webtv) );
var is_ie4    = ( (is_ie) && (uagent.indexOf("msie 4.") != -1) );
var is_moz    = ( (navigator.product == 'Gecko')  && (!is_opera) && (!is_webtv) && (!is_safari) );
var is_ns     = ( (uagent.indexOf('compatible') == -1) && (uagent.indexOf('mozilla') != -1) && (!is_opera) && (!is_webtv) && (!is_safari) );
var is_ns4    = ( (is_ns) && (parseInt(navigator.appVersion) == 4) );
var is_kon    = (uagent.indexOf('konqueror') != -1);
var is_win    =  ( (uagent.indexOf("win") != -1) || (uagent.indexOf("16bit") !=- 1) );
var is_mac    = ( (uagent.indexOf("mac") != -1) || (navigator.vendor == "Apple Computer, Inc.") );

var ua_vers   = parseInt(navigator.appVersion);

var g_var_cookieid 		= '';         
var g_var_cookie_domain = '';
var g_var_cookie_path  	= '';

/*-------------------------------------------------------------------------*/
// Кроссбраузерно возвращает объект по его ID
/*-------------------------------------------------------------------------*/

function my_getbyid( id )
{
	obj = null;

	if( document.getElementById )
	{
		obj = document.getElementById( id );
	}
	else if( document.all )
	{
		obj = document.all[ id ];
	}
	else if( document.layers )
	{
		obj = document.layers[ id ];
	}

	return obj;
}

/*-------------------------------------------------------------------------*/
// Get cookie
/*-------------------------------------------------------------------------*/

function my_getcookie( name )
{
	cname = g_var_cookieid + name + '=';
	cpos  = document.cookie.indexOf( cname );

	if ( cpos != -1 )
	{
		cstart = cpos + cname.length;
		cend   = document.cookie.indexOf(";", cstart);

		if (cend == -1)
		{
			cend = document.cookie.length;
		}

		return unescape( document.cookie.substring(cstart, cend) );
	}

	return null;
}

/*-------------------------------------------------------------------------*/
// Set cookie
/*-------------------------------------------------------------------------*/

function my_setcookie( name, value, sticky )
{
	expire = "";
	domain = "";
	path   = "/";

	if ( sticky )
	{
		expire = "; expires=Wed, 1 Jan 2020 00:00:00 GMT";
	}

	if ( g_var_cookie_domain != "" )
	{
		domain = '; domain=' + g_var_cookie_domain;
	}

	if ( g_var_cookie_path != "" )
	{
		path = g_var_cookie_path;
	}

	document.cookie = g_var_cookieid + name + "=" + value + "; path=" + path + expire + domain + ';';
}

/*-------------------------------------------------------------------------*/
// Array: Push stack
/*-------------------------------------------------------------------------*/

function pushstack( thearray, newval )
{
	arraysize = stacksize( thearray );
	thearray[ arraysize ] = newval;
}

/*-------------------------------------------------------------------------*/
// Array: Pop stack
/*-------------------------------------------------------------------------*/

function popstack( thearray )
{
	arraysize = stacksize( thearray );
	theval = thearray[ arraysize - 1 ];

	delete thearray[ arraysize - 1 ];
	return theval;
}

/*-------------------------------------------------------------------------*/
// Array: Get stack size
/*-------------------------------------------------------------------------*/

function stacksize( thearray )
{
	for( i = 0 ; i < thearray.length; i++ )
	{
		if ( (thearray[i] == '') || (thearray[i] == null) || (thearray == 'undefined') )
		{
			return i;
		}
	}

	return thearray.length;
}

/*-------------------------------------------------------------------------*/
// Prevent browser bubblin'
/*-------------------------------------------------------------------------*/

function _cancel_bubble(obj, extra)
{
	if ( ! obj || is_ie)
	{
		if ( extra )
		{
			window.event.returnValue = false;
		}

		window.event.cancelBubble = true;

		return window.event;
	}
	else
	{
		if (obj.target.type == 'submit')
		{
			obj.target.form.submit();
		}

		obj.stopPropagation();

		if ( extra )
		{
			obj.preventDefault();
		}

		return obj;
	}
}

/*-------------------------------------------------------------------------*/
// Получаем объект текущего эвента
/*-------------------------------------------------------------------------*/

function _get_event(e)
{
	if( e != null )
    {
		return e.target;
    }
    else
    {
    	if( window.event != null )
		{
        	return window.event.srcElement;
        }
        else
        {
        	return null;
        }
    }
}

/*-------------------------------------------------------------------------*/
// Получаем родителя объекта
/*-------------------------------------------------------------------------*/

function _get_parent( obj )
{
	if( obj == null )
    {
    	return null;
    }

    if( obj.parentNode != null )
    {
    	return obj.parentNode;
    }
    else
    {
    	if( obj.parentElement != null )
        {
        	return obj.parentElement;
        }
        else
        {
            return null;
        }
    }
}

/*-------------------------------------------------------------------------*/
// Зуум!
/*-------------------------------------------------------------------------*/
/*
function zoom( url, width, height )
{
	var name = '';
	
	for( var i = 0; i < 16; i++ )
	{ 
		 name += String.fromCharCode( Math.round( Math.random() * 25 ) + 97 )
	}
	
    var hwnd = window.open( '', name, 'height='+height+',width='+width+',scrollbars=0,AlwaysRaised' );

    with( hwnd.document )
    {
    	write('<html><head><title>.:</title></head><body style="margin:0;padding:0">');
        write('<img src="'+url+'" border="0"></body></html>');
        close();
		focus();
    }	

}
*/

function zoom( url, width, height, name, printable )
{
	if( name == '' ) {
		for( var i = 0; i < 16; i++ ) {
			name += String.fromCharCode( Math.round( Math.random() * 25 ) + 97 );
		}
	}
	
	width = parseInt( width );
	height = parseInt( height );
	
	if( is_safari ) {
		width  += 4;
		height += 4;
	}	
	
	var scrollbars = ( screen.availHeight < height ) ? 1 : 0;
		
	var hwnd = window.open( '', name, 'height='+height+',width='+width+',scrollbars='+scrollbars+',alwaysraised=1' );						 

	with( hwnd.document )
	{
		write('<html><head><title>Фото</title></head><body style="margin:0;padding:0">');
		write('<img src="'+url+'" border="0">');
		
		if( printable == true ) {
			write('<div style="position:absolute;left:0;top:0;">');
			write('<span style="background:#fff;color:#222;font-family:Verdana;font-size: 11px;cursor:pointer;padding:2px;');
			write('text-decoration:underline;" onclick="this.style.display=\'none\';window.print();">Распечатать</span></div>');
		}
		
		write('</body></html>');
		close();				
	}
	
	hwnd.focus();
}

/*-------------------------------------------------------------------------*/
// DST Auto correction
/*-------------------------------------------------------------------------*/

function global_dst_check( tzo, dst )
{
	var thisoffset = tzo + dst;
	var dstoffset  = new Date().getTimezoneOffset() / 60;
	var url        = 'index.php?u=j&do=dst-autocorrection&md5check=' + md5_check;

	if ( Math.abs( thisoffset + dstoffset ) == 1 )
	{
		try
		{
			//----------------------------------
			// No fancy - no dancy
			//----------------------------------

			xml_dst_set( url + '&xml=1' );
		}
		catch(e)
		{
			// Dst not set 
		}
	}
}

/*-------------------------------------------------------------------------*/
// Hide / Unhide menu elements
/*-------------------------------------------------------------------------*/

function ShowHide(id1, id2, id3)
{
	if (id1 != '') toggleview(id1);
	if (id2 != '') toggleview(id2);
	if (id3 != '') toggleview(id3);
}

/*-------------------------------------------------------------------------*/
// Show/hide toggle
/*-------------------------------------------------------------------------*/

function toggleview( id )
{	
	if( ! id ) return;

	if( itm = my_getbyid( id ))
	{
		if (itm.style.display == "none")
		{
			my_show_div(itm);
		}
		else
		{
			my_hide_div(itm);
		}
	}
}

/*-------------------------------------------------------------------------*/
// Set DIV ID to hide
/*-------------------------------------------------------------------------*/

function my_hide_div(itm)
{	
	if ( ! itm ) return;

	itm.style.display = "none";
}

/*-------------------------------------------------------------------------*/
// Set DIV ID to show
/*-------------------------------------------------------------------------*/

function my_show_div(itm)
{	
	if ( ! itm ) return;

	itm.style.display = "";
}

/*-------------------------------------------------------------------------*/
//
/*-------------------------------------------------------------------------*/

function checkRadioSelect( sender )
{
	for( var i = 0; i < sender.length; i++ ) if( sender[i].checked ) return sender[i].value;
	
	return false;
}

/*-------------------------------------------------------------------------*/
//
/*-------------------------------------------------------------------------*/

var net = new Object();

net.READY_STATE_UNINITIALIZED	= 0;
net.READY_STATE_LOADING		= 1;
net.READY_STATE_LOADED		= 2;
net.READY_STATE_INTERACTIVE	= 3;
net.READY_STATE_COMPLETE	= 4;

net.ContentLoader = function( url, onload, onerror )
{
	this.url	= url;
	this.req	= null;
	this.onload	= onload;
	this.onerror= (onerror) ? onerror : this.defaultError;
	this.loadXMLDoc(url);
}

net.ContentLoader.prototype = 
{	
	loadXMLDoc:function(url)
	{				
		if( window.XMLHttpRequest )
		{
			this.req = new XMLHttpRequest();
		}
		else if( window.ActiveXObject )
		{
			this.req = new ActiveXObject("Microsoft.XMLHTTP");
		}
		
		if( this.req ) try
		{
			var loader = this;						
			
			this.req.onreadystatechange = function()
			{
				loader.onReadyState.call(loader);
			}
						
			this.req.open('GET', url, true);
			this.req.send(null);			
		}
		catch(err) { this.onerror.call(this); }
	},

	onReadyState:function()
	{
		var req = this.req;
		var ready = req.readyState;
		
		if( ready == net.READY_STATE_COMPLETE )
		{
			var httpStatus = req.status;
			
			if( httpStatus==200 || httpStatus==O )
			{
				this.onload.call(this);
			} else {
				this.onerror.call(this);
			}
		}
	},
	
	defaultError:function()
	{
		alert( "error fetching data!"
				+"\n\nreadyState:" + this.req.readyState
				+"\nstatus: " + this.req.status
				+"\nheaders: " + this.req.getAllResponseHeaders());
	}
}

function hiddenBlockShowHide( sender )
{
	try {
		var hb = sender.parentNode.childNodes[1];
	}
	catch(e) {
		if( is_moz ) {
			try {
				var hb = sender.parentNode.nextSibling;
			} catch(e) {
				return false;
			}
		}
		else return false;
	}
	
	if( hb.style.display == '' )
	{						
		hb.style.display = 'none';
	} else {
		hb.style.display = '';			
	}
				
	return false;
}

function addtofav( url, title )
{	
	if( is_ie ) // IE
	{
		window.external.AddFavorite( url, title );
	}
	else if( is_moz ) // FF
	{
		window.sidebar.addPanel( title, url, '');
	}
	else if( is_opera ) // Opera
	{
		var chr = 'CTRL-D';
		var agt = navigator.userAgent.toLowerCase();	
		if( agt.substr( agt.indexOf('opera')+6,1 ) < 9 ) chr = 'CTRL-T';
		
		alert('Нажмите '+chr+' чтобы добавить эту страницу в избранное.');                  
	}
	
	return false;
}

function showPrintV()
{
	var s = location.href;
	s  = s.replace(/[\?|\&]print/,'').replace(/\#[a-zA-Z0-9]*/,'');
	s += (/\/\?/.test(s)?'&':'?')+'print';
	window.open(s);		
	return false;
}

