/* 
Contains Parts of code from Sérgio Dinis Lopes
Copyright (c) 2009 DigaCultura.net
*/

/*
 Link Ratings
*/
	document.observe("dom:loaded", function(){
		
		var elemts = $('wrap').select('[class="linksummary"]');
		
		for(var i=0; i<elemts.length; i++){
			elemts[i].observe("mouseover",function(event){
				var ratingover = this.descendants();
				for(var int=0; int < ratingover.length; int++){
					if(ratingover[int].className=='linkrating'){
						var queue = Effect.Queues.get('linkratingout'+ratingover[int].id);
						if(queue)queue.each(function(effect) { effect.cancel(); });
						$(ratingover[int]).setOpacity(1);
					}
					if(ratingover[int].className=='linkstats'){
						$(ratingover[int]).setOpacity(0.7);
					}
				}
	
				this.observe("mouseout",function(event){
					var ratingout = this.descendants();
					for(var int2=0; int2 < ratingout.length; int2++){
						if(ratingout[int2].className=='linkrating'){
							new Effect.Opacity(ratingout[int2], { from: 1.0, to: 0, duration: 0.5, delay:0.5, queue:{scope:'linkratingout'+ratingout[int2].id}});
						}
						if(ratingout[int2].className=='linkstats'){
							$(ratingout[int2]).setOpacity(0);
						}
					}
				});
			});
		}
		
		var ratingsels = $('wrap').select('[class="linkrating"]');
		for(var i=0; i<ratingsels.length; i++){
			new Effect.Opacity(ratingsels[i], { from: 1.0, to: 0, duration: 1, delay:'1.' + i });
		}
		
		var linkstats = $('wrap').select('[class="linkstats"]');
		for(var i=0; i<linkstats.length; i++){
			new Effect.Opacity(linkstats[i], { from: 1.0, to: 0, duration: 1, delay:'1.' + i });
		}
		
		// Not liked stories Story page
		if($('desliked')){
			var storydesliked = $('desliked').select('[class="linksummary"]');
			for(var i=0; i<storydesliked.length;i++){
				new Effect.Opacity(storydesliked[i], { from: 1.0, to: 0.6, duration: 1});
			}
		}
		
		// Not liked Stories river
		var elemtdesliked = $('wrap').select('[class="linkdesliked"]');
		for(var i=0; i<elemtdesliked.length;i++){
			new Effect.Opacity(elemtdesliked[i], { from: 1.0, to: 0.6, duration: 1});
		}
		
		
		
		/* Friends That Liked On Mouse Over */
		var friendsthatliked = $('wrap').select('[class="friendsthatliked"]');
		
		for(var i=0; i<friendsthatliked.length; i++){
			friendsthatliked[i].observe("mouseover",function(event){
				var theboxsover = this.siblings();
				for(var int=0; int < theboxsover.length; int++){
					if(theboxsover[int].className=='friendsthatlikedbox'){
						var queue = Effect.Queues.get('friendsthatlikedboxout'+theboxsover[int].id);
						if(queue)queue.each(function(effect) { effect.cancel(); });
						
						new Effect.Parallel([
							new Effect.Appear(theboxsover[int], { sync: false}),
							new Effect.Morph(theboxsover[int], { style: 'left:40px;', sync: false})
						], { 
						  duration: 0.5,
						  queue: { scope:'friendsthatlikedboxover'+theboxsover[int].id }
						});
					}
				}
				this.observe("mouseout",function(event){
					var theboxsout = this.siblings();
					for(var int2=0; int2 < theboxsout.length; int2++){
						if(theboxsout[int2].className=='friendsthatlikedbox'){
							new Effect.Parallel([
								new Effect.Morph(theboxsout[int2], { style: 'left:26px;', sync: true}),
								new Effect.Fade(theboxsout[int2], { sync: true})
							], { 
							  duration: 0.2,
							  queue: { scope:'friendsthatlikedboxout'+theboxsout[int2].id}
							});

						}
					}
				});
			});
		}
		
		
		/* Friends That Liked On Mouse Over Sidebar */
		var friendsthatliked = $('content-sidebar').select('[class="friendsthatliked"]');
		
		for(var i=0; i<friendsthatliked.length; i++){
			friendsthatliked[i].observe("mouseover",function(event){
				var theboxsover = this.siblings();
				for(var int=0; int < theboxsover.length; int++){
					if(theboxsover[int].className=='friendsthatlikedbox'){
						var queue = Effect.Queues.get('sidebarfriendsthatlikedboxout'+theboxsover[int].id);
						if(queue)queue.each(function(effect) { effect.cancel(); });
						
						new Effect.Parallel([
							new Effect.Appear(theboxsover[int], { sync: false}),
							new Effect.Morph(theboxsover[int], { style: 'left:40px;', sync: false})
						], { 
						  duration: 0.5,
						  queue: { scope:'sidebarfriendsthatlikedboxover'+theboxsover[int].id }
						});
					}
				}
				this.observe("mouseout",function(event){
					var theboxsout = this.siblings();
					for(var int2=0; int2 < theboxsout.length; int2++){
						if(theboxsout[int2].className=='friendsthatlikedbox'){
							new Effect.Parallel([
								new Effect.Morph(theboxsout[int2], { style: 'left:26px;', sync: true}),
								new Effect.Fade(theboxsout[int2], { sync: true})
							], { 
							  duration: 0.2,
							  queue: { scope:'sidebarfriendsthatlikedboxout'+theboxsout[int2].id}
							});

						}
					}
				});
			});
		}
		
	});
	
	
	
