var image_preloads = 'r_right_min.png,r_right.png,blank.gif,m_l_t.png,m_r_t.png,m_r_b.png,m_l_b.png'.split(',')
var image_preloads_object = []
for (var i = 0; i < image_preloads.length; i++) {
    image_preloads_object[i] = new Image()
    image_preloads_object[i].src = '/resources/images/' + image_preloads[i]
}


var body_click = 0;

$(document).ready(function() {

	$("#div_enter").click(function() {
		body_click = 1;
	});
	$("#div_search").click(function() {
		body_click = 1;
	});
	$("#body").click(function() {
		if (body_click == 1) {
			body_click = 2;
			return;
		} 			
		if (body_click == 2) {
			$('#div_search').hide();
			$('#div_enter').hide();
		}
	});
	//$(".left_menu").mouseover(function() {return false;});
	$(".div_menu_one").mouseover(function() {
		$("#"+this.id).attr("class", "div_menu_one_activ");
		$("#submenu_"+this.id).show();
	});
	
	$(".div_menu_one").mouseout(function() {
		$("#"+this.id).attr("class", "div_menu_one");
		$("#submenu_"+this.id).hide();
	});
	
	$(".submenu").mouseover(function() {
		$("#"+this.id).show();
		eee = this.id.split("_");
		$("#"+eee[1]).attr("class", "div_menu_one_activ");
	});
	
	$(".submenu").mouseout(function() {
		$("#"+this.id).hide();
		eee = this.id.split("_");
		$("#"+eee[1]).attr("class", "div_menu_one");
	});
	
	$("#tbl_part IMG").mouseover(function() {
		$("#a"+$("#"+this.id).attr("id")).css({color:"f26522"});
	});
	
	$("#tbl_part IMG").mouseout(function() {
		$("#a"+$("#"+this.id).attr("id")).css({color:"3678c8"});
	});
	
	$("#ph_drag_3 DIV").mouseover(function() {
		$("#"+this.id+" IMG").css("margin-top", "-65px");
	});
	
	$("#ph_drag_3 DIV").mouseout(function() {
		$("#"+this.id+" IMG").css("margin-top", "0px");
	});

});

function change_text(id, text) {
	if ($("#"+id).val() == text) {
		$("#"+id).attr("value", "");
	} else if ($("#"+id).val() == ''){
		$("#"+id).attr("value", text);
	}
}

var c_pos=0;

function c_prev(id, id2) {
	if (c_pos==0) {
		c_set(get_total_photo(id), 1000, id, id2);
	} else {
		c_set(--c_pos,1000,id,id2);
	}
}

function c_next(id, id2) { 
	if (c_pos==get_total_photo(id)) {
		c_pos = 0;
		c_set(0, 1000, id, id2);
	} else {
		c_set(++c_pos, 1000, id, id2);
	}
}

function c_set(pos, delay, id, id2) {
	spans = $("IMG", $("#promo_navig")).length;
	for (j=0;j<spans;j++) {
		$("#po_c_"+j+" IMG").attr("src", "/resources/images/pr_c.png");
	}
	$("#po_c_"+pos+" IMG").attr("src", "/resources/images/pr_c_activ.png");
	c_pos = pos;
	$('#'+id).animate({marginLeft:-pos*parseInt($("#"+id2).css("width"))}, delay );
	//$('#'+id).css("margin-left", "-"+pos*parseInt($("#"+id2).css("width")));
}  

function get_total_photo(id) {
	return $(".promo", $("#"+id)).length-1;
}


var c_part=0;

function c_prev_part(id, id2) {
	if (c_part==0) {
		c_set_part(get_total_photo_part(id), 1000, id, id2,-2);
	} else {
		c_set_part(--c_part,1000,id,id2,-1);
	}
}

function c_next_part(id, id2) { 
	if (c_part==get_total_photo_part(id)) {
		c_part = 0;
		c_set_part(0, 1000, id, id2,2);
	} else {
		c_set_part(++c_part, 1000, id, id2,1);
	}
}

function c_set_part(pos, delay, id, id2, action) {
	/*divs = $("DIV", $("#"+id)).length;
	for (k=1;k<(divs+1);k++) {
		if (k == (win_par+1)) {
			width = parseInt($("#div_pr"+k+" IMG").css("width"))+20
			win_par++;
			//alert("smith");
		}
	}
	alert(width+"----"+win_par);*/
	
	//$('#'+id).animate({marginLeft:'+=160px'}, delay );
	
	$('#'+id).animate({marginLeft:'+=' + (-pos*160)}, delay );
	
	//$('#'+id).animate({marginLeft:-pos*160}, delay );
	/*
	if (action == 1)
		$('#'+id).animate({marginLeft:-160}, delay );
	else if (action == -1)
		$('#'+id).animate({marginLeft:parseInt($('#'+id).css('marginLeft'))+160}, delay );
	*/
		
	/*
	else if (action == 2)
		$('#'+id).animate({marginLeft:0}, delay );
	else if (action == -2)
		$('#'+id).animate({marginLeft:-get_total_photo_part(id)*160}, delay);
	*/
}  

var win_par = 0;
function get_total_photo_part(id) {
	all = count = 0;
	width_data = parseInt($("#carousel2").css("width"));
	divs = $("DIV", $("#"+id)).length;
	for (k=0;k<divs;k++) {
		/*if (all > width_data) {
			count++;
		} else {
			win_par++;
		}*/
		all += parseInt($("#div_pr"+(k+1)+" IMG").css("width"))+30;
	}
	//return count;

	return Math.ceil((all-1100)/160);
}

