jQuery.fn.reverse = [].reverse;

// XHR

var xhr_timeout = 'Время ожидания ответа истекло';
var xhr_error = 'Ошибка при выполнении запроса';

function sendXHR( http_string, post_string, on_ready, on_error, obj ) {
	// Create an object
	var xhr;
	try { xhr = new ActiveXObject( 'Msxml2.XMLHTTP' ) }
	catch( e ) {
		try { xhr = new ActiveXObject( 'Microsoft.XMLHTTP' ) }
		catch( E ) { xhr = false }
	}
	if( !xhr && typeof XMLHttpRequest != 'undefined' ) { xhr = new XMLHttpRequest() }

	// Initiate the object
	if( post_string == null ) { xhr.open( 'GET', http_string, true ) }
	else { xhr.open( 'POST', http_string, true ) }
	xhr.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded' );
	xhr.setRequestHeader( 'X-Requested-With', 'XMLHttpRequest' );
	xhr.send( post_string );

	// Check readyState
	var xhrTimeOut = setTimeout( function() { xhr.abort(); on_error.call( null, xhr_timeout ) }, 10000 );
	var checkReady = function() {
		if( xhr.readyState == 4 ) {
			clearTimeout( xhrTimeOut );
			if( xhr.status == 200 ) { on_ready.call( null, eval( '(' + xhr.responseText + ')' ), obj ) }
			else { on_error.call( null, xhr_error + ': ' + xhr.status, obj ) }
		}
		else { setTimeout( checkReady, 100 ) }
	}
	setTimeout( checkReady, 100 )
};

// HIDE RESULT BLOCK
function hide_result() {
	setTimeout( function() { $( "#result" ).addClass( "dn" ) }, 2000 );
}

// UPDATE HEADER CART INFORMATION
function update_header_cart( $count, $total ) {
	$( "#cart-q" ).text( $count );
	$( "#cart-t" ).text( $total );
}

// SETTING COOKIE

function setCookie( c_name, value, expiredays, path, domain ) {
	var expdate = new Date();
	expdate.setDate( expdate.getDate() + expiredays );
	document.cookie = c_name + "=" + escape( value ) +
		( ( expiredays == null ) ? "" : ";expires=" + expdate.toGMTString() ) +
		( ( path ) ? ";path=" + path : "" ) +
        ( ( domain ) ? ";domain=" + domain : "" );
}

// INSERTING FLASH

function insertFlash( node, url, width, height, params ) {
	var object, param, key;
	function newParam( name, value ) {
		if( 0 /*@cc_on + 1 @*/ ) return ['<param name="', name, '" value="', value, '" />'].join( '' );
		else {
			param = document.createElement( 'param' );
			param.name = name;
			param.value = value;
			return param;
		}
	}
	if( 0 /*@cc_on + 1 @*/ ){
		object = ['<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="', width, '" height="', height, '"><param name="movie" value="', url, '" />'];
		if(params){
			for( key in params ){
				if( params.hasOwnProperty( key ) ) {
					object.push( newParam( key, params[key] ) );
				}
			}
		}
		object.push( '</object>' );
		node.innerHTML = object.join( '' );
	}
	else {
		object = document.createElement( 'object' );
		object.type = 'application/x-shockwave-flash';
		object.data = url;
		object.width = width;
		object.height = height;
		if( params ){
			for( key in params ){
				if( params.hasOwnProperty( key ) ) {
					object.appendChild( newParam( key, params[key] ) );
				}
			}
		}
		while( node.firstChild ) { node.removeChild( node.firstChild ) };
		node.appendChild( object );
	}
};

// HEADER SLIDESHOW

