// Add site spesific javascript here

/**
 * Configuration for site components javascript
 *
 * Configuration array structure:
 * {<component>: {<parameter>: value, <parameter>: value, ... }, ...}
 */
var siteComponentsConfig = {
    'tooltip': {
        'positionby': 'element' //Valid values: mouse (default), element
     },

    'keywords': {
        'elements': ['placeholder-content'],
        'skiptags': ['h1','h2','h3','h4','h5','h6'],
        'usetooltip': true
    }
};


function entitycommentToggleCommentForm(id) {
	if ($('entitycomment-formwrapper-'+id) != null) {

		$('entitycomment-formwrapper-'+id).toggle();
		$('entitycomment-textarea-'+id).toggle();
	
		if (!$('entitycomment-textarea-'+id).visible()) {
			$('textarea-'+id).focus();
		}
	}
}


function entityCommentSubmit() {

    // Submitting entity comment form
    $$('form.entitycomment-form').each(function(element) {
    	element.observe('submit', function(event) {
			event.stop();
		    
		    var serializedParams = element.serialize(true);
		    serializedParams['jsverification'] = 'ok';
		    
		    elementID = element.identify();
		    elementlength = elementID.length;
		    elementIDnumber = elementID.substring(elementlength-1, elementlength);
		    
		    $$('div.entitycomment-form-submit input.submit').each(function(submitbutton) {
		    	submitbutton.writeAttribute("value", "Sender...");
		    });
		    
		    new Ajax.Request('/xmlhttprequest.php',
		    	{parameters: serializedParams,
		         onSuccess: function(transport) {
		         	$('entitycomment-tilewrapper').update(transport.responseText);
		         	// Make sure listeners are added again
		         	entityCommentSubmit();
		         	entityRatingSubmit('entitycomment-tilewrapper');
		         	
		         	$$('div.entitycomment-form-submit input.submit').each(function(submitbutton) {
		    			submitbutton.writeAttribute("value", "Send");
		    		});
		         }, 
			     onFailure: function(transport) {
			     	$('message-'+elementIDnumber).update(transport.responseText);
			     	
			     	$$('div.entitycomment-form-submit input.submit').each(function(submitbutton) {
		    			submitbutton.writeAttribute("value", "Send");
		    		});
			     }
			});
		    
		    
		    
		    
		});
    });
}


function entityRatingSubmit(rootelementID) {
	
	if (rootelementID != null) {
		elementsToSearch = $(rootelementID).getElementsBySelector('div.rating-container');
	} else {
		elementsToSearch = $$('div.rating-container');
	}
	
    // Submitting entity rating
    elementsToSearch.each(function(element) {
    	element.serializedParams = element.firstDescendant().serialize(true);
    	element.serializedParams['jsverification'] = 'ok';
    	element.result = element.down('span.rating-result');
    	
    	element.update('<a href="#recommend-this" class="rating">' + element.result.innerHTML + '</a>').observe('click', function(event) {
    		event.stop();
    		
    		if (!element.hasClassName('rating-container-disabled')) {
    			element.pulsate({ pulses: 1, duration: 0.8 });
    			element.addClassName('rating-container-disabled');
    		}
    		
    		element.resultamount = element.down('span.rating-result-amount');

			// Send request and update amount    		
    		new Ajax.Request('/xmlhttprequest.php',
    			{parameters: element.serializedParams,
    			 onSuccess: function(transport) {
    			 	element.resultamount.update(transport.responseText);
    			 	
    			 	if (transport.responseText == "1") {
    			 		element.resultamount.next().update("anbefaling");
    			 	} else {
    			 		element.resultamount.next().update("anbefalinger");
    			 	}
    			 }
			});
    	});
    });
    
}


