var dom = document.getElementById;
var iex = document.all;
var ns5 = ((navigator.userAgent.indexOf("Gecko")>-1) && dom) ? true: false;
var opera = ((navigator.userAgent.indexOf("Opera")>-1) && dom) ? true: false;
var mozilla = ((navigator.userAgent.indexOf("Gecko")>-1) && dom) ? true: false;
var ie5 = ((navigator.userAgent.indexOf("MSIE")>-1) && dom) ? true : false;
var ns4 = (document.layers && !dom) ? true : false;
var ie4 = (document.all && !dom) ? true : false;
var nodyn = (!ns5 && !ns4 && !ie4 && !ie5) ? true : false;
var safari = ((navigator.userAgent.indexOf("Safari")>-1) ) ? true : false;

document.isReady = false;

YearMvr = function(){
	this.mooving = false;
	
	this.totalWidth = $('#id_year_choose_string > .year').length*$('#id_year_choose_string > .year').outerWidth();
	this.visWidth = $('#id_year_choose_string_outer_container').outerWidth();
	
	this.year_move_left = function(){
		pos = $('#id_year_choose_string_container').position();
		if (pos.left > this.visWidth - this.totalWidth && !this.mooving) {
			this.mooving = true;
			$('#id_year_choose_string_container').animate({
				left: '-=' + $('.year').outerWidth()
			}, {
				complete: this.resetMooving
			})
		};
			}
	
	this.year_move_right = function(){
		pos = $('#id_year_choose_string_container').position();
		if (pos.left < 0 && !this.mooving) {
			this.mooving = true;
			$('#id_year_choose_string_container').animate({
				left: '+=' + $('.year').outerWidth()
			}, {
				complete: this.resetMooving
			});
		}
	}
	
	this.resetMooving = function(){
		YearMoover.mooving = false;
	}
}
YearMoover = null;
$(document).ready(function(){
	YearMoover = new YearMvr();
});

function formOneSubmitStep(step){
	$('#id_form1 > input[name=step]').val(step);
	$('#id_form1').submit();
}

function removeParentNodeByClass(ob, className){
	if(!ob)return false;
	while(jQuery(ob).attr('class')!=className){
		ob = ob.parentNode;
		if(!ob)return false;
	}
	jQuery(ob).remove();
	return false;
}

function getElement ( name )
	{
	var el = dom ? document.getElementById(name) : iex ? document.all[name] : ns4 ? eval ( 'document.' + name ) : false;
	return el;
	}
	
function C_goToPage( href ) {
	if( href ) {
		document.location.href = href;
		}
	}
	
function KC_openKlarnetWindow( ) {
	//var width = parseInt( screen.availWidth * 0.8 );
	var width = 20;
	var height = 20;
	//alert( width );
	var tmp_window = window.open( "/klarnetCMS/" , "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();
		}
	}
$(function() {

if ($.browser.opera && $(".menuTop .lev_2")) {
	$(".menuTop .lev_2").children('.point').toggleClass('displayForOpera', true);
}

// brands rollover
if ($(".brand a")) {
	
	$(".brand a").mousemove(function(e){
		var pageCoords = "( " + e.pageX + ", " + e.pageY + " )";
		var clientCoords = "( " + e.clientX + ", " + e.clientY + " )";
		$("#footnote_" + this.id).css("top", e.pageY - 59);
		mappos = $(".box").position();
		$("#footnote_" + this.id).css("left", e.pageX - mappos["left"] + 5);
		var mappos = {};
	});
	
	$(".brand a").mouseover(function(e){
		$("#footnote_" + this.id).css("display", "");
		
	});
	
	$(".brand a").mouseout(function(e){
		$("#footnote_" + this.id).css("display", "none");
		
	});
}

if ($('.popuplink')) {
	$('.popuplink').click(function(e){
		$("#popup").show();
		return false;
	});
}

if ($('.popuplink')) {
	$('.popuplink').ajaxContent({
		event: 'click',
		target: '#popup .popupBox .popupBoxLoader'
	});
}
	
if ($('#popup .close a')) {
	$('#popup .close a').click(function(e){
		$("#popup").hide();
		return false;
	});
}



});


