jQuery(document).ready(function($){
	
	
	$('#sendbutton').click(function(){
		
		Affi.Contact();
		
	});
	
	$('#bottomclose').click(function(){
		$('#fb_sliver').hide();
	});
	
	//$('.info-block').corner();
	//$('a.button').corner("5px");
	
	//$('.features').corner('tl');
	
	$('.cont-selector a').click(function(){
		var current = $(this).attr('href');
		
		$('.city-block').hide();
		$(current).show();
		
		if($(this).parent().hasClass('hassub') || $(this).parent().parent().parent().hasClass('hassub')) {
			$('.cont-subselector', $(this).parent()).show();
		} else {
			$('.cont-subselector').hide();
		}
		
		
		return false;
	});
	
	//$('.rally:nth-child(2n)').css('background-color','#FFE8BF');
	 
	/*$('#frontpage-routes .slides').after('<div id="nav">').cycle({ 
	    fx:     'turnDown', 
	    speed:  'fast', 
	    timeout: 5000, 
	    pager:  '#nav' 
	});*/
	var $scroll  = $("#frontpage-routes").scrollable({circular: true}).autoscroll({ autoplay: true});//.navigator();
	
	$scroll.each(function() {
		var $itemsToClone = $(this).scrollable().getItems().slice(1);
		var $wrap = $(this).scrollable().getItemWrap();
		var clonedClass = $(this).scrollable().getConf().clonedClass;
		$itemsToClone.each(function() {
			$(this).clone(true).appendTo($wrap)
				.addClass(clonedClass + ' hacked-' + clonedClass);
		})
	});
	
	/*$('#route-data a').click(function(){
		var current = $(this).attr('href');
		$('.route-block').hide();
		$(current).show();
		return false;
	});*/
	$('#ryears').tabs();
	$('#topnews').tabs();
	$('#tripnews').tabs();
	
	$('.itinerary tbody  tr:first-child td').addClass('itiheader');//({'background-color':'#ffdd57 !important'});
	
	//$('.itinerary tbody  tr:nth-child(1+2n)').addClass('odd');
	 
	var color = 'none';
	$('.itinerary tbody  tr').hover(
		function(){
				$('td',this).css('background-color','#ffdd57');
		}, 
		function(){
				$('td',this).css('border','none').removeAttr('style');
		});
	
	$('.extras td').each(function(){
		if(this.innerHTML =='Y') {
			this.innerHTML = '<img src="/wp-content/themes/cemsmaster/images/icons/tick-icon.png" alt="Availble"/>';
			$(this).css('text-align','center');
		}
		if(this.innerHTML =='N') {
			this.innerHTML = '<img src="/wp-content/themes/cemsmaster/images/icons/delete-icon.png" alt="Unavailble"/>';
			$(this).css('text-align','center');
		}
	});
	
	$('.matrix tr').hover(function(){ $('td', this).css({'background-color':'#F8EFDA' }) }, function(){ $('td', this).css({'background-color':'#fff'}) });
	$('.extras tr:not(tr.th)').hover(function(){ $('td', this).css({'background-color':'#F8EFDA','font-weight':'bold' }) }, function(){ $('td', this).css({'background-color':'#fff','font-weight':'normal'}) });
	
	$('.matrix tr td:last-child a').css({'font-weight':'bold','color':'#C4171E'});

	$('.details a').toggle(
			function(){
				
				var id = $(this).attr('id').split("-")[1];

				$('#detbl-'+id).slideDown();
				$(this).html("-details");
				
			},
			function(){
				
				var id = $(this).attr('id').split("-")[1];
				$('#detbl-'+id).slideUp();
				$(this).html("+details");
				
			}
	);
	
	$('.free-features-open').toggle(
		function() {
			$('#free-features').slideDown();
			$(this).css({'background-image':'url(/wp-content/themes/cemsmaster/images/icons/arrow-up-icon.png)'});
		},
		function() {
			$('#free-features').slideUp();
			$(this).css({'background-image':'url(/wp-content/themes/cemsmaster/images/icons/arrow-down-icon.png)'});
		}
	);

	$('.free-comp-open').toggle(
			function() {
				$('#compare-packs').slideDown();
				$(this).css({'background-image':'url(/wp-content/themes/cemsmaster/images/icons/arrow-up-icon.png)'});
			},
			function() {
				$('#compare-packs').slideUp();
				$(this).css({'background-image':'url(/wp-content/themes/cemsmaster/images/icons/arrow-down-icon.png)'});
			}
	);
	
	$('.see-moreh a').toggle(
			function() { $('.highlights').show(); $(this).html('Collapse');  },
			function() { $('.highlights').hide(); $(this).html('See trip highlights / detailed description')}
	);
	
	$('#fin-header a').click(
			function(){
				
				$('.free-comp-open').click();
			  return true;	
			}
			
	);
	
	/*$('.expand-info').toggle(
			function() {
				var id = $(this).attr('href').replace('#','');
				$('#bottom-'+id).slideDown();
			},
			function() {
				var id = $(this).attr('href').replace('#','');
				$('#bottom-'+id).slideUp();
			}
		);
	*/
	
	if(window.trips) {
		
		Registration.init();
	}
	
	
	
	$('#city-tabs').tabs();
	
	
	$('.order-trip').click(function(){
		var id = $(this).attr('id').replace('order-','');
		$('#book-'+id).submit();
		return false;
	});
	
	
	$('#show-adv').toggle(function(){
		$('.sub-adv').show("fold", { mode:'show' }, 500);
	},
	function(){
		$('.sub-adv').hide("fold", { mode:'hide' }, 500);
	}
	
	);
	
	/*$('.sub-adv').mouseout(	function(){
		$(this).hide();
	});*/
	

});
$ = jQuery.noConflict();
Registration = {};
Registration.validator = null;

