// jQuery
var origin_href = '#';

$(function () {

	//Rounded edges for IE
	DD_roundies.addRule('.content-box', '8px');
	
	// Modal for logging in
	var modal = document.createElement('DIV');
	$(modal).dialog({
		autoOpen: false,
		title: 'Login',
		resizable: false,
		draggable: false,
		width: '300',
		height: '240',
		modal: true,
		open: function(ev, ui) {
			$('div.ui-widget-overlay').click( function(){
				$(modal).dialog('close');
			});
		},
		close: function(ev, ui) {
			//Clear html contents on close
			$(this).html('');
		}
	});
	
	// Modal for video
	var modal_video = document.createElement('DIV');
	$(modal_video).dialog({
		autoOpen: false,
		title: 'Drive Mint',
		resizable: false,
		draggable: false,
		width: '770',
		height: '600',
		modal: true,
		open: function(ev, ui) {
			$('div.ui-widget-overlay').click( function(){
				$(modal_video).dialog('close');
			});
		},
		close: function(ev, ui) {
			//Clear html contents on close
			$(this).html('');
		}
	});
	
	// Modal for city selection
	var modal_city_select = document.createElement('DIV');
	$(modal_city_select).dialog({
		autoOpen: false,
		title: 'Mint City Select',
		resizable: false,
		draggable: false,
		width: '350',
		height: 'auto',
		modal: true,
		create: function(event, ui) {

		},
		open: function(ev, ui) {
			$('div.ui-widget-overlay').click( function(){
				$(modal_city_select).dialog('close');
			});
			$('li#city_ny a').click(function() {
				drop_cookie('ny');
				if(origin_href != '#') {
					window.top.location = origin_href;
				} else {
					$(modal_city_select).dialog('close');
					window.top.location = this.href;
				}
				return false;
			});
			$('li#city_boston a').click(function() {
				drop_cookie('boston');
				if(origin_href != '#') {
					window.top.location = origin_href;
				} else {
					$(modal_city_select).dialog('close');
					window.top.location = this.href;
				}
				return false;
			});
		},
		close: function(ev, ui) {
			//Clear html contents on close
			$(this).html('');
		}
	});
	
	function show_city_select_modal() {
		$(modal_city_select).load('/cities.php div.popup', function() {
			$(modal_city_select).dialog('open');
			$('.ui-dialog-titlebar').hide();
		});
	}
	
	// Modal for popups
	var modal_popup = document.createElement('DIV');
	$(modal_popup).dialog({
		autoOpen: false,
		title: 'Drive Mint',
		resizable: false,
		draggable: false,
		width: '600',
		height: 'auto',
		modal: true,
		open: function(ev, ui) {
			$('div.ui-widget-overlay').click( function(){
				$(modal_popup).dialog('close');
			});
		},
		close: function(ev, ui) {
			//Clear html contents on close
			$(this).html('');
		}
	});
	
	// Sign-in Dialog
	$('div#button_sign-in_wrapper a').click( function() {
		$(modal).prepend('<iframe id="surrounding_member_sign_in" src="https://reserve.drivemint.com/my_mini_login.php" frameborder="0" width="230" height="190" scrolling="no"></iframe>');
		$(modal).dialog('open');
		$('.ui-dialog-titlebar').hide();
		return false;
	});
	
	// Open Video modal
	$('div#btn_mint-basics a').click( function() {
		// Detect screen size
		if( ($(window).width() >= 1024) && ($(window).height() >= 768) ) {
			// normal sizing
			$(modal_video).prepend('<iframe id="video_frame" src="/video.php?iframe" frameborder="0" width="740" height="540" scrolling="no"></iframe>');
		} else {
			// smaller screen size
			$(modal_video).dialog('option', 'width', 640);
			$(modal_video).dialog('option', 'height', 480);
			$(modal_video).prepend('<iframe id="video_frame" src="/video.php?iframe&small" frameborder="0" width="600" height="435" scrolling="no"></iframe>');
		}
		$(modal_video).dialog('open');
		$('.ui-dialog-titlebar').hide();
		return false;
	});
	
	// City select modal
	$('li#nav_join_mint a:first, li#nav_find_cars a:first').click( function() {
		// Open City Select modal if no cookie exists
		if (!check_cookie_exists('city')) {
			origin_href = this.href;
			show_city_select_modal();
			return false;
		}
	});
	
	$('a.modal').click( function() {
		var href = this.href;
		var origin_href = this.href;
		// Add space separator if there is only a subsection to load ( HACK )
		if(href.indexOf('#') != -1) { 
			href = href.split('#').join(' #'); 
		} else {
			href += ' .popup';
		}

		$(modal_popup).load(href, function() {
			// Pay as you go chart sizing HACK
			if(origin_href =='http://dev.drivemint.com/join_mint/ind_pay_go.php#pay_go_chart') {
				$(modal_popup).dialog( "option", "width", 800);
			}
			$(modal_popup).dialog('open');
		});
		$('.ui-dialog-titlebar').hide();
		return false;
	});
	
	$('.external').click( function() { window.open(this.href); return false; });
	
	/**************** BEGIN: City Select Behaviors **********************/

	// Determine if cookie exists already
	var cookie_exists = check_cookie_exists('city');
	
	// Helper functions
	function select_city() {
		$('div#cities_state_2').hide(0,function() {
			$('div#cities_state_1').fadeTo(250, 1);
		});
	}
	
	function display_city() {
		$('div#cities_state_1').hide(0,function() {
			var city = (get_cookie('city') == 'ny') ? 'New York' : 'Boston';
			$('span#city_name').text(city);
			$('div#cities_state_2').show();
		});
	}
	
	// Setup default state
	(cookie_exists) ? display_city() : select_city();

	// Additional catches
	if( (window.location.hash == '#/mint-cities') || ($('body#booking').length && !cookie_exists) ) {
		show_city_select_modal();
	}
	// Button behavior
	$('div#cities_state_2 a').click(function() {
		select_city();
		return false;
	});
	
	$('div#cities_state_1 a').click(function() {
		($(this).attr('id') == 'btn_city-ny') ? drop_cookie('ny') : drop_cookie('boston');
	});
	
	/**************** END: City Select Behaviors  **********************/
});

// Drop a cookie with the name of city
function drop_cookie(city) {
	document.cookie = 'city='+city+'; path=/; domain='+window.location.hostname;
}

function check_cookie_exists(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 true
	}
	return false;
}

function get_cookie(name)
{
  var results = document.cookie.match('(^|;) ?' + name + '=([^;]*)(;|$)');

  if (results)
    return(unescape(results[2]));
  else
    return null;
}
