
$(window).ready(function(){
	$('#top_bar #search_form input').focus(function(){
		var id = $(this).attr('id');
		var label = $('#top_bar #search_form label[for=' + id + ']');

		if(label.length > 0){
			$(label).hide();
		}
	});

	$('#top_bar #search_form input').blur(function(){
		if($(this).val() == ''){
			var id = $(this).attr('id');
			var label = $('#top_bar #search_form label[for=' + id + ']');

			if(label.length > 0){
				$(label).show();
			}
		}
	});
});


function isInt(num){
	return parseInt(num, 10) == num;
}

function isArray(obj){
	if(obj.constructor.toString().indexOf("Array") == -1){
		return false;
	}else{
		return true;
	}
}

function isObject(object){
	return (typeof object == 'object') && (object !== null);
}

function isButton(object){
	return ((object.type != undefined) && (object.type == 'button'));
}
/**
 * Returns an object by its Id
 */
function returnObjById(id){
	return $('#' + id);
}

/**
 * Returns a list of elements by their class name
 */
function getElementsByClassName(classname, node){
	if(isObject(node) || isObject(node = returnObjById(node))){
		return $(node).find('.' + classname);
	}else{
		return $('.' + classname);
	}
}

/**
 * checks if a regular expression is found in the given string
 */
function regExpMatch(regExp, mods, string){
	var re = new RegExp(regExp, mods);
	return re.test(string);
}

/**
 * carries out a regular expression replace
 */
function regReplace(regExp, replace, string){
	return string.replace(regExp, replace);
}

/**
 * Searches an array for a given value.
 * returns true if found otherwise false
 */
function inArray(array, value){
	if(isArray(array)){
		for(var i in array){
			if(array[i] === value){
				return true;
			}
		}
	}
	return false;
};

/**
 * removes an object from the DOM
 */
function removeObject(id){
	var object = $('#' . id);
	if(object.length > 0){
		$(object).remove();
		return true;
	}else{
		return false;
	}
}

function displayObject(id, visible){
	var object = $('#' . id);
	if(object.length > 0){
		if(visible){
			$(object).show();
		}else{
			$(object).hide();
		}
		return true;
	}else{
		return false;
	}
}


/** Disables a form input */
// TODO - check this. Only used on ac/discounts.php
function disableInput(id, value){
	/*var inputObject = returnObjById(id);
	if(isObject(inputObject)){
		if(value != null){
			value = (value == true) ? true : false;
			inputObject.disabled = value;
		}else{
			inputObject.disabled = inputObject.disabled ? false : true;
		}
	}*/

	var inputObject = returnObjById(id);
	if(isObject(inputObject)){
		if(value != null){
			value = (value == true) ? true : false;
			$(inputObject).attr('disabled', value);
		}else{
			$(inputObject).attr('disabled', !$(inputObject).attr('disabled'));
		}
	}
}


// TODO - test this. only used in scripts/js_bbcode.php
function getInputData(inputId){
	var inputObject = returnObjById(inputId);
	if(isObject(inputObject)){
		return $(inputObject).val();
	}
	return false;
}


/** Returns the label for a form element */
// TODO -test this. Only used in function below
function getFormLabel(formElement){
	/*if(isObject(formElement.parentNode)){
		if(formElement.parentNode.tagName == 'label'){
			return formElement.parentNode;
		}
	}

	var labels = document.getElementsByTagName('label');
	for(var i = 0; i < labels.length; i++){
		if(labels[i].htmlFor == formElement.id){
			return labels[i];
		}
	}
	return false;*/

	var label = $('label[for=' + $(formElement).attr('id') + ']');
	if(label.length > 0){
		return label;
	}else{
		var label = $(formElement).parent('label');
		return (label.length > 0) ? label : false;
	}
}


/** Creates a text input */
// TODO - test this, used multiple times
function createInput(id, name, value, parent){
	function appendToParent(parent){
		document.body.appendChild(this.newInp);
		return document.getElementById(this.newInp.id);
	}
	
	this.newInp = document.createElement('input');
	this.newInp.setAttribute('id', id);
	this.newInp.setAttribute('name', name);
	this.newInp.setAttribute('value', value);
	this.append = appendToParent;
	
	if(parent != undefined){
		return this.append(parent);
	}else{
		return this.newInp;
	}
}


/** Creates a text area */
function createTextArea(id, name, value, cols, rows, parent){
	function appendToParent(parent){
		document.body.appendChild(this.newInp);
		return document.getElementById(this.newInp.id);
	}
	
	cols = isInt(cols) ? cols : 30;
	rows = isInt(rows) ? rows : 5;
	
	this.newInp = document.createElement('textarea');
	this.newInp.setAttribute('id', id);
	this.newInp.setAttribute('name', name);
	this.newInp.setAttribute('value', value);
	this.newInp.setAttribute('cols', cols);
	this.newInp.setAttribute('rows', rows);
	this.append = appendToParent;
	
	if(parent != undefined){
		return this.append(parent);
	}else{
		return this.newInp;
	}
}


