var path;

$(function(){
	if( $("#sites-nav").append('<div id="navsreplace"></div>') ) {
		
		var flashvars = {};
		flashvars.num = SITE_ID;
		var params = {};
		params.wmode = "transparent";
		params.allowscriptaccess = "always";
		var attributes = {};
		swfobject.embedSWF("http://static.campuscars.net/assets/flash/campus_buttons.swf", "navsreplace", "450", "75", "9.0.0", false, flashvars, params, attributes);
	}

	$("#topNav ul > li ul li:first-child").each(function(i, el){
		var parent = $(el).parent().parent().clone(); 
		$(el).parent().prepend(parent);
	});

  $("#carFocus img").click(function() {
      var url = "http://www.campuscars.net/image.php?w=550&aoe=1&src="+$(this).attr("filename");
      $.colorbox({href: url});
  });
  
  $("#carPictures ul li img").click(function() {        
    var newImage = $("#carFocus img[thumbnail=" + $(this).attr("thumbnail") + "]")
      , currentImage = $("#carFocus img:visible")
      , fadeDuration = 250;
    
    if(currentImage.attr("thumbnail") != newImage.attr("thumbnail")) {
      currentImage.fadeOut(fadeDuration, function() {
        newImage.fadeIn(fadeDuration);
      }); 
    }
  });
  $(".similarVehicleThumbs li a").hover(function(){
    $(this).find("div").fadeIn();
  }, function() {
    $(this).find("div").fadeOut();
  });

});

	function launch_dialog(id, name)
	{
	//console.log(name);
			var $this = $(this);
			var horizontalPadding = 30;
			var verticalPadding = 30;
			var url= '/view-used-car/?mini=true&id=' + id;
			var absurl= escape(location.href);
			
	        $('<iframe class="externalSite" src="' + url + '" />').dialog({
	            title: 'All Roads Lead To Campus',
				stack:true,
	            autoOpen: true,
	            width: 950,
	            height: 625,
	            modal: true,
	            resizable: true,
				position: 'top',
				autoResize: true,
				closeOnEscape: true,
				closeText: 'Click to Close',
				draggable: false,
				dialogClass: 'used-dialog',
				close: function() {
					//$("#usedreplace").removeClass("hide");
					$("#externalSite").remove();
					$.address.path('');
				},
				open: function() {
					trackURL(id, name);
					//$("#usedreplace").addClass("hide");
				},
				buttons: {
				'Close Window': function() {
					$("#externalSite").remove();
					$.address.path('');
					$(this).dialog('close');
				},
				'Express Contact': function(e) {
					launch_site('/contact-us/?mini=true');
					//$(this).dialog('close');
				},
				'Test Drive': function() {
					//$(this).dialog('close');
					launch_site('/contact-us/request-a-test-drive/?mini=true&id='+id+'&carname='+name);
				},
				'Send to a Friend': function() {
					//$(this).dialog('close');
					launch_site('/contact-us/tell-a-friend/?mini=true&id='+id+'&carname='+name);
				},
				'Print': function() {
					//$(this).dialog('close');
					window.open('http://manage.campuscars.net/pdf/window_sticker/'+id);
				}
				
			}


	        }).width(950 - horizontalPadding).height(625 - verticalPadding);
	}
	function launch_site(url)
	{		
			var $this = $(this);
			var horizontalPadding = 30;
			var verticalPadding = 30;
	        $('<iframe class="externalSite" src="' + url + '" />').dialog({
	            title: 'All Roads Lead To Campus',
	            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]);
			
	})



