// JavaScript Document

function reset_share_form(){
	$(".share_form").html('<form name="shareForm" method="post"><div class="instructions">Enter email addresses, separated by commas. Max 200 characters.</div> <input type="hidden" id="posturl" />                      <div class="line"><input class="defaultText" id="friend" type="input" name="shareFriend" value="Friend&#39;s Email Address" title="Friend&#39;s Email Address" maxlength="75"></div><div class="line"><input class="defaultText" id="email" type="input" name="shareEmail" value="Your Email Address" title="Your Email Address" maxlength="75"></div> <div class="line"><textarea class="defaultText" id="msg" name="shareMsg" title="Check out this article. I thought you&#39;d might enjoy it!" cols="48" rows="5">Check out this article. I thought you&#39;d might enjoy it!</textarea></div><div class="option"><input type="checkbox" id="copy" name="shareCopy" />&nbsp;Send me a copy of this message&nbsp;</div><div class="send"><a id="post_msg" style="cursor:pointer;">SEND</a>&nbsp;</div></form>');
}

function reset_article(){
	 id =  $("input#quote_id").val();
	//alert(id); 
	$("#quotecheck" + id).css('display','block');
	 
	 $("#blurb").html('');
         $("#publication").html('');
         $("#title").html('');
         $("#reported_by").html('');
	 $("input#quote_id").val('');
    	 $("#comment_list").html('');
	 $(".comments").slideUp('slow');

}

function show_share_form(){
 var $preview_width = parseInt($(".preview").css("width"));
            if ($preview_width < 465) {
                $(".share").css('width',$preview_width+'px');
                $(".share_form .line input, .share_form .line textarea").css('width','335px');
                $(".share_form .send").css('width','135px');
            } else {
                $(".share").css('width','465px');
                $(".share_form .line input, .share_form .line textarea").css('width','400px');
                $(".share_form .send").css('width','200px');
            }   
                
            if ($(".comments").is(':visible')) {
                $(".share").css('display','block').corner();     
                $(".comments").css('display','none');
                $(".listmod li.tab2").css('background', '#E1E1E4');
                $(".listmod li.tab2 a").css({'background' : '#E1E1E4', 'color' : '#605DA5'});
            } else {                        
                $(".share:hidden").slideDown('slow').corner();
            }
  
            $(this).css({'background' : 'transparent url(images/roundleft.gif) no-repeat left top'});
            $(".listmod li.tab1 a").css({'background' : '#AEADD1 url(images/roundright.gif) no-repeat right top', 'color' : '#FFFFFF'});
}

