jQuery = jQuery.noConflict();



// vars

var constructorWidth = 580;

var pageWidth = 580;

var pageMarginRight = 50;



var p_old;

var is_visible = new Array();

var pageX = new Array();

var currPage = 0;



//selectable days

var selectebleDays = new Array();

selectebleDays['EN'] = 2;

selectebleDays['FR'] = 1;



var currency = 'EUR';

var guestId = '2373';



// steps description to load

var pref = '';

if(lang == 'FR'){

	pref = '/fr';

}

var page = new Array();

page[0] = pref + '/constructor/intro';

page[1] = pref + '/constructor/city';

page[2] = pref + '/constructor/date';

page[3] = pref + '/constructor/length';

page[4] = pref + '/constructor/convenience';

page[5] = pref + '/constructor/hours';

page[6] = pref + '/constructor/accommodations';

page[7] = pref + '/constructor/meals';

page[8] = pref + '/constructor/services';

page[9] = pref + '/constructor/confirmation';



jQuery(document).ready(function(){

	

	// set constructor styles

	jQuery('#constructor').css('width', constructorWidth+'px');

	jQuery('#p_container, #p_container div.page').css('width', pageWidth+'px');

	jQuery('#p_container div.page').css('margin-right', pageMarginRight+'px');

	

	// translate

	translate();

	// load content & tips

	loadPages();



	// pathway setup

	is_visible[5] = jQuery('#page_5:visible').length;

	if(!is_visible[5]){

		jQuery('#pathway li:eq(4)').hide();

	}

	is_visible[7] = jQuery('#page_7:visible').length;

	if(!is_visible[7]){

		jQuery('#pathway li:eq(6)').hide();

	}

	jQuery('#pathway li:not(:last)').append('&nbsp;&gt;&nbsp;');

	

	

	// page coords

	f = 0;

	jQuery('#i_container div.page').each(function(){

		key = jQuery(this).attr('id');

		pageX[f] = (pageWidth + pageMarginRight) * f;

		f++;

	});

									

	// nav btns

	jQuery('.btn_next').click(function(){

		getPage(currPage+1);

	});

	

	jQuery('.btn_prev').click(function(){

		getPage(currPage-1);

	});

	

	// выбор формы занятия (и обработка страницы hours)

	jQuery('#constructor input[name*="convenience"]').change(function(){

		conv = jQuery(this).val();

		if(conv == 1){

			// прячем 5 страницу и ссылку

			jQuery('#page_5, #pathway li:eq(4)').hide();

			jQuery('#constructor_hours0').attr('checked','checked');

			jQuery('#page_5 button.btn_next').attr('disabled','disabled');

			is_visible[5] = 0;

			// пересчитаем координаты страниц с учетом выбывшей

			for(i=6; i<=(pageX.length-1); i++){

				pageX[i] = pageX[i] - (pageWidth + pageMarginRight);

			}

		} else {

			if(!is_visible[5]){

				jQuery('#page_5, #pathway li:eq(4)').show();

				jQuery('#page_5 button.btn_next').removeAttr('disabled');

				jQuery('#constructor_hours2').attr('checked','checked');

				// востанавливаем координаты страниц с учетом вернувшейся

				for(i=6; i<=(pageX.length-1); i++){

					pageX[i] = pageX[i] + (pageWidth + pageMarginRight);

				}

				is_visible[5] = 1;

			}

		}

	});

	// обработка формы hours

	jQuery('#constructor input[name*="hours"]').change(function(){

		hours = jQuery(this).val();

		if(hours == 0){

			jQuery('#page_5 button.btn_next').attr('disabled','disabled');

		} else {

			jQuery('#page_5 button.btn_next').removeAttr('disabled');

		}

	}).click(function(){

		hours = jQuery(this).val();

		if(hours == 0){

			jQuery('#page_5 button.btn_next').attr('disabled','disabled');

		} else {

			jQuery('#page_5 button.btn_next').removeAttr('disabled');

		}

	});

	

	

	// выбор accomodation (и обработка страницы meals)

	jQuery('#constructor input[name*="accomodation"]').change(function(){

		accom = jQuery(this).val();

		if(accom != 1){

			// прячем 7 страницу и ссылку

			jQuery('#page_7, #pathway li:eq(6)').hide();

			jQuery('#constructor_meals0').attr('checked','checked');

			is_visible[7] = 0;

			// пересчитаем координаты страниц с учетом выбывшей

			for(i=8; i<=(pageX.length-1); i++){

				pageX[i] = pageX[i] - (pageWidth + pageMarginRight);

			}

		} else {

			if(!is_visible[7]){

				jQuery('#page_7, #pathway li:eq(6)').show();

				// востанавливаем координаты страниц с учетом вернувшейся

				for(i=8; i<=(pageX.length-1); i++){

					pageX[i] = pageX[i] + (pageWidth + pageMarginRight);

				}

				is_visible[7] = 1;

			}

		}

		

	});

	

	//

	jQuery('#datepicker').datepicker({

		changeMonth: true,

		changeYear: true,

		minDate: 0,

		dateFormat: 'mm-dd-yy',

		altField: '#constructor_depdate',

		altFormat: 'dd-MM-yy',

		showOtherMonths: true,

		yearRange: '2010:2015',

		onSelect: function() {

			jQuery('#page_2 button.btn_next').removeAttr('disabled');

			setTimeout('disableDays()', 1);

		},

		onChangeMonthYear: function() {

			setTimeout('disableDays()', 1);

		}

	});

	

});