function getIframeLayer(elementId) {
	iframeId = elementId+"IframeLayer";
	if ($('#'+iframeId).attr('id')+'' == 'undefined') {
		var iframeLayer = document.createElement("iframe");
		iframeLayer.setAttribute("id", iframeId);
		iframeLayer.setAttribute("scrolling", "no");
		iframeLayer.setAttribute("frameborder", "0");
		iframeLayer.setAttribute("style", "z-index:0; position:absolute; border:0; top:0; left:0; width:0; height:0; ");
		iframeLayer.setAttribute("src", "javascript:'<html></html>';");
//		iframeLayer.setAttribute("src", "http://www.ya.ru");
		first = document.body.firstChild,
		document.body.insertBefore(iframeLayer, first);
		
	} else {
		var iframeLayer = $(iframeId);
		return iframeLayer;
	}
	return $(iframeLayer);
}

function relocateIframeLayer(elementId) {
	iframeId = '#'+elementId+"IframeLayer";
	if ($(iframeId).attr('id')+'' == 'undefined') {
		var iframeLayer = getIframeLayer(elementId);
	} else {
		var iframeLayer = $(iframeId);
	}
	$('#'+elementId).css('display', 'block');
	pos = $('#'+elementId).offset();
	width = $('#'+elementId).outerWidth();
	height = $('#'+elementId).outerHeight();
	iframeLayer.css('position', 'absolute');
	iframeLayer.css('top', pos.top);
	iframeLayer.css('left', pos.left);
	iframeLayer.css('width', width);
	iframeLayer.css('height', height);
	iframeLayer.css('z-index', 1);
}

function hideIframeLayer(elementId) {
	iframeId = '#'+elementId+"IframeLayer";
	if ($(iframeId).attr('id')+'' != 'undefined') {
		var iframeLayer = $(iframeId);
		iframeLayer.css('width', 0);
		iframeLayer.css('height', 0);
		iframeLayer.css('z-index', 0);
		document.getElementById(elementId+"IframeLayer").style.zIndex = 0;
	}
}

function mainMenuLvlShowById(elementId, state) {
	if(state+'' == 'undefined' || state == null || state+'' == 'null') state = true;
	if ($('#'+elementId).attr('id')+'' != 'undefined') {
		if(state){
			$('#'+elementId).css('display', 'block');
			pos = $('#'+elementId).offset();
			relocateIframeLayer(elementId);
		} else {
			$('#'+elementId).css('display', 'none');
			hideIframeLayer(elementId);
		}
	}
	return false;
}

$(document).ready(function(){
	document.isReady = true;
});

function loadContactForm(e_id) {
	if(!document.isReady) return;
	$('#popup .popupBox .popupBoxLoader').load('/popupContactFormGet', {'email_id' : e_id}, function(){
		$('#popup').show();
			
		$('#contPopFrm').bind('submit', function() {
			fields = $(this).find(':input, :text');
			var allFilled = true;
			for(i = 0; i < fields.size(); i++){
				ob = fields.eq(i);
				if (ob.val().length <= 0 && ob.attr('id') != 'id') {
					alert('Пожалуйста, заполните все поля');
					return false;
				}
			}
			$(this).ajaxSubmit({
				dataType: 'json',
				url: '/popupContactFormSubmit',
				success: function(res){
					if (res.success) {
						$('#contPopFrm').slideUp(400);
						$('.messageRpl').hide().toggleClass('messageRplError', false).html(res.message).slideDown(500);
					} else {
						$('#contPopFrm').find('img.captcha').attr('src', "/captcha?r=" + Math.random() + "&reload=1");
						$('.messageRpl').hide().toggleClass('messageRplError', true).html(res.message).slideDown(500);
					}
				}
			});
			return false;
		});
	});
};

integratePopupForm = function(data, status, form_id) {
	$('#popup .popupBox .popupBoxLoader').html(data);
	$('#popup').show();
	
	$('#popup').find('form').bind('submit', function(){
		fields = $(this).find(':input, :text');
		var allFilled = true;
		for (i = 0; i < fields.size(); i++) {
			ob = fields.eq(i);
			if (ob.val().length <= 0 && ob.attr('id') != 'id') {
				alert('Пожалуйста, заполните все поля');
				return false;
			}
		}
		$(this).ajaxSubmit({
			data: { form_id: form_id },
			dataType: 'html',
			type: 'post',
			url: '/popupFormProxy',
			success: function(data, status){
				integratePopupForm(data, status, form_id);
			}
		});
		return false;
	});
};

function loadPopupForm(form_id) {	
	if(!document.isReady) return;
	$.ajax({
		url: '/popupFormProxy',
		dataType: 'html',
		type: 'post',
		data: {
			'form_id': form_id
		},
		success: function(data, status){
			integratePopupForm(data, status, form_id);
		}
	});
};