function show_comments(){
 		var $prefix = $("input#prefix").val();

                //get comments
                url = $prefix + 'main/get_comments/id/' + $("input#quote_id").val();
                //alert(url);

		data = new Array();
                $.post(url,data,function(ret){
			document.getElementById("comment_list").innerHTML=ret;
                },'html');

                var $preview_width = parseInt($(".preview").css("width"));
            // alert($preview_width);
            if ($preview_width < 600) {
                $(".preview .comments").css({'width' : '401px', 'min-height' : '320px', 'height' : '520px', 'max-height' : '520px'});
                $(".preview .holder").css({'margin-top' : '40px', 'padding-left' : '15px', 'width' : '360px', 'height' : '200px'});
                // $(".holder .scroll-pane").css({'padding-left' : '15px', 'width' : '360px', 'height' : '200px'});
                $(".quote_list").css({'width' : '305px'});
                $(".comment_form").css({'padding-left' : '40px', 'width' : '335px'});
            } else {
                $(".preview .comments").css({'width' : '855px', 'min-height' : '240px', 'height' : '240px', 'max-height' : '240px'});
                $(".preview .holder").css({'margin-top' : '0px', 'padding-left' : '45px', 'width' : '420px', 'height' : '200px'});
                $(".quote_list").css({'width' : '368px'});
                $(".comment_form").css({'padding-left' : '20px', 'width' : '355px'});
            }

            if ($(".share").is(':visible')) {
                $(".comments").css('display','block').corner();
                $(".share").css('display','none');
                $(".listmod li.tab1").css('background', '#E1E1E4');
                $(".listmod li.tab1 a").css({'background' : '#E1E1E4', 'color' : '#605DA5'});
            } else {
                $(".comments:hidden").slideDown('slow').corner();
            }

            $(this).css({'background' : '#E1E1E4 url(images/roundleft.gif) no-repeat left top'});
            $(".listmod li.tab2 a").css({'background' : '#AEADD1 url(images/roundright.gif) no-repeat right top', 'color' : '#FFFFFF'});
            // $(".scroll-pane").jScrollPane({scrollbarWidth: 15});
            $('.defaultText').blur();
}

      String.prototype.trim = function() {
      	return this.replace(/^\s+|\s+$/g,"");
      }
      String.prototype.ltrim = function() {
      	return this.replace(/^\s+/,"");
      }
      String.prototype.rtrim = function() {
      	return this.replace(/\s+$/,"");
      }
		
  $(document).ready(function() { 
   
	$(".go").click(function(){
		var $prefix = $("input#prefix").val();
		//alert($prefix);
        	url = $prefix + 'main/index/date/'  + document.getElementById('date').value;
                window.location = url;
        });

	$(".sharelink").click(function(){
		$(this).parent().parent().parent().parent().find("p:first").click(); 
		setTimeout("show_share_form()",1000);

	});

	$(".commentslink").click(function(){
		$(this).parent().parent().parent().parent().find("p:first").click();   
		setTimeout("show_comments()",1000);
	});
  	/*
	$(".zeitgeistsharelink").click(function(){
		reset_article();
	    	$(this).parent().parent().parent().parent().find("p:first").click();
		setTimeout("show_share_form()",500);
	});
	
	$(".zeitgeistcommentslink").click(function(){
		reset_article();
		$(this).parent().parent().parent().parent().find("p:first").click();
		setTimeout("show_comments()",500);
	});

	$(".zingersharelink").click(function(){
	    	$(this).parent().parent().parent().find("p:first").click();
		//alert($(this).parent().parent().parent().html());
		setTimeout("show_share_form()",500);
        });
        
        $(".zingercommentslink").click(function(){
	    	$(this).parent().parent().parent().parent().find("p:first").click();
		setTimeout("show_comments()",500);
        });
	*/

        $(".quote, .quote1, .quote2, .quote3, .quote4,").preview();
      
	$(".zingerquote").click.cancelBubble = true;

	$("#zeitgeistquote").click.cancelBubble = true;
        //------------------------------------------------------------   		                              
      
        $(".who, .learn").hover(function () {
            $(this).css({'cursor' : 'pointer'});
            /*
		$('.who').css({'background' : 'url(images/whoHover.gif) no-repeat'});
            $('.learn').css({'background' : 'url(images/learnHover.gif) no-repeat'});
        	*/
	}, function () {
            $(this).css({'cursor' : 'text'});
          /*
		  $('.who').css({'background' : 'url(images/who.gif) no-repeat'});
            $('.learn').css({'background' : 'url(images/learn.gif) no-repeat'});
        */
	});
        
        $(".who, .learn").click(function () {
		 var $prefix = $("input#prefix").val();

                $.get($prefix + 'popup_content/jpz_bio.html',function(data){
                        $(".jpz_bio .main_body").html(data);
                        $(".jpz_bio").css({'filter' : 'alpha(opacity=95)', '-moz-opacity' : '0.95', 'opacity' : '0.95'});
                        $(".jpz_bio").slideDown('slow').corner();
                        $(".jpz_bio .main_nav").html('<img class="done" src="' + $prefix + 'images/btnImDone.gif"><img class="more" src="' + $prefix + 'images/btnIWantMore.gif">');
           });

        });     

        $(".jpz_bio .close").hover(function () {
            $(this).css({'cursor' : 'pointer'});            
        /*   
	 $("#close3").attr('src', 'images/bioCloseHover.gif');
        */
	}, function () {
            $(this).css({'cursor' : 'text'});            
        /*  
	  $("#close3").attr('src', 'images/bioClose.gif');
        */
	});
      
        $(".jpz_bio .close").click(function () {            
            $(".jpz_bio").slideUp('slow');                                                  
        });

        $('.main_nav, img.done, .main_nav img.more').hover(function () {
            $(this).css({'cursor' : 'pointer'});
        }, function () {
            $(this).css({'cursor' : 'text'});
        });
        
        $(".main_nav img.done").livequery('click', function () {
            $(".jpz_bio").slideUp('slow');
        });
        
        $('.main_nav img.more').livequery('click', function () {
 		 
		var $prefix = $("input#prefix").val();
		   $.get($prefix + 'popup_content/jpz_bio_more.html',function(data){
                        $(".jpz_bio .main_body").html(data);
                        $(".jpz_bio").css({'filter' : 'alpha(opacity=95)', '-moz-opacity' : '0.95', 'opacity' : '0.95'});
                        $(".jpz_bio").slideDown('slow').corner();
		        $(".jpz_bio .main_nav").html('<img class="back" src="'+ $prefix + 'images/btnBackToBio.gif">');            
        	});
	}); 
        
        $('.main_nav img.back').livequery('click', function () {
		 var $prefix = $("input#prefix").val();
 
		$.get($prefix + 'popup_content/jpz_bio.html',function(data){
                        $(".jpz_bio .main_body").html(data);
                        $(".jpz_bio").css({'filter' : 'alpha(opacity=95)', '-moz-opacity' : '0.95', 'opacity' : '0.95'});
                        $(".jpz_bio").slideDown('slow').corner();
        		$(".jpz_bio .main_nav").html('<img class="done" src="' + $prefix + 'images/btnImDone.gif"><img class="more" src="' + $prefix + 'images/btnIWantMore.gif">');
	   });

        });                
             

	$(".home").click(function(){
		if (window.sidebar){ // firefox
	                window.sidebar.addPanel(document.title, 'http://www.jpzenger.com','');
	        }else if (document.all){
	                window.external.AddFavorite('http://www.jpzenger.com', document.title);
	        }else if(window.opera && window.print){
	                var elem = document.createElement('a');
	                elem.setAttribute('href','http://www.jpzenger.com');
	                elem.setAttribute('title',document.title);
	                elem.setAttribute('rel','sidebar');
	                elem.click();
	        }
	});

	$(".about").click(function(){
		 var $prefix = $("input#prefix").val();

		$.get($prefix + 'popup_content/about.html',function(data){
			$(".about_popup .main_body").html(data);
			$(".about_popup").css({'filter' : 'alpha(opacity=95)', '-moz-opacity' : '0.95', 'opacity' : '0.95'});
         		$(".about_popup").slideDown('slow').corner();
		});
	});
 
	 $(".about_popup .close").click(function () {
            $(".about_popup").slideUp('slow');
        });

	$(".subscribe").click(function(){
	  	var $prefix = $("input#prefix").val();
        
                $.get($prefix + 'popup_content/subscribe.html',function(data){
                        $(".about_popup .main_body").html(data);
                        $(".about_popup").css({'filter' : 'alpha(opacity=95)', '-moz-opacity' : '0.95', 'opacity' : '0.95'});
                        $(".about_popup").slideDown('slow').corner();
                	$(".subscsubmit").click(function(){
				var $name = $("input#subname").val().trim();
 	                       	var $email = $("input#subemail").val().trim();
        	                $.post($prefix + "main/subscribe/",{name: $name,email:$email},function(data){
                	                 $(".about_popup .main_body").html(data);
                        	});

			});
		});
	});

	$(".talk").click(function(){
		var $prefix = $("input#prefix").val();

                $.get($prefix + 'popup_content/talk.html',function(data){
                        $(".about_popup .main_body").html(data);
                        $(".about_popup").css({'filter' : 'alpha(opacity=95)', '-moz-opacity' : '0.95', 'opacity' : '0.95'});
                        $(".about_popup").slideDown('slow').corner();
        		$(".talksubmit").click(function(){
		        	
				var $name = $("input#talkname").val().trim();
                                var $email = $("input#talkemail").val().trim();
				var $comments = $("textarea#talkcomments").val().trim();

				$.post($prefix + "main/talk_to_us/",{name: $name,email:$email,comments:$comments},function(data){
					$(".about_popup .main_body").html(data);
                                });
			});
	      	});
	});
	/*
	$(".talksubmit").click(function(){
		alert('hi');
	}
	*/
	$(".send").click(function(){
                 var $prefix = $("input#prefix").val();

                $.get($prefix + 'popup_content/missed.html',function(data){
                        $(".about_popup .main_body").html(data);
                        $(".about_popup").css({'filter' : 'alpha(opacity=95)', '-moz-opacity' : '0.95', 'opacity' : '0.95'});
                        $(".about_popup").slideDown('slow').corner();
                	$(".missedsubmit").click(function(){
                                var $name = $("input#name").val().trim();
                                var $email = $("input#email").val().trim();
                        	var $comments = $("textarea#comments").val().trim();

			        $.post($prefix + "main/missed_quotes/",{name: $name,email:$email,comments: $comments},function(data){
                                         $(".about_popup .main_body").html(data);                                });

                        });
		});
        });

	$(".advertise").click(function(){
                 var $prefix = $("input#prefix").val();

                $.get($prefix + 'popup_content/advertise.html',function(data){
                        $(".about_popup .main_body").html(data);
                        $(".about_popup").css({'filter' : 'alpha(opacity=95)', '-moz-opacity' : '0.95', 'opacity' : '0.95'});
                        $(".about_popup").slideDown('slow').corner();
                });
        });
	  $(".privacy").click(function(){
                 var $prefix = $("input#prefix").val();

                $.get($prefix + 'popup_content/privacy.html',function(data){
                        $(".about_popup .main_body").html(data);
                        $(".about_popup").css({'filter' : 'alpha(opacity=95)', '-moz-opacity' : '0.95', 'opacity' : '0.95'});
                        $(".about_popup").slideDown('slow').corner();
                });
        });
	  $(".terms").click(function(){
                 var $prefix = $("input#prefix").val();

                $.get($prefix + 'popup_content/terms.html',function(data){
                        $(".about_popup .main_body").html(data);
                        $(".about_popup").css({'filter' : 'alpha(opacity=95)', '-moz-opacity' : '0.95', 'opacity' : '0.95'});
                        $(".about_popup").slideDown('slow').corner();
                });
        });

	


	$(".facebook").click(function(){
		window.open('http://www.facebook.com/addfriend.php?id=1057990957');

	});
        //-----------------------------------------------------
        $(".listmod li.tab1").click(function () {
          	show_share_form();
	 });
        $(".listmod li.tab2").click(function () {
		show_comments();
        });
        
        $(".preview img.done").hover(function () {
            $(this).css({'cursor' : 'pointer'});
        }, function () {
            $(this).css({'cursor' : 'text'});
        });
        
        $(".preview img.done").click(function () {
		 $(".share").slideUp('slow');
            	reset_share_form();
		$(".preview").slideUp('slow');
        	//setTimeout(reset_article,500);
		reset_article();
	});

	
        $(".close").hover(function () {
	    var $prefix = $("input#prefix").val();

	    $(this).css({'cursor' : 'pointer'});
            $("#close1").attr('src', $prefix + 'images/btnCloseHover.gif');
            $("#close2").attr('src', $prefix + 'images/btnCloseHover.gif');
        }, function () {
	    var $prefix = $("input#prefix").val();
            
	    $(this).css({'cursor' : 'text'});
            $("#close1").attr('src', $prefix + 'images/btnClose.gif');
            $("#close2").attr('src', $prefix + 'images/btnClose.gif');
        });
                
        $('#post_msg').livequery('click', function() {
            //alert($(this));

	    var friend = $("input#friend").val().trim();
            var email = $("input#email").val().trim();
	
            var msg = $("textarea#msg").val().trim();
	    var posturl = $("input#posturl").val().trim();		
            var copy = $('input#copy').val().trim();
		
		//alert(posturl);
	    // Form validation
            if (friend.length == 0 || friend == ($("input#friend")[0].title) 
                || email.length == 0  || email == ($("input#email")[0].title))                
            {
                // No complex validation for now ...
                return false;
            }
		//alert(posturl);

	    $.post(posturl,{friendemail: friend, youremail: email, message: msg,copy: copy},
			function(data){
			//alert(data);	
			$(".share_form").html('<div id="message"></div>');
                        $("#message").html('<h2>Your Email has been sent!</h2>').append('<p>Thank you for sharing.</p>');
			});

        });
                     
        $('#post_comment').livequery('click', function() {                    
            var writer = $("input#writer").val().trim();
            var comment = $("textarea#comment").val().trim();
	    var quote_id = $("input#quote_id").val().trim();            
	
	    var $prefix = $("input#prefix").val();


            // Form validation
            if (writer.length == 0 || writer == ($("input#writer")[0].title) 
                || comment.length == 0  || comment == ($("textarea#comment")[0].title))
            {
                // No complex validation for now ...
                return false;
            }
	//alert('writer' + writer + 'comment' + comment + ' quote_id ' + quote_id);
	 $.post($prefix + "main/add_comment/",{name: writer,comments:comment,quote_id: quote_id},function(data){
                 //alert(data);        
	});
		numcomments = $("#numcomments" + quote_id).html();
		numcomments++;
		$("#numcomments" + quote_id).html(numcomments);	
	 //get comments
                url = $prefix + 'main/get_comments/id/' + quote_id;
                
		//alert(url);

		setTimeout('show_comments()',500);
		/*
		$.get(url,function(ret){
			//alert(ret);
			// $("#comment_list").html(ret);
		},'html');
		*/
		/*
            $("#comment_list").append('<div class="quote_list"><p>' + comment + '</p><span>&nbsp;</span><em>' + writer + '</em></div>');
            // $(".scroll-pane").jScrollPane({scrollbarWidth: 15});
		*/
		/*
            var $preview_width = parseInt($(".preview").css("width"));
            // alert($preview_width);
            if ($preview_width < 600) {                
                $(".preview .comments").css({'width' : '401px', 'min-height' : '320px', 'height' : '520px', 'max-height' : '520px'});
                $(".preview .holder").css({'margin-top' : '40px', 'padding-left' : '15px', 'width' : '360px', 'height' : '200px'});                
                // $(".holder .scroll-pane").css({'padding-left' : '15px', 'width' : '360px', 'height' : '200px'});
                $(".quote_list").css({'width' : '305px'});                                
                $(".comment_form").css({'padding-left' : '40px', 'width' : '335px'});                
            } else {
                $(".preview .comments").css({'width' : '855px', 'min-height' : '240px', 'height' : '240px', 'max-height' : '240px'});
                $(".preview .holder").css({'margin-top' : '0px', 'padding-left' : '45px', 'width' : '420px', 'height' : '200px'});
                // $(".scroll-pane").css({'padding-left' : '45px', 'width' : '420px', 'height' : '200px'});
                $(".quote_list").css({'width' : '368px'});
                $(".comment_form").css({'padding-left' : '20px', 'width' : '355px'});                                       
            }
            */
	    //alert('am i here?');
            $(".listmod li.tab2 .counter").html(numcomments);
            /*
            $(".comment_form").html('<div id="message"></div>');
            $("#message").html('<h2>Your Comment has been Submitted!</h2>').append('<p>Thank you for posting.</p>');
            */
            return false;
        });
        
        $(".comments .close").click(function () {            
		var $prefix = $("input#prefix").val();
            $(".comments").slideUp('slow');
           	$("input#writer").val('');
 		$("textarea#comment").val('');


		/*
		 $(".listmod li.tab2").css('background', '#E1E1E4');
            $(".listmod li.tab2 a").css({'background' : '#E1E1E4', 'color' : '#605DA5'});
            */
		/*
            $(".comment_form").html('<form name="commentForm"><div class="line"><input class="defaultText" id="writer" type="input" name="commentWriter" title="Your Name" maxlength="50"></div><div class="line"><textarea class="defaultText" id="comment" name="commentText" title="Write your comments here" rows="6"></textarea></div><div class="post">POST COMMENT&nbsp;<input id="post_comment" type="image" src="' + $prefix + 'images/postBtn.gif"></div></form>');            
        	*/
		});

        $(".share .close").click(function () {            
            $(".share").slideUp('slow');
            $(".listmod li.tab1").css('background', '#E1E1E4');
            $(".listmod li.tab1 a").css({'background' : '#E1E1E4', 'color' : '#605DA5'});
            
        });

	reset_share_form();
        $('.defaultText').livequery('focus', function()
        {
            if ($(this).val() == $(this)[0].title)
            {
                $(this).removeClass("defaultTextActive");
                $(this).val("");
            }
        });
        
        $('.defaultText').livequery('blur', function()
        {
            if ($(this).val() == "")
            {
                $(this).addClass("defaultTextActive");
                $(this).val($(this)[0].title);
            }
        });
        
        $('.defaultText').blur();              
        
      });