document.observe('dom:loaded', function(event) {

    $$('div.rating-container').each(function(element) {
    	
    	var entityId = element.select('[name="entity_id"]').first().readAttribute('value');
    	var entityType = element.select('[name="entity_type"]').first().readAttribute('value');
    	
    	// Make sure that disabled-class is set even though cache is used
    	new Ajax.Request('/xmlhttprequest.php',
    			{parameters: {service: 'entity.hasratedcheck', entity_id: entityId, entity_type: entityType},
                 onSuccess: function(transport) {
    				if (transport.responseText == '1') {
    					if (!element.hasClassName('rating-container-disabled')) {
    						element.addClassName('rating-container-disabled');
    					}
					}
                 }
    			});
    });
	
	
	entityCommentSubmit();
    entityRatingSubmit(null);

        
    // Submitting entity rating (agree/disagree)
    $$('div.agreedisagree-rating-container').each(function(element) {
    	element.serializedParams = element.firstDescendant().serialize(true);
    	element.serializedParams['jsverification'] = 'ok';
    	
    	var entityId = element.select('[name="entity_id"]').first().readAttribute('value');
    	var entityType = element.select('[name="entity_type"]').first().readAttribute('value');
    	
    	// Get buttons from form and get the HTML of the amount element
    	element.actionlinks = element.select('button');    	
		var agreeAmountElementHTML = element.actionlinks[0].next().down().innerHTML;
    	var disagreeAmountElementHTML = element.actionlinks[1].next().down().innerHTML;

		// Replace the old content of agreedisagree-rating-container with the new link elements
    	element.update();
    	element.insert('<a href="#agree" class="agreedisagree-rating">Ja, enig (<span>' + agreeAmountElementHTML + '</span>)</a><a href="#disagree" class="agreedisagree-rating">Nei, uenig (<span>' + disagreeAmountElementHTML + '</span>)</a>');
    	
    	// Action for clicking "agree"
    	element.down().observe('click', function(event) {
    		event.stop();
    		element.serializedParams['rating'] = element.actionlinks[0].value;
    		
    		new Ajax.Request('/xmlhttprequest.php',
    			{parameters: element.serializedParams,
    			 onSuccess: function(transport) {
    			 	element.down().down('span').update(transport.responseText);
    			 }
			});
			
			element.addClassName('agreedisagree-rating-container-disabled');
    	});
    	
    	// Action for clicking "disagree"
    	element.down().next().observe('click', function(event) {
    		event.stop();
    		element.serializedParams['rating'] = element.actionlinks[1].value;
    		
    		new Ajax.Request('/xmlhttprequest.php',
    			{parameters: element.serializedParams,
    			 onSuccess: function(transport) {
    			 	element.down().next().down('span').update(transport.responseText);
    			 }
			});
			
			element.addClassName('agreedisagree-rating-container-disabled');
    	});
    	
    	
    	// Make sure that disabled-class is set even though cache is used
    	new Ajax.Request('/xmlhttprequest.php',
    			{parameters: {service: 'entity.hasratedcheck', entity_id: entityId, entity_type: entityType},
                 onSuccess: function(transport) {
    				if (transport.responseText == '1') {
    					if (!element.hasClassName('agreedisagree-rating-container-disabled')) {
    						element.addClassName('agreedisagree-rating-container-disabled');
    					}
					}
                 }
    			});
    });
    
    // Toggle tabs in popularity articlelist
    $$('a.popularity-tiletabs').each(function(element) {
    	element.observe('click', function(event) {
    		event.stop();
    		
    		// Hide all lists
    		$$('ul.articlelist-togglable').each(function(ulelement) {
    			ulelement.hide();
    		});
    		
    		// Show the list that's been clicked
    		elementID = element.identify();
    		listtype = elementID.substring(5, elementID.length);
    		$('articlelist-'+listtype).show();
    		
    		// Set new classname for selected link
    		element.siblings().each(function(sibling) {
    			sibling.removeClassName('selected');
    		});
    		element.addClassName('selected');
    	
    	});
    });
   	
   	// Observe click event for suggestion form
   	$$('a.emailpage-link').each(function(element) {
   		element.observe('click', function(event) {
   			event.stop();
   			$('emailpage').toggle();
   		});
   	});
    
    // Observe click event for suggestion form
   	$$('#emailpage a.close').each(function(element) {
   		element.observe('click', function(event) {
   			event.stop();
   			$('emailpage').hide();
   		});
   	}); 	
   	
   	// Hover effects for full articlelist headers
   	$$('div.full-articlelist h3 span').each(function(element) {
   		element.up().descendants().each(function(spanelement) {
   	
	   		spanelement.observe('mouseover', function(event) {
	   			event.stop();
	   			element.toggleClassName('hover');
	   		});
	   		
	   		spanelement.observe('mouseout', function(event) {
	   			event.stop();
	   			element.toggleClassName('hover');
	   		});
   		
   		});
   		
   	});
   	
   	
    
});


