// Trip Planner Widget JS
// Minor customization for Vail Cascade ( removing the push banner at the bottom)

//Set flash widget global var
var theWidget;

$(document).ready(function(){
				
	$("#widgetWrap").each(function(){
		// Get flash widget on page load
		theWidget = $('#widget').get(0);
		if ( $("#solstice-close").length ) {  $("#solstice-close").trigger('click'); }
		
		// Hide the flash widget
		$("#widgetWrap").show();
		
		// Add to wish list click
		$(".tp-add").click(function(e) {
			e.preventDefault();
			// If the solstice banner exists, hide it to show the flash.
			if ( $("#solstice-close").length ) {  $("#solstice-close").trigger('click'); }
			$("#widgetWrap").show();
			var itemID = $(this).attr('id').substr(1);
			setTimeout("add('"+itemID+"','#')",1000)
		});
		
		// Test functions
		$(".tp-clear").click(function(e) {
			e.preventDefault();
			theWidget.clearData();
			window.location.reload();
		});
		$(".tp-show").click(function(e) {
			e.preventDefault();
			if ( $("#solstice-close").length ) {  $("#solstice-close").trigger('click'); }
			showTPwidget();
		});
	});
	
	// TP Intro Flash
	$("#tp-flash").each(function(){
			var client = 'vailcascade';
			//Center modal function
			var loadTP=function(hash){
				$("#tp-flash").flash({
					src:"http://"+client+".buildmyitinerary.com/clients/"+client+"/flash/trip-planner.swf",
					width:"879",
					height:"500",
					id:"widget",
					name:"widget",
					allowscriptaccess:"always",
					wmode:"transparent"},
					{version:"9"},
						function(htmlOptions) { 
							htmlOptions.flashvars.ID = "154";
							htmlOptions.flashvars.xmlPath = "http://buildmyitinerary.com/clients/"+client+"/flash/xml/trip-planner-intro.xml";
							htmlOptions.flashvars.submitURL = "http://"+client+".buildmyitinerary.com/index.php"; 
							htmlOptions.flashvars.sugarID = "d0347218-4f42-408d-2cec-412a38c3fb7d"; 
							htmlOptions.flashvars.tripPlannerDomain = "http://www.buildmyitinerary.com"; 
							htmlOptions.flashvars.servicesURL = "http://"+client+".buildmyitinerary.com/soap/tripplanner/server.php?WSDL&flash=y"; 
							htmlOptions.flashvars.saveURL = "http://"+client+".buildmyitinerary.com/login.php"; 
							htmlOptions.flashvars.myTripPlannerURL = "http://"+client+".buildmyitinerary.com/profile.php"; 
							$(this).html($.fn.flash.transform(htmlOptions));
					});
				hash.w.show();
				hash.w.css("position","absolute");
				hash.w.css("top", ( $(window).height() - hash.w.outerHeight() ) / 2+$(window).scrollTop() + "px");
				hash.w.css("left", ( $(window).width() - hash.w.outerWidth() ) / 2+$(window).scrollLeft() + "px");
			};
			//Clear html after close of popup
			var clearTP=function(hash){
				hash.w.hide()
				hash.o.remove();
				//hash.w.html('');
				$("#tp-flash").html('');
			};

			$("#tp-start").click(function(e){
				e.preventDefault();
				$("#tp-modal").jqm({onShow:loadTP,onHide:clearTP}).jqmShow();
			});
			
			$("#tp-side-start").click(function(e){
				e.preventDefault();
				$("#tp-modal").jqm({onShow:loadTP,onHide:clearTP}).jqmShow();
			});
	});
	
});

function add(id,url){
	theWidget.addToWishList(id,url);
}


function getTPitemDetails(id,activity){
	//Center modal function
	var centerModal=function(hash){
		hash.w.show();
		hash.w.css("position","absolute");
		var top = ( $(window).height() - hash.w.outerHeight() ) / 2+$(window).scrollTop();
		if ( top < 0 ) {
			top = 10;
		}
		hash.w.css("top", top + "px");
		hash.w.css("left", ( $(window).width() - hash.w.outerWidth() ) / 2+$(window).scrollLeft() + "px");
	};
	//Clear html after close of popup
	var clearPopup=function(hash){
		hash.w.hide()
		hash.o.remove();
		hash.w.html('');
	};
	if ( activity == 'true' ) {
		$.get('includes/tp-details.php?type='+id+'a=1',function(data){
			$("#tp-popup").html(data);
			$("#tp-popup").jqm({onShow:centerModal,onHide:clearPopup}).jqmShow();
			getTPclose();
		});	
	} else {
		$.get('includes/tp-details.php?type='+id,function(data){
			$("#tp-popup").html(data);
			$("#tp-popup").jqm({onShow:centerModal,onHide:clearPopup}).jqmShow();
			getTPclose();
		});
	}

}
function getTPclose(){
	$("#item-popup-close .jqmClose").click(function(e){
		e.preventDefault();
		$("#tp-popup").jqm({onShow:centerModal,onHide:clearPopup}).jqmHide();						
	});
}

function showTPwidget() {
	if ( $("#solstice-close").length ) {  $("#solstice-close").trigger('click'); }
	$("#widgetWrap").show();
}
