
// Standard object
var weObj = new Object();

// Reg array
weObj.registry = new Array(10);

/**
 * Update layer with an AJAX request
 *
 * @param  {string}		section		active tab
 * @param  {string}		divItem		layer id
 * @param  {string}		divList		selected element
 * @param  {string}		uri			uri
 * @param  {string}		flv			flash video url
 * @param  {mixed}		optional	mixed
 * @param  {string}		anchor		HTML anchor
 * @param  {boolean}	retValue	return value
 * @return {boolean}
 */
weObj.getContent = function(section, divItem, divList, uri, flv, optional, anchor, retValue) {

	// get layer and list element
	eleItem = document.getElementById(divItem);
	eleList = (typeof divList == 'string') ? document.getElementById(divList) : false;

	// store last uri
	prev_uri = this.get('curr_uri');
	if (typeof uri == 'string' && eleItem.id == 'content_item') {
		this.set('curr_uri', uri);
	}

	// optinal stuff (if defined)
	if (typeof optional == 'object') {

		switch (optional[0]) {

			case 'eleList':
				if (this.get('eleListActiveId') != null) {
					this.get('eleListActiveId').className = '';
				}
				document.getElementById(optional[1]).className = 'active';
				this.set('eleListActiveId', document.getElementById(optional[1]));

				if (typeof optional[2] != undefined) {
					if (false !== weObj.get('eleListParActiveId') && typeof weObj.get('eleListParActiveId') == 'object')
						weObj.get('eleListParActiveId').className = '';

					if (false !== optional[2]) {
						var ele = document.getElementById(optional[2]);
						if (ele !== null)  {
							ele.className = 'active';
							weObj.set('eleListParActiveId', ele);
						}
					}
				}
				break;

			case 'eleAwardList':
				if (this.get('eleListAwardActiveId') != null)
					this.get('eleListAwardActiveId').className = this.get('eleListAwardActiveId').className.replace('active', '');
				document.getElementById(optional[1]).className += ' active';
				this.set('eleListAwardActiveId', document.getElementById(optional[1]));
				break;

			case 'blogList':

				break;

			default:
				alert('Page error\r\n - Requested content missing\r\n\r\nPlease try again later');
		}
	}

	// callback setup for request
	var callback = {
		success:AjaxObject.handleSuccess,
		failure:AjaxObject.handleFailure,
		scope:AjaxObject,
		timeout:30000,
		argument:{ eleItem:eleItem, eleList:eleList, section:section, flv:flv, uri:uri }
	};

	// run call
	AjaxObject.startRequest('GET', uri, callback);

	// if not defined set return to false to prevent a tags
	if (typeof retValue == 'undefined') {
		retValue = false;
		// update address bar, remove page name
		location.href = '#' + uri.substring(uri.substring(1).indexOf('/') + 1);
	}

	return retValue;
}

/**
 * Post data within the object
 *
 * @param  {object}		eleForm		form id
 * @param  {string}		divItem		layer id
 * @param  {string}		uri			form action
 * @param  {boolean}	retValue	return value
 * @return {boolean/viod}
 */
weObj.postData = function(eleForm, divItem, uri, retValue, optional) {
	var postData = '';
	var tmpEle = null;

	if (typeof optional == 'object') {
		if (optional[0] == 'search') {
			document.getElementById('search').style.backgroundImage = 'url(/images/loading.gif)';
			if (document.getElementById('search2'))
				document.getElementById('search2').style.backgroundImage = 'url(/images/loading.gif)';
		}
	}

	// get layer and list element
	eleItem = document.getElementById(divItem);

	// if posting to content item flv needs to reload
	flv = (eleItem.id == 'content_item') ? this.get('curr_flv') : null;

	// collect data from form
	for (n = 0; n < eleForm.elements.length; n++ ) {
		if (eleForm.elements[n].type == 'checkbox') {
			if (eleForm.elements[n].checked)
				postData += eleForm.elements[n].name+'='+eleForm.elements[n].value+'&';
		}
		else if (eleForm.elements[n].type != 'submit' && eleForm.elements[n].value.length > 0)
			postData += eleForm.elements[n].name+'='+eleForm.elements[n].value+'&';
	}

	// callback setup for request
	var callback = {
		success:AjaxObject.handleSuccess,
		failure:AjaxObject.handleFailure,
		scope:AjaxObject,
		timeout:30000,
		argument:{ section:'post', eleItem:eleItem, flv:flv }
	};

	// run call
	AjaxObject.startRequest('POST', uri, callback, postData);

	// if defined return boolean value
	if (typeof retValue != 'undefined') {
		return retValue;
	}
}

/**
 * Stores a value within the object
 *
 * @param  {string}		name
 * @param  {mixed}		value
 */