Registration.init = function() {
	Registration.step = 0;
	
	$('#reg-tabs').tabs({ disabled: [1] });
	
	var v =  $("#reg-form").validate();
	Registration.validator = v;
	
	$.validator.messages.required = "";
	
	
	$.validator.addMethod("pageRequired", function(value, element) {
		var $element = $(element)
		function match(index) {
			return Registration.step == index && $(element).parents("#reg" + (index + 1)).length;
		}
		if (match(0) || match(1) || match(2)) {
			return !this.optional(element);
		}
		return "dependency-mismatch";
	}, $.validator.messages.required);
	
	
	var price = Registration.getPrice(Registration.currentTrip());
	$('#totalsum').html(price+" &euro;");
	$('#price').val(price);

	
	$('.next-step').click(function(){
		Registration.proceed($(this));
	});
	
	$('#chal').change(function(){
				
		var trip = Registration.currentTrip();
		
		
		$('#cTrip').html(trip.name);
		$('#cDates').html(trip.start+' - '+trip.end+', '+trip.duration+' days');
		$('#cDescription').html(trip.description.replace(/\\/g, ''));
		var price = Registration.getPrice(trip);
		$('#totalsum').html(price+" &euro;");
		$('#price').val(price);
		
	});
	
	$('#package').change(function(){
		var price = Registration.getPrice(Registration.currentTrip());
		$('#totalsum').html(price+" &euro;");
		$('#price').val(price);
	});
	
	$('#part-num').change(function(){
		var price = Registration.getPrice(Registration.currentTrip());
		$('#totalsum').html(price+" &euro;");
		$('#price').val(price);
	});
	
	$('#paymentbank').change(function(){
		
		$('.bankdetails').hide();
		if($(this).val()) {
			
			$('#pay-'+$(this).val()).show();
			
		}
		
	});
		
	
	
	
};
Registration.disable = function(el){
	return false;
};

Registration.proceed = function(el){
	
	var step = parseInt($(el).attr('id').replace('step',''));
	

	if(step && Registration.validator.form()) {
		//$('#reg-tabs').tabs("option",{enable:step, select:step});
		$('#step1').unbind('click').hide();
		Registration.register(step);
		
	}
	
};



Registration.getPrice = function(trip) {
	var pack = $('#package').val();
	var users = $('#part-num').val();
	//console.log(trip);
	
	if (trip==null) return 0;
	
	if( pack == 'basic') {
		
		if (users==3) {
			return trip.price3;
		} 
		return trip.price;
		
		
	}
	
	if( pack == 'standard') {
		if (users==3) {
			return trip.aprice3;
		} 
		return trip.aprice;
	}
	
	if( pack == 'delux') {
		if (users==3) {
			return trip.sprice3;
		} 
		return trip.sprice;
	}
	
};

Registration.currentTrip = function() {
	var sel = $('#chal').val();
	
	var trip = null;
	$.each(trips , function(k,v) {
		
		if(v.id == sel) {
			//console.log(v);
			trip = v;
		}
	});
	return trip;
};

Registration.register = function(step) {
	var params = new Object();
	
	$('.send').each(function(k,v){
		
		var val = $(this).val();
		var name = $(this).attr('name');
		
		params[name] = val;
		
	});
	
	$.ajax({
	 type:'POST',
	 url:'/wp-content/themes/cemsmaster/rpc/reg.php',
	 data:params,
	 dataType:'json',
	 success: function(data, txt) {

		if(data['trip']) {
			$('#confirmation #trip-name').html(data['trip']['name']);
			$('#confirmation #trip-regemail').html(data['reg_email']);
			$('#confirmation #trip-regid').html(data['reg_code']);
			$('#confirmation #trip-regdate').html(data['reg_date']);
		}

		$('#reg-tabs').tabs({ disabled: [0] });
		
		$('#reg-tabs').tabs("enable",step);
		$('#reg-tabs').tabs("select",step);
		Registration.step = Registration.step+1;
		$('#step2').click(function(){
			Registration.payment();
		});
		Affi.Register();
	 },
	 error: function(xhr,msg) {
		 $('#step1').click(function(){
			 Registration.proceed($(this));
		 });
		 $('#step1').show().click(function(){
				Registration.proceed($(this));
			});
	 }
	
	});
	
};

Registration.payment = function() {
	$('#confirmation').hide();
	$('.method').hide();
	var payment = $('.payments input:checked').val();
	
	if($('.payments input:checked').val()=='paypal') {
		//location.href="http://www.paypal.com";
		var params = new Array();
		params.push("price="+$('#price').val());
		params.push("trip="+$('#chal option:selected').text()+' / '+$('#package option:selected').text()+' package / '+$('#part-num').val()+' persons');
		$.ajax({
			 type:'POST',
			 url:'/wp-content/themes/cemsmaster/rpc/paypal.php',
			 data:params.join('&'),
			 dataType:'html',
			 success: function(data, txt) {
					$('.'+payment).show();
					$('#paypal-button').html(data);
				
			 },
			 error: function(xhr,msg) {
				 
			 }
			
			});
		
	} else {
		$('.'+payment).show();
	}
	
	$('#step2').text("Refresh");
}