/*
 Hide / Show Description
*/
	function showHideDescription(linkid){
		
		var displayvalue = $('description'+linkid).getStyle('display');
		
		if(displayvalue == 'none'){
			new Effect.Appear('description'+linkid, {duration:0});
			$('showhidedescription'+linkid).update(DC_Story_HideDescription);
		} else {
			new Effect.Fade('description'+linkid, {duration:0});
			$('showhidedescription'+linkid).update(DC_Story_ShowDescription);
		}
	}
	
	
/*
 Add Story Vote
*/
	function vote (id, md5, value){
	
		if (current_user_id == 0) {
			ModalLoginRegister(DC_Modal_PleaseLoginToVoteStory);
			return;
		}
		
		else if (value==0) {
			var answer = confirm(DC_Unvote_Confirm + ' ' + current_user_login + '?');
			if(!answer)return;
		}
		
		if($('ilikeit'+id))$('ilikeit'+id).onclick = 'void(0);';
		if($('idontlikeit'+id))$('idontlikeit'+id).onclick = 'void(0);';
		
		if($('linksummary'+id)){
			$('linksummary'+id).stopObserving('mouseover');
			$('linksummary'+id).stopObserving('mouseout');
		}
		
		if($('linkstats'+id))if($('linkstats'+id).getStyle('opacity')<0.7)new Effect.Opacity('linkstats'+id, { from: 0, to: 0.7, duration: 1});
		
		processingRequest(true);
	
		url = thisurl + "/vote.php";
	
		new Ajax.Request(url, {
			parameters: {on: 'link', id: id, user: current_user_id, md5: md5, value: value},
			onSuccess: function(transport){
				var response = transport.responseText.evalJSON();
				processingRequest();
				
				if(response.output){
					responseMessages(response.output);
					new Effect.Fade('linkrating'+id, { duration:0.5});
					setTimeout(function(){$('linkrating'+id).remove()}, 500);
					
					$('views' + id).update(response.views);
					$('clicks' + id).update(response.clicks);
				}
				
				else if (value==1) {
					
					new Effect.Fade('linkrating'+id, { duration:0.5});
					setTimeout(function(){$('linkrating'+id).remove()}, 500);
					
					new Effect.Fade('ReportLink'+id, { duration:0.5});
					setTimeout(function(){$('ReportLink'+id).remove()}, 500);
					
					var linkstats = new Element('div', {className:'linkrated', id:'linkstats'+id, style:'display:none;'});
					var favoritbutton = new Element('a', {className:'FavoriteLink', id:'favorite'+id, onclick:'favorite(' + id + ',1)', title:DC_Profile_Links_Favorite});
					$('linktools'+id).insert({'bottom':linkstats});
					$('linktools'+id).insert({'bottom':favoritbutton});
					
					new Effect.Appear('favorite'+id, { duration:0.5, delay:0.6});
					
					$('views' + id).update(response.views);
					$('clicks' + id).update(response.clicks);
					
					new Effect.Morph('ShareLink'+id, { style: 'color:#559d00;' , duration:0.5});
					
				}
				
				else if (value==-1) {
					if($('aboutsubmitter')){
						new Effect.Fade('linkrating'+id, { duration:0.5});
						setTimeout(function(){$('linkrating'+id).remove()}, 500);
						new Effect.Opacity('linksummary'+id,{ from: 1.0, to: 0.6, duration:0.5});
						
					} else {
						
						var childs = $('linksummary'+id).childElements();
						for(var int=0; int < childs.length; int++ ){
							childs[int].remove();
						}
						
						new Effect.Morph('linksummary'+id, { style: 'padding:3px 0 3px 170px;margin:0 0 25px;min-height:20px;' , duration:0.5});
						setTimeout(function(){ $('linksummary'+id).className = 'linkdesliked', 500 });
						
						// Create Elements
						var linkdetails = new Element('div', {className:'linkdetails', style:'display:none;'});
						var linktitlespan = new Element('span', {className:'title'});
						var linktitlea = new Element('a', {href:response.title_url, title:response.title}).update(response.title);
						//
						if(response.comments >0)var commentsClass = 'HasComments';
						else var commentsClass = 'NoComments';
						var linkcomments = new Element('a', {className:commentsClass, href:response.title_url}).update(response.comments);
						
						// Insert
						$('linksummary'+id).insert({'top':linkdetails});
						linkdetails.insert({'top':linktitlespan});
						linktitlespan.insert({'top':linktitlea});
						linkdetails.insert({'bottom':linkcomments});
						// show
						new Effect.Appear(linkdetails,{duration:0.5});
						new Effect.Opacity('linksummary'+id,{ from: 1.0, to: 0.6, duration:0.5,delay:0.5});
					}
				}
				
				else if (value==0) {
					if($('profile_voted'+id)) new Effect.DropOut('profile_voted'+id);
					if($('profile_upcoming'+id)) new Effect.DropOut('profile_upcoming'+id);
				}
			}
		});
	}

