/*
*	Handle the namespace assignments for the Purple Sage package
*/

/*
*  	Create a namespace
*  	@param {String} ns The namespace to create. i.e. ps.test.namespace
*/

function in_package(ns)
{
	var base = window;
	var parts = ns.split(".");
	
	parts.each(function(i){
		if ($type(base[i]) != 'object'){
			base[i]={};
			base=base[i];
		} else {
			base=base[i];
		}
	});
}

/*
*	Analytics tracking for outgoing links
*/
/*var tagAllOutGoingLinks = function()
{
	var links = $$('a');
	var domain = document.domain;
	var redirect = domain+'/redirect/';

	for(var i=links.length-1; i>=0; i--)
	{		
		if ((links[i].href.indexOf(domain) == -1) && (links[i].href.indexOf('http://') != -1))
		{
			var e = links[i];
			if (e.onclick) return;			
			
			e.onclick=function(){
				var url = this.href.replace('http://', '');
				if (url.indexOf('javascript:') ==0) return;
				urchinTracker('/outgoing/'+url);
			};
		}
		
		//redirct
		if (links[i].href.indexOf(redirect) > -1)
		{			
			var e = links[i];
			
			//if (e.onclick) return;
			
			e.addEvent('click', function(){
			//e.onclick=function(){
				var url = this.href.replace('http://'+redirect, '');
				if (url.indexOf('?')>-1)
				{
					var parts = url.split('?');
					url = parts[0];
				}

				urchinTracker('/outgoing/'+url);
			});
		}
		
	}
}*/
//window.addEvent('domready', tagAllOutGoingLinks);

//Framebreak
//if (top.location != self.location) {
//	top.location = self.location.href
//}


// Set screen width cookie
if (!Cookie.get('sw')){
	Cookie.set('sw', window.getWidth(), {'path': '/'});
}

// Tag adnet links
function tagAdnetLinks()
{
	var verify = window._wr_verify;
	if (!verify) return;
	
	var links = $$('a[target=_blank]');
	
	var domain = document.domain;
	var adnet = domain+'/adnet/';

	for(var i=links.length-1; i>=0; i--)
	{
		var link = links[i];
		if (link.href.indexOf(adnet) >= 0) {
			
			var sep = '?';
			
			if (link.href.indexOf('?') >= 0) sep='&';
			
			link.href += sep + 'verify='+verify;
		}
	}
}

tagAdnetLinks();
//window.addEvent('domready', tagAdnetLinks);

var COMMENT_FORM_MODE_COMMENT = 'COMMENT';
var COMMENT_FORM_MODE_REVIEW = 'REVIEW';
var COMMENT_FORM_MODE_QUESTION = 'QUESTION';

var CommentForm = {
	
	init : function()
	{
		$('comment_blah').value = $('comment_verify').value;
		
		this.form = $('comments-form');
		
		this.submit = $E('input[type=submit]', this.form);
		this.rows = [];
		this.rows[0] = $E('tr.comments-title-row', this.form);
		this.rows[1] = $E('tr.comments-review-row', this.form);
		this.rows[2] = $E('tr.comments-rating-row', this.form);
		
		for(var i=0; i<this.form.comment_type.length;i++) {
			$(this.form.comment_type[i]).addEvent('click', this.updateMode.bind(this));
		}
		
		this.updateMode();
	},
	
	updateMode : function()
	{
		var mode = COMMENT_FORM_MODE_COMMENT;
		
		var radios = $ES('input[type=radio]', this.form);
		
		for(var i=0; i<radios.length;i++) {
			if (radios[i].checked) mode = radios[i].value;
		}
								
		switch(mode)
		{
			case COMMENT_FORM_MODE_REVIEW:
				this.setModeReview();
				break;
			case COMMENT_FORM_MODE_QUESTION:
				this.setModeQuestion();
				break;	
			default:
				this.setModeComment();
				break;
		}
	},
	
	setModeComment : function()
	{
		this.rows[0].style.display = 'none';
		this.rows[2].style.display = 'none';
		
		this.rows[1].getElement('th').innerHTML = 'Your comment';
		this.submit.value = "Post comment";
	},
	
	setModeReview : function()
	{
		this.rows[0].style.display = '';
		this.rows[2].style.display = '';
		
		this.rows[0].getElement('th').innerHTML = 'Review title';
		this.rows[1].getElement('th').innerHTML = 'Your review';
		this.rows[0].getElement('div.faint').innerHTML = 'E.g. "A great day out!" or "Dont bother going."';
		this.submit.value = "Post review";
	},
	
	setModeQuestion : function()
	{
		this.rows[0].style.display = '';
		this.rows[2].style.display = 'none';
		
		this.rows[0].getElement('th').innerHTML = 'Question';
		this.rows[1].getElement('th').innerHTML = 'Question detail';
		this.rows[0].getElement('div.faint').innerHTML = 'E.g. "Where are the best hotels in paris?"';
		this.submit.value = "Post question";
	}
	
}