var period;
var minfirst;
var mincredit;
var persent;

formMod = function() {
	$('select').each( function(i){
		var _id = 'select' + i;
		$(this).wrap('<div class="select-case" id="case' + _id + '"></div>')
		$('#case' + _id).append(
			"<div id='" + _id + "' class='select'></div>"
		);
		$('#' + _id).append('<dl></dl>');
		$($('#' + _id + ' dl')[0]).append('<dt><span>' + $(this).find('option:selected').text() + '</span><span class="formitem-decor"></span></dt>');
		$("#staticcase").append('<ul class="select-list" id="list' + _id + '"></ul>');
		var parentSelect = $(this);
		$(this).find('option').each( function(j) {
			$('#list' + _id).append('<li>' + $(parentSelect.find('option')[j]).text() + '</li>')
		});
		$('#list' + _id).addClass('active-formitem');
		$($('#' + _id + ' dl dt')[0]).css('width', parseInt($('#list' + _id).width()) + 10 + 'px');
		$('#list' + _id).removeClass('active-formitem');
		$($('#' + _id + ' dl dt')[0]).click( function() {
			if( $('#list' + _id).hasClass('active-formitem') ) {
				$('#list' + _id).removeClass('active-formitem');
			}
			else {
				$('ul.active-formitem').removeClass('active-formitem');
				$('#list' + _id).addClass('active-formitem');
				$('#list' + _id).css(
					'left',
					$('#case' + _id).offset().left -  $("#staticcase").offset().left + 0
				)
				$('#list' + _id).css(
					'top',
					$('#case' + _id).offset().top - $("#staticcase").offset().top + 22
				)
			}
		});
		$(this).css('position','absolute');
		$(this).css('left','-11111px');
		$('#list' + _id + ' li').click( function() {
			$('#' + _id + ' dt span:first').html($(this).html());
			$('ul.active-formitem').removeClass('active-formitem');
		})
	});
}

programmslist = function() {
	$('#programmslist option').remove();
	for (var i in creditPrograms) {
		$('#programmslist').append('<option>' + creditPrograms[i]["name"] + '</option>');
	}
}
creditperiod = function() {
	$('#creditPeriod option').remove();
	for (var i in creditPrograms[$("#programmslist option").index($("#programmslist option:selected"))]["month"]) {
		$('#creditPeriod').append('<option>' + creditPrograms[$("#programmslist option").index($("#programmslist option:selected"))]["month"][i][0] + '</option>');
	}
}
calccredit = function(mode) {
	if (typeof mode == "undefined") {
		mode = false;
	}
	if (parseInt($('#cost').html())>0) {
		if ( ($('#firstper').val() == '' && $('#firstrub').val() == '') || !mode) {
			$('#firstrub').css('backgroundColor', '');
			period = creditPrograms[$("#programmslist option").index($("#programmslist option:selected"))]["month"][$("#creditPeriod option").index($("#creditPeriod option:selected"))][0];
			minfirst = creditPrograms[$("#programmslist option").index($("#programmslist option:selected"))]["month"][$("#creditPeriod option").index($("#creditPeriod option:selected"))][1];
			mincredit = creditPrograms[$("#programmslist option").index($("#programmslist option:selected"))]["month"][$("#creditPeriod option").index($("#creditPeriod option:selected"))][2];
			persent = creditPrograms[$("#programmslist option").index($("#programmslist option:selected"))]["month"][$("#creditPeriod option").index($("#creditPeriod option:selected"))][3];
			if ( minfirst < 0 ) {
				minfirst = parseInt(-minfirst*$('#cost').html()/100);
				/*$('#firstper').val(-minfirst);
				$('#firstrub').val(parseInt(-minfirst*$('#cost').html()/100));*/
			}
			$('#firstrub').val(minfirst);
			$('#firstper').val(parseInt(minfirst*100/$('#cost').html()));
		}
		$('#persent').html(persent);
		$('#permonth').html(
			parseInt (
				($('#cost').html()-$('#firstrub').val())*persent/100/12/((1-1/Math.pow((1+persent/100/12),(period-1))))
			)
		);
	}
}

