/*
 * Klevstul's JS Utility : Utility Functions
 *
 * Frode Klevstul
 * frode at klevstul dot com
 *
 */


//--------------------------------------------------------
// display properties for an object
//--------------------------------------------------------
var kUtil_showProps = function(obj) {
	var message="";
	for(prop in obj){
		if(obj.name){ 
			message += obj.name+".";
		}
		message += prop+"="+obj[prop]+"\n";
	}
	alert(message);
}


//--------------------------------------------------------
// main function that is executed on startup
//--------------------------------------------------------
var kUtil_main = function() {
	// send mail response function
	function showResponse(responseText, statusText, xhr, $form)  {
		document.getElementById('captcha').src = '/securimage/securimage_show_shinagara.php?' + Math.random();

		if (responseText.match(/^ERROR/) != null){
			var $dialog = $('<div></div>').html('<div class="gFont body">'+responseText+'</div>').dialog({
				  autoOpen: false
				, title: '<div class="gFont body">Sorry Bud!</div>'
				, width: 600
				, height: 300
				, modal: true
			});
			$dialog.dialog('open');
		} else {
			$form.resetForm();

			var $dialog = $('<div></div>').html('<div class="gFont body">Thanks for your message, we will get back to you!</div>').dialog({
				  autoOpen: false
				, title: '<div class="gFont body">Thank you!</div>'
				, width: 600
				, modal: true
			});
			$dialog.dialog('open');
		}
	};

	// send mail form options
	var formOptions = {
		  success:	showResponse
		, url : 	'http://shinagara.com/forms/sendEmailFromForm.php'
		, type:		'post'
		, timeout:   3000
	};

	// init function
	$(function() {

		// buttons
		$(".button").button();

		// top menu tabs
		$('#tabs').tabs({
			  panelTemplate:	'<div style="padding-top: 40px;"></div>'
			, tabTemplate:		'<li style="width: 150px;"><a href="#{href}"><span>#{label}</span></a></li>'
			, spinner: 			''
			, load:				function(event, ui) {
									$(".button").button();
									// contact form has id = 5 (index starts with 0)
									if (ui.index == 5){
										// ref : http://jquery.malsup.com/form/
										$.getScript('/forms/jquery.form.js', function(){
											$('#form_contactUs').ajaxForm(formOptions);

											$('#form_contactUs').submit(function() {
												$(this).ajaxSubmit();

												// return false to prevent normal browser submit and page navigation
												return false;
											});

											$("#btn_reset").click( function(){
												$('#form_contactUs').resetForm();
											});
										});
									} else if (ui.index == 4){
										$(".button").button().css('width', '150px');
									
									};
								}
			, select:			function(event, ui) {
								}
		});

		// add tabs
		$('#tabs').tabs('add', '/include/about.html', 'about');
		$('#tabs').tabs('add', '/include/products.html', 'products');
		$('#tabs').tabs('add', '/include/plans.html', 'plans');
		$('#tabs').tabs('add', '/include/geek.html', 'geek stuff');
		$('#tabs').tabs('add', '/include/affiliated.html', 'affiliated');
		$('#tabs').tabs('add', '/forms/contactUs.html', 'contact us');

		// fade-in of the entire page
		$('#body_').fadeIn(2000);
	});
}



