/*
Moosecatch, 2009
*/

var config = {
	list: Array(),
	tab: 0,			//0-post, 1-setting
	acc: 0,			//0-post, 1-account
	click: 0,		//inline editing
	form: false,	//inline editing
	formet: '',		//inline editing
	textarea: '',	//inline editing
	p: '',			//inline editing
	pElements: '',	//inline editing
	textvalue: '',	//count chars
	imageList: Array(),		//for image swap
	imageCount: 0,			//for image swap
	clear: true,			//inputClick()
	root: 'moosecatch'	//root
	
};

window.onload = function  () {
	setImages();
	swapImage();
	//checkHeader();
}


/**
 * this funtion fires onload to set the font size of the header
 * 
 * @param 
 * @return
 */
function checkHeader() {
	if($('name')) {
		var name = $('name');
		var value = (name.firstChild).nodeValue ;
		if(value.length > 13) {
			if(value.length > 25) {
				name.style.fontSize = '350%';
			}
			name.style.fontSize = '400%';
		}
	}
}


/**
 * the following function belongs to settings.php
 * 
 * @param 
 * @return
 */
function membershipChange(e) {
	$('mem_desc').innerHTML = e;
}

function membershipUpdate() {
	alert('Sorry this button does not work yet. ');
}

function smsPurchase() {
	alert('Sorry this button does not work yet. ');
}

function searchHoover(el, newImage) {
	
	$(el).style.backgroundImage = 'url(/images/'+newImage+')';
}

function subscriptionPurchase() {
	alert('Sorry this button does not work yet. ');
}

/**
* this funtion fires every regurarly to swap image on front page
* 
* @param 
* @return
*/
function swapImage() {
	if($('swap_image')) {
		var el = $('swap_image');
		if(config.imageCount < (config.imageList).length) {
			el.src = config.imageList[config.imageCount];
			config.imageCount ++;
		} else {
			config.imageCount = 0;
		}
		setTimeout("swapImage()", 7000);
	}
}

/**
* this function sets the images on the front page
* 
* @param 
* @return
*/
function setImages() {
	config.imageList[0] = '/images/front/front_camo_shop.png'; 
	config.imageList[1] = '/images/front/front_bakery.png'; 
	//config.imageList[2] = '/images/front/front_market.png'; 
	//config.imageList[3] = '/images/front/front_bakery.png'; 
	//config.imageList[4] = '/images/front/front_skate.png';
}

function submit() {
	var s = 'du har valt ';
	var l = $(input_form_list);
	alert(l);
	for(i=0; i<l.length; i++) {
		if(l[i].style.color == 'rgb(255, 255, 255)') {
		}
	}
}

function showSubscribe() {
	$('subscribe_undergoing').style.display = 'block';
	//$('share').style.display = 'none';
}

function showShare() {
	$('share_hidden').style.display = 'block';
	//$('subscribe').style.display = 'none';
}

/* close pressed in popups */
function closeDiv(e) {
	e.parentNode.style.display = 'none';
}

/* the input fields clicked */
function inputclick(e) {
	if(config.clear == true) {
		e.value = '';
		config.clear = false;
	} 
}


/* account button clicked */
function showAccountPost() {

	if(config.acc == 0) {
		$('post_form').style.display = 'none';
		$('feed').style.display = 'none';
		
		$('account').style.display = 'block';
		$('btn_account_post').src = '/images/btn_post.png';
		config.acc = 1;
		
	} else if(config.acc == 1) {
		$('post_form').style.display = 'block';
		$('feed').style.display = 'block';
		
		$('account').style.display = 'none';
		$('btn_account_post').src = '/images/btn_account.png';
		config.acc = 0;
	}
}


/* counts the chars in post form in form.php */
function countChars(e, max) {
	var element = e;
	var left = max - (element.value).length - 1;
	
	if(left >= 0) {
		($('charsleft')) ? $('charsleft').update(left) : null;
		config.textvalue = element.value;
	} else {	
		element.value = config.textvalue;
	}
}

/* facebook sharer */
function fbs_click(id) {
	if(id == 0) {
		u=location.href;
	} else {
		u='http://moosecatch.com/dynamicpost/'+id+'/';
	}
	t=document.title;
	window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t)+'sharer,toolbar=0,status=0,width=626,height=436');
	return false;
}

