// JavaScript Document

$( document ).ready( function()
{	
	homepageFlash();
	contactForm();
	relatedProject();
});

/* render the homepage flash */
function homepageFlash()
{
	var pageUrl = window.location.href;
	if ( pageUrl.indexOf( 'index.php/web-pos' ) != -1 ) {
		var currentSlideIndex = 1;
	} else if ( pageUrl.indexOf( 'index.php/web-cms' ) != -1 ) {
		var currentSlideIndex = 2;
	} else if ( pageUrl.indexOf( 'index.php/web-design-and-development' ) != -1 ) {
		var currentSlideIndex = 3;
	} else {
		var currentSlideIndex = 0;	
	}	 
		
	var hf = new SWFObject( '/img/bioniq.swf', 'bioniq', '980', '432', '8', '#000000' );
	hf.addParam( 'allowScriptAccess', 'always' );
	hf.addParam( 'wmode', 'opaque' );
	hf.addVariable( 'currentSlideIndex', currentSlideIndex );
	hf.write( 'flash-nav' );
}

function contactForm()
{
	/* email form */
	
	//if( !readCookie( 'bioniq_email' ) )
	//{
		var form = "<form name='contact' action='http://bioniq.ca/scripts/mail.php' method='post'>";
		form += "<fieldset>";
		
		form += 	"<input name='name' value='Your Name:' type='text' />";
		form += 	"<input name='company' value='Company:' type='text' />";
		form += 	"<input name='email' value='Email:' type='text' />";
		form += 	"<input name='phone' value='Phone:' type='text' />";
		form += 	"<input name='url' value='URL:' type='text' />";
		form += 	"<select name='business_need'>";
		form += 	"<option value='Business need'>Business need</option>";
		form += 	"<option value='Ecommerce'>Ecommerce</option>";
		form += 	"<option value='POS System'>POS System</option>";
		form += 	"<option value='Inventory System'>Inventory System</option>";
		form += 	"<option value='Content Management'>Content Management</option>";
		form += 	"<option value='Web Design'>Web Design</option>";
		form += 	"<option value='Custom Development'>Custom Development</option>";
		form += 	"</select>";
		form += 	"<select name='business_range'>";
		form += 	"<option value='Budget range'>Budget range</option>";
		form += 	"<option value='$5000 - $10,000'>$5000 - $10,000</option>";
		form += 	"<option value='$10,000 - $15000'>$10,000 - $15000</option>";
		form += 	"<option value='$15000 - $20000'>$15000 - $20000</option>";
		form += 	"<option value='$20000 plus'>$20000 plus</option>";
		form += 	"</select>";
		form += 	"<textarea name='message'></textarea>";
		
		form += "</fieldset>"
		form += "</form>"
		
		/* add form and link to page */
		
		$( '#contact' ).html( form );
		$( '#contact' ).after( "<a href='#' class='send-button'>send</a>" );
		
		/* bind contact send button */
	
		$( '.column.right a.send-button' ).bind( 'click', function( e )
		{
			$( this ).blur();
			e.preventDefault(); $( '#contact .alert' ).remove();
			
			var name = $( '#contact form input[name="name"]' ).attr( 'value' );
			var email = $( '#contact form input[name="email"]' ).attr( 'value' );
			var phone = $( '#contact form input[name="phone"]' ).attr( 'value' );
			var message = $( '#contact form textarea' ).val();
			
			if( name != 'Your Name:' && email != 'Email:' && phone != 'Phone:' && message != '' )
			{
				var ajax_form_options = { success: showResponce, beforeSubmit: pleaseWait };
				$( '#contact form' ).ajaxSubmit( ajax_form_options );
				return false;
			}
			
			else
			{
				$( '#contact form' ).before( '<div class="alert">Please check to see all form fields have been filled out and<br />try again.</div>' );
				$( '#contact .alert' ).slideToggle( 'fast' );
			}
		});
		
		/* pretty up the form so it hides the example text and changes bg colour when selected */
		
		$( '#contact input' ).each( function( i ) { this.orgin_txt = this.value; } );
		
		$( '#contact input' ).bind( 'focus', function( e )
		{
			if( this.value == this.orgin_txt ) { this.value = '' };
			$( this ).css( { 'background-color' : '#CEE6FD' } );
		});
		
		$( '#contact input' ).bind( 'blur', function( e )
		{
			if( this.value == '' ) { this.value = this.orgin_txt };
			$( this ).css( { 'background-color' : '#FFF' } );
		});
	//};
}

function showResponce( responseText, statusText )
{
	$( '#contact form' ).before( '<div class="alert">Thank you for your interest in BIONIQ<br /> we\'ll get back to you shortly.</div>' );
	$( '#contact form' ).remove(); $( '.column.right a.send-button' ).remove();
	$( '#contact .alert' ).slideToggle( 'fast' );
	
	//createCookie( 'bioniq_email', 'true', 30 );
}

function pleaseWait()
{
	$( '#contact form' ).before( '<div class="alert">We are processing your form, please wait...</div>' );
	$( '#contact .alert' ).slideToggle( 'fast' );
}

function relatedProject()
{
	$( '.column.left .related-project a.project' ).bind( 'click', function( e )
	{
		e.preventDefault();
		
		GB_show( '', $( this ).attr( 'href' ) );
	});
}

/* cookies */

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;
}


