$(document).ready(function(){
	$( document ).ready( function() {
		$wr = $("div.wr");
		if( $wr.length ) {
			function mainMove( target ) {
				params = target.split( "_" );
				from = params[ 0 ];
				to = params[ 1 ];
				direction = params[ 2 ];			
				//alert( direction );				
				//var dir				
				$container = $("div.wr");				
				//$container.css( "overflow" , "hidden" );		
				$container.html( '<div id="construction"><div id="left" class="fl"></div><div id="right" class="fr"></div></div>' );				
				$construction = $("#construction" , $container );
				//$construction.css( "overflow" , "hidden" );		
				//direction = direction == "toLeft" ? "toRight" : "toLeft";
				$construction.css( "width" , ( $(window).width() * 2 ) + "px" );				
				$old_data_block = $( ( direction == "toRight" ? "#left" : "#right" ) , $construction );	
				$new_data_block = $( ( direction == "toRight" ? "#right" : "#left" ) , $construction );
				
				//alert( $new_data_block.length );				
				//alert( $old_data_block.html() );				
				$old_data_block
					.html( page_main_blocks_html[ from ] )
					.css( "width" , $(window).width() + "px" )
					.addClass( from );				
				//alert( $old_data_block.html() );				
				//return;
				$new_data_block
					.html( page_main_blocks_html[ to ] )
					.css( "width" , $(window).width() + "px" )
					.addClass( to );				
				
				if( direction == "toRight" ) {
					$construction
						.animate( { marginLeft: ( - $(window).width() ) + "px" }, 1000 , null , function() { 
							$container.html( "<div class=" + to + ">" + $new_data_block.html() + "</div>" );
							$( "a.main_move_link" , $container ).bind( "click" , function() {
								return !mainMove( this.id );
								} );
							}  );	
					}
				else {
					$construction
						.css( "marginLeft" , ( - $(window).width() ) + "px" )
						.animate( { marginLeft: "0px" }, 1000 , null , function() { 
							$container.html( "<div class=" + to + ">" + $new_data_block.html() + "</div>" );
							
							$( "a.main_move_link" , $container ).bind( "click" , function() {
								return !mainMove( this.id );
								} );
							}  );	
					}
				
				return true;
				}
				
			page_main_blocks_html = new Array();
			page_main_blocks_html[ "solve" ] = $( "div.solve" , $wr ).html();
			page_main_blocks_html[ "create" ] = $( "div.create" , $wr ).html();
			page_main_blocks_html[ "help" ] = $( "div.help" , $wr ).html();
			
			//alert( $( "a.main_move_link" , $wr ).length );
			$( ".main_move_link" , $wr ).live( "click" , function() {
				return !mainMove( this.id );
				} );
			
			}
		} );				

	if ($('a.ctrl-next').length || $('a.ctrl-previous').length){
	
		$(document).keypress(function(e){
			if (e.ctrlKey && e.which==39 && $('a.ctrl-next').length){
				document.location = $('a.ctrl-next').get(0).href;
				return false;
			}
			if (e.ctrlKey && e.which==37 && $('a.ctrl-previous').length){
				document.location = $('a.ctrl-previous').get(0).href;
				return false;
			}
		});
		$(document).keydown(function(e){
			if (e.ctrlKey && e.which==39 && $('a.ctrl-next').length){
				document.location = $('a.ctrl-next').get(0).href;
				return false;
			}
			if (e.ctrlKey && e.which==37 && $('a.ctrl-previous').length){
				document.location = $('a.ctrl-previous').get(0).href;
				return false;
			}
		});
		}
		
	//Навигация по картинкам в галереях
	$("#images_bookmarks a" ).bind( "mouseenter" , function() {
		goTo( this.id );
		} );
	$("#img_container .img_list a" ).bind( "click" , function() {
		return !goTo( this.id );
		} );	
	
	//навигация по вкладкам
	$rollover_bookmarks = $(".rollover_bookmarks");
	if( $rollover_bookmarks.length > 0 ) {
		function goToBookm( bookm_id ) {
			var $bookm_list = $(".rollover_bookmarks" );
			var $bookm_container = $("#bookmark_container_main" );
			var $bookm_container_inner = $("#bookmark_container_main #bookmark_container" );
			if( $bookm_list.length ) {
				var boorm_cur_id = $(".active" , $bookm_list ).attr( "id" );
				if( bookm_id == boorm_cur_id ) return true;
				var tmp_height = $bookm_container.height();
				$bookm_new_content_container = $("#" + bookm_id , $bookm_container );
				$("#" + boorm_cur_id , $bookm_list ).removeClass( "active" ).addClass( "noactive" );
				$bookm_cur_content_container = $("#" + boorm_cur_id , $bookm_container );
				$bookm_container
					.css( "overflow" , "hidden" )
					.animate( { height: "1px" }, 200 , null , function() {
						$bookm_cur_content_container.hide();
								
						$bookm_new_content_container.show();
						$bookm_container.animate( { height: tmp_height + "px" }, 200 );
						} );
					
				$("#" + bookm_id , $bookm_list ).addClass( "active" ).removeClass( "noactive" );
				}

			return true;
			}
		$(".rollover_bookmarks a" ).bind( "click" , function() {
			return !goToBookm( this.id );
			} );	
		}
	
	//Попап для клиентских отзывов
	$(".clientsResponsesPopup").live( "click" , function() {
		//alert( 'ddd' );
		$.fn.popup( { width: 480 , height: 660 , position: 'center' , header: 'Отзыв' , src: '/klarnetCMSlocal/modules/clients_responses/CR_show_popup.php?obj_id=' + this.id } );
		} );

	//Online-заявка
	$(".online-order").live( "click" , function() {
		//alert( 'ddd' );
		$.fn.popup( { width: 900 , height: 470 , position: 'center' , header: 'Online-заявка' , src: '/klarnetCMSlocal/modules/contacts/contact_form.htm' } );
		} );

	
	//Анимация для распределения процентов в Портфолио (по отраслям)
	$animateBars = $(".animateBars");
	if( $animateBars.length > 0 ) {
		$animateBars.each( function() {
			$("img" , $(this)).each(function() {
				$(this)
					.animate( { width: this.id +  "%" }, { queue:false, duration: 500 } );
				} );
			} );
		}
		
	$("a.klarnet-open" ).click( function() {
		var width = 20;
		var height = 20;
		var lang = "rus";
		//alert( width );
		//alert( "/klarnetCMS/" + ( lang ? "?_lang=" + lang : "" ) );
		var tmp_window = window.open( "/klarnetCMS/" + ( lang ? "?_lang=" + lang : "" ) , "cms" , "width=" + width +",height=" + height + ",menubar=no,resizable=yes" );
		if( tmp_window ) { 
			tmp_window.moveTo( parseInt( screen.availWidth - width ) / 2 , parseInt( screen.availHeight - height ) / 2 );
			tmp_window.focus();
			}
		} );

	/* Блоки слева на главной */
	$block_selectors = $("h3.block-selector" );
	if( $block_selectors.length > 0 ) {
		$block_selectors.click( function() {
			cur_el = this;
			$block_selectors.each( function( num , el ) {
				if( cur_el == el ) {
					$(el).addClass( "active" );
					$( "#" + el.id + "_block" ).show();
					}
				else {
					$(el).removeClass( "active" );
					$( "#" + el.id + "_block" ).hide();
					}
				} );
			} );
		}
	});
