if( !SITE_URL )	var SITE_URL = 'http://'+ document.domain; 
if( !SETTINGS )	var SETTINGS = []; 
if( !ELEMENTS )	var ELEMENTS = []; 
if( !XMLHTTP )	 var XMLHTTP = (	window.ActiveXObject	?	new ActiveXObject("Microsoft.XMLHTTP")	:	new XMLHttpRequest()	); 
if( !CONTAINER ) var CONTAINER = []; 


$( document ).ready( function(){

	sli_update();

	ELEMENTS[ 'top_menu_max' ] = $( '#top_menu_max' );
	ELEMENTS[ 'top_menu_min' ] = $( '#top_menu_min' );

	SETTINGS[ 'menu_mode' ] = get_cookie( 'menu_mode' );
	/*
	if( SETTINGS[ 'menu_mode' ] == 'min' ){
		$( ELEMENTS[ 'top_menu_max' ] ).hide();
		$( ELEMENTS[ 'top_menu_min' ] ).show();
	}else{
		$( ELEMENTS[ 'top_menu_max' ] ).show();
		$( ELEMENTS[ 'top_menu_min' ] ).hide();
	}
	*/
	if( SETTINGS[ 'bg' ][ 'min' ]	&&	SETTINGS[ 'bg' ][ 'mid' ]	&&	SETTINGS[ 'bg' ][ 'max' ]	&&	SETTINGS[ 'bg' ][ 'color' ] ){
		if( screen.width <= 1050 ){ bg_size = 'min'; }else if( screen.width <= 1300 ){ bg_size = 'mid'; }else{ bg_size = 'max'; }
		$( 'body' ).css( {'background-image':'url(\''+ SITE_URL +'/'+ SETTINGS[ 'bg' ][ bg_size ] +'\')', 'background-color':'#'+ SETTINGS[ 'bg' ][ 'color' ] } );
	}else{
		$( 'body' ).css( {'background-image':'url(\''+ SITE_URL +'/img/bg.gif\')', 'background-position':'top right', 'background-color':'#f3f3f3' } );
	}
	
	input_image_update();

	auth_email_obj		= $( '#auth_email' );
	auth_password_obj	= $( '#auth_password' );

	if( auth_email_obj != null  &&  auth_password_obj != null  ){

		var auth_form_confirm = { 'email':0, 'password':0 };

		$( auth_email_obj ).val( '' );
		$( auth_password_obj ).val( '' );

		$( auth_email_obj ).bind( 'focus', function(){
			$( '#auth_email_lbl' ).hide();
		} );
		$( auth_email_obj ).bind( 'blur', function(){
			if( $( this ).val().length == '' )
			$( '#auth_email_lbl' ).show();
		} );

		$( auth_password_obj ).bind( 'focus', function(){
			$( '#auth_password_lbl' ).hide();
		} );
		$( auth_password_obj ).bind( 'blur', function(){
			if( $( this ).val().length == '' )
			$( '#auth_password_lbl' ).show();
		} );

		$( auth_email_obj ).bind( 'keyup', function(){
			obj_value = $( this ).val();
			email_format = /^[a-zA-Z0-9\.\-_]{1,}@[a-zA-Z0-9\.\-]{1,}\.[a-zA-Z0-9\.\-]{1,}$/;
			if( email_format.test( obj_value ) ){
				$( this ).css( 'border', '1px solid #66FF66' );
				auth_form_confirm[ 'email' ] = 1;
			}else{
				$( this ).css( 'border', '1px solid #FF6666' );
				auth_form_confirm[ 'email' ] = 0;
			}
			if( auth_form_confirm[ 'email' ] == 1	&&	auth_form_confirm[ 'password' ] == 1 ) $( '#auth_submit' ).html( '<a href="javascript:$(\'#form_auth\').submit();">войти</a>' ); else $( '#auth_submit' ).html( 'войти' ); 
		} );

		$( auth_password_obj ).bind( 'keyup', function(){
			obj_value = $( this ).val();
			password_format = /^[a-zA-Z0-9]{5,20}$/;
			if( password_format.test( obj_value ) ){
				$( this ).css( 'border', '1px solid #66FF66' );
				auth_form_confirm[ 'password' ] = 1;
			}else{
				$( this ).css( 'border', '1px solid #FF6666' );
				auth_form_confirm[ 'password' ] = 0;
			}
			if( auth_form_confirm[ 'email' ] == 1	&&	auth_form_confirm[ 'password' ] == 1 ) $( '#auth_submit' ).html( '<a href="javascript:$(\'#form_auth\').submit();">войти</a>' ); else $( '#auth_submit' ).html( 'войти' ); 
		} );
	}
	
	$( '.headX' ).each( 
		function(){
			pd_sub_hover( this );
		}
	);
});


