jQuery(document).ready(function(){
	
	// min-height 
	
	var winheight = $(window).height() - 91; 
	$('div.page-wrapper').css('min-height', winheight);
	
	// input click 
	
	$('input#input_8').focus(function(){
		$(this).val('');
	});
	$('input#input_9').focus(function(){
		$(this).val('');
	});
	
	// slideshow on homepage
	if ($('#slideshow').length > 0) {
		var counts = $('#slideshow').children('img').length;
		var curr = 0;
		var timeout = 5000;
		$('#slideshow').children('img').each(function(ind){
			if (ind > 0) { 
				$(this).css('display','none'); 
			}
		});
		setTimeout(doSlide,timeout);
	}
	function doSlide(){
		$($('#slideshow').children('img')[curr]).fadeToggle("slow", "linear");
		if (curr < counts-1) { curr += 1; } else { curr = 0; }
		$($('#slideshow').children('img')[curr]).fadeToggle("slow", "linear");
		setTimeout(doSlide,timeout);
	}
	
	// click functions for footer
	$('#Bar .close').click(function(){
		$(this).fadeOut();
		$('#Bar .open').fadeIn();
		$('.bar-wrapper').animate({'width':'44px'}); 
		$('.bar-left').fadeOut();
		return false;
	});
	$('#Bar .open').click(function(){
		$(this).fadeOut();
		$('#Bar .close').fadeIn();
		$('.bar-wrapper').animate({'width':'1120px'}); 
		$('.bar-left').fadeIn();
		return false;
	});
	
	// footerbar position
	
	/*var winheight = $(window).height() - 36; 
	$('#Bar').css('top', winheight); */
	
	// form functions
	
	if ($('#11525149498').length > 0) {
		// jquery-styled selectbox
		$(function () {
			$('.select').selectBox()
		});
		
		// clear input textboxes when clicked and return old values if nothing entered
		var values = new Array();
		$('input.text, input.text_sm').each(function(ind){
			values[ind] = $(this).val();
			
			$(this).focus(function(){
				$(this).val('');
			});
			$(this).blur(function(){
				if ($(this).val() == ''){
					$(this).val(values[ind]);
				}
			});
		});
		
		// register calendars

		$('.date-pick').each(function(ind){
		Date.firstDayOfWeek = 0;
		Date.format = 'mm/dd/yyyy';
			$(this).datePicker( { createButton:false }
			).bind('click', function() {
				$(this).dpDisplay();
				this.blur();
				return false;
			}).bind('dateSelected', function(event, date, $td, status) {
				i = '#input_'+(ind+24);
				$(i).val(date.asString());
			});
		});
		
		// # sailing_date + ind+1
		
		// variable to keep track of added form items;
		var last = new Array( new Array(''), new Array(''), new Array(''));
		
		$('.addable').each(function(ind){
			last[ind] = 0;
			
			// add button function for addable rows
			$(this).find('a.add').each(function(one){
				$(this).click(function(){
					$this = $(this);
					$addable = $this.parent().parent(); 
					total_rows = $addable.children().length;
					
					for (i=last[ind];i<total_rows;i++){
						$length = $(this).parent().children().length;

						// if select:option else input:text
						if ($length > 1) {
							$('.select').selectBox('destroy');
							$content = $($(this).parent()[0]).html();
							$content = $content.replace('add','remove');
							$content = $content.replace('Add another','Remove');
							$addable.append('<div class="form_row">'+$content+'</div>');
							$new_len = $addable.children().length-1;
							$($addable.children()[$new_len]).children().remove('.top_label');
							$($addable.children()[$new_len]).children('label').html('');
							$('.select').selectBox();
						} else {
							$content = $($(this).parent()[0]).html();
							$content = $content.replace('add','remove');
							$content = $content.replace('Add another','Remove');
							$addable.append($content);
							$new_len = $addable.children().length-1;
							$($addable.children()[$new_len]).children('label').html('');
						}
						if (i==last[ind]) { last[ind] +=1; }
						
						register_handlers(ind);
					}
					return false;
				});
			});
			
		});

		// handlers for removing rows buttons
		function register_handlers(num) {
			$($('.addable')[num]).find('a.remove').unbind('click');
			$($('.addable')[num]).find('a.remove').each(function(){
				$(this).click(function(){
					$('.select').selectBox('destroy');
					$(this).parent().remove();
					last[num] -=1;
					$('.select').selectBox();
					return false;
				});	
			})
		}
		
		/* ----------------------THIS NEEDS FIXING--------------------- */
		/* TODO: make so that when other fields are dynamically 		*/
		/* added on the spot, the dom can recognize, which to disable.	*/
		
		// handler for enabling disabled section
		$('#share').change(function(){
			the_val = $(this).val();

			if (the_val == 'share') {
				$($('.addable').children()[2]).find('.disabled').remove();
			} else {
				$($('.addable').children()[2]).find('.disabled').remove();
				$($('.addable').children()[2]).prepend('<div class="disabled"></div>');
			}
		});
	}
	
	// load maps
	if ($('#map').length > 0) {
		// variables and init for map
		var clicked = 0;
		var dur = '1';
		gmap_init();
		
		// setup carousel
		$('#carousel').jcarousel({
	    	start: 1,
	    	scroll: 5,
	    	initCallback: scroll_carousel
	    });
	    
	    // clicks for carousel
		$('.jcarousel-skin-cruise li.jcarousel-item').each(function(ind){
			$(this).click(function(){
				$(this).parent().children().each(function(){
					$(this).removeClass('active');
				})
				$(this).addClass('active');
				gmap_remote(ind);
				return false;
		    });
		})
	}
	function gmap_remote(ind) {
		sel = 0 + ind
		$('.map-popup').fadeOut(dur);
		
		$this = $($('.marker')[sel]);
		$this.parent().find('a.active').removeClass('active'); 
		$this.addClass('active'); 
		$next = $this.next();
		offset_x = $next.width()/2;
		_ctr = parseFloat($this.css('left')) - ($this.width()/2) - offset_x + 2;
		_top = parseFloat($this.css('top')) - $next.height() - $this.height()*1.25;
		
		$next.css('left',_ctr+'px');
		$next.css('top',_top+'px');
		$next.fadeIn(dur);
		
		clicked = 1;
	}
	function gmap_init() {
			var last = new Array();
			var i = 0;
			
			$('.marker').hover(function(){
				$('.marker').css({'z-index':'20'});
				$(this).css({'z-index':'50'});
			})
			
			$('.marker').click(function(e){
				//active class 
				
				
				$(this).parent().find('a.active').removeClass('active'); 
				$(this).addClass('active'); 
				
				// get current
				last[i] = e.target;
				i++;
				
				$this = $(this);
				$next = $this.next();
				offset_x = $next.width()/2;
				_ctr = parseFloat($this.css('left')) - ($this.width()/2) - offset_x + 2;
				_top = parseFloat($this.css('top')) - $next.height() - $this.height()*1.25;
				
				if (clicked == 0) {
					$next.css('left',_ctr+'px');
					$next.css('top',_top+'px');
					$next.fadeIn(dur);
					clicked = 1;
				} else {
					if (last[i-1] != last[i]) {
						$('.map-popup').fadeOut(dur);
						$next.css('left',_ctr+'px');
						$next.css('top',_top+'px');
						$next.fadeIn(dur);
						clicked = 1;
					} else {
						$('.map-popup').fadeOut(dur);
						clicked = 0;
					}
				}
				
			});
			
			$('#map').click(function(e){
				if (clicked == 1 && e.target.id == 'map') {
					$('.map-popup').fadeOut(dur);
					clicked = 0;
					$(this).find('a.active').removeClass('active'); 
					$('#carousel').find('li.active').removeClass('active');
				}
			});
	}
	function scroll_carousel(carousel){
		$('a.marker').each(function(ind) {
			$(this).click(function(){
				carousel.scroll($.jcarousel.intval(ind));
				$('.jcarousel-skin-cruise li.jcarousel-item').removeClass('active');
				$($('.jcarousel-skin-cruise li.jcarousel-item')[ind]).addClass('active');
				return false;	    		
			})
		});
	}
	
	// hr 
	
	$('hr').wrap('<div class="hr">'); 
	
	// border 
	
	$('.specs-left .item:last').css('border-bottom', '0px'); 
	$('.specs-right .item:last').css('border-bottom', '0px'); 
	
	// margins
	
	$('div.boat-other div.other:last').addClass('last'); 
	$('.itinerary-details div.boat-other div.other:nth-child(6)').css('margin-right', '0px'); 
	
	// destination cycle
	
	$('#dest_shell').cycle({
		timeout: 0,
		speed: 'fast',
		prev: '.prev_modal', 
		next: '.next_modal'
	});
	
	$('#chongqing').click(function() { 
	    $('#dest_shell').cycle(0); 
		return false; 
	});
	$('#fengdu').click(function() { 
	    $('#dest_shell').cycle(1); 
		return false; 
	});
	$('#shibaozhai').click(function() { 
	    $('#dest_shell').cycle(2); 
		return false; 
	});
	$('#wanzhou').click(function() { 
	    $('#dest_shell').cycle(3); 
		return false; 
	});
	$('#white_emperor_city').click(function() { 
	    $('#dest_shell').cycle(4); 
		return false; 
	});
	$('#wushan_the_daning_river').click(function() { 
	    $('#dest_shell').cycle(5); 
		return false; 
	});
	$('#shennong_stream').click(function() { 
	    $('#dest_shell').cycle(6); 
		return false; 
	});
	$('#three_gorges').click(function() { 
	    $('#dest_shell').cycle(7); 
		return false; 
	});
	$('#three_gorges_dam').click(function() { 
	    $('#dest_shell').cycle(8); 
		return false; 
	});
	$('#new_zigui').click(function() { 
	    $('#dest_shell').cycle(9); 
		return false; 
	});
	$('#yichang').click(function() { 
	    $('#dest_shell').cycle(10); 
		return false; 
	});
	$('#wuhan').click(function() { 
	    $('#dest_shell').cycle(11); 
		return false; 
	});
	$('#huangshan').click(function() { 
	    $('#dest_shell').cycle(12); 
		return false; 
	});
	$('#nanjing').click(function() { 
	    $('#dest_shell').cycle(13); 
		return false; 
	});
	$('#shanghai').click(function() { 
	    $('#dest_shell').cycle(14); 
		return false; 
	});
	
	// destination 
	
	$('#dest_container').jqm({trigger: 'a.trigger_destination', toTop: true}); 
	$('a.trigger_destination').click(function(){
		$('#dest_container').css('top', '65px');
		$('html,body').animate({scrollTop: 0}, 300);
		$('#dest_shell').hide(); 
		$('#dest_shell').fadeIn(300); 
		$('.map-popup').fadeOut(300);
		$('#map').find('a.active').removeClass('active'); 
		$('#carousel').find('li.active').removeClass('active'); 
	});
	
	// modals 
	
	$('#plan_deck').jqm({trigger: 'a.plan_deck', toTop: true});
	$('a.plan_deck').click(function(){
		$('#plan_deck').css('top', $(window).scrollTop() + 65);
	});
	
	$('#plan_superior').jqm({trigger: 'a.plan_superior', toTop: true});
	$('a.plan_superior').click(function(){
		$('#plan_superior').css('top', $(window).scrollTop() + 65);
	});
	
	$('#plan_exec').jqm({trigger: 'a.plan_exec', toTop: true});
	$('a.plan_exec').click(function(){
		$('#plan_exec').css('top', $(window).scrollTop() + 65);
	});
	
	$('#plan_deluxe').jqm({trigger: 'a.plan_deluxe', toTop: true});
	$('a.plan_deluxe').click(function(){
		$('#plan_deluxe').css('top', $(window).scrollTop() + 65);
	});
	
	$('#plan_shangri-la').jqm({trigger: 'a.plan_shangri-la', toTop: true});
	$('a.plan_shangri-la').click(function(){
		$('#plan_shangri-la').css('top', $(window).scrollTop() + 65);
	});
	
	// modal cycles 
	
	$('#plan_deck_main').cycle({
		timeout: 0,
		speed: 'fast',
		prev: '.modal_prev', 
		next: '.modal_next'
	});
	
	$('#plan_superior_main').cycle({
		timeout: 0,
		speed: 'fast',
		prev: '.modal_prev', 
		next: '.modal_next'
	});
	
	$('#plan_exec_main').cycle({
		timeout: 0,
		speed: 'fast',
		prev: '.modal_prev', 
		next: '.modal_next'
	});
	
	$('#plan_deluxe_main').cycle({
		timeout: 0,
		speed: 'fast',
		prev: '.modal_prev', 
		next: '.modal_next'
	});

	$('#plan_shangri-la_main').cycle({
		timeout: 0,
		speed: 'fast',
		prev: '.modal_prev', 
		next: '.modal_next'
	});
	
	// gallery
	
	$('.thumb-container a').lightBox({ 
		containerResizeSpeed: 0,
		imageBtnClose: '/img/close_btn.png'
	});
	
	$('#plan_deck').jqmAddClose('.plan_superior');
	$('#plan_superior').jqmAddClose('.plan_deck');
	$('#plan_superior').jqmAddClose('.plan_exec');
	$('#plan_exec').jqmAddClose('.plan_superior');
	$('#plan_exec').jqmAddClose('.plan_deluxe');
	$('#plan_exec').jqmAddClose('.plan_shangri-la');
	$('#plan_deluxe').jqmAddClose('.plan_exec');
	$('#plan_deluxe').jqmAddClose('.plan_shangri-la');
	$('#plan_shangri-la').jqmAddClose('.plan_deluxe');
	$('#plan_shangri-la').jqmAddClose('.plan_exec');
	
	// if less than two cycle items 
	
	if($('#plan_deck_main img').size() < 2) { 
		$('#plan_deck a.modal_prev').hide(); 
		$('#plan_deck a.modal_next').hide(); 
	}
	
	if($('#plan_superior_main img').size() < 2) { 
		$('#plan_superior a.modal_prev').hide(); 
		$('#plan_superior a.modal_next').hide(); 
	}
	
	if($('#plan_exec_main img').size() < 2) { 
		$('#plan_exec a.modal_prev').hide(); 
		$('#plan_exec a.modal_next').hide(); 
	}
	
	if($('#plan_deluxe_main img').size() < 2) { 
		$('#plan_deluxe a.modal_prev').hide(); 
		$('#plan_deluxe a.modal_next').hide(); 
	}
	
	if($('#plan_shangri-la_main img').size() < 2) { 
		$('#plan_shangri-la a.modal_prev').hide(); 
		$('#plan_shangri-la a.modal_next').hide(); 
	}
	
	// new dropdown
	
	$('ul.sub-nav li.hassubsub').hover(function() { 
		$(this).css('height', 'auto'); 
		$(this).css('line-height', 'auto'); 
		$(this).children('ul.subsubnav').slideDown(350); 
	}, function() {
		$(this).children('ul.subsubnav').slideUp(350); 
		$(this).css('line-height', '40px'); 
	});
	
	$('#navigation').mouseleave(function() { 
		$('#navigation ul.subsubnav').hide(); 
	}); 
	
	//sidenav dropdown 
	
	/*$('#side-nav li').hover(function() { 
		$(this).children('ul.subsubnav').slideDown(450); 
	}, function() {
		$(this).children('ul.subsubnav').slideUp(100); 
	});*/
	$('#side-nav li').not('.active').hover(function() { 
		$(this).children('a.itin').css('color', '#000'); 
		$(this).children('a.itin').css('background-position', '0px -22px'); 
	}, function() {
		$(this).children('a.itin').css('color', '#999'); 
		$(this).children('a.itin').css('background-position', '0px 0px');
	});
	
	
	// calendar cycles 
	
	$('#calendar_months_shell').cycle({
		timeout: 0,
		speed: 'fast',
		prev: '.cal_left', 
		next: '.cal_right'
	});
	
	// form "single share" function 
	
	var newtext;
	var newtext2;
	$('td.singleshare input.form-textbox').live('change', function(event) { 
			newtext = $(this).val(); 
			if((newtext == '0') || (newtext == '')) { 
				
				$('td.singleshare input.form-textbox').each(function() { 
					newtext2 = $(this).val(); 
					if((newtext2 == '0') || (newtext2 == '')) { 
						$('li#id_21').hide();
						$('li#id_22').hide();
					}
					else { 
						$('li#id_21').show();
						$('li#id_22').show();
						return false;	
					}	
				});
			}
			else { 
				$('li#id_21').show();
				$('li#id_22').show();
			}
		
	}); 
	
	// form add br? 
	
	$("#11525149498").submit(function () {
		if($(this).valid()) {
	    	$("li#id_3 :text").each(function () {
	        	var value = $(this).val();
	        	var newValue = value + '<br/>';
	        	$(this).val(newValue);
	    	});
		}
	});
	
	// faq 
	
	$('p.faq_question').toggle(function() { 
		$(this).parent('div').children('p.faq_answer').slideDown(500); 
		$(this).addClass('faq_question_active'); 
	}, function() { 
		$(this).parent('div').children('p.faq_answer').slideUp(500); 
		$(this).removeClass('faq_question_active'); 
	}); 
	
	var deviceAgent = navigator.userAgent.toLowerCase();
			var agentID = deviceAgent.match(/(iphone|ipod|ipad)/);
			if (agentID) {

		        $('#Bar').css('position', 'relative'); 
				$('#Bar').css('width', '1020px');
				$('#Bar').css('margin', '0 0 0 -510px');  
				$('.bar-wrapper').css('position', 'relative'); 
				$('.bar-wrapper').css('width', '1020px'); 
				$('.bar-left').css('width', '600px'); 
				$('.bar-left').css('padding-left', '20px'); 
				$('.bar-right ul li p').css('padding', '0 13px'); 
				$('.bar-right ul li a').css('border', '0px'); 
			}
		
});