/**
 * Inline editing, create the form
 * @param 
 * @return
 */	
function createForm(e) {
	if(config.click == 0 && config.form == false) {
	this.e = e;
	config.click = 1;
	config.form = true;	
	
	if(e.id == 'logo') {
		$('company_logo_upload').style.display = 'block';
		config.p = e.src;
	} else {
		config.p = e.firstChild.nodeValue;
		e.firstChild.nodeValue = '';
	}
	
	config.textarea = new Element('textarea');
	config.textarea.value = config.p.toString();
	config.textarea.name = 'text';
	config.textarea.rows = 1;
	config.textarea.cols = 18;

	formet = new Element('div');
	formet.id = 'form';
		
	var buttonok = new Element('input');
	buttonok.type = 'submit';
	buttonok.value = 'save';
	if(e.id == 'tags'){
		buttonok.addEventListener('click',validateTags,false);
	} else {
		buttonok.addEventListener('click',validate,false);
	}
	var buttonavbryt = new Element('input');
	buttonavbryt.type = 'submit';
	buttonavbryt.value = 'cancel';
	buttonavbryt.addEventListener('click',cancel,false);
		
	var br = new Element('br');
	formet.appendChild(config.textarea);
	formet.appendChild(buttonok);
	formet.appendChild(buttonavbryt);
	e.appendChild(formet);	
	}
	else {
		config.click = 0;
	}
	
}

function validateTags (){

	var reg = /[^a-z],/ 
	if (config.textarea.value.search(',')==-1) {
		alert("Please enter a comma between all tags. It's also nice to use more than one tag.");
	} else {
		validate();
	}

}

/**
 * Inline editing, when save is clicked
 * @param 
 * @return
 */	
function validate() {
	if(config.click = 1 && config.form == true) {
		config.click = 2;
		config.form = false;
		var p = config.textarea.value;
		formet.style.display = 'none';
		e.firstChild.nodeValue = p;
	}
	settingsSave(e, p);
	/*
	for (var i=0; i < config.pElements.length; i++) {
		if(config.pElements[i].className == 'noneditable')
			config.pElements[i].className == 'editable';
	}; */
}

/**
 * Inline editing, when cancel is clicked
 * @param 
 * @return
 */	
function cancel() {

	if(config.click = 1 && config.form == true) {
		config.click = 2;
		config.form = false;
		formet.style.display = 'none';
		e.firstChild.nodeValue = config.p;
	}
	for (var i=0; i < config.pElements.length; i++) {
		if(config.pElements[i].className == 'noneditable')
			config.pElements[i].className == 'editable';
	}; 
}

/**
 * Inline editing, when cancel is clicked ON IMAGE UPLOAD
 */	
function close_image_upload(e) {
	if($(e)) {
		$(e).style.display = 'none';
		config.click = 0;
		config.form = false;
	}
}


function elementEffect (eId, mode){
	
	switch (mode) {
		case 1:
			new Effect.BlindDown($(eId));
			break;
		case 2:
			new Effect.Appear($(eId));
			break;
		case 3:
			new Effect.Fade($(eId));
			break;
		case 4:
			new Effect.SlideDown($(eId));
			break;
		case 5:
			new Effect.BlindUp($(eId));
			break;
		default:
			break;
	}
	
	
}

/**
*	switch the display value in style
*/
function switchView (id, mode){
	
	if($(id)){
		if(!$(id).visible())
			elementEffect($(id), mode);
		else{
			$(id).visible();
			elementEffect($(id), 5);
		}
			
		
	}
}
/**
 * compares with two passwords inputed when user registers
 * @return boolean
 */
function pswdConfirmation(){
	var frm = document.forms["register_form"];
	if(frm.reg_password.value != frm.reg_password2.value){
		alert('The Password and verified password does not match!');
		return false;
	}
	
	return true;
	
}
 
 /**
  * close the helpdiv
  * @return
  */
 function closeHelp() {
	 if($('helpdiv')) { $('helpdiv').style.display = 'none'; }
 }