/**
 * The displayLabel class allows the displaying and/or
 * hiding of a form label
 */
function displayLabel(formElement, focus){
	function hideLabel(){
		if(isObject(this.label)){
			$(this.label).css('visibility', 'hidden');
			return true;
		}
		return false;
	}
	
	function showLabel(){
		if(isObject(this.label)){
			$(this.label).css('visibility', 'visible');
			return true;
		}
		return false;
	}
	
	this.label = getFormLabel(formElement);
	this.showLabel = showLabel;
	this.hideLabel = hideLabel;
	
	if(focus){
		this.hideLabel();
	}else{
		if(isObject(formElement) && ($(formElement).val() != undefined)){
			if($(formElement).val() == ''){
				this.showLabel();
			}else{
				this.hideLabel();
			}
		}
	}
}


/** Disables or enables all input field within a form */
function disableForm(formId, disable){
	function disableItems(formId, disable){
		var formObj = returnObjById(formId);
		if(isObject(formObj)){
			$(formObj).find('input, textarea, select').attr('disabled', disable);
			return true;
		}
		return false;
	}
	
	disable = disable ? true : false;
	return disableItems(formId, disable);
}


/** Checks or unchecks all checkboxes in a parent object */
function changeAllChecks(value, pId){
	value = value ? true : false;
	var pObj = returnObjById(pId);
	if(isObject(pObj)){
		var chks = pObj.getElementsByTagName("input");
		for(box in chks){
			if((chks[box].type != undefined) && (chks[box].type == 'checkbox')){
				chks[box].checked = value;
			}
		}
		return true;
	}
	return false;
}


/** Checks a radio button */
function checkRadio(objId){
	var object = returnObjById(objId);
	if(isObject(object)){
		if((object.type != undefined) && (object.type == 'radio')){
			object.checked = true;
		}
	}
	return false;
}
function creatediv(id, html, width, height, percent, left, top, right, bottom){
	function setWidth(width){
		width = isInt(width) ? width : 100;
		$(this.newDiv).css('width', width + this.percent);
	}
	
	function setHeight(height){
		height = isInt(height) ? height : 100;
		$(this.newDiv).css('height', height + this.percent);
	}
	
	function setPosition(left, top, right, bottom){
		if(isInt(left) || isInt(top)){
			$(this.newDiv).css('position', 'absolute');
			
			if(isInt(left)){
				$(this.newDiv).css('left', left + 'px');
			}
			if(isInt(top)){
				$(this.newDiv).css('top', top + 'px');
			}
		}else if(isInt(right) || isInt(bottom)){
			$(this.newDiv).css('position', 'absolute');

			if(isInt(right)){
				$(this.newDiv).css('right', right + 'px');
			}
			if(isInt(bottom)){
				$(this.newDiv).css('bottom', bottom + 'px');
			}
		}
	}
	
	function setHTML(html){
		if((html != undefined) && (html != null)){
			$(this.newDiv).html(html);
		}else{
			$(this.newDiv).html('');
		}
	}
	
	function create(html, width, height, left, top, right, bottom){
		this.setWidth(width);
		this.setHeight(height);
		this.setPosition(left, top, right, bottom);
		this.setHTML(html);
		
		$('body').append(this.newDiv);
		return returnObjById($(this.newDiv).attr('id'));
	}
	
	this.newDiv = $('<div>');
	$(this.newDiv).attr('id', id);
	
	this.percent = percent ? '%' : 'px';
	this.setWidth = setWidth;
	this.setHeight = setHeight;
	this.setPosition = setPosition;
	this.setHTML = setHTML;
	this.create = create;
	return this.create(html, width, height, left, top, right, bottom);
}

function changeImage(imgObject, url, width, height){
	var object = returnObjById(imgObject);
	if(isObject(object)){
		$(object).attr('src', url);
		
		if((width != undefined) && isInt(width)){
			$(object).attr('width', width);
		}
		if((height != undefined) && isInt(height)){
			$(object).attr('height', height);
		}
	}
}