weObj.set = function(name, value) {
	this.registry[name] = value;
}

/**
 * Gets a value within the object, returns false if not set
 *
 * @param  {string}		name
 * @param  {mixed}		value
 */
weObj.get = function(name) {
	retValue = false;

	if (typeof this.registry[name] != 'undefined') {
		retValue = this.registry[name];
	}

	return retValue;
}

/**
 * manully remove show & page var from uri and add new values
 *
 * access  {private}
 * @param  {string}		uri
 * @param  {string}		value
 * @return {string}
 */
weObj.contLink = function(uri, value) {
	if (this.get('umw') == true) {
		// remove
		uri = uri.replace("s/sendtofriend/", "");
		uri = uri.replace("s/comment/", "");
		uri = uri.replace("s/register/", "");
		uri = uri.replace("s/forgotpassword/", "");
		uri = uri.replace("s//", "");

		if (uri.indexOf('compage') >= 0) {
			// page always last in uri
			uri = uri.substring(0, uri.indexOf('compage'));
		}

		// add new value
		uri += "s/" + value + "/";
	}
	else {
		// remove
		uri = uri.replace("&show=sendtofriend", "");
		uri = uri.replace("&show=comment", "");
		uri = uri.replace("&show=register", "");
		uri = uri.replace("&show=forgotpassword", "");
		uri = uri.replace("&show=", "");

		if (uri.indexOf('&page=') >= 0) {
			// page always last in uri
			uri = uri.substring(0, uri.indexOf('&page='));
		}

		// add new value
		uri += "&show=" + value;
	}
	return uri;
}

/**
 * Rewrites url from hash value to nice url
 *
 * @return {string}	new url
 */
weObj.rewrite_hashURL = function() {
	// get page url from current url
	var hrefURL = location.href.substring(0, location.href.indexOf('#')).replace('http://', '');

	hrefURL = hrefURL.substring(hrefURL.indexOf('/'));

	hrefURL = hrefURL.substring(0, hrefURL.substring(1).indexOf('/') + 1);

	// glue with hash value
	var newURL = hrefURL + location.hash.slice(1);

	// fixes bug with on root of page ie. /news/
	if ( hrefURL == '/' )
		newURL = location.pathname;

	return newURL;
}

if (document.images) {
	/*
	// menu bars
	var img_bar1 = new Image();
	img_bar1.src = '/images/bar_yellow.gif';
	var img_bar2 = new Image();
	img_bar2.src = '/images/bar_blue.gif';
	var img_bar3 = new Image();
	img_bar3.src = '/images/bar_pink.gif';
	var img_bar4 = new Image();
	img_bar4.src = '/images/bar_purple.gif';
	var img_bar5 = new Image();
	img_bar5.src = '/images/bar_turquoise.gif';
	var img_bar6 = new Image();
	img_bar6.src = '/images/bar_steelblue.gif';

	// backgrounds/content
	var img_b1 = new Image();
	img_b1.src = '/images/background.gif';
	var img_b2 = new Image();
	img_b2.src = '/images/content_background2.png';
	var img_b3 = new Image();
	img_b3.src = '/images/content_bottom.gif';
	var img_b4 = new Image();
	img_b4.src = '/images/content_background.png';

	// buttons/widgets
	var img_bw1 = new Image();
	img_bw1.src = '/images/btn_sprite.gif';
	var img_bw2 = new Image();
	img_bw2.src = '/images/btn_sm_sprite.gif';
	var img_bw3 = new Image();
	img_bw3.src = '/images/btn_sm_sprite.gif';
	var img_bw4 = new Image();
	img_bw4.src = '/images/btn_sm_sprite_wider.gif';
	var img_bw5 = new Image();
	img_bw5.src = '/images/wn_t.gif';
	var img_bw6 = new Image();
	img_bw6.src = '/images/wn_b.gif';
	var img_bw7 = new Image();
	img_bw7.src = '/images/wn_w.gif';
	var img_bw8 = new Image();
	img_bw8.src = '/images/wn_g.gif';

	// headers/custom
	var img_hc1 = new Image();
	img_hc1.src = '/images/blog_header.gif';
	var img_hc2 = new Image();
	img_hc2.src = '/images/press_header.gif';
	var img_hc3 = new Image();
	img_hc3.src = '/images/channels-CC9933.gif';
	var img_hc4 = new Image();
	img_hc4.src = '/images/featured-79005C.gif';
	var img_hc5 = new Image();
	img_hc5.src = '/images/videos_head.gif';
	var img_hc6 = new Image();
	img_hc6.src = '/images/weeknews_bg.gif';
	var img_hc7 = new Image();
	img_hc7.src = '/images/whats_new_bg.gif';*/

}
