var body_w;
var header_time = 0;
var header_index = 1;
var el_name = "";
var el = "";
var hov_el = "";
var selected_item;
var t;

$(function() {
	$('#about_company_menu li').each(
		function () {
			if ($(this).hasClass('active'))
				selected_item = this;
		}
	);

	$('#about_company_menu li').hover(
		function () {
			if (this !== selected_item)
				$(this).addClass('active');
		},
		function () {
			if (this !== selected_item)
				$(this).removeClass('active');
		}
	);

	$('#navigate li').each(
		function () {
			if ($(this).hasClass('active'))
				selected_item = this;
		}
	);

	$('#navigate li').hover(
		function () {
			if (this !== selected_item)
				$(this).addClass('active');
		},
		function () {
			if (this !== selected_item)
				$(this).removeClass('active');
		}
	);

	$('#english_version li').each(
		function () {
			if ($(this).hasClass('active'))
				selected_item = this;
		}
	);

	$('#english_version li').hover(
		function () {
			if (this !== selected_item)
				$(this).addClass('active');
		},
		function () {
			if (this !== selected_item)
				$(this).removeClass('active');
		}
	);
}); 

function hoverMe() {
	var a = hoverMe.arguments;
	hov_el = a[0];
	$(hov_el).css('background-color', '#b8d8f6');
}

function unhoverMe() {
	$(hov_el).css('background-color', '');
}

function preloadImages() {
    var d = document;
    if (d.images) {
	if (!d.M_p)
	    d.M_p = new Array();
	var i, j = d.M_p.length, a = preloadImages.arguments;
	for (i = 0; i < a.length; i++)
	    if (a[i].indexOf("#") != 0) {
		d.M_p[j] = new Image;
		d.M_p[j++].src = a[i];
	    }
    } // if
}

function selectImage() {
    var a = selectImage.arguments;
    el_name = $(a[0]).attr('src');
    el = a[0];
    $(a[0]).attr('src', a[1])
}


function imgRestore() {
    $(el).attr('src', el_name)
}

function check_price_item() {
    var way;
    way = "assets/templates/default/images/button_price_active.png";
    $("#price_item").find("img").attr("src", way);
}

function check_search_item() {
    var way;
    way = "assets/templates/default/images/search_button_clicked.png";
    $("#search_item").find("img").attr("src", way);
    var t = 0;
}

function M_swapClear() {
    var i, x, a = document.M_sr;
    var but_numb = M_swapClear.arguments;
    var ss = "#menu_item0" + but_numb;
    way = "assets/templates/default/images/button_menu_0" 
	+ but_numb + "_checked.png";
    $(ss).find("img").attr("src", way);
}

function tune() {
    $("#background").css("visibility", "hidden");

    body_w = $(document.body).width();
    body_h = $(document.body).height();
    var cc = $('#content').offset();
    var cc_height = $('#content').outerHeight() + 20;
    $("#background").css("visibility", "visible");

    if (body_w > 1048) {
	var holl = Math.round( (body_w - 1048) / 2 );
	var l_offset = 476 - holl;
	$("#background_left").css("background-position",  (-l_offset) + "px 0px" );
	$("#background_left").css("width", holl + "px");
	$("#background_left").css("visibility", "visible");

	if (body_w > 2000) {
	    $("#shadow").css("left", ( Math.round((body_w - 2000) / 2)) + "px");
	    $("#shadow").css("width", "2000px");
	}

	var r_offset = holl + 1048;
	r_offset = $('#background').outerWidth() + $('#background').offset().left;
	$("#background_right").css("left", r_offset + "px");
	$("#background_right").css("width", holl + "px");
	$("#background_right").css("visibility", "visible");
    }
    if (body_w < 2000) {
	$("#shadow").css("left", "0px");
	$("#shadow").css("width", "100%");
    }
	
}

function change_header() {
    t = $('#header_img' + header_index);
    t = t.attr('complete');
    if (header_time == 0) {
	  $.post('index-ajax.php', {q: 'assets/snippets/ajax/ajax.php', req: 'header_image'},
	         function (req_data) {
		       $('#header_img' + header_index).attr('src', req_data);
	         });
    } else {
	  if ($('#header_img' + header_index).attr('complete') && (header_time >= 20)){ 
	    $('#header_img' + Math.abs(header_index - 1)).css('display', 'none');
	    $('#header_img' + header_index).css('display', 'block');
	    header_index = Math.abs(header_index - 1);
	    header_time = -2;
	  }
    }    
    header_time += 2;
}


$(document).ready (
    function () {
	tune();
	$(window).resize(tune);
	var way;
	if (page_id > 20) {
		page_id -= 20;
	}
	way = "assets/templates/default/images/button_menu_0" 
	      + page_id + "_checked.png";
	elem = "menu_item0" + page_id;
 	var menu  = $("#buttons").find("div")
	            .slice(page_id - 1, page_id).find("img");
	menu.attr("src", way);
	menu.attr("onMouseOver", "");
	menu.attr("onMouseOut", "");
	window.setInterval('change_header()', 2000);
    }
		  );