/*
 Handle Ajax Forms
*/
	function linkReport(form_elem) {
		
		var url = thisurl + '/_modalRequest.php';
		// Processing...
		processingRequest(true);
		// Disable button
		disableEnableButton(form_elem,true);
		// ajax Request
		new Ajax.Request(url, {
			parameters: $(form_elem).serialize(true),
			onSuccess: function(transport){
				processingRequest();
				disableEnableButton(form_elem,false);
					
				var response = transport.responseText.evalJSON();
				if(response){
					vote(response.link_id, response.md5, -1);
					responseMessages(response.output);
					Modalbox.hide();
				}
			},
		});
	}








/*
  Comment Votes
*/
	function cvote (user, id, md5, value) {
	
		good_c = document.getElementById ('good_c' + id);
		bad_c = document.getElementById ('bad_c' + id);
		var good_button = document.getElementById ('good_button' + id);
		var bad_button = document.getElementById ('bad_button' + id);
	
		if (xmlhttp) {
			url = thisurl + "/vote.php";
			mycontent = "on=comment&id=" + id + "&user=" + user + "&md5=" + md5 + "&value=" + value;
	
			if (user == 0) {
				ModalLoginRegister(DC_Modal_PleaseLoginToVoteComment);
			} else {
	
				good_c.href='javascript:void(0);';
				bad_c.href='javascript:void(0);';
			
				dcxmlhttp[id] = new myXMLHttpRequest ();
				if (dcxmlhttp) {
					processingRequest(true);
					dcxmlhttp[id].open ("post", url, true);
					dcxmlhttp[id].setRequestHeader ('Content-Type', 'application/x-www-form-urlencoded');    
					dcxmlhttp[id].send (mycontent);
						   
					dcxmlhttp[id].onreadystatechange = function () {
						if (dcxmlhttp[id].readyState == 4 && dcxmlhttp[id].status == 200) {
							processingRequest(false);
							xvotesString[id] = dcxmlhttp[id].responseText;
							
							if (xvotesString[id].indexOf('Opss!') == -1) {
							
								if (value==1){
									good_button.src = url_design_images + "/goodcbutton_disabled.png";
									bad_c.href="javascript:cvote(" + user + "," + id + ",'" + md5 + "',0)";
									
								}else if(value==0){
									good_c.href="javascript:cvote(" + user + "," + id + ",'" + md5 + "',1)";
									bad_c.href="javascript:cvote(" + user + "," + id + ",'" + md5 + "',-1)";
									good_button.src = url_design_images + "/goodcbutton.png";
									bad_button.src = url_design_images + "/badcbutton.png";
									
								}else if(value==-1){
									good_c.href="javascript:cvote(" + user + "," + id + ",'" + md5 + "',0)";
									bad_button.src= url_design_images + "/badcbutton_disabled.png";
								}
							
								changecvotevalues (id, value, false);
							} else {
								alert(xvotesString[id]);
								changecvotevalues (id, value, true);
							}
						}
					}
				}
			}
		}
	}

