jQuery(document).ready(function(){
	
	var slider = $('#slider')
	var slides = $('#slide1, #slide2, #slide3, #slide4')
	
	// Slider
	slider.slider({
		value: 1,
		min: 1,
		max: 4,
		step: 1,
		animate: true,
		slide: function(event, ui) {
			change_slide(ui.value)
		},
		change: function(event, ui) {
			change_slide(ui.value)
		}
	});
	
	/**
		if (Current != Next) hide Current & show Next
	**/
	function change_slide(val) {
		current    = $('.slides:visible')	// current
		current_id = current.attr('id')		// current.id

		next_id = 'slide' + val				// next.id
		next    = $('#'+next_id)			// next

		if (current_id != next_id) {
			current.hide()					// hide current
			next.fadeIn()					// show next
		}
	}
	
	$('.button_slides').hover(function(){
		val = $(this).attr('rel')
		slider.slider( "value", val)
	})
	
	$('#slide_left').click(function(){
		val = slider.slider( "value" )
		slider.slider( "value", val*1-1 )
	})
	$('#slide_right').click(function(){
		val = slider.slider( "value" )
		slider.slider( "value", val*1+1 )
	})
	
    $('#container-1').tabs();

	var subsubitems = $('.subsubitems .items li a');
	var faces = $('div.face1, div.face2')
	if (subsubitems) {
		subsubitems.click(function(){
			faces.hide();
			face = $(this).attr('rel');
			$('div.'+face).show();
		})
	}
	
	var items = $('#mainmenu .items li');
	var subsubitems = $('.subsubitems li a');

	items.hover(function(){
		if (!$(this).hasClass('sel')) $(this).addClass('active');
	}, function(){
		if (!$(this).hasClass('sel')) $(this).removeClass('active');
	})
	
/*	items.click(function(){
		var val1 = $(this).css('background-position')
		var val2 = val1.substr(0,6)
		str = parseInt( val2 )
		$(this).css('background-position', str+'px bottom');
	})
	
	subsubitems.click(function(){
		subsubitems.removeClass('sel');
		$(this).addClass('sel');
	}) */
	
	$('.button_slides').hover(function(){
		$(this).css('background-position', '0 -34px')
	}, function(){
		$(this).css('background-position', '0 0')
	})
	
	// Bonus Malus by RNN
	if ($('#bm1').length != 0) {

		function toggleEva(status, button, el) {
			if (status == 'hidden') {
				button.css('display', 'none');
				el.css('display', 'block')
		
			} else {
				button.css('display', 'block');
				el.css('display', 'none');
			}
		}
		
		var toggleEvaluates = $('.toggleEvaluate');
		toggleEvaluates.click(function() {
			var evaluate = $(this).nextAll('div');
			toggleEva('hidden', $(this), evaluate);
		})
	}
	
	// OGPO
	if ($('#getbm1').length != 0) {
	
		function getAJAXbm ( ele, TRN ){
			
			$('.bmError').remove();			// del previous msgss
			var bmError = $(document.createElement("div")).attr("class", "bmError")
			bmError.css({
				'padding': 10,
				'color': '#990000',
				'display': 'inline',
				'background-image': 'url(/img/loading.gif)',
				'background-repeat': 'no-repeat',
				'background-position': '3px 10px'
			})
			bmError.insertAfter(ele)

			$.get('/calcs/getbm/' + TRN, function(responseHTML) {
				bmError.css('background-image', 'none')		// stop loading.gif
				if (responseHTML.length <= 4) {
					ele.val(responseHTML)
				} else {
					bmError.html(responseHTML)
				}
			});
		}

		var getbms = $('#getbm1').add($('#getbm2')).add($('#getbm3'));	// all buttons
		
		getbms.click(function(){
			var tmp = $(this).parent();							// one level upper

			var ele = tmp.prevAll('select')						// BM selecbox
			var TRN = tmp.children('input[name^=TRN]').val()	// TRN

			getAJAXbm( ele, TRN )
		})
	}
	
	// FOR BLANKS: CONSULT
	if ($('.consult').length != 0) {
		// options
		var f_face = $('.f_face')
		var f_goal = $('.f_goal')
		// inputs
		var in_face = $('#ContactUsertype')
		var in_goal = $('#ContactGoal')
		var in_prog = $('#ContactProgram')
		// accordions
		var l_prog  = $('#choose_program')
		var acc_all = $('#accordion_yur, #accordion_fiz')
		var acc_fiz = $('#accordion_fiz')
		var acc_yur = $('#accordion_yur')
		// question
		var f_question  = $('.question')
		// for comments
		var for_consult = $('.for_consult')
		var for_order   = $('.for_order')
		// texts
		var text_order   = $('.text_order')
		var text_consult = $('.text_consult')
		var text_all     = $('.text_order, .text_consult')

		// events
		f_face.click(function(){
			var v_goal = f_goal.filter('.sel').attr('rel')	// read value
			var v_face = $(this).attr('rel')				// new value
			show_acc(v_goal, v_face)
			select_item(f_face, $(this))
		})
		f_goal.click(function(){ 
			var v_goal = $(this).attr('rel')				// new value
			var v_face = f_face.filter('.sel').attr('rel')	// read value
			show_acc(v_goal, v_face)
			select_item(f_goal, $(this))
			show_text(v_goal)
		})
		// Show Text after Form
		function show_text( v_goal ){
			text_all.hide()
			if (v_goal == 'order') text_order.show()
			else text_consult.show()
		}
		// Show Accordion
		function show_acc(v_goal, v_face) {
			acc_all.hide()
			l_prog.hide()
			f_question.hide()
			// console.log(v_goal)
			if (v_goal == 'order') {
				switch (v_face) {
					case 'fiz': acc_fiz.show(); break;
					case 'yur': acc_yur.show(); break;
				}
				l_prog.show()
				for_consult.hide()
				for_order.show()
				// in_prog.attr('disabled', false)
			} else {
				// in_prog.attr('disabled', 'disabled')
				f_question.show()
				for_consult.show()
				for_order.hide()
			}
			

		}
		// SELECT ITEM
		function select_item(opts, sel) {
			if (sel.hasClass('sel')) {
				// do nothing
			} else {
				var rel   = sel.attr('rel')
				var field = sel.attr('class')
				// inputs
				if (field == 'f_face') in_face.val(rel)
				else in_goal.val(rel)							
				// visual
				opts.removeClass('sel')
				sel.addClass('sel')
				
				// courners for IE
				if ($.browser.msie) {
					opts.uncorner()
					sel.corner("10px");
					// accordion corner
					$('.ui-state-active a').corner("10px");
				}
				
			}
		}
	}
	// choose from Spravo4nik
	var pop_city  = $('#pop_city')
	var acts_vals = $('#pop_city a')
	var pop_link  = $('#pop_link')
	var f_city    = $('#ContactCity')

	acts_vals.click(function(){
		var val = $(this).attr('rel')
		var tit = $(this).text()

		$('#f_ :selected').removeAttr('selected')
		$("#activity option[value='"+val+"']").attr('selected', 'selected')

		pop_link.text(tit)
		f_city.val(val)
		pop_city.hide()

		return false
	})
	pop_link.click(function(){

		pop_city.css("top", ( $(window).height() - pop_city.height() ) / 2+$(window).scrollTop() + "px");
		pop_city.show()

		return false
	})
	
	// Round corners NOT IE
	if (!$.browser.msie) {
		$('#cities_map div').corner("6px tr bl br");
		$('.cities_address').corner("12px");
	} else {
		// IE corner()
		$('div.ul_like span.sel').corner("10px");
		
		$('.ui-accordion .ui-accordion-header a').live('click', function(){
			$('.ui-accordion .ui-accordion-header a').uncorner()
			$(this).corner("10px");
		})
	}
	
	
	/** Search **/
	if ($('#sections')) {
		
		function toggleSearch() {
			if($('#sections').is(':checked')){
				$('#sections-list input').attr('disabled', '');
				$('#sections-list label').attr('style', 'color: #000000');
			}else{
				$('#sections-list input').attr('disabled', 'disabled');
				$('#sections-list label').attr('style', 'color: #909090');
			}
		}
		toggleSearch();
		$('#sections, #all-site').click(function() {
			toggleSearch();
		});
	}
	
})

$(window).load(function(){ // after all the images are loaded
    if ($.browser.msie && parseInt($.browser.version.substr(0, 1)) < 7) { // ie6 or 5
        $("a.ui-slider-handle").each(function(){
            var bg = $(this).css("backgroundImage");
            bg.match(/^url[("']+(.*\.png)[)"']+$/i);
            bg = RegExp.$1;
            $(this).css("filter", "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + bg + "', sizingMethod='crop')").css("backgroundImage", "none");
        });
    }

	// Rounded corners for IE
	if ($.browser.msie) {
		$('.ui-state-active a').corner("10px");
	}
	
});



