// VERSION 5.1.1
var $j = jQuery.noConflict();


/**
 * The <b>base</b> creative agency object with loads of stuff
 */
if (typeof Base == 'undefined') {
	var Base = {};
}

/**
 * Add general javascript to the page
 */
Base.javascriptify = function(){
	// add js class to the body, so we can easily tell us the user has js enabled with css
	$j("body").addClass('js');
	
	// make input boxes with class "blurValue" clear their default value if focused
	$j("input.blurValue")
		.focus(function() {
			if (this.value == this.defaultValue) {	this.value = ""; }
		})
		.blur(function() {
			if (this.value == "") { this.value = this.defaultValue; }
		}); 
		
	// zebra tables
	//$j(".stripeMe tr").removeClass('alt');
	$j(".stripeMe tr:not(.prev-has-children):odd").addClass("alt");
	
//	$j(".stripeMe tr:odd").addClass("alt");
	$j(".stripeMe tr").mouseover(function() {$j(this).addClass("over");}).mouseout(function() {$j(this).removeClass("over");});

	// show/hide things
	$j('.collapseNext, .toggleNext').append(' <a href="javascript:void(0)" title="Show / Hide">&raquo;</a>').click(function(){
				$(this).next().slideToggle();
	});	
	$j('.collapseNext').next().hide();
	
	if (typeof $j().cycle != 'undefined') {
		$j('.cycleArticle').cycle({ 
		    fx:     'fade', 
		    speed:   300, 
		    timeout: 0, 
			pagerActiveClass: 'selected',
			pager:  '#article-image .pagination',
			pagerAnchorBuilder: function(idx, slide) { 
		        return '<span><a href="javascript:void(0)">' + slide.title + '</a> </span>'; 
		    }   
		});
	}
	
};

Base.Url = {
	/**
	 * Add key=value to a url. Returns baseUrl + ?/& + queryString 
	 * @param string queryString
	 * @param string(optional) baseUrl. Default document.location
	 */
	appendQueryString : function (queryString, baseUrl) {
		if (baseUrl === undefined) {
			baseUrl = document.location.toString().split('#')[0]; // remove hash
		}
		separator = '?';
		if (baseUrl.indexOf('?') !== -1) {
			separator = '&';
		}
		
		var ret = baseUrl + separator + queryString; // this url may contain ?a=b&a=c, which works ok, but looks wrong
		
		// check for repeating keys
		if (ret.indexOf('[') == -1) { // dont do this if there's an array, eg: ?a[]=1&a[]=2&a[]=5
			var args = ret.split('?')[1].split('&');
			var url = ret.split('?')[0] + '?'; // example.com/path/file.html?
			var newArgs = {}; // hash of args
			var newQuery = ''; // new string query that will come from args
			// loop through parts, saving keys and values
			for (var i = 0; i < args.length; i++) {
				var pair = args[i].split('=');
				newArgs[pair[0]] = pair[1]; // this will overwrite old ones
			}
			
			// build the new query string
			for (arg in newArgs) {
				newQuery += arg + '=' + newArgs[arg] + '&';
			}
			ret = url + newQuery.slice(0, -1); // chop  last &;
		}
		return ret;
	},
	/**
	 * Get an object of $_GET parameters and values or a specific variable if supplied
	 * @param String variable(optional)
	 */
	query : function (variable) {
		if (!this.params) {
			this.params = {}; // chache it!
			var args = document.location.search.substring(1).replace(/\+/g, ' ').split('&'); // replace '+' with ' '
			for (var i = 0; i < args.length; i++) {
				var pair = args[i].split('=');
				this.params[decodeURIComponent(pair[0])] = decodeURIComponent(pair[1]);
			}
		}
		return variable ? this.params[variable] : this.params;
	}
};


Base.Flash = {
	videoPlayerOptions: {},
	video: function(jwo) { 
		var so = new SWFObject(jwo.baseUrl + jwo.playerUrl, 'mpl', jwo.width, (jwo.height + jwo.skinHeight), '9');
		so.addParam('allowscriptaccess','always');
		so.addParam('allowfullscreen','true');
		so.addParam('flashvars','&image=' + jwo.videoBase + jwo.thumb + '&skin=' + jwo.baseUrl + '/flash/jw-skin-' + jwo.skin + '.swf&abouttext=About&aboutlink=' + jwo.videoBase + '/about/&file=' + jwo.videoBase + jwo.video + '&screencolor=000000&controlbar=bottom');
		so.write(jwo.div);
	}	
}


//email-a-friend function
function emailPage(URLStr, left, top, width, height) {
/********************************************
DESCRIPTION: emails a friend
DATE ADDED: 10th November 2007
*********************************************/
	popUpWin = open(URLStr, 'popUpWin', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbar=no,resizable=no,copyhistory=yes,width='+width+',height='+height+',left='+left+', top='+top+',screenX='+left+',screenY='+top+'');
}

function emailPageCheck(emailOne, emailTwo) {
/********************************************
DESCRIPTION: email-a-page-to-a-friend check
DATE ADDED: 10th November 2007
*********************************************/

	if (!emailValid(emailOne)) {
		return false;
	}
	if (!emailValid(emailTwo)) {
		return false;
	}
	return true;
}

function emailValid(email) {
/********************************************
DESCRIPTION: email address validator
DATE ADDED: 10th November 2007
*********************************************/
	
	// email validation
	var field = email; // email field
  	var str = field.value; // email string
  	var reg1 = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/; // not valid
  	var reg2 = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/; // valid (doesnt validate emails with long tld, e.g. .name)
  	if (!reg1.test(str) && reg2.test(str)) { // if syntax is valid
		return true;
  	}
  	else {
		alert("Please enter a valid email address."); 
		field.focus();
		field.select();
		return false;
  	}
}

function deleteItem(ID) {
/********************************************
DESCRIPTION: updates action and submits page
DATE ADDED: 5th April 2007
DATE UPDATED: -
*********************************************/
	
	document.frm_item.a.value = 3; //delete
	document.frm_item.step.value = 1; //wizard start
	document.frm_item.remove.value = ID;
	if (confirm("You are about to delete this item.\n\nAre you sure you want to continue?")) {
		document.frm_item.submit();
	}
}

function doItem(action) {
/********************************************
DESCRIPTION: updates action and submits page
DATE ADDED: 5th April 2007
DATE UPDATED: -
*********************************************/
	
	document.frm_item.a.value = action;
	document.frm_item.step.value = 1;
	document.frm_item.submit();
}

function selectItem(pID, ID, nID, stepID) {
/********************************************
DESCRIPTION: updates action and submits page
DATE ADDED: 6th April 2007
DATE UPDATED: -
*********************************************/
	document.frm_item.prev_id.value = pID;
	document.frm_item.item_id.value = ID;
	document.frm_item.next_id.value = nID;
	document.frm_item.step.value = stepID;
	document.frm_item.submit();
}

function selectItemExtra(pID, ID, nID, stepID, extraID) {
/********************************************
DESCRIPTION: updates action and submits page
DATE ADDED: 6th April 2007
DATE UPDATED: -
*********************************************/
	document.frm_item.prev_id.value = pID;
	document.frm_item.item_id.value = ID;
	document.frm_item.next_id.value = nID;
	document.frm_item.step.value = stepID;
	document.frm_item.extra_id.value = extraID;
	document.frm_item.submit();
}



// DOM ready
$j(function() {
	Base.javascriptify();
});