function userViewport(){
	function getWidth(){
		if(typeof window.innerWidth != 'undefined'){
			return window.innerWidth;
		}else if((typeof document.documentElement != 'undefined') && (typeof document.documentElement.clientWidth != 'undefined')){
			return document.documentElement.clientWidth;
		}else{
			return NULL;
		}
	}
	
	function getHeight(){
		if(typeof window.innerHeight != 'undefined'){
			return window.innerHeight;
		}else if((typeof document.documentElement != 'undefined') && (typeof document.documentElement.clientHeight != 'undefined')){
			return document.documentElement.clientHeight;
		}else{
			return NULL;
		}
	}
	
	function getXOffset(){
		if(typeof window.pageXOffset != 'undefined'){
			return window.pageXOffset;
		}else if(typeof document.body.scrollLeft != 'undefined'){
			return document.body.scrollLeft;
		}else{
			return NULL;
		}
	}
	
	function getYOffset(){
		if(typeof window.pageYOffset != 'undefined'){
			return window.pageYOffset;
		}else if(typeof document.body.scrollTop != 'undefined'){
			return document.body.scrollTop;
		}else{
			return NULL;
		}
	}
	
	this.width = getWidth;
	this.height = getHeight;
	this.xOffset = getXOffset;
	this.yOffset = getYOffset;
}

/**
 * Copyright Gimp Productions - gimppro.co.uk
 * This page cannot be used without permission
 *
 * Author: Gimp Productions
 * Date Created: 05/05/11 23:29
 */

$(window).ready(function(){
	$('#menu a').click(function(){
		var sub = $(this).siblings('.sub');
		if(sub.length > 0){
			if($(sub).is(':visible')){
				$(sub).stop().slideUp('fast');
			}else{
				$('#menu .sub').stop().slideUp('fast');
				$(sub).stop().slideDown('slow');
			}

			return false;
		}
	});
});/**
 * Copyright Gimp Productions - gimppro.co.uk
 * This page cannot be used without permission
 *
 * Author: Gimp Productions
 * Date Created: 05/05/11 23:40
 */

function bbCodeExtra(inputId, textStart, textEnd){
	return textStart + getInputData(inputId) + textEnd;
}

function bbCodeDisplay(textObject, textStart, textEnd, textMiddle, alertText){
	function addText(){
		if(isObject(this.textObject)){
			if((typeof(this.textObject.caretPos) != "undefined") && this.textObject.createTextRange){
				var caretPos = this.textObject.caretPos;
				var temp_length = caretPos.text.length;

				if(textMiddle){
					caretPos.text = (caretPos.text.charAt(caretPos.text.length - 1) == ' ') ? this.textStart + textMiddle + this.textEnd + ' ' : this.textStart + textMiddle + this.textEnd;
				}else{
					caretPos.text = (caretPos.text.charAt(caretPos.text.length - 1) == ' ') ? this.textStart + caretPos.text + this.textEnd + ' ' : this.textStart + caretPos.text + this.textEnd;
				}
				if(temp_length == 0){
					caretPos.moveStart("character", -this.textEnd.length);
					caretPos.moveEnd("character", -this.textEnd.length);
					caretPos.select();
				}else{
					this.textObject.focus(caretPos);
				}
			}else if(typeof(this.textObject.selectionStart) != "undefined"){
				var begin = this.textObject.value.substr(0, this.textObject.selectionStart);
				var selection = this.textObject.value.substr(this.textObject.selectionStart, this.textObject.selectionEnd - this.textObject.selectionStart);
				var end = this.textObject.value.substr(this.textObject.selectionEnd);
				var newCursorPos = this.textObject.selectionStart;
				var scrollPos = this.textObject.scrollTop;

				if(textMiddle){
					this.textObject.value = begin + this.textStart + textMiddle + this.textEnd + end;
				}else{
					this.textObject.value = begin + this.textStart + selection + this.textEnd + end;
				}
				if(this.textObject.setSelectionRange){
					if(selection.length == 0){
						this.textObject.setSelectionRange(newCursorPos + this.textStart.length, newCursorPos + this.textStart.length);
					}else{
						this.textObject.setSelectionRange(newCursorPos, newCursorPos + this.textStart.length + selection.length + this.textStart.length);
					}
					this.textObject.focus();
				}
				this.textObject.scrollTop = scrollPos;
			}
		}
	}

	this.textObject = returnObjById(textObject);
	this.addText = addText;
	this.textStart = textStart;
	this.textEnd = textEnd;
	this.alertId = 'bbcodeAlert';

	switch(alertText){
		case 'url':
			var html = '<form method="post" action="" onsubmit="return false;" id="bbform">';
			html += '<ul>';
			html += '<li><label for="wtext">link text</label><input type="text" name="web_text" value="" id="wtext" /></li>';
			html += '<li><label for="wurl">website url</label><input type="text" name="web_url" value="" id="wurl" /></li>';
			html += '<li>';
			html += '<input type="button" name="set" value="go" class="btn" onclick="bbCodeDisplay(\'' + this.textObject.id + '\', bbCodeExtra(\'wurl\', \'[url=\', \']\'), \'' + this.textEnd + '\',  getInputData(\'wtext\')); removeObject(\'' + this.alertId + '\');" />';
			html += '<input type="button" name="cancel" value="cancel" class="btn" onclick="removeObject(\'' + this.alertId + '\');" />';
			html += '</li>';
			html += '</ul>';
			html += '</form>';
			var alertBox = creatediv(this.alertId, html, 100, 100, true);
		break;
		case 'quote':
			var html = '<form method="post" action="" onsubmit="return false;" id="bbform">';
			html += '<ul>';
			html += '<li><label for="qname">quotee</label><input type="text" name="quote_name" value="" id="qname" /></li>';
			html += '<li><label for="qtext">text</label><textarea name="quote_text" cols="30" rows="10" id="qtext"></textarea></li>';
			html += '<li>';
			html += '<input type="button" name="set" value="go" class="btn" onclick="bbCodeDisplay(\'' + this.textObject.id + '\', bbCodeExtra(\'qname\', \'[quote=\', \']\'), \'' + this.textEnd + '\',  getInputData(\'qtext\')); removeObject(\'' + this.alertId + '\');" />';
			html += '<input type="button" name="cancel" value="cancel" class="btn" onclick="removeObject(\'' + this.alertId + '\');" />';
			html += '</li>';
			html += '</ul>';
			html += '</form>';
			var alertBox = creatediv(this.alertId, html, 100, 100, true);
		break;
		default:
			this.addText();
		break;
	}
}/**
 * Copyright Gimp Productions - gimppro.co.uk
 * This page cannot be used without permission
 *
 * Author: Gimp Productions
 * Date Created: 05/05/11 22:13
 */
