function windowInit(){
	$(document).ready(function(){ 
	$('a.cart').toggle (
			function(){	
				$('div#cart').animate({height:'show',opacity:'show'},'fast'); 
				return false;
				},
			function(){	
				$('div#cart').animate({height:'hide',opacity:'hide'},'fast'); 
				return false;
				}
		) 
	removeAlerts();
	});

}

function removeAlerts() {
	var el = setTimeout(fadeOut, 5000);
	function fadeOut() {
		$.each($('.remove'), function() {
			$(this).fadeOut(500);
		});
	}
	
}


function loadFarb(){
$(document).ready(function(){ 
	var f = $.farbtastic('#picker');
	var p = $('#picker').css('opacity', 0.25);
	var selected;
	$('.colorwell')
	  .each(function () { f.linkTo(this); $(this).css('opacity', 0.75); })
	  .focus(function() {
		if (selected) {
		  $(selected).css('opacity', 0.75).removeClass('colorwell-selected');
		}
		f.linkTo(this);
		p.css('opacity', 1);
		$(selected = this).css('opacity', 1).addClass('colorwell-selected');
	  });
  });
}

function showHide(id){
	var el = document.getElementById(id);
	if (el.style.display == 'block'){
		el.style.display = 'none';
		} else {
			el.style.display = 'block';
			}
}



function showMessage(id){
	$(id).animate({ right: -10, opacity: 'show'}, 500);
	setTimeout (eraseMessage, 10000);
	function eraseMessage(){
		$(id).animate({ left: 10, opacity: 'hide'}, 500);
	}
		
}
	
function addLink(id){
	var el = document.getElementById(id);
	var url = prompt('Enter the web address');
	var title = prompt('Enter the link title');
	var urlString = '<a href="' + url + '">' + title + '</a>';
	
	if (url && title){
		el.value += urlString;
	} else {
		el.value += '';
	}
	el.select();
	
	}
	
function addImage(id){
	var el = document.getElementById(id);
	var url = prompt('Enter the URL of the image');
	var urlString = '<img src="' + url + '" />';
	
	if (url){
		el.value += urlString;
	} else {
		el.value += '';
	}
	el.select();
	
	}
	
function nsfw(id){
	var el = document.getElementById(id);
	var text = prompt('Enter the content Not Safe For Work');
	var urlString = '<nsfw class="nsfw">' + text + '</nsfw>';
	
	if (text){
		el.value += urlString;
	} else {
		el.value += '';
	}
	el.select();
	
	}


function commentQuote(author, id) {
	var el = document.getElementById(id);
	var text = el.innerHTML.replace(/^\s*|\s*$/g,'');
	document.getElementById('commentb').value += '<blockquote><cite>' + author + ' said:</cite> ' + text + '</blockquote>\n\n';
	
	$.scrollTo('#commentb', 800, { easing:'linear' } );
	
	$('#commentb:text').select();

}


function scrollToEl(id) {
		var target = $(id);		
		$.scrollTo(target, 800, {easing:'linear'});
		target.select();
}

/** AJAX COMMENT FUNCTIONS **/
function editThreadComment(comment_id, author, post_id, tee){
	$('.edit#edit-' + comment_id).attr('onclick','');
	var el = '#post_text-' + comment_id;
	var thetext = $(el).html();
	var before = jQuery.trim(thetext);
	$(el).html(' ');

	var inp = $('<textarea></textarea>').attr({ id:'box', rows:'8', value: before.replace(/<br>/gi,'') }).addClass('box'); 	 
	var subm = $('<input>').attr({ type:'button',  value:'Submit' }).addClass('submit'); 	
	var cancel = $('<input>').attr({ type:'button', value:'Cancel' }).addClass('cancel'); 	
	
	$(el).append(inp);
	$(el).append(subm);
	$(el).append(cancel);
				
	//add blur event to input
	subm.click( function(){
		var val = inp.val();
		$.ajax({
			   url: post_id + '/cedit',
			   type: 'post',
			   data: {'comment_id': comment_id, 'commenttext': val, 'author': author, 'post_id': post_id, 'tee':tee},
			   beforeSend: function() {
				  	$('.submit').addClass('loading').attr('value','');
				  	$('.cancel').remove();
			   },
			   success: function(data) {
				  	$('.submit', this).removeClass('loading');
					$(el).html(data).fadeIn(500);
			   }
			});
	});
	
	cancel.click( function(){
		$('textarea.box').fadeOut(500);
		$('input.submit').fadeOut(500);
		$('input.cancel').fadeOut(500);
		$(el).html(thetext);
	});
	

}

