var  left_sec_handle= function(){
	var section = parseInt(window.location.hash.replace('#section-',''));
	// console.log('section::',section); 
	$('#sections_nav').find('a').removeClass('active').addClass('inactive');
	for(i=1;i<=6;i++){
		if(i<=section){
			$('#sections_nav').children('li').eq(i-1).children('a').removeClass('inactive');
		}else{
			$('#sections_nav').children('li').eq(i-1).children('a').addClass('inactive');
		}
		if(i==section){
			$('#sections_nav').children('li').eq(i-1).children('a').addClass('active');
		}
	}
};
var section_loader = function (){
	var section = parseInt(window.location.hash.replace('#section-',''))-1;
	if(section){
		$('.sections').hide();
		$('#signMeUp').hide();
		$('.sections').eq(section).show();
		if(section>=1){
			$('.bttn.prev').show();
			$('#left_content').fadeOut(500).queue(function (){
				$('#sections_nav').fadeIn(300);
				$(this).dequeue();
			}); 
			
		}
		if(section <5){//section is -1
			$('.bttn.next:not(.bttn.finish)').show();
			$('.bttn.finish').hide()
		}
		if(section == 5){
			$('.bttn.finish').show().addClass('next');
		}
		$('.prevnext').show();
	}
};
$(document).ready(function (){
	autofit_column();// change column width using jquery to automatically fit the width and be devided by the parent_width/column.length
	//sections on load
	var section = parseInt(window.location.hash.replace('#section-',''))-1;
	section_loader();
	left_sec_handle();
	//sections on load finish
	// turn radio into buttons
	$('.buttonset').not('.ui-buttonset').buttonset(); 
	// network provider
	$('#mob_network').change(function (e){
		if($(this).val().toLowerCase() == 'other'){
			$(this).remove();
			$('#networkOther').show().focus();
		}else{
			$('#networkOther').hide();
		}
	});
	//navigation mirror effect
	
	// section hashing
	var sec = parseInt(window.location.hash.replace('#section-','')) -1;
	// sign up click instructions
	$('#signMeUp').unbind().bind('click',function (e){
		$(this).hide();
		$('.sections').eq(0).show();
		$('.bttn.next').show();
		$('.bc_nav').children().removeClass('active').eq(sec).addClass('active');
		$('.bttn.next').parents('table.prevnext').show();
		$('#left_content').hide(0).queue(function (){
			$('#sections_nav').hide().fadeIn(700);
			$(this).dequeue();
		}); 
		
		window.location.hash = 'section-1';
		left_sec_handle();
	});
	//form controlls
	// console.log('sec: '+sec);
	if(sec==0){
		$('#signMeUp').trigger('click');
	}
	
	
	
	
	
	$('.bttn.prev').unbind().bind('click',function (e){
		e.preventDefault();
		var section = parseInt(window.location.hash.replace('#section-',''))-1;
		$('.sections').eq(section).hide(250);
		$('.sections').eq(section-1).fadeIn(1500);
		window.location.hash = 'section-'+(section);
		if(section<2){
			$('.bttn.prev').hide();
		}
		if(section >=2 ){
			$('.bttn.next').show();
		}
		$('.bttn.finish').hide();
		left_sec_handle();
		return;
	});
	$('.bttn.next').die().live('click',function (e){
		e.preventDefault();
		$('.bttn.next').parents('table.hidden').show();
		var section = parseInt(window.location.hash.replace('#section-',''));
		// console.debug(section);
		var url 	= site_url+'home/signup/'+section+'/';
		var getdata = $('.sections:eq('+(section-1)+') form').serializeArray();
		var aerr = function (){
			$('#messages').html('<div class="ui-state-error p10"><h1>Error: </h1><p>There was a problem with this page. </p><p>Please contact an administrator so we can fix this error.</p><p> We appologise for the inconvenience this may have cause.</p></div>')
			.siblings('a').trigger('click');
			if ($('#errorLink').length)
			$('#errorLink').fancybox().trigger('click');
		};
		$.ajax({
			type		: 'POST',
			url			: url,
			dataType	: 'json',
			cache		: false,
			data		: getdata,
			success		: function (ret){
				if(!ret){var ret = {success :false,error:false}}
				var success = ret.success || false;
				var error   = ret.error   || aerr;
				
				// if(section>=6){
					// alert('Me');
					// return;
				// }
				if(ret.success && !ret.error){
					// console.log(section);
					if(section==6){
						$('#messages').html(ret.successMessage)
						.siblings('a').trigger('click');
						
						if($('#errorLink'))
						$('#errorLink')
							.fancybox({autoDimensions:false,width:800,height:'auto'})
							.fancybox()
							.trigger('click');
					}else{
						$('.sections').eq(section-1).hide();
						$('.sections').eq(section).fadeIn(1500);
						if(section>0){
							$('.bttn.next').show();
							if(section >=1){
								$('.bttn.prev').show();
							}
						}
						if(section < 5){
							$('.bttn.finish').hide();
						}
						if(section==5){
							$('.bttn.next').hide();
							$('.bttn.finish').show().addClass('next');
							// $('.bttn.finish').show().addClass('next').click(function (){
								// $('.bttn.next').click();
							// });
						}
						window.location.hash= 'section-'+((section)+1);
					}
					left_sec_handle();
				}else{
					$('#messages').html(error)
					.siblings('a').trigger('click');
					$('#errorLink').fancybox().trigger('click');
				}
			},
			error		: aerr
		});
		left_sec_handle();
		return;
	});
	// referer
	$('#refererbuttons').buttonset().children('input').each(function(i,n){
		$(n).click(function (){
			$('.'+$('#refererbuttons input:checked').attr('id')).show().siblings('span').hide();
			$('#referVal').show();
		});
	});
	if($('#refererbuttons input:checked').length){
		$('.'+$('#refererbuttons input:checked').attr('id')).show().siblings('span').hide();
		$('#referVal').show();
	}
	
	
	
	
	// $('input[placeholder]').placeholder();
	if(!window.Modernizr){
		$('input[placeholder]').each(function (i,n){
			$(n).val($(n).attr('placeholder')).addClass('hasPlaceholder')
			.focus(function(){
				if($(n).val() == $(n).attr('placeholder')){
					$(n).val('')
					$(n).removeClass('hasPlaceholder');
				}
			}).blur(function(){
				if($(n).val() == ''){
					$(n).val($(n).attr('placeholder'))
					$(n).addClass('hasPlaceholder');
				}
			});
		});
	}
	//placeholders
	
});