//disable unselectable days

function disableDays(){

	exclude = selectebleDays[lang]-1;

	jQuery('table.ui-datepicker-calendar tr').each(function(){

		jQuery(this).find('td:not(:eq('+exclude+'))').

		addClass('ui-datepicker-unselectable ui-state-disabled').

		attr('onclick','return false;').find('a').

		each(function(){

			d = jQuery(this).text();

			jQuery(this).replaceWith('<span class="ui-state-default">'+d+'</span>');

		});

	});

}



//

function translate(){

	//page_0

	jQuery('#c_title').text(ln['Welcome on your course’s booking space!']);

	jQuery('#btn_begin').text(ln['Begin!']);

	

	//btns

	jQuery('.btn_next').text(ln['Next']);

	jQuery('.btn_prev').text(ln['Back']);

	

	//pathway

	jQuery('#link_1').text(ln['City']);

	jQuery('#link_2').text(ln['Departure date']);

	jQuery('#link_3').text(ln['Length of course']);

	jQuery('#link_4').text(ln['Convenience']);

	jQuery('#link_5').text(ln['Hours']);

	jQuery('#link_6').text(ln['Accomodation']);

	jQuery('#link_7').text(ln['Meals']);

	jQuery('#link_8').text(ln['Services']);

	jQuery('#link_9').text(ln['Confirmation']);

	

	//page_1

	jQuery('#l_city').text(ln['l_city']);

	jQuery('#l_city .msk').text(ln['Moscow']);

	jQuery('#l_city .spb').text(ln['Saint-Petersburg']);

	jQuery('#l_city .nn').text(ln['Nijnij-Novgorod']);

	

	//page_3

	jQuery('#l_length').text(ln['l_length']);

	o = 1;

	jQuery('#constructor_length option').each(function(){

		if(o == 1){

			jQuery(this).text('1 '+ln['Week']);

		} else {

			jQuery(this).text(o+' '+ln['Weeks']);

		}

		o++;

	});

	

	//page_4

	jQuery('#l_constructor_convenience1').text(ln['Individual tuition']);

	jQuery('#l_constructor_convenience2').text(ln['Group tuition']);

	

	//page_5

	jQuery('#l_hours1').text('10 '+ln['Hours']);

	jQuery('#l_hours2').text('20 '+ln['Hours']);

	jQuery('#l_hours3').text('30 '+ln['Hours']);

	

	//page_6

	 jQuery('#l_accomodation1').text(ln['Accommodations with a host family']);

	 jQuery('#l_accomodation2').text(ln['Accommodations in a hotel']);

	 jQuery('#l_accomodation3').text(ln['Booking of a flat']);

	 jQuery('#l_accomodation4').text(ln['Course without accommodation']);

	 

	 //page_7

	 jQuery('#l_yes').text(ln['Yes']);

	 jQuery('#l_no').text(ln['No']);

	 

	 //page_8

	 jQuery('#l_transfer').text(ln['Booking of car transfer to and from airport']);

	 jQuery('#l_visa').text(ln['Visa support in your country']);

	 

	 //page_9

	 jQuery('#btn_cart').text(ln['Add to Cart']);

	

}