function deleteThreadComment(comment_id, post_id, tee){
	$(this).attr({ onclick: ''})
	$(this).parent('ul').addClass('loading').html(' ');
	if (comment_id == post_id){		
		if (confirm('Are you sure you want to delete this post? \n All comments will be removed', {textBoxBtnOk: 'Ok', textBoxBtnCancel: 'Cancel'})){
			deleteIt();
		} else {
			return false;
			}
	} else {
		if (confirm('Are you sure you want to delete this comment?', {textBoxBtnOk: 'Ok', textBoxBtnCancel: 'Cancel'})){
			deleteIt();
		} else {
			return false;
		}
	}

	function deleteIt(){
		$.post(	post_id + '/cdelete', { 
			comment: comment_id, 
			parent: post_id, 
			tee: tee,
			beforeSend: function() {
				if (comment_id == post_id){												  
					$('#comment-' + comment_id).addClass('loading');
				} else {
					$('#comment-' + comment_id).addClass('loading');
				}
			},
			success: function() {
				if (comment_id == post_id){												  
						$('#comment-' + comment_id).parent('div').fadeOut(500);
						function doFade(){
							window.location = "../../talk";
						};
						setTimeout(doFade, 700);
				} else {
					$('#comment-' + comment_id).animate({opacity:'hide'},500);
				}
	
			}
		});
	}
}

function addThreadComment(author, post_id, commenttext, tee){
	var elid = 'newel' + Math.floor(Math.random()*11).toString();
	var newel = $('<div>').addClass(elid).css({height:'100px', marginBottom:'80px'});
	if(tee == 1){
		$('div.post_dummy').after(newel);
	} else {
		$('div.post:last').after(newel);
	}

	$.ajax({
		type: 'POST',
		dataType: 'html',
		url: post_id + '/cadd',
		data: { 'commenttext':commenttext,  'author':author,  'post_id': post_id , 'tee' :tee },
			beforeSend: function(){
				$('#submit').addClass('loading').html(' ').css('height','13px').animate({ opacity:'show'}, 500);
				$('.'+elid).addClass('loading').css('height','13px').animate({ opacity:'show'}, 500);
				},
			success: function(transport) {
				$('#submit').removeClass('loading').html('SUBMIT').animate({ opacity:'show'}, 500);
				$('.'+elid).removeClass('loading');
				$('.'+elid).html(transport);
				$('.'+elid).fadeIn(500);
				var contHeight = 0;
				$.each($('.newone').children(), function(){
					contHeight += ($('.newone').children().attr('height') / 3.3) ;
				});
				$('.'+elid).css('height', contHeight  );

				$('#commentb').attr({ value:' '});
				$('#commentb').val(' ');
			}
		});
}

function lockThread(post_id){
$(this).attr({ onclick: ''})
$.post( post_id + '/lock', {'post_id': post_id}, 
	function(data){ 						
			$('#commentbox').html(data);
			$('.quote').each (
				function() {
					$(this).fadeOut('slow');
					}
				);
			$('#commentbox').fadeIn('slow');
			$('.lock').attr('class','unlock');
		}
		
	);
}

function unlockThread(post_id){
	$(this).attr({ onclick: ''})
	$.post( post_id + '/unlock', {'post_id': post_id},
	function(data) {
			$('#commentbox').html(data);
			$('#commentbox').fadeIn('slow');
			$('a.unlock').attr('class','lock');
		}
	);
}

function stickyThread(post_id){
$(this).attr({ onclick: ''})
$.post(post_id + '/sticky', {'post_id': post_id},
		function(data) {
			$('a.sticky').attr('class','unsticky');
		}
	);
}

