//from quirksmode, to the cookie function of the fare message
function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

holderTimeout=Array();

/* distinguish between Safari and Chrome (Chrome thinks it's Safari 'cos it uses Webkit) */
(function() {
    var userAgent = navigator.userAgent.toLowerCase();
    
    jQuery.extend(jQuery.browser, {
        safari : /webkit/.test(userAgent) && !/chrome/.test(userAgent),
        chrome : /chrome/.test(userAgent)
    });
})();


function noAlphaFadeSupport(){
    if($.browser.msie && parseInt($.browser.version) < 7)
             {return true;}
    return false;
}

$(document).ready(function(){
	
	$("a.new_window").click(function () {
		window.open(this.href);
		return false;
	});
	
	/*
	$(".fare_close").click(function () {
		if ($(this).parent("div").hasClass("fare_info")) {
			$(this).parent("div").slideUp("slow");
		} else {
			$(this).parent("div").hide();
		}
	});
	*/

	// show/hide inline booking form
    $('div#inline_booking_form_button a').toggle(
        function () {
            $(this).parents('div#inline_booking_form_button').removeClass('book_now_right');
            $(this).parents('div#inline_booking_form_button').addClass('book_now_right_off');
            //return false;
            //scrollIndex();
            if ($.browser.msie && $.browser.version == 6) {
                $('div.inline_form').show();
            } else {
                $('div.inline_form').slideDown(600);
            }
        },
        function () {
            $(this).parents('div#inline_booking_form_button').removeClass('book_now_right_off');
            $(this).parents('div#inline_booking_form_button').addClass('book_now_right');
            
            if ($.browser.msie && $.browser.version == 6) {
                $('div.inline_form').hide();
            } else {
                $('div.inline_form').slideUp(600);
            }
        }
    );

    //scroll to page top
    $("div#inline_booking_form_button a").click(function(event){
        //prevent the default action for the click event
        event.preventDefault();

        var windowHeight = $(window).height();
        var formTopPostion = $('div#inline_booking_form_button').offset().top;
        var formHeight = 534;        
        var pixelsAboveScroll = $('html').scrollTop();
        var scrollOffset = formTopPostion - (windowHeight - formHeight);
        
        if( $('div#inline_booking_form_button').hasClass('book_now_right_off') && (windowHeight + pixelsAboveScroll) < (formTopPostion + formHeight) ) {
            $('html, body').animate({scrollTop:scrollOffset}, 500);
        }        
    });	
	

//	$(".fare_details").hide()
//	$(".extra_details").hide();
	
    
	$("table.sailings td[class^='fare_'] label").each(function (i) {
		$(this).parent("td").addClass("selectable_fare").click(function () {

			var fare_type = $(this).attr("class").substring(5, 10);
			var journey_type = $(this).parents("table").attr("class").substring(8, 9);
			
			// find out journey type to open fare details in correct place
			if (journey_type == 1) {
				journey_type = "fare_outward"
			} else if (journey_type == 2) {
				journey_type = "fare_return"
			}
			
			$("#" + journey_type + "").children(".fare_details").addClass("hello");
			
			$("#" + journey_type + "").children(".fare_details").hide();
			$("#" + journey_type + "").children("#" + fare_type + "_details").show().children("div#" + fare_type + "_info").show().children("img.fare_close").show().siblings(".info1").show().siblings(".info2").hide();
					
			clearTimeout(holderTimeout[journey_type]);
			holderTimeout[journey_type]=setTimeout(function(){
		        if(noAlphaFadeSupport()) {
		            $("#" + journey_type + "").children(".fare_details").children("img.fare_close").hide();
                    $("#" + journey_type + "").children(".fare_details").children("div.fare_info").hide();
                } else {
		            $("#" + journey_type + "").children(".fare_details").children("img.fare_close").slideUp("slow");
		            $("#" + journey_type + "").children(".fare_details").children("div.fare_info").slideUp("slow");
		        }
			},6000);// time spent open in milliseconds
			
			$("#" + journey_type + " a.fare_details_" + fare_type + "").click(function () {
				$("#" + journey_type + "").children(".fare_details").children("div.fare_info").show().children("img.fare_close").show().siblings(".info1").hide().siblings(".info2").show();
				
				return false; 
			});
			
		});
		
	});

	///***** FARE MESSAGE COOKIE CONTROL ******////	
	var date = new Date();
	date.setTime(date.getTime()+(24*60*60*365*10)); //10 years, so it "never" expires

	seenMessage=readCookie("seenFareMessage");

	if(seenMessage!="1") {
	    $("#fare_message").css("display","block");
	    document.cookie ='seenFareMessage=1; expires='+date.toGMTString()+'; path=/';
	}		
    ///*****  END FARE MESSAGE COOKIE CONTROL ******////
    
    
    // Payment Continue Button Management
	$("input[name$='payment_method']").click(managePaymentMethod);
    
});

hideboxHolder=setTimeout("hidebox()",8000);

function hidebox() {
    if(noAlphaFadeSupport() || $.browser.msie) {
        $("#fare_message").hide();
    } else {
        $("#fare_message").fadeOut("slow");
    }
    
    clearTimeout(hideboxHolder);
}


function managePaymentMethod(){
	var radio_value = $("input[name=payment_method]:checked").val();

	var cruiseyPay_value = $("input#chkIWantCruiseyPay").attr("checked");
	var cruiseyPaySelected = false;
	var poliPermitted = true;

	if (typeof(cruiseyPay_value) != "undefined" && (cruiseyPay_value.toString().toLowerCase() == "checked" || cruiseyPay_value.toString().toLowerCase() == "true")) {
	    cruiseyPaySelected = true;
	}
    
	if (cruiseyPaySelected == true) {
		$('div#cruisey_pay_details').show();
	} else {
		$('div#cruisey_pay_details').hide();
	}

    if (radio_value == 'internet' && cruiseyPaySelected == true) {
		alert("POLi is not available with the Cruisey Pay option.\n\nPlease enter your credit card details.")
		$("input#payment_method_credit").attr("checked",true);
		poliPermitted = false;
	}
	
//	if(radio_value=='internet') {
//		alert("POLi is not available at the moment.")
//		$("input#payment_method_credit").attr("checked",true);
//		poliPermitted = false;
//	}
	
	$("div#divActionButton").hide();    
	$("div#divActionButton2").hide();    
	$("div#divActionButton3").hide();    
	
	if(radio_value=='internet' && poliPermitted) {
		$("ul.credit_card_reveal").hide(200);
		$("div#divActionButton3").show();
	} else {
		$("ul.credit_card_reveal").show(200);
		if (cruiseyPaySelected == true) {
			$("div#divActionButton2").show();
		} else {
			$("div#divActionButton").show();
		}
	}
}


