var blank = new Image();

var header_img_count=0;
var last_time = false;
var override = false;
var header_text = '';

function fixPng(png) {
	var src = png.src;
	if (!png.style.width) { png.style.width = $(png).width(); }
	if (!png.style.height) { png.style.height = $(png).height(); }
	png.onload = function() { };
	png.src = blank.src;
	png.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "',sizingMethod='scale')";
}
function print_page(){
	window.print();
	window.location.reload([true]);
}
$(window).load( function(){
	$(".print_button").click( function () {
		$('div.polesno').css("display","none");
		$('#contact').css("display","none");

		$print_text = $("#text").html();
		$("table.body_table").css("display","none");
		$("body").append("<div id='print_text'></div>");
		$("#print_text").append($print_text);
		$("div#more_information").remove();
		$("a.print_button").remove();
		$("img.printer_img").remove();
		setTimeout(print_page, 500);
		return false;
	});
	if (window.location.toString() != 'http://www.woodh.ru/call_back.php' && window.location.toString() != 'http://woodh/call_back.php') {
		var $call_back_form = $('#call_back_form');
		var $all_text = $('#all_text');
		var $call_submit = $('input.call_submit');
		var temp;
		$('a.callme').click(function() {
			var t_offset = $('a.callme').offset();
			$call_back_form.css('left', t_offset.left - 350).css('top', t_offset.top).show();
			return false;
		});
		$(window).resize(function() {
			var t_offset = $('a.callme').offset();
			if ($call_back_form.css('display') == 'block') {
				$call_back_form.css('left', t_offset.left - 350).css('top', t_offset.top);
			}
		});
		$('span.close_callback').click(function() {
			$call_back_form.hide();
			$call_back_form.find('*').show();
			$all_text.hide();
		});
		$call_submit.click(function() {
			var phone = $('input.call_input[name=phone]').val();
			var name = $('input.call_input[name=name]').val();
			var question = $('textarea.call_textarea[name=question]').val();
			$call_submit.hide();
			if (phone.length > 5 && question.length > 5) {
				$.post("/call_back.php", { phone: phone, name: name, question: question, js: 'true' },
						function (data) {
							$call_back_form.find('*').hide();
							temp = $call_back_form.position();
							$all_text.html('Спасибо, ваше сообщение отправлено').css('left', temp.left + 5).css('top', temp.top + 150);
							$all_text.show();
							setTimeout('$("span.close_callback").click()', 2000);
							$call_submit.show();
						});
			} else {
				temp = $call_back_form.position();
				$all_text.html('Сообщение не может быть пустым').css('left', temp.left + 15).css('top', temp.top + 150);
				$all_text.show();
				setTimeout('$("#all_text").hide()', 2000);
				$call_submit.show();
			}
			return false;
		});
	}

	$("#text a[target$=blank]").addClass("gallery");
	$("#text a[target$=blank]").attr("rel", "lightbox");
	$("a.gallery").fancybox();
		
	var ie = (/MSIE ((5\.5)|6)/.test(navigator.userAgent) && navigator.platform == "Win32");
	if (ie) {
		$('img[src$=.png]').each(function() {
			if (!this.complete) {
				this.onload = function() { fixPng(this) };
			}else {
				fixPng(this);
			}
		});
	}

	var temp, item_count = 0, ul_length = 0, $ul = $('div.gallery ul'), current_item = 0, current_item_vert = 0, item_width = 125, leaf_kol = 4, leaf_kol_vert = 4, current_action, current_action_vert, $scroll_div = $('div.gallery div'), $scroll_div_vert = $('div.gallery div'), $gallery = $('div.center_gallery a'), $gallery_vert = $('div.gallery a'), $preview = $('div.gallery_preview img');

	$gallery.each(function() {
		item_count++;
	});
	$gallery_vert.each(function() {
		item_count++;
	});

	
	var IE='\v'=='v';

	if(IE) {		
		item_width=121;
		item_width+=5;
	}
	$('div.gallery ul').width( item_width * item_count );
	function center_picture( $img, $par, mot ) {
		
		var img_height	= $img.height(), par_height	= $par.height(), new_top = 0;
		new_top += parseInt( ( par_height - img_height ) / 2 );
		if ( mot == 'fast' ) {
			$img.css('top', new_top);
		}else {
			$img.animate({
				top: new_top
			},'slow');
		}
	}
	
	function ul_leaf( direction ) {
		current_action = '';
		temp = leaf_kol;
		if ( direction == 'left' ) {
			if ( current_item > leaf_kol - 1 ) {
				current_item-=leaf_kol;
				current_action = '-';
			}else {
				current_action = '-';
				temp = current_item;
				current_item = 0;
			}
		}else {
			if ( current_item + 2 * leaf_kol < item_count  ) {
				current_item+=leaf_kol;
				current_action = '+';
			}else {
				current_action = '+';
				current_item = item_count - leaf_kol;
				temp = item_count % leaf_kol;
			}
		}
		if ( current_action != '' ) {
			$scroll_div.animate({
				scrollLeft: current_action + '=' + item_width * temp
			},'slow');
		}
	}

	$('span.center_gallery_left').click(function(){
		ul_leaf('left');
	});
	$('span.center_gallery_right').click(function(){
		ul_leaf('right');
	});
	
	center_picture( $preview, $preview.parent(), 'fast' );

	function ul_leaf_vert( direction ) {
				alert('ghb');
		current_action = '';
		temp = leaf_kol_vert;
		if ( direction == 'top' ) {
			if ( current_item > leaf_kol_vert - 1 ) {
				current_item-=leaf_kol_vert;
				current_action = '-';
			}else {
				if ( current_item != 0 ){
					current_action = '-';
					temp = current_item;
					current_item = 0;
				}
			}
		}else {
			if ( current_item + 2 * leaf_kol_vert < item_count  ) {
				current_item+=leaf_kol_vert;
				current_action = '+';
			}else {
				if ( current_item != item_count - leaf_kol_vert ) {
					current_action = '+';
					current_item = item_count - leaf_kol_vert;
					temp = item_count % leaf_kol_vert;
				}
			}
		}
		if ( current_action != '' ) {
			$scroll_div_vert.animate({
				scrollLeft: current_action + '=' + item_height * temp
			},'slow');
		}
	}
	
	
	$gallery_vert.click(function() {

		$gallery_vert.removeClass('gallery_current_item');
		$(this).addClass('gallery_current_item');
		var preview_img = new Image();
		temp = $(this).attr('href');
		$(preview_img).attr('src', temp).load( function() {
			$preview.fadeOut('fast', function() {
				$preview.attr('src', temp).fadeIn('slow', function() {
					center_picture( $preview, $preview.parent() );
				});
			});
		});
		return false;
	});
	
	$('span.gallery_top').click(function() {
		ul_leaf_vert('top');
	});
	$('span.gallery_bottom').click(function() {
		ul_leaf_vert('bottom');
	});
});

function getElementsByClass( searchClass, node, tag ) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;	
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
		for (i = 0, j = 0; i < elsLen; i++) {
			if ( pattern.test(els[i].className) ) {
				classElements[j] = els[i];
				j++;
			}
		}
	return classElements;
}
