// JavaScript Document

    $.fn.x = function(n) {
    var result = null;
    this.each(function() {
    var o = this;
    if (n === undefined) {
    var x = 0;
    if (o.offsetParent) {
    while (o.offsetParent) {
    x += o.offsetLeft;
    o = o.offsetParent;
    }
    }
    if (result === null) {
    result = x;
    } else {
    result = Math.min(result, x);
    }
    } else {
    o.style.left = n + 'px';
    }
    });
    return result;
    };
    
    $.fn.y = function(n) {
    var result = null;
    this.each(function() {
    var o = this;
    if (n === undefined) {
    var y = 0;
    if (o.offsetParent) {
    while (o.offsetParent) {
    y += o.offsetTop;
    o = o.offsetParent;
    }
    }
    if (result === null) {
    result = y;
    } else {
    result = Math.min(result, y);
    }
    } else {
    o.style.top = n + 'px';
    }
    });
    return result;
    };
    
    $.fn.preview = function() {
      
          return this.each(function() {
      
                var $this = $(this);
                var $quote = $this.find("p:first");
                var $color = $quote.css("color");
                $quote.hover(function () {
                    $(this).css({'color' : '#E38500', 'cursor' : 'pointer'});
                }, function () {
                    $(this).css({'color' : $color, 'cursor' : 'text'});
                }); 
                
                var $container_div = $this.parent();                                              
                var $parent_div = $container_div.parent();
                
                $quote.click(function() {

			var $quoteid = $this.find("input:first").val();
		
			//alert("setting quote id to " + $quoteid);

			var $prefix = $("input#prefix").val();
			
			var $posturl = $prefix + 'main/email_to_friend/id/' + $quoteid;
			$("input#posturl").val($posturl);
			
			var $url = $prefix + "main/get_quote/id/" + $quoteid;
			//alert($url);
			$.get($url,function(data){
				publications = new Array();
				publications['WSJ'] = 'the_wall_street_journal.jpg';
				publications['NYTIMES'] = 'the_new_york_times.jpg';
				publications['USATODAY'] = 'usa_today.jpg';
				publications['AJC'] = 'the_atlantic.jpg';
				publications['PLAINDEALER'] = 'the_plain_dealer.jpg';
				publications['BUSINESSWEEK'] = 'business_week.jpg';
				publications['CHICAGOTRIBUNE'] = 'chicago_tribune.jpg';
				publications['DAILYNEWS'] = 'daily_news.jpg';
				publications['HOUSTONCHRONICLE'] = 'houston_chronicle.jpg';	
				publications['TIME'] = 'logo_time_home.jpg';
				publications['LATIMES'] = 'los_angeles_times.jpg';		
				publications['NYPOST'] = 'new_york_post.jpg';
				publications['NEWSWEEK'] = 'newsweek.jpg';
				publications['NPR'] = 'npr.jpg';
				publications['SFCHRONICLE'] = 'san_francisco_chronicle.jpg';
				publications['THEATLANTIC'] = 'the_atlantic.jpg';
				publications['DALLASMORNINGNEWS'] = 'the_dallas_morning_news.jpg';
				publications['MIAMIHERALD'] = 'the_miami_herald.jpg';
				publications['NEWYORKREVIEW'] = 'the_new_york_review.jpg';
				publications['NEWYORKER'] = 'the_new_yorker.jpg';
				publications['WASHINGTONPOST'] = 'the_washington_post.jpg';

				quotedata = data.split('::');		
				
				document.getElementById("blurb").innerHTML = quotedata[6];
				document.getElementById("title").innerHTML = quotedata[5];
				document.getElementById("urlLink").setAttribute('href','http://'+quotedata[4]);
				document.getElementById("reported_by").innerHTML = 'Reported By ' + quotedata[1];
				document.getElementById('quote_id').value = $quoteid;
				pub = quotedata[7];
				pub = pub.replace(/^\s+|\s+$/g, '');
				//alert('|' + pub + '|');
				//alert(publications[pub]);
				/*
				logo = publications[pub];
				alert(logo);
				*/
				//alert(publications[pub]);
				
				if(publications[pub]){
					
					imgtag = "<img src='" + $prefix + "images/" + publications[pub] + "' />";

					document.getElementById("publication").innerHTML = imgtag;
				}else{
					document.getElementById("publication").innerHTML = quotedata[7];
				}

				$("span.counter").html(quotedata[8]);
			});
		    
      
                    var $container_x = $container_div.x();
                    var $container_left = 0;
                    
                    if ($container_x < 300) { 
                        $container_left = 10+"px";                                                                           
                    } else {                        
                        $container_left = $container_x-$("#main").x()-10+"px";                                                                       
                    }   
                    
		    //var $previewdiv = $("#preview" + $quoteid);

	
                    $(".preview").css({"left" : $container_left});                                                                           
        	     //$previewdiv.css({"left" : $container_left});                      
	                                      
                    var $container_top = $container_div.y()-27+"px";
                    var $container_width = parseInt($container_div.css("width"));                                                            
                    var $container_width = $container_width - 35;                                       
                    $container_width = $container_width+"px";
                                                       
                    $(".preview").css({"top" : $container_top, "width" : $container_width});                                        
                    //$previewdiv.css({"top" : $container_top, "width" : $container_width});    
			$(".preview").css({'filter' : 'alpha(opacity=95)', '-moz-opacity' : '0.95', 'opacity' : '0.95'});
                	//$previewdiv.css({'filter' : 'alpha(opacity=95)', '-moz-opacity' : '0.95', 'opacity' : '0.95'});
   
		 $(".preview").slideDown('slow').corner();
		//$previewdiv.slideDown('slow').corner();                                           
                });
          });
      };