var toStr = function( index ){
	if( typeof this == 'object' ){
		if( index == undefined ){
			return_str = typeof( this ) +' {\n';
			for( key in this )
				if( typeof this[ key ] == 'string' )
					return_str += '\t'+ key +'\t:\t'+ this[ key ] +'\n';
				else if( typeof this[ key ] == 'array' )
					return_str += '\t'+ key +'\t:\t'+ this[ key ] +'\n';
				else if( typeof this[ key ] == 'object' )
					return_str += '\t'+ key +' type of this is Object, see more with use Obj.get( index )\n';
			return_str += '}';
			return return_str;
		}else if( this[ index ] != undefined ){
			return_str = '';
			if( typeof this[ index ] == 'string' )
				return_str += index +'\t:\t'+ this[ index ] +'\n';
			else if( typeof this[ index ] == 'array' )
				return_str += '\t'+ index +'\t:\t'+ this[ index ] +'\n';
			else if( typeof this[ index ] == 'object' )
				return_str += index +' is multilevel Object{\n'; step = 0;
				for( key in this[ index ] ){
					return_str += '\t'+ key +'\t:\t'+ this[ index ][ key ] +'\n';
					if( step == 10 ){ return_str += '\tSo many elements of the object......\n'; break; }
					step += 1;
				}
				return_str += '}';
			return return_str;
		}
	}else{
		return undefined;
	}
}


function build_href_url_js( url_str, js_str ){
	return_str = '';
	if( XMLHTTP ){
		return_str = 'javascript:'+ js_str;
	}else{
		return_str = url_str;
	}
	return return_str;
}


function goto_url( url_str ){
	window.location.href = url_str;
}

function set_cookie( name, value, days ){
	var today = new Date();
	expires = new Date(today.getTime() + days*24*60*60*1000);
	document.cookie = name + "=" + escape(value) +"; expires=" + expires.toGMTString()+"; path=/; domain="+ document.domain;
}

function del_cookie( name, value ){
	var today = new Date();
	expires = new Date(today.getTime() - 10*24*60*60*1000);
	document.cookie = name + "=" + escape(value) +"; expires=" + expires.toGMTString()+"; path=/; domain="+ document.domain;
}

function get_cookie( name ){
	var search = name + "="
	if (document.cookie.length > 0) {
		offset = document.cookie.indexOf(search);
		if (offset != -1) { 
			offset += search.length 
			end = document.cookie.indexOf(";", offset) 
			if (end == -1) 
				end = document.cookie.length
			return unescape(document.cookie.substring(offset, end))
		} 
	}
}

function add_script( id, file_str ){
	if( file_str.indexOf( SITE_URL ) >= 0 )	file_str = file_str.replace( SITE_URL+'/', '' )	+	SITE_URL+'/';
	document.getElementsByTagName( 'head' )[0].appendChild(	add_el( 'script', {id: id, type: 'text/javascript', src: file_str } )	);
}

function add_el(tagName, attr, style) {
	var el = document.createElement(tagName);
	if (attr) extend(el, attr);
	if (style) setStyle(el, style);
	return el;
}


function sli_build( sli_type, sli_title, sli_step, func_obj, row_arr, row_sel, arg_arr, sets_arr ){
	var inner_html = '';
	var inner_html_head = '';
	var inner_html_body = '';
	for( z in sets_arr ){
		if( z == 'head' ){
			for( x in sets_arr[ z ] ){
				inner_html_head += ' '+ x +'="'+ sets_arr[ z ][ x ] +'" ';
			}
		}else if( z == 'body' ){
			for( x in sets_arr[ z ] ){
				inner_html_body += ' '+ x +'="'+ sets_arr[ z ][ x ] +'" ';
			}
		}
	}
	inner_html += '<div id="sli_head_'+ sli_title + ( sli_step !== '' ? '_'+ sli_step : '' ) +'" class="sli_'+ ( sli_type.length > 0 ? sli_type +'_' : '' ) +'head'+ ( row_sel === false	&&	( sli_type == 'hd' || sli_type == 'mn' ) ? '_empty' : '' ) +'" '+ inner_html_head +'>'+ ( row_sel !== false ? '<nobr>'+ row_arr[ row_sel ] +'</nobr>' : '' ) + '</div>';
	inner_html += '<div id="sli_body_'+ sli_title + ( sli_step !== '' ? '_'+ sli_step : '' ) +'" class="sli_'+ ( sli_type.length > 0 ? sli_type +'_' : '' ) +'body" '+ inner_html_body +'>';
	for( z = 0; z < row_arr.length; z++ ){
		if( $.isArray( func_obj ) ){
			func = func_obj[ z ];
		}else{
			func = func_obj;
		}
		row_title = row_arr[ z ];
		if( arg_arr[ z ].length > 0 ){
			for( x = 0; x < arg_arr[ z ].length; x++ ){
				arg_arr[ z ][ x ] = '\''+ arg_arr[ z ][ x ] +'\'';
			}
			inner_html += '<a href="javascript:'+ func +'('+ arg_arr[ z ].join(',') +');" >'+ row_title +'</a><br>';
		}else{
			inner_html += ''+ row_title +'<br>';
		}
	}
	inner_html += '</div>';
	$( '#sli_'+ sli_title + ( sli_step !== '' ? '_'+ sli_step : '' ) ).html( inner_html );
}