// загрузка контента страниц

function loadPages(){

	// загрузка интро

	for(i=0; i <= page.length; i++){

		jQuery('#page_'+i+' div.ajax_content').load(page[i]);

	}

}



// обработка клика по pathway

function linkClick(l){

	is_done = jQuery('#link_'+l).hasClass('done');

	if(is_done){

		getPage(l);

	}

}



// переход к запрошенной странице

function getPage(p){

	

	jQuery('#pathway').css('display', 'block');

	

	// просчет курса на последнем шаге

	if(p == 9){

		sendRequest('calculate','');

	}

	

	// корректировка относительно доп. страниц

	if(p == 5 && !is_visible[5]){

		if(p > p_old){

			p = 6;

		} else if(p < p_old) {

			p = 4;

		}

	} else if(p == 7 && !is_visible[7]){

		if(p > p_old){

			p = 8;

		} else if(p < p_old){

			p = 6;

		}

	}

	

	// скролл

	jQuery('#i_container').animate({ left: -pageX[p]+'px' }, 300);

	

	// раскрашиваем pathway

	if(p < p_old){

		jQuery('#pathway li a').removeClass('active done');

	}

	if(p>1){

		jQuery('#pathway li:lt('+p+') a').removeClass('active').addClass('done');

	}

	jQuery('#link_'+p).removeClass('done').addClass('active');

	

	p_old = p;

	currPage = p;

	

	if(p == 9){

		jQuery('#p_container').height(700);

	} else {

		hgt = jQuery('#page_'+p).height();

		hgt = hgt + 70;

		jQuery('#p_container').height(hgt);

	}



}



// запрос выбранного курса

function sendRequest(action, uid){

	

	if(action == 'addToCart'){

		jQuery('#page_9 button').attr('disabled', 'disabled');

		jQuery('#pathway li a').removeClass('done');

	} else {

		jQuery('#page_9 .btn_cart').attr('disabled', 'disabled');

	}

	var	constructor_city = jQuery('#constructor_city').val();

	var	constructor_depdate = jQuery('#constructor_depdate').val();

	var	constructor_length = jQuery('#constructor_length').val();

	var	constructor_convenience = jQuery('#constructor input[name*="convenience"]:checked').val();

	var	constructor_hours = jQuery('#constructor input[name*="hours"]:checked').val();

	var	constructor_accomodation = jQuery('#constructor input[name*="accomodation"]:checked').val();

	var	constructor_meals = jQuery('#constructor input[name*="meals"]:checked').val();

	var	constructor_transfer = jQuery('#constructor_transfer:checked').val();

	var	constructor_visa = jQuery('#constructor_visa:checked').val();

	

	jQuery.post("/constructor/process.php", {

		   constructor_city: constructor_city,

		   constructor_depdate: constructor_depdate,

		   constructor_length: constructor_length,

		   constructor_convenience: constructor_convenience,

		   constructor_hours: constructor_hours,

		   constructor_accomodation: constructor_accomodation,

		   constructor_meals: constructor_meals,

		   constructor_transfer: constructor_transfer,

		   constructor_visa: constructor_visa,

		   constructor_action: action,

		   constructor_lang: lang,

		   constructor_currency: currency

		   },

		function(data){

			if(action == 'calculate'){

				jQuery('#order_info').html(data);

				jQuery('#page_9 .btn_cart').removeAttr('disabled');

			} else if(action == 'addToCart'){

				if(data){

					//alert(data);

					curse_id = data;

					umiBasket.getInstance().addElement(curse_id);

					jQuery('#page_9 .btn_cart').text(ln['Added to Cart']);

					window.location = pref+'/cart/';

					return false;

				} else {

					alert('error');

					jQuery('#page_9 .btn_cart').removeAttr('disabled');

					jQuery('#pathway li:lt(9) a').addClass('done');

				}

			}

   });

	

}