function unstickyThread(post_id){
$(this).attr({ onclick: ''})	
$.post(post_id + '/unsticky', {'post_id': post_id},
		function(data) {
			$('a.unsticky').attr('class','sticky');
		}
	);
}


function addNewPost(post_text, post_title){
		if ($('#sticky')){
			if ( $('#sticky').attr('checked')){
				var sticky = 1;
			} else {
				var sticky = 0;
			}
		} else {
			var sticky = 0;
			}
		var elid = 'newel' + Math.floor(Math.random()*11).toString();
		var enew = $('<div>').addClass(elid).css({height:'55px'});
		$('div.thread:first').before(enew);
		$.ajax({
			url: 'talk/add', 
			type: 'post',
			data: {'post_text': post_text, 'post_title': post_title, 'sticky': sticky },
			beforeSend: function() {
				$('#submit').html(' ').addClass('loading').css('height','13px');
				$('.'+elid).addClass('loading');
				scrollToEl('#main');
			},
			success: function(data){
				$('.'+elid).removeClass('loading');
				$('#submit').html('SUBMIT').removeClass('loading').css('height','13px');
				$('#title').val(' ');
				$('#commentb').val(' ');
				$('.'+elid).html(data).css('display','none');
				$('.'+elid).fadeIn(1300);
			}
			  
	});
			
}

function nsfwLoader(){
 $.each($('.nsfw'), function() {
		var before = $(this).html();
		var wrap = $('<div>').addClass('nsfw-wrapper');
		var text = $('<div>').addClass('badcontent').html(before).css('display', 'none');
		var span = $('<span>').html('WARNING! NOT SAFE FOR WORK');
		var blocker = $('<div>').addClass('blocker');
		
		$(this).parent('div').append(wrap);
		$(wrap).append(blocker);
		$(blocker).append(span);
		$(wrap).append(text);
		
		setTimeout(getHeight, 1500);
		
		function getHeight() {
			$.each($('.nsfw'), function() {
				var contHeight = $(this).next().children('.badcontent').children().attr('height');
				if ( contHeight < 40) {
					$(this).next().children('.blocker').css('height', '15px');
				} else {
					$(this).next().children('.blocker').css('height', $(this).next().children('.badcontent').children().attr('height') );
				}
			});
		}
		
		$(this).css('display','none');
		
		$(this).siblings('.nsfw-wrapper').mouseenter(function(){
			$(this).children('.badcontent').css('display', 'block');
			$(this).children('.blocker').css('display', 'none');
		});
		$(this).siblings('.nsfw-wrapper').mouseleave(function(){
			$(this).children('.badcontent').css('display', 'none');
			$(this).children('.blocker').css('display', 'block');
		});
});
}



function voteItem(item_id, val) {
	$.ajax({
		url: item_id + '/vote',
		type: 'post',
		data: {
			'item_id':item_id
		},
		beforeSend: function() {
			$('#votes').html(val += 1);
			$('#votes').fadeIn(500);		
		},
		success: function() {
				$('vote').unbind('click');
				$('vote').click(function() {
						alert('you have already voted on this tee');
						});

		}
	});
}