function slideSwitch() {
	var $active = $( '#h-slider a.active' );
	if( $active.length == 0 ) { $active = $( '#h-slider a:last' ) }

	// use this to pull the divs in the order they appear in the markup
	var $next =  $active.next().length ? $active.next() : $( '#h-slider a:first' );

	// uncomment below to pull slides randomly
	/*
	var $sibs  = $active.siblings();
	var rndNum = Math.floor( Math.random() * $sibs.length );
	var $next  = $( $sibs[rndNum] );
	*/

	$active.addClass( 'last-active' );

	$next.css( { opacity: 0 } ).addClass( 'active' ).animate( { opacity: 1 }, 2222, function() {
		$active.removeClass( 'active last-active' )
	} );
}

// NAVIGATION IN PAGBOX

function navPrev( obj, len ) {
	var _jr = $( obj ).next().children();
	var left = parseInt( _jr.css( 'margin-left' ) );
	if( left + len <= 0 ) {
		var full_shift = left + len;
		_jr.animate( { marginLeft: full_shift + 'px' }, 333 );
	}
}
function navNext( obj, len ) {
	var _jr = $( obj ).prev().children();
	var w = parseInt( _jr.width() );
	var left = parseInt( _jr.css( 'margin-left' ) );
	if( left - len > -w ) {
		var full_shift = left - len;
		_jr.animate( { marginLeft: full_shift + 'px' }, 333 );
	}
}

// SWITCH ON/OFF CATEGORIES

function slideToggle( el, bShow ) {
	var $el = $( el ), height = $el.data( "originalHeight" ), visible = $el.is( ":visible" );

	// if the bShow isn't present, get the current visibility and reverse it
	if( arguments.length == 1 ) bShow = !visible;

	// if the current visiblilty is the same as the requested state, cancel
	if( bShow == visible ) return false;

	// get the original height
	if( !height ) {
		// get original height
		height = $el.show().height();
		// update the height
		$el.data( "originalHeight", height );
		// if the element was hidden, hide it again
		if( !visible ) $el.hide().css( { height: 0 } );
	}

	// expand the knowledge (instead of slideDown/Up, use custom animation which applies fix)
	if( bShow ) {
		$el.show().animate( { height: height }, { duration: 222, complete: function() { this.style.height = "auto" } } );
	} else {
		$el.animate( { height: 0 }, { duration: 222, complete: function () { $el.hide()	} } );
	}

	return false;
}
function onOff() {
	if( jQuery.browser.msie ) {
		$( this.nextSibling.nextSibling ).toggleClass( "dn" );
		$( this ).toggleClass( "switch-on" )
	}
	else {
		slideToggle( this.nextSibling.nextSibling )
	}
	$( this ).toggleClass( "switch-on" );
}
function on( id ) {
	var obj = document.getElementById( id );
	if( obj && obj.nextSibling && obj.nextSibling.nextSibling && obj.nextSibling.nextSibling.className == "dn" ) {
		obj.nextSibling.nextSibling.className = "";
	}
	$( this ).toggleClass( "switch-on" );
}

// PRODUCT PHOTOS


// -------------------------------------------------------------------------------------------------
// DOCUMENT READY
// -------------------------------------------------------------------------------------------------

$( document ).ready( function() {
	// VERT MENU HANDLER
	$( "div.switch" ).bind( "click", onOff );

	// EMAIL CLICK IN FOOTER
	$( 'span.email_c' ).click( function() { document.location.href = 'mailto:' + this.getAttribute( 'n1' ) + '@' + this.getAttribute( 'n2' ) } )

	// FIND
	$( 'span.h_find' ).click( function() {		if( $( '#find' ).val() == 'Поиск по каталогу' || $( '#find' ).val().length < 3 )	return;
		$( '#index' ).val( $( this ).attr( 'index' ) );
		$( '#search' ).submit();	} )
	$( '#find' ).blur( function() {		if( $( this ).val() != 'Поиск по каталогу' && $( this ).val().length < 3 )	$( this ).val( 'Поиск по каталогу' );	} ).focus( function() {
		if( $( this ).val() == 'Поиск по каталогу' )	$( this ).val( '' );
	} )
} )
