
	$(document).ready(function(){

		$('a[rel*=facebox]').facebox(); 
		
		$(".paper_editions a").popupWindow({ height:700, width:1000, centerScreen:1 });
		
		$(".save").click(function(e){
			ad_id = $(this).attr("id")
			ad_id = ad_id.replace("save_", "")
			//$.get("/inc/api/int/save_ad.php", {id: ad_id })
			
			$.facebox(function() {
			  $.get("/inc/api/int/save_ad.php", {id: ad_id }, function(data){
				$.facebox('<div class="c"> <img src="/img/icons/bank.png" /> <br /> <h3>Ad Saved!</h3> <p>Ads will only be saved as long as they are available online. Please print a copy or email your saved ads to keep them for longer.</p> <p><a href="/saved/">Go to your saved ads</a></p></div>')
			  })
			}) 
			
			//$("#popup_inline").html('<div class="r"><img src="/img/icons/close.png" id="close" /></div> <div class="c"> <img src="/img/icons/bank.png" /> <br /> <h3>Ad Saved!</h3> <p>Ads will only be saved as long as they are available online. Please print a copy or email your saved ads to keep them for longer.</p> <p><a href="/saved/">Go to your saved ads</a></p></div>');
			
			e.preventDefault()
			
		});
		
		$(".watch_adv").click(function(e){
			ad_id = $(this).attr("id")
			ad_id = ad_id.replace("watch_", "")
			$.get("/inc/api/int/watch_adv.php", {id: ad_id })
			
			$.facebox(function() {
			  $.get("/inc/api/int/watch_adv.php", {id: ad_id }, function(data){
				$.facebox('<div class="c"> <img src="/img/icons/watched.png" /> <br /> <h3>Added to Watch List!</h3> <p>This advertiser has been added to your watch list. Their current ad will always be displayed in your my favorites page.</p> <p><a href="/favorites/">Go to My Favorites</a></p></div>')
			  })
			}) 
			
			//$("#popup_inline").html('<div class="r"><img src="/img/icons/close.png" id="close" /></div> <div class="c"> <img src="/img/icons/watched.png" /> <br /> <h3>Added to Watch List!</h3> <p>This advertiser has been added to your watch list. Their current ad will always be displayed in your my favorites page.</p> <p><a href="/favorites/">Go to My Favorites</a></p></div>');
			
			e.preventDefault()
			
		});
		
		$(".lightbox").click(function(e){
			photo = $(this).attr("id");
			img = new Image();
			$(img).load(function (){
				$(this).hide();
				$("#popup_inline").html(this);
				
				imgWidth = this.width;
				imgHeight = this.height;
				windowWidth = self.innerWidth;
				windowHeight = self.innerHeight;

				$("#popup_inline").css({
							"width": imgWidth,
							"height": imgHeight,
							"top" : (windowHeight/2) - (imgHeight/2) -20,
							"left": (windowWidth/2) - (imgWidth/2),
							"padding": 20
				});
				
				$("#popup_inline").append('<img src="/img/icons/close.png" id="close" style="position: absolute; right: 5px; top: 5px;" />');
				
				$(this).fadeIn();
			}).attr('src', '/photos/'+photo);	
		});
		
		$(".remove_watch").click(function(e){
			ad_id = $(this).attr("id")
			ad_id = ad_id.replace("remove_", "")
			$.get("/inc/api/int/remove_watch.php", {id: ad_id })
			
			$(this).parent().parent().parent().parent().fadeOut("slow")
			$(this).parent().parent().parent().parent().next("hr").fadeOut("slow")			
			
			e.preventDefault()
			
		});
		
		$(".delete").click(function(e){
			ad_id = $(this).attr("id")
			ad_id = ad_id.replace("delete_", "")
			if(confirm('Are you sure you want to delete this ad?')){
				$.get("/inc/api/int/delete.php", {id: ad_id })
				
				$(this).parent().parent().parent().parent().fadeOut("slow")
				$(this).parent().parent().parent().parent().next("hr").fadeOut("slow")			
			}
			
			e.preventDefault();
			
		});
		
		$(".back_viewer").click(function(e){
			ad_id = $(this).attr("id")
			ad_id = ad_id.replace("back_", "")
			
			$.get("/inc/api/int/back_viewer.php", {id: ad_id })
			
			$(this).parent().parent().parent().parent().fadeOut("slow")
			$(this).parent().parent().parent().parent().next("hr").fadeOut("slow")			
			
			e.preventDefault();
			
		});
		
		$(".print").click(function(e){
			window.print()
			e.preventDefault()
		});
		
		$("#toggle_pref_box").click(function(e){
			$("#pref_box").slideToggle("normal");
			e.preventDefault();
		});
		
		$("#save_prefs").click(function(){
			
			editions = new Array();
			$(".ed").each(function(){
				if($(this).attr("checked")){
					editions.push($(this).val());
				}
			});
			
			ed_list = editions.join(",");
			
			$.post("/inc/api/int/browsing_prefs.php", {z: $("#z").val(), e: ed_list},
			function(data){
				$("#pref_box").slideToggle("normal");
				if(data == 1){
					window.location.reload();
				}else{
					$("#browsing").html(data);
				}	
			}, "text");	

		});
		
		var fileTypes = {
		  pdf: 'pdf.png'
		};
		 
		// this is like $.each() above, except
		// it iterates over the matched elements
		$('a').each(function() {
		 
		  // get a jQuery object for each anchor found
		  var $a = $(this);
		 
		  // get the href attribute
		  var href = $a.attr('href');
		 
		  // get the extension from the href
		  var hrefArray = href.split('.');
		  var extension = hrefArray[hrefArray.length - 1];
		 
		  var image = fileTypes[extension];
		 
		  if (image) {
			$a.css({
			  paddingLeft: '22px',
			  background: 'url("/img/icons/' + image + '") no-repeat center left'
			});
		  }
 
	});

				
	});