/*======== Inventory Functions ==========*/
function newChild(id) {
	var num = $('input').length + 1;
	var tr = $('<tr>');
	var td1 = $('<td>');
	var td2 = $('<td>');
	var td3 = $('<td>');
	var td4 = $('<td>');
	var td5 = $('<td>');
	var name = $('<input>').attr({ 
		type: 'text', 
		size: '50', 
		//onchange: 'saveInv(' + id + ', $(\'#' + id + '_opt' + num + '\').val(),' + num + ')', 
		name: id + '[]', 
		id: id + '_opt' + num
		}).addClass('opt');
	
		
	var q = $('<input>').attr({ 
		type: 'text',  
		size: '10', 
		//onchange: 'saveInv(' + id + ', $(\'#' + id + '_opt' + num + '\').val(),' + num + ')', 
		name: id + '[]', 
		id: id + '_optq' + num 
		}).addClass('opt');
		
	var sold = $('<input>').attr({ 
		type: 'text', 
		size: '10',
		//onchange: 'saveInv(' + id + ', $(\'#' + id + '_opt' + num + '\').val(),' + num + ')', 
		name: id + '[]', 
		id: id + '_opts' + num,
		style:'background:transparent; border:0px;',
		readonly: 'readonly'
		}).addClass('opts');
			
	var s = $('<a>').addClass('delete');
	
	
	$('#new' + id).before(tr);
	tr.append(td1);
	tr.append(td2);
	tr.append(td3);
	tr.append(td4);
	tr.append(td5);
	td2.append(name);
	td3.append(q);
	td4.append(sold);
	td5.append(s);
	
	$('#'+id + '_opt' + num).change(function(){
			saveInv(id , $('#' + id + '_opt' + num ).val(), num );
		}); 
	
	$('#'+id + '_optq' + num).change(function(){
			saveInv(id , $('#' + id + '_opt' + num ).val(), num );
		}); 
	
	$('#'+id + '_opts' + num).change(function(){
			saveInv(id , $('#' + id + '_opt' + num ).val(), num );
		}); 
	
	$('a.delete').click(function(){
		$(this).parent().parent().animate({opacity:'hide'}, 300); 
		$('td input', $(this).parent().parent()).attr({ id:'', name:'', onchange:''}).addClass('');	
		saveInv( id , $('#' + id + '_opt' + num + '').val(), num );
	});

}

function saveInv(id, opt, num){
	$('#' + id + '_opt' + num).attr({ name: id + '[' + opt + ']'});
	$('#' + id + '_optq' + num).attr({ name: id + '[' + opt + ']'});
	$('#' + id + '_opts' + num).attr({ name: id + '[' + opt + ']'});

	var post = $('input.opt').fieldSerialize();
	var sold = $('input.opts').fieldSerialize();
										
	$.ajax({
		url:'edit/inventory',
		type:'post',
		data:{ 'post':post, 'sold':sold },
		beforeSend: function(){
			$('#load'+id).addClass('loading');
		},
		success:function(responseData){
			$('#load'+id).removeClass('loading');
			//alert(post);
		}
	});
}

function newCodeChild(uid) {
	$.ajax({
		   url:'edit/codeadd',
		   type:'post',
		   data:{'uid':uid},
		   success:function(id){
				var num = $('input.passcodeid').length + 1;
				var tr = $('<tr>').attr();
				var td1 = $('<td>').attr();
				var td2 = $('<td>').attr();
				var td3 = $('<td>').attr();
				var td4 = $('<td>').attr();
				var td5 = $('<td>').attr();
				var name = $('<input>').attr({ 
					type: 'text', 
					onchange: 'saveCode(' + id + ', $(\'#' + id + '_code' + num + '\').val(), $(\'#' + id + '_desc' + num + '\').val(), $(\'#' + id + '_action' + num + '\').val() )',
					size: '10', 
					maxlength: '10',
					name: id, 
					id: id + '_code' + num
					}).addClass('opt');
					
				var q = $('<input>').attr({ 
					type: 'text', 
					onchange: 'saveCode(' + id + ', $(\'#' + id + '_code' + num + '\').val(), $(\'#' + id + '_desc' + num + '\').val(), $(\'#' + id + '_action' + num + '\').val() )',
					size: '40', 
					name: id, 
					id: id + '_desc' + num 
					}).addClass('opt');;
					
				var sold = $('<select>').attr({ 
					onchange: 'saveCode(' + id + ', $(\'#' + id + '_code' + num + '\').val(), $(\'#' + id + '_desc' + num + '\').val(), $(\'#' + id + '_action' + num + '\').val() )',
					id: id + '_action' + num
					}).addClass('opts');
				
				var drop1 = $('<option>').attr({
					value: 'freeship'	
					}).html('Free Shipping');
				var drop2 = $('<option>').attr({
					value: '10per'	
					}).html('10% Off Total');
				var drop3 = $('<option>').attr({
					value: '15per'	
					}).html('15% Off Total');
				var drop4 = $('<option>').attr({
					value: '20per'	
					}).html('20% Off Total');
				var drop5 = $('<option>').attr({
					value: '25per'	
					}).html('25% Off Total');
				var drop6 = $('<option>').attr({
					value: '30per'	
					}).html('30% Off Total');
					
				var s = $('<a>').addClass('delete').attr('id','codedel');
				
				
				$('#newCode').before(tr);
				tr.append(td1);
				tr.append(td2);
				tr.append(td3);
				tr.append(td4);
				tr.append(td5);
				td2.append(name);
				td3.append(q);
				td4.append(sold);
				sold.append(drop1);
				sold.append(drop2);
				sold.append(drop3);
				sold.append(drop4);
				sold.append(drop5);
				sold.append(drop6);
				td5.append(s);
				
							
				$('a.delete').click(function(){
					$(this).parent().parent().animate({opacity:'hide'}, 300); 
					$('td input', $(this).parent().parent()).attr({ id:'', name:'', onchange:''}).addClass('');	
					saveInv( id , $('#' + id + '_opt' + num + '').val(), num );
				});
			   
		   	}
     });
}

