/**/

$(function(){

	$('.block-portfolio li').hover(function(){
		$(this).find('.pf-hint').show();
	},function(){
		$(this).find('.pf-hint').hide();
	});

});


var Calculator = (function() {
	var $form, $departments, $modules, $lines, $hint;

	return {
		create: function() {
			$form = $('#form_calculator');

			this.initDapartments();
			this.initModules();

			$form.pseudous();

			return this;
		},

		initDapartments: function() {
			$departments = $('.list-departments', $form);

			var self = this;

			// TODO
			$('#form_activity').val( $('label', $departments).eq(0).text() );

			$('label', $departments).bind('click', function() {
				var field = $('input#' + $(this).attr('for'), $departments);

				if( !field.is(':checked') ) {
					self.hide_all();
					self.show( field.val() );

					$('#form_activity').val( field.text() );
					//console.log( $('#form_activity') );

					//console.log( $(this), field.is(':checked'), field.val() );
				}
			});
		},

		initModules: function() {
			$modules = $('.list-modules', $form);
			$lines = $('dd', $modules);

			//$modules.get(0).ondrag = function(){ return false };
			//$modules.get(0).onselectstart = function(){ return false };

			$hint = $('.hint', $form);
			var top = $modules.position().top,
				height = $modules.height();

			$lines.bind('hover:modules', function(e){
				var line = $(this),
					id = line.find('span').attr('id');

				if( id ) {
					var index = parseInt( id.substring(1) ),
						t = line.position().top;

					$hint.html( content[index * 1] );

					var hint_top = t - top;

					if( hint_top + $hint.height() >  $modules.height() ) {
						$hint.css({
							top: 'auto',
							bottom: 0
						});
					} else {
						$hint.css({
							top: hint_top + 'px',
							bottom: 'auto'
						});
					}
				}
			});

			// FIXME double-click on label
			/*
			$('label', $lines).bind('click', function(e){
				$(e).stop();

				field_id = $(this).attr('for');

				//console.log( field_id );
				//price( field_id );
			});
			*/

			$('input', $lines).bind('click', function(e){
				field_id = $(this).attr('id');

				price( field_id );
				//console.log( 'changed' );
			});
		},

		show: function( items ) {
			//console.log( 'show' );
			items = items.toString();

			if (items.indexOf(',')){
				items_array=items.split(',');

				for(i=0; i < items_array.length; i++) {
					$lines.filter('#items_row' + items_array[i]).show();
				}
			} else {
				$lines.filter('#items_row' + items).show();
			}
		},

		hide_all: function() {
			//console.log( 'hide_all' );
			$hint.html('');

			$lines.removeClass('hover');
			$lines.removeClass('checked');

			// WTF?
			for(ii=0; ii < max_id; ii++) {
				if(document.getElementById('price' + ii).checked){
					document.getElementById('price' + ii).checked = false;
					price('price' + ii);
				}

				$lines.filter('#items_row' + ii).hide();
				//document.getElementById('items_row'+ii).style.display='none';
			}

			this.reset_price();
			$('#moduls_price').text( $('#mPrice').text() );
		},

		reset_price: function() {
			var fields = $lines.find('label');

			$('#mPrice').text('0');
			total_price_count();

			for(ii=0; ii < max_id; ii++) {
				if(check_array[ii] != ''){
					fields.filter('[for=price' + ii + ']').trigger('click');
					price('price' + ii);
				}
			}
		}

	}


})();

function reverseString(str)
{
	res = "";
	for(i=str.length; 0 <= i; i-- )
	{
		res = res + str.substr(i,1);
	}
	return res;
}

function stripNumberFormat(num_str)
{
	if( num_str.length )
	{
		while(0 < num_str.indexOf(" "))
		{
			num_str = num_str.replace( " ", "" );
		}
	}
	return num_str;
}

function doNumberFormat(num_str)
{
	tmp_str =  reverseString(num_str);
	res_str = "";

	if( tmp_str.length ) {
		for(i=0; i < tmp_str.length - 3; i+=3 )
		{
			res_str = res_str + tmp_str.substr(i,3) + " ";
		}
		res_str = res_str + tmp_str.substr(i);
	}

	return reverseString(res_str);
}

function total_price_count() {
	if (document.getElementById('total_price').innerHTML){
		document.getElementById('total_price').innerHTML = parseInt(stripNumberFormat(document.getElementById('core_price').innerHTML)) + parseInt(stripNumberFormat(document.getElementById('design_price').innerHTML));
	}

	total_initialPrice = stripNumberFormat(document.getElementById('total_price').innerHTML);
	total_mPrice = stripNumberFormat(document.getElementById('mPrice').innerHTML);
	c = parseInt(total_initialPrice) + parseInt(total_mPrice);

	document.getElementById('total_price').innerHTML = doNumberFormat(c.toString());
	document.getElementById('form_total_sum').value = doNumberFormat(c.toString());
}

function price(m) {
	current_price = document.getElementById(m).value;
	initialPrice = stripNumberFormat(document.getElementById('mPrice').innerHTML);

	if( document.getElementById(m).checked ){
		c = parseInt(initialPrice) + parseInt(current_price);
	} else {
		c = parseInt( initialPrice ) - parseInt( current_price );
	}

	//console.log( c );

	document.getElementById('mPrice').innerHTML = doNumberFormat( c.toString() );
	document.getElementById('moduls_price').innerHTML = doNumberFormat( c.toString() );

	total_price_count();
}

/*
function reset_price() {
	for(ii=0; ii < max_id; ii++) {
		if(check_array[ii] != ''){
			document.getElementById('price' + ii).checked = true;
			price('price'+ii);
		}
	}
}

// новая функция hide_all();
function hide_all() {
	document.getElementById('div1').innerHTML = '';

	for(ii=0; ii < max_id; ii++) {
		if(document.getElementById('price'+ii).checked){
			document.getElementById('price'+ii).checked = false;
			price('price'+ii);
		}
		document.getElementById('items_row'+ii).style.display='none';
	}
}
*/

/*
function show(items) {
	items = items.toString();

	if (items.indexOf(',')){
		items_array=items.split(',');
		for(i=0; i < items_array.length; i++) {
			document.getElementById('items_row'+items_array[i]).style.display = '';
		}
	}
	else {
		document.getElementById('items_row'+items).style.display = '';
	}
}
*/


$(function(){
    $("#company").validate({
    	expression: "if (VAL) return true; else return false;",
    	message: "���� ����������� ��� ����������"
    });
    $("#uname").validate({
    	expression: "if (VAL) return true; else return false;",
    	message: "���� ����������� ��� ����������"
    });
    $("#phone").validate({
    	expression: "if (VAL) return true; else return false;",
    	message: "���� ����������� ��� ����������"
    });
    $("#text").validate({
    	expression: "if (VAL) return true; else return false;",
    	message: "���� ����������� ��� ����������"
    });
    $("#email").validate({
        expression: "if (VAL.match(/^[^\\W][a-zA-Z0-9\\_\\-\\.]+([a-zA-Z0-9\\_\\-\\.]+)*\\@[a-zA-Z0-9_]+(\\.[a-zA-Z0-9_]+)*\\.[a-zA-Z]{2,4}$/)) return true; else return false;",
        message: "�������� ������ ����������� �����"
    });
    /*$("#phone").validate({
        expression: "if (VAL.match(/^[9][0-9]{9}$/)) return true; else return false;",
        message: "Should be a valid Mobile Number"
    });*/
});

