$.fn.extend({ 
	disableSelection : function() { 
		this.each(function() { 
				this.onselectstart = function() { return false; }; 
				this.unselectable = "on"; 
				jQuery(this).css('-moz-user-select', 'none'); 
		}); 
	} 
});

$(function() {				   
	$('<img />').attr('src', '/images/grabbing.cur');
			
	function auto_shift(){
		var go = parseInt(wrapper.css('left'))-980;
		
		wrapper.draggable('disable').animate({
			left: go
		}, 200, function(){
			wrapper.animate({
				left: parseInt(wrapper.css("left"))+980
			}, 1, function(){
				wrapper.children().filter(':first').appendTo(wrapper);
				wrapper.draggable('enable');
			})
			
			handle_position_change(wrapper.css('left'));
		});		
		
		timeout = setTimeout(function(){ auto_shift() }, 15000);
	}
		   
	var wrapper = $(".wrapper"),
		children = wrapper.children().length,
		fix_offset = $('html').height() == ($(window).height()+1),
		offset = (fix_offset ? $('#carousel_ref').offset().left+10 : $('#carousel_ref').offset().left);
		
	if(children > 1){
		timeout = setTimeout(function(){ auto_shift() }, 15000);
	}
	
	wrapper.disableSelection();
	
	function is_float(num){
		num = String(num);
		
		var regex = /(([1-9]+\.[0-9]*)|([1-9]*\.[0-9]+)|([1-9]+))([eE][-+]?[0-9]+)?/,
			result = num.match(regex);
		
		return (!result ? false : true);
	}
										 
	function init_position(){
		var start_slide = children / 2;
		if(is_float(start_slide)){
			start_slide = Math.floor(start_slide);
		}
		
		var start_pos = (start_slide * 980);
			start_pos = (start_pos - offset) + 10;
			start_pos = (start_pos - (start_pos * 2));
					
		wrapper.css({
			left: start_pos,
			width: (children * 980)
		})
	}
	
	function handle_position_change(left){
		left = parseInt(left);
		
		var current_live_pos = (offset - left);
			current_live_pos = Math.ceil(current_live_pos-10);
			current_live_pos = (current_live_pos != 1 ? current_live_pos / 980 : 0);
					
		wrapper.find('.active').removeClass('active');
		wrapper.children().eq(current_live_pos).addClass('active');
	}
	
	if(children > 1){
		
		wrapper.draggable({
			axis: 'x',
			start: function(e, ui) {
				$('.instruction').hide();
				start_position = ui.originalPosition.left;
				wrapper.draggable('disable');
				clearTimeout(timeout);
			},
			stop: function(e, ui) {					
				var start = ui.originalPosition.left
					stop = ui.position.left,
					diff = stop-start;
						
				if(diff > 0){
					var go = start + 980;
					ui.helper.animate({
						left: go
					}, 200, function(){
						wrapper.animate({
							left: parseInt(wrapper.css("left"))-980
						}, 1, function(){
							wrapper.children().filter(':last').prependTo(wrapper);
							wrapper.draggable('enable');
							timeout = setTimeout(function(){ auto_shift() }, 15000);
						})
						
						handle_position_change(ui.helper.css('left'));
					});	
				}
				else {
					var go = start - 980;
					ui.helper.animate({
						left: go
					}, 200, function(){
						wrapper.animate({
							left: parseInt(wrapper.css("left"))+980
						}, 1, function(){
							wrapper.children().filter(':first').appendTo(wrapper);
							wrapper.draggable('enable');
							timeout = setTimeout(function(){ auto_shift() }, 15000);
						})
						
						handle_position_change(ui.helper.css('left'));
					});	
				}
			}
		}).mouseup(function(){
			wrapper.css('cursor', 'url(../images/grab.cur), default');
		});
		
		wrapper.find('div').mousedown(function(){
			wrapper.css('cursor', 'url(../images/grabbing.cur), default');
			wrapper.trigger('mousemove');
		})
	}
	
	$('.colorbox, .client_login a').colorbox({
		initialWidth: 10,
		initialHeight: 10,
		height: 673,
		onComplete: function(){
			$('#crm_form_1 input, #crm_form_1 textarea').each(function(){
				var t = $(this),
					form = $('#crm_form_1'),
					val = t.val(),
					id = t.attr('id'),
					label = form.find('label').filter('[for="'+id+'"]');
					
				label.find('span').replaceWith('*');
				var label_html = label.html();
				
				if(!val.length){
					t.val(label_html)	
				}
				
				$(this).css('filter', '');
			})
			
			$('#cboxLoadedContent input#username, #cboxLoadedContent input#password').each(function(){
																									
				var t = $(this),
					form = $('#cboxLoadedContent form'),
					val = t.val(),
					id = t.attr('id'),
					label = form.find('label').filter('[for="'+id+'"]'),
					label_html = label.html();
								
				if(!val.length){
					t.val(label_html)	
				}
				
				$(this).css('filter', '');
				
			}).live('focus blur', function(e){
				
				var t = $(this),
					form = $('#cboxLoadedContent form'),
					val = t.val(),
					id = t.attr('id'),
					label = form.find('label').filter('[for="'+id+'"]'),
					label_html = label.html();
				
				if(e.type == 'focusin'){
					if(val == label_html){
						t.val('');
					}
				}
				else {
					if(!val.length){
						t.val(label_html);
					}
				}
				
			})
		}
	})
	
	$('#crm_form_1 input.text, #crm_form_1 textarea').live('focus blur', function(e){
		var t = $(this),
			form = $('#crm_form_1'),
			val = t.val(),
			id = t.attr('id'),
			label = form.find('label').filter('[for="'+id+'"]');
			
		label.find('span').replaceWith('*');
		var label_html = label.html();
		
		if(e.type == 'focusin'){
			if(val == label_html){
				t.val('');
			}
		}
		else {
			if(!val.length){
				t.val(label_html);
			}
		}
	})	
	
	$('#crm_form_1').live('submit', function(){
		$('#crm_form_1 input, #crm_form_1 textarea').not('[type="hidden"], [type="submit"]').each(function(){
			var t = $(this),
				form = $('#crm_form_1'),
				val = t.val(),
				id = t.attr('id'),
				label = form.find('label').filter('[for="'+id+'"]');
			
			label.find('span').replaceWith('*');
			var label_html = label.html();
				
			if(label_html.length){
				if(val == label_html){
					t.val('');
				}
			}
		})
											 
		var data = $(this).serialize();
		
		$.ajax({
			type: "POST",
			url: "/contact-us",
			data: data,
			success: function(html){
				$('#cboxLoadedContent').html(html);
				
				$('#crm_form_1 input, #crm_form_1 textarea').each(function(){
					var t = $(this),
						form = $('#crm_form_1'),
						val = t.val(),
						id = t.attr('id'),
						label = form.find('label').filter('[for="'+id+'"]');
					
					label.find('span').replaceWith('*');
					var label_html = label.html();
					
					if(!val.length){
						t.val(label_html)	
					}
				})
			}
		});
		
		return false; 
	})
	
	$('.navbar_services_navigation li li.curr_page').parents('li').addClass('curr_page');
	
	wrapper.find('.slide').append('<img src="/images/carousel_instruction.png" class="instruction" />').hover(function(){
		$(this).find('.instruction').show();																										   
	}, function(){
		$(this).find('.instruction').hide();		
	});
	
	$('#horizontal_nav li:last').css({
		paddingRight: 0,
		marginRight: 0
	});
	
	if(children > 1){
		init_position();
		handle_position_change(wrapper.css('left'));
	}
	else {
		wrapper.css({
			left: offset,
			cursor: 'auto'
		});
		wrapper.children().fadeTo(1, 1, function(){  });
	}
});