/*
  Change Comment Vote Values
*/
	function changecvotevalues (id, value, error){
		split = new RegExp ("~--~");
		b = xvotesString[id].split (split);
		
		target1 = document.getElementById ('cvote-' + id);
		pvotes = document.getElementById ('pvbox' + id);
		nvotes = document.getElementById ('nvbox' + id);
		toggle_link = document.getElementById ('toggle_buried' + id);
		hidden_text = document.getElementById ('hidden_text' + id);
		comment_wrap = document.getElementById ('comment-wrap' + id);
		var showhide = document.getElementById ('toggle_buried_link' + id);
		var comment_wrap_class = comment_wrap.className;
		
		if (error) {
			return false;
		}
		
		if (value==1){
			if (hidden_text.innerHTML.indexOf('limiar') == -1){
				toggle_link.style.display = "none";
			}
			new_class = comment_wrap_class.replace("_buried", "");
			comment_wrap.className = new_class;
			showhide.innerHTML = DC_Comment_Minimize;
			
		}else if(value==0){
			if (hidden_text.innerHTML.indexOf('limiar') == -1){
				toggle_link.style.display = "none";
			}
			var new_class = comment_wrap_class.replace("_buried", "");
			comment_wrap.className = new_class;
			showhide.innerHTML = DC_Comment_Minimize;
			
		}else if(value==-1){
			toggle_link.style.display = 'inline-block';
			comment_wrap.className = comment_wrap_class + '_buried';
			showhide.innerHTML = DC_Comment_Maximize;
		}
		
		if (b.length <= 4) {
			
			if (b[0] >= 2 || b[0] == 0) {
				pointstext = DC_Comment_RatingPlural;
			} else {
				pointstext = DC_Comment_Rating;;
			}
			
			if(b[1] && b[2]){
				pvotes.innerHTML = "+" + b[1];
				nvotes.innerHTML = "-" + b[2];
			 }
			
			target1.innerHTML = "<strong>" + b[0] + "</strong> " + pointstext;
		}
		return false;
	}

/*
  Toggle Buried Comment
*/
	function toggle_cburied(id,good,bad) {
	
		var elem_id = document.getElementById ('comment-wrap' + id);
		var showhide = document.getElementById ('toggle_buried_link' + id);
		var class_name = elem_id.className;
		
		if (class_name != 'comment_wrap' && class_name != 'comment_wrap_friend' && class_name != 'comment_wrap_author') {
			new_class = class_name.replace("_buried", "");
			elem_id.className = new_class;
			showhide.innerHTML = DC_Comment_Minimize;
		} else {
			elem_id.className = class_name + '_buried';
			showhide.innerHTML = DC_Comment_Maximize;
		}
	}

/*
  Endorse a Site
*/
	function endorseSite(host,linkid) {
		
		processingRequest(true);
		url = thisurl + '/_AjaxRequest.php';
		new Ajax.Request(url, {
			parameters: {change: 'endorse', host: host},
			onSuccess: function(transport){
				var response = transport.responseText;
				processingRequest();
				//alert(response);
				if(response == 1){
					$('endorseSite'+linkid).className = 'endorsed';
				}
			}
		});
	}