function sli_update(){
	$('.sli_body').hide();
	$('.sli_body').mouseleave( function(){ $(this).fadeOut( 500 ); } );
	$('.sli_head').each( function(){
		head_id = this.id;
		body_id = head_id.replace( 'sli_head', 'sli_body' );
		$(this).unbind( 'click' );
		$(this).bind( 'click', {obj_id:body_id}, function( event ){
			obj_id = event.data.obj_id;
			obj_item = document.getElementById( obj_id );
			obj_display = obj_item.style.display;
			$('.sli_body').hide();
			$('.sli_mn_body').hide();
			$('.sli_hd_body').hide();
			$('.sli_gr_body').hide();
			$('.msli_body').hide();
			obj_item.style.display = obj_display;
			if( !obj_item ) return;
			if( obj_item.style.display == 'block'	||	obj_item.style.display == '' ){
				obj_item.style.display = 'none';
			}else{
				obj_item.style.display = 'block';
			}
		});
	});
	$( '[id^=top_menu_min_body]' ).mouseleave( function(){ $( this ).fadeOut( 500 ); } );
}

function input_image_update(){
	$( ':input:image' ).each( function(){
		image_a = this.src;
		if( image_a.indexOf( '.gif' ) > 0 ){
			image_ext = 'gif';
		}else if( image_a.indexOf( '.jpg' ) > 0 ){
			image_ext = 'jpg';
		}else if( image_a.indexOf( '.png' ) > 0 ){
			image_ext = 'png';
		}
		image_o = image_a.replace( '_a.'+ image_ext, '_o.'+ image_ext );
		$( this ).bind( 'mouseover', {obj_item:this,image_src:image_o}, function( event ){ 
			obj_item = event.data.obj_item;
			obj_item.src = event.data.image_src; 
		} );
		$( this ).bind( 'mouseout', {obj_item:this,image_src:image_a}, function( event ){ 
			obj_item = event.data.obj_item;
			obj_item.src = event.data.image_src; 
		} );
	} );
}


function popup_open( obj_body ){
	div_container = '<div id="popup_bg" style="margin:0px; position:absolute; float:left; left:0px; top:0px; width:100%; height:100%; z-order:99; background-color:#ffffff; FILTER:alpha(opacity=0); -moz-opacity:0; opacity:0;"></div><div id="popup_body" style="margin:0px; position:absolute; float:left; left:0px; top:0px; z-order:98;">'+ obj_body +'</div>';
	$( 'body' ).prepend( div_container );
	var html = document.documentElement;
	var body = document.body;
	scroll_top = (	html	&&	html.scrollTop	 ||	 body	&&	body.scrollTop	 ||	 0	);
	$( '#popup_bg' ).css( 'top', scroll_top );
	$( '#popup_body' ).css( 'top', (	scroll_top	+	( $( '#popup_bg' ).innerHeight() - $( '#popup_body' ).outerHeight() )	/	2	) ).css( 'left', (	( $( 'body' ).outerWidth() - $( '#popup_body' ).outerWidth() )	/	2	) );
	$( '#popup_bg' ).click( function(){ popup_close(); } );
}

function popup_close(){
	$( '#popup_bg' ).hide(); 
	$( '#popup_body' ).hide();
	$( 'body' ).attr('scroll','yes');
}

function nl_to_br( return_str ){
	nl_frmt = new RegExp( '(\r|\n|\r\n)+', 'm' );
	if( nl_frmt.test( return_str ) ){
		while( nl_frmt.test( return_str ) )	return_str = return_str.replace( nl_frmt, '<br>' );
	}
	return return_str;
}

function br_to_nl( return_str ){
	br_frmt = new RegExp( '(<br>|<br/>|<br />|<br >)+', 'i' );
	if( br_frmt.test( return_str ) ){
		while( br_frmt.test( return_str ) )	return_str = return_str.replace( br_frmt, '\n' );
	}
	return return_str;
}


window.set_hash = function( hash ){
	if( hash.indexOf( '#' ) == -1 )	hash = '#'+ hash;
	window.location.hash = hash;
}

window.ini_hash = function(){

}