function saveCode(id, opt, desc, num){
	$.ajax({
		   url:'edit/code',
		   type:'post',
		   data:{ 'id':id, 'code':opt, 'desc':desc, 'action':num },
		   success: function(data){
				//alert(data);
			}
		   });
}

function delCode(id){
	$.ajax({
		   url:'edit/codedel',
		   type:'post',
		   data:{ 'id':id},
		   success: function(data){
				//alert(data);
			}
		   });
}





/* === === === === === === === === === === === === === === === === === === === === === === === === === === === === === === === === === === */ 
/* == === === === === === === === === === === === === === === Page Specific Function Processes === === === === === === === === === === === */ 
/* === === === === === === === === === === === === === === === === === === === === === === === === === === === === === === === === === === */

/* ==================================
			ITEM PAGE
=================================== */
		
function itemAddProcess(){
$(document).ready(function(){
	$('#status').change(function(){
			var ind = $('#status').val();
			if ( ind == 'forsale'){
				$('#options').animate({ opacity:'show'}, 500);	
				$('#price').animate({ opacity:'show'}, 500);	
				$('#ship').animate({ opacity:'show'}, 500);	
			} else {
				$('#options').animate({ opacity:'hide'}, 500);
				$('#price').animate({ opacity:'hide'}, 500);	
				$('#ship').animate({ opacity:'hide'}, 500);	
				$('input=[name="price_each"]').val('');
				$('input=[name="shipping"]').val('');
			} 
			
	});
	
	$('input[name="name"]').keyup(function(){
		$('#na').html($('input[name="name"]').val());
	});
});

$('.submit').submit(function(){
	$('.submit').addClass('loading');
	});
}



function addChild() {
	var num = $('input').length + 1;
	var tr = $('<tr>').attr();
	var td1 = $('<td>').attr();
	var td2 = $('<td>').attr();
	var td3 = $('<td>').attr();
	var td4 = $('<td>').attr();
	var name = $('<input>').attr({ 
		type: 'text', 
		size: '50', 
		onchange: '$(this).attr({ name: "new[" + $("#new' + num + '").val() + "]" });',
		id:'new' + num
		});
		
	var q = $('<input>').attr({ 
		type: 'text', 
		size: '10', 
		onchange: '$(this).attr({ name: "new[" + $("#new' + num + '").val() + "]" });'
		});
					
	var s = $('<a>').addClass('delete');
	
	
	$('#newtr').before(tr);
	tr.append(td1);
	tr.append(td2);
	tr.append(td3);
	tr.append(td4);
	td2.append(name);
	td3.append(q);
	td4.append(s);
	
	$('a.delete').click(function(){
		$(this).parent().parent().animate({opacity:"hide"}, 300); 
		$("td input", $(this).parent().parent()).attr({ id:"", name:""});	
	});

}	


//*************************************************************************************************
// Thanks to Peter-Paul Koch for these cookie functions! (http://www.quirksmode.org/js/cookies.html)
//*************************************************************************************************
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}