function imagePop(){
	function setDimensions(image){
		if(isObject(image)){
			var imgWidth = $(image).width();
			var imgHeight = $(image).height();
			if(imgWidth && imgHeight){
				var descObject = returnObjById('itmdesc');
				var re = getElementsByClassName('box', this.holderId);
				if(re.length > 0){
					var dWidth = imgWidth;
					var dHeight = imgHeight;
					$(re).css('width', dWidth + 'px');
					$(re).css('position', 'relative');
					if(isObject(descObject)){
						dHeight += $(descObject).outerHeight()+5;
					}
					$(re).css('height', dHeight + 'px');
					$(re).css('margin', '-' + (((dHeight/2) < 0) ? '0' : (dHeight/2)) + 'px 0px 0px -' + (((dWidth/2) < 0) ? '0' : (dWidth/2)) + 'px');
				}

				$(image).css('visibility', 'visible');
				if(isObject(descObject)){
					re = getElementsByClassName('img', this.holderId);
					if(re.length > 0){
						$(re).css('height', imgHeight + 'px');
					}
					$(descObject).css('visibility', 'visible');
				}

				this.centerHolder();
			}
		}
		return false;
	}

	function centerHolder(){
		var view = new userViewport();
		var re = getElementsByClassName('box', this.holderId);
		if(re.length > 0){
			var h = Math.round(parseInt($(re).height()/2));
			var top = view.yOffset() + (view.height()/2);
			$(re).css('top', top + 'px');
		}
	}

	function addImage(imageId, holder){
		this.imageDescription = '';
		var parent = this;

		var view = new userViewport();
		$.post('/imageshow/' + imageId + '/', {'fetch':true, 'width':view.width()-50, 'height':view.height()-150}, function(data){
			if((data == undefined) || (data == '')){
				// an error occurred
				alert('An error occurred, please try again.');
				
			}else{
				data = data.split('qt&ik');
				var regPattern = new RegExp('^/thumbs/items/(([0-9]+)-([0-9]+))/(([a-z0-9\_]+)\.(jpg|gif|png))$');
				if(data[0] && regPattern.test(data[0])){
					parent.imageUrl = data[0];
				}
				if(data[1]){
					parent.imageDescription = data[1];
				}

				var html = '<div class="box">';
				html += '<div class="img">';
				html += '<img src="' + parent.imageUrl + '" alt="image" border="0" onload="imagePopup.setDimensions(this);" />';
				html += '</div>';
				if(parent.imageDescription != ''){
					html += '<div id="itmdesc">' + parent.imageDescription + '</div>';
				}
				html += '<div class="close" onclick="imagePopup.closeImage()"><strong>close image</strong></div>';
				html += '</div>';

				$(holder).html(html);
			}
		});
	}

	function createHolder(imageId){
		creatediv(this.backId, '', 100, 100, true);
		var holder = creatediv(this.holderId, '', 100, 100, true);
		this.addImage(imageId, holder);
		return false;
	}

	function closeImage(){
		$('#' + this.backId + ', #' + this.holderId).remove();
	}

	this.holder = null;
	this.backId = 'imgback';
	this.holderId = 'imgholder';
	this.addImage = addImage;
	this.createHolder = createHolder;
	this.setDimensions = setDimensions;
	this.centerHolder = centerHolder;
	this.closeImage = closeImage;

	this.imageUrl = '/thumbs/items/100-100/no-image.png';
	this.imageDescription = '';
}