$(document).ready(function () {

	/*if ($('#gallery').length == 1) {
		$('#gallery a').fancyzoom();
	}*/

	/*if ($('.photo-section').length > 0) {
		$('.photo-section a').fancyzoom();
	}*/

	//Cufon
	Cufon.replace('h1');
	Cufon.replace('ul.menu li');
	Cufon.replace('.head .address');
	Cufon.replace('.head .phone');
	Cufon.replace('.models-menu a');
	Cufon.replace('.sort a');
	Cufon.replace('.sort span');
	Cufon.replace('.models th');
	Cufon.replace('.models td p');
	Cufon.replace('.cost-name');
	Cufon.replace('h4');
	Cufon.replace('.contacts .phone p');
	/*Cufon.replace('.cost-new', { fontFamily: 'NeoSansProLight' });*/
	Cufon.replace('.cost-new');
	Cufon.replace('.cost-text');
	Cufon.replace('.cost-old', { fontFamily: 'NeoSansProLight' });
	Cufon.replace('.credits .item div', { fontFamily: 'NeoSansProLight' });
	//Cufon


	//Actions

	$('.color a').each( function(i) {
		$(this).click( function(event) {
			$('.color').removeClass('color-active');
			$($('.color')[i]).addClass('color-active');
			$('.podium img').attr('src', $(this).attr('href'));
			event.preventDefault();
		})
	});

	//calc
	if ( $('#opencalc').length == 1 ) {
		$('#opencalc').click( function(event) {
			$('#activecalc').css('left','0');
			$('#activecalc').css('top','-85px');
			$('.programms .item').css('display', 'none');
			event.preventDefault();
		});
		$('#closecalc').click( function(event) {
			$('#activecalc').css('left','-11111px');
			$('#activecalc').css('top','-11111px');
			event.preventDefault();
		});
		$('#calcmodel').change( function() {
			var index = $("#calcmodel option").index($("#calcmodel option:selected"));
			if ( index > 0 ) {
				$("#calccode option").remove();
				for ( var item =0; item < calcdata[$("#calcmodel option:selected").val()].length; item+=2 ) {
					$("#calccode").append("<option value='" + calcdata[$("#calcmodel option:selected").val()][item+1] + "'>" + calcdata[$("#calcmodel option:selected").val()][item] + "</option>");
				}
				$('#cost').html($('#calccode').val());
				calccredit();
			}
			else {
				$("#calccode option").remove();
				$('#cost').html('');
			}
		});
		$('#calccode').change( function() {
			$('#cost').html($(this).val());
			calccredit();
		});
		programmslist();
		creditperiod();
		$('#programmslist').change( function() {
			creditperiod();
			calccredit();
		});
		$('#creditPeriod').change( function() {
			calccredit();
		});
		$('#firstrub').keyup( function() {
			if (parseInt($('#firstrub').val())>0 || parseInt($('#firstper').val())>0) {
				if ( ( $('#firstrub').val() < minfirst ) || ( ( $('#cost').html() - $('#firstrub').val() ) < minfirst ) ) {
					$('#firstrub').css('backgroundColor', '#fcc');
				}
				else {
					$('#firstrub').css('backgroundColor', '');
				}
				$('#firstper').val( parseInt($('#firstrub').val()*100/$('#cost').html()) );
				calccredit(true);
			}
		});
		$('#firstrub').blur( function() {
			if (parseInt($('#firstrub').val())>0 || parseInt($('#firstper').val())>0) {
				if ( ( $('#firstrub').val() < minfirst ) || ( ( $('#cost').html() - $('#firstrub').val() ) < minfirst ) ) {
					$('#firstrub').css('backgroundColor', '#fcc');
				}
				else {
					$('#firstrub').css('backgroundColor', '');
				}
				$('#firstper').val( parseInt($('#firstrub').val()*100/$('#cost').html()) );
				calccredit(true);
			}
		});
		$('#firstper').keyup( function() {
			if (parseInt($('#firstrub').val())>0 || parseInt($('#firstper').val())>0) {
				$('#firstrub').val( parseInt($('#firstper').val()*$('#cost').html()/100) );
				if ( ( $('#firstrub').val() < minfirst ) || ( ( $('#cost').html() - $('#firstrub').val() ) < minfirst ) ) {
					$('#firstrub').css('backgroundColor', '#fcc');
				}
				else {
					$('#firstrub').css('backgroundColor', '');
				}
				calccredit(true);
			}
		});
		$('#firstper').blur( function() {
			if (parseInt($('#firstrub').val())>0 || parseInt($('#firstper').val())>0) {
				$('#firstrub').val( parseInt($('#firstper').val()*$('#cost').html()/100) );
				if ( ( $('#firstrub').val() < minfirst ) || ( ( $('#cost').html() - $('#firstrub').val() ) < minfirst ) ) {
					$('#firstrub').css('backgroundColor', '#fcc');
				}
				else {
					$('#firstrub').css('backgroundColor', '');
				}
				calccredit(true);
			}
		});
	}
	//calc

	if ( $('.sub-menu-decor').length>0 ) {
		if ( $('.sub-menu-decor ul li').length == 0 ) {
			$('.sub-menu').css('display', 'none');
		}
	}
	$('.credits a').each( function(i) {
		$(this).click( function(event) {
			$('#activecalc').css('left','-11111px');
			$('#activecalc').css('top','-11111px');
			$('.programms .item').css('display', 'none');
			$($('.programms .item')[i]).css('display', 'block');
			event.preventDefault();
		});
	});
});

$(function() {

//Set the default directory to find the images needed
//by the plugin (closebtn.png, blank.gif, loading images ....)
$.fn.fancyzoom.defaultsOptions.imgDir='/imgs/fancy/';//very important must finish with a /

// Select all links in object with gallery ID using the defaults options
//$('#gallery a').fancyzoom();

// Select all links with tozoom class, set the open animation time to 1000
//$('a.tozoom').fancyzoom({Speed:1000});

// Select all links set the overlay opacity to 80%
//$('a').fancyzoom({overlay:0.8});

//New, you can now apply the fancy zoom effect on an image
//apply the fancyzoom effect on all images that have the fancyzoom class
$('.photo-section a').fancyzoom();

});