function display_vac(id) {
	// Спрячем окошко с ответом на вакансию.
	$(".r-border-shape").hide();
	$("#vacform_"+id+" #vacid").attr("value", "");

	if ($("#"+id+" SPAN").attr("class") == "activ") {
		$("#"+id+" SPAN").attr("class", "");
		$("#"+id+" DIV").animate({"height": "toggle", "opacity": "toggle"}, "slow");
		$("#"+id+" P").hide();
	} else {	
		// Спрячем другие вакансии.
		$(".vacancy SPAN").attr("class", "");
		$(".vacancy DIV").animate({"height": "hide", "opacity": "hide"}, "slow");
		$(".vacancy P").hide();
		// Покажем нашу.
		$("#"+id+" SPAN").attr("class", "activ");
		$("#"+id+" DIV").animate({"height": "toggle", "opacity": "toggle"}, "slow");
		$("#"+id+" P").show();
	}
}

function display_slide(id) {
	// Спрячем окошко с ответом на вакансию.
	$(".r-border-shape").hide();
	$("#vacform_"+id+" #vacid").attr("value", "");

	if ($("#"+id+" SPAN").attr("class") == "activ") {
		$("#"+id+" SPAN").attr("class", "");
		$("#"+id+" DIV").animate({"height": "toggle", "opacity": "toggle"}, "slow");
		$("#"+id+" P").hide();
	} else {	
		// Спрячем другие вакансии.
		$(".vacancy SPAN").attr("class", "");
		$(".vacancy DIV").animate({"height": "hide", "opacity": "hide"}, "slow");
		$(".vacancy P").hide();
		// Покажем нашу.
		$("#"+id+" SPAN").attr("class", "activ");
		$("#"+id+" DIV").animate({"height": "toggle", "opacity": "toggle"}, "slow");
		$("#"+id+" P").show();
	}
}

function display_form_vac(id, realid) {
	//alert('ok');
	//$(".r-border-shape").hide();
	//$("#vacform_"+id).html($("#vacform").html());
	$("#vacform_"+id+" #vacid").val('');
	$("#vacform_"+id+" #vacid").val(realid);	
	$("#vacform_"+id+" .r-border-shape").show();
	
	$("#vacform_"+id+" form").val(realid);
	$("#vacform_"+id+" form").attr('id', 'send_vacancy_form_' + realid);
	$("#vacform_"+id+" form").attr('class', 'send_vacancy_form_' + realid);

	// При клике вне области -- скрываем.
	setTimeout(function() {
		$('html').click(function() {
			$('.r-border-shape').hide();
			$('html').unbind('click');
		});
		$('.r-border-shape').click(function(e) {
			e.stopPropagation();
		});
	}, 200);
}

var image_current = 0;
function image_count() {
	return $('#ph_drag_3 div').size();
}
function image_prev() {
	image_current--;
	if (image_current == -1) {
		$('#ph_drag_3').animate({marginLeft: -(image_count() - 5) * 160});
		image_current = image_count() - 5;
	}
	else {
		$('#ph_drag_3').animate({marginLeft: '+160'});
	}
}
function image_next() {
	image_current++;
	if (image_current == image_count() - 5 + 1) {
		$('#ph_drag_3').animate({marginLeft: '0'});
		image_current = 0;
	}
	else {
		$('#ph_drag_3').animate({marginLeft: '-160'});
	}
}

var promo_image_current = 0;
function promo_image_count() {
	return $('#promo_navig span').size();
}
function promo_image_prev() {
	promo_image_current--;
	if (promo_image_current == -1) {
		$('#ph_drag_1').animate({marginLeft: -(promo_image_count() - 1) * 950}, 'fast');
		promo_image_current = promo_image_count() - 1;
	}
	else {
		$('#ph_drag_1').animate({marginLeft: '+950'});
	}
	$('#promo_navig span img').attr('src', '/resources/images/pr_c.png');
	$('#po_c_' + promo_image_current + ' img').attr('src', '/resources/images/pr_c_activ.png');
}
function promo_image_next() {
	promo_image_current++;
	if (promo_image_current == promo_image_count()) {
		$('#ph_drag_1').animate({marginLeft: '0'}, 'fast');
		promo_image_current = 0;
	}
	else {
		$('#ph_drag_1').animate({marginLeft: '-950'});
	}
	$('#promo_navig span img').attr('src', '/resources/images/pr_c.png');
	$('#po_c_' + promo_image_current + ' img').attr('src', 'resources/images/pr_c_activ.png');
}
function promo_image_goto(num) {
	var old = promo_image_current;
	promo_image_current = num;
	
	var delta = promo_image_current - old;
	
	if (delta < 0) {
		$('#ph_drag_1').animate({marginLeft: '+' + (-950 * delta) + 'px'});
	}
	else {
		$('#ph_drag_1').animate({marginLeft: (-950 * delta) + 'px'});
	}
	
	$('#promo_navig span img').attr('src', '/resources/images/pr_c.png');
	$('#po_c_' + promo_image_current + ' img').attr('src', '/resources/images/pr_c_activ.png');
}

function dynamicToggle(id){
	$("#"+id).parent().find('div').slideToggle();
}
