// On-load functions ####################################################################

$(document).ready(function(){
	$('#dialog').jqm();
	$('#navigation ul').droppy({speed: 200});
	connectChanger();
	thumbnailViewer();
});


// Thumbnail viewer #####################################################################

function thumbnailViewer(){
	$('a.thumbnail').click(function(){
		var p = $(this).parent('div').attr('rel');
		var img = escape($(this).attr('rel'));
		if(p!='no_thumb'){
			openModal('modules/view_image.php?img='+img,544);
		}
	})
}

// Connect Changer ######################################################################

function connectChanger(){
	$('.connect_controls li a').each(function(){
		$(this).click(function(){
			var c = $(this).attr('rel');
			$('.c_active').removeClass('c_active');
			$(this).addClass('c_active');
			
			$('.connect_contents li').hide()
			$('#c_'+c).show();
		});
	})
}

// Process sign-up form #################################################################

function processSignUp(){
	var c = $('#sign_up_field').val();
	$.get('modules/sign_up.php?e='+c);
	$('#sign_up').addClass('bold').addClass('align-center').html('<br />Thank you for signing up!');
}

// AJAX POST Processor ##################################################################

function ajaxPost(url,formid,containerid){
  $.post(url, $('#'+formid).serializeArray(), function(data){
  	$('#' + containerid).html(data)
  });			
}

// Process product request ##############################################################

function processProductRequest(){
	var n = $('#r_name').val();
	var e = $('#r_email').val();
	
	if(n=='' || e==''){
		$('.r_error').fadeIn(300);
	}
	else{
		ajaxPost('modules/request_processor.php','request','modal_content');
	}
}

// Show madal dialog ####################################################################

function openModal(url,width){
	$('#dialog').jqmShow();
	$('#modal_content').html('<div class="align-center note"><br /><img src="/images/loading.gif" /><br /><br /></div>');
	$('#dialog').css('width',width+'px').css('margin-left','-'+((width/2)+10)+'px');
	$('#modal_content').load(url,function(){ 
		$('input:first').focus(); $('.error').fadeOut(8000);
	});
}

// Comments #############################################################################

function loadComments(id){
	$('#comments').load('modules/comments.php?id='+id);
}

function submitComment(id){
	var author = $('#author').val();
	var comment = $('#comment').val();
	
	if (author == '' || comment == '') {
		$('.notification').show();
		$('.notification').fadeOut(8000);
	}
	else {
		$('.notification').hide();
		$.post('modules/comment_save.php?id='+id, $('#comment_form').serializeArray());
		$('#comments ul').append('<li><span class="cmt_author">' + author + ' just posted:</span><p>' + comment + '</p></li>');
		// Clear fields
		$('#author').val('').focus();
		$('#comment').val('');
	}
}

function deleteComment(id){
	$('#cmt'+id).fadeOut(200);
	$.get('modules/comment_delete.php?id='+id);
}

// Email form ###########################################################################

function sendEmail(){
	$('#btn_send').val('Sending...');
	$.post('modules/emailform.php?submit=t', $('#email_form').serializeArray(), function(data){
      $('#emailform').html(data);$('.notification').fadeOut(8000);
	});
}
