var path;

$(function(){
	$("#topNav ul > li ul li:first-child").each(function(i, el){
		//var parent = $(el).parent().parent().clone(); 
		//$(el).parent().prepend(parent);
	});



});
function window_close(){
  var flashObjects = document.getElementsByTagName("object");
        for (i = 0; i < flashObjects.length; i++) {
            flashObjects[i].style.visibility = "visible";
        }
        var flashEmbeds = document.getElementsByTagName("embed");
        for (i = 0; i < flashEmbeds.length; i++) {
            flashEmbeds[i].style.visibility = "visible";
        }	
}
function window_open(){
	 var flashObjects = document.getElementsByTagName("object");
        for (i = 0; i < flashObjects.length; i++) {
            flashObjects[i].style.visibility = "hidden";
        }
        var flashEmbeds = document.getElementsByTagName("embed");
        for (i = 0; i < flashEmbeds.length; i++) {
            flashEmbeds[i].style.visibility = "hidden";
        }
}
 var $dialog
	function launch_dialog(id, name)
	{
	//console.log(name);
			window_open();
			var $this = $(this);
			var horizontalPadding = 30;
			var verticalPadding = 30;
			var url= 'http://www.midgleymotors.com/view-used-car/?mini=true&id=' + id;
			var absurl= escape(location.href);
			
	      var $dialog =   $('<iframe  class="externalSite" src="' + url + '" />').dialog({
	            title: 'Midgley Motors',
				stack:true,
	            autoOpen: false,
	            width: 940,
	            height: 625,
	            modal: true,
				position: 'top',
				autoResize: true,
				closeOnEscape: true,
				closeText: 'Click to Close',
				dialogClass: 'used-dialog',
				close: function() {
					window_close()
					//$("#usedreplace").removeClass("hide");
					//$("#externalSite").remove();
					$.address.path('');
					
				},
				open: function() {
					trackURL(id, name);
					//$("#usedreplace").addClass("hide");
				},
				buttons: {
				'Close Window': function() {
					$(this).dialog('close');
					$(".externalSite").remove();
					$.address.path('');
					window_close()
					
				},
				'Express Contact': function(e) {
					launch_site('/contact/?mini=true');
					//$(this).dialog('close');
				},
				
				'Get Financing!': function() {
					//$(this).dialog('close');
					window.location = ('https://www.midgleymotors.com/finance-center/?url='+escape(url));
				}
				
			}


	        }).width(910 - horizontalPadding).height(770 - verticalPadding);
			$dialog.dialog('open').width(910 - horizontalPadding).height(770 - verticalPadding);;

	}
	function launch_site(url)
	{		
			var $this = $(this);
			var horizontalPadding = 30;
			var verticalPadding = 30;
	        $('<iframe class="externalSite" src="' + url + '" />').dialog({
	            title: 'Midgley Motors',
	            autoOpen: true,
				stack:true,
	            width: 950,
	            height: 500,
	            modal: true,
	            resizable: true,
				autoResize: true,
				close: function() {
					//$("#usedreplace").removeClass("hide");
					
				},
				open: function() {
					//trackURL(id, name);
					//$("#usedreplace").addClass("hide");
				}
	        }).width(950 - horizontalPadding).height(500 - verticalPadding);
	}

	var trackURL = function(id, name)
	{
		if(id != path[2])
			$.address.path("/view-car/"+name+"/"+id+"/");	
	}
	$.address.init(function(event) {
		path = $.address.pathNames();
		if(path[0] == 'view-car' && !isNaN(path[2]))
			launch_dialog(path[2], path[1]);
			
	})
	$.address.change(function(event) {
		path = $.address.pathNames();
		if(path[0] != 'view-car')
		{
			$('.externalSite').dialog('close');
		}
			
	})



