(function($){	
	
	// -------------------------------------------------------------------------------------------------------
	// Form Validation script - used by the Contact Form script
	// -------------------------------------------------------------------------------------------------------
	
	function validateMyAjaxInputs() {

		$.validity.start();
		// Validator methods go here:
		$("#name").require();
		$("#email").require().match("email");
		$("#subject").require();	

		// End the validation session:
		var result = $.validity.end();
		return result.valid;
	}
	
	// -------------------------------------------------------------------------------------------------------
	// Form Validation script - used by the Contact Form script
	// -------------------------------------------------------------------------------------------------------
	
	function validateJoinFormAjaxInputs() {

		$.validity.start();
		// Validator methods go here:
		$("#email").require();
		$("#email2").require();
		$("#email").require().match("email");
		$("#password").require();
		$("#password2").require();	

		// End the validation session:
		var result = $.validity.end();
		return result.valid;
	}
	
	// -------------------------------------------------------------------------------------------------------
	// Form Clear
	// -------------------------------------------------------------------------------------------------------
	
	$.fn.clearForm = function() {
	  return this.each(function() {
	 var type = this.type, tag = this.tagName.toLowerCase();
	 if (tag == 'form')
	   return $(':input',this).clearForm();
	 if (type == 'text' || type == 'password' || tag == 'textarea')
	   this.value = '';
	 else if (type == 'checkbox' || type == 'radio')
	   this.checked = false;
	 else if (tag == 'select')
	   this.selectedIndex = -1;
	  });
	};

	$(document).ready(function(){
//////////////////////////////////////////////////////////////////////////////////////////////////////////////						   				
		var lastValue = '';
		var lastId = '';

		$("input[type=submit]").click(function () 
		{
			// $('.noticemsg').fadeOut(100);
			
			lastId = $(this).attr('id');
			lastValue = $(this).val();
			
			//$(this).attr('disabled', true);
			$(this).attr('id', 'ButtonID');
			//$(this).val('Please wait..');
			
			//setTimeout("$('#ButtonID').attr('disabled', false);", 15000);
			// setTimeout("$('#ButtonID').val('" + lastValue + "');", 15000);
			// setTimeout("$('#ButtonID').attr('id', '" + lastId + "');", 15000);
		});

		// -------------------------------------------------------------------------------------------------------
		// Dropdown Menu
		// -------------------------------------------------------------------------------------------------------
		
		$("ul#dropdown-menu li").hover(function () {
												 
			$(this).addClass("hover");
			$('ul:first', this).css({visibility: "visible",display: "none"}).slideDown(200);
		}, function () {
			
			$(this).removeClass("hover");
			$('ul:first', this).css({visibility: "hidden"});
		});
		
		if ( ! ( $.browser.msie && ($.browser.version == 6) ) ){
			$("ul#dropdown-menu li ul li:has(ul)").find("a:first").addClass("arrow");
		}
								
		// -------------------------------------------------------------------------------------------------------
		// Hover over Services Overview + make entire service overview clickable
		// -------------------------------------------------------------------------------------------------------
		
		$(".services-overview-overlay").hide();
		
		 $("#services-overview li").hover(
			  function(){$(this).find('.services-overview-overlay').show();},
			  function(){$(this).find('.services-overview-overlay').hide();}
			);
		 
		 $("#services-overview li").click(function(){
			 window.location=$(this).find("a").attr("href");
			 return false;
		});
		 
		// -------------------------------------------------------------------------------------------------------
		// Contact Form 
		// -------------------------------------------------------------------------------------------------------
		
		/*
		$("#contact-form").submit(function () {
											
			if (validateMyAjaxInputs()) { //  procced only if form has been validated ok with validity
				var str = $(this).serialize();
				$.ajax({
					type: "POST",
					url: "contact",
					data: str,
					success: function (msg) {
						$("#formstatus").ajaxComplete(function (event, request, settings) {
							if (msg == 'OK') { // Message Sent? Show the 'Thank You' message
								result = '<div class="successmsg">Your message has been sent. Thank you!</div>';
								$('#contact-form').clearForm();
							} else {
								result = msg;
							}
							$(this).html(result);
						});
					}
		
				});
				return false;
			}
		});
		*/
		
		// -------------------------------------------------------------------------------------------------------
		// Join Form 
		// -------------------------------------------------------------------------------------------------------
		
		$("#ajax-form, #contact-form, #join-form, #login-form, #forgot-form, #send-bulk-form, #ajax-form-html").submit(function () {
					
			if (this.id == 'ajax-form' || this.id == 'contact-form' || validateJoinFormAjaxInputs()) { //  procced only if form has been validated ok with validity
				var str = $(this).serialize();
				var id = $(this).attr('id');
						
				$.ajaxSetup({
					cache: false,
					timeout: 1000000
				});
			
				if (id == 'ajax-form-html' && $("#ajax-form-output").html() == '') {
					$("#ajax-form-output").fadeIn(100, function() {
						$("#ajax-form-output").html('<img src="system/template/admin.template/img/ajax-loader.gif" border="0">');
					});
				}
				
				// alert(id);
				
				//  (id == 'ajax-form-html') {
					// $(this).attr('disabled', true);
					
				$(this).val('Please wait..');
				$('#ButtonID').attr('disabled', true);
					
				if (lastId != '') {
					$('#' + lastId).attr('disabled', true);
				}
				
				// }
				// $(this).attr('disabled', true);
				
				$('#ButtonID').val('Please wait..');
				
				// setTimeout("$('#ButtonID').attr('disabled', false);", 15000);
				// setTimeout("$('#ButtonID').val('" + lastValue + "');", 15000);
				
				$('#formstatus div').fadeOut(300);
				
				$.ajax({
					type: "POST",
					url: $(this).attr('title'),
					data: str,
					cache: false,
					success: function (msg) {
						if (id == 'ajax-form-html') {
							$('#ButtonID').attr('disabled', false);
							if (lastId != '') {
								$('#' + lastId).attr('disabled', false);
							}
							$('#ButtonID').val(lastValue);
							$('#ButtonID').attr('id', '');
							
							$("#ajax-form-output").html(msg);
							
							// $("input[type=submit]").attr('disabled', false);
							
							/*
							$("#ajax-form-output").fadeOut(10, function() {
								$("#ajax-form-output").html(msg);
								$("#ajax-form-output").fadeIn(600);
							});
							*/
								
//							$("#ajax-form-output").ajaxComplete(function (event, request, settings) {
//								$(this).fadeOut(10, function() {
//									$(this).html(msg);
//									$(this).fadeIn(600);
//								});
//							});
						}
						else {
							$("#formstatus").ajaxComplete(function (event, request, settings) {
								$('#ButtonID').attr('disabled', false);
								if (lastId != '') {
									$('#' + lastId).attr('disabled', false);
								}
								$('#ButtonID').val(lastValue);
								$('#ButtonID').attr('id', '');
								
								if (msg == 'OK') { // Message Sent? Show the 'Thank You' message
									result = '<div class="successmsg">Your message has been sent. Thank you!</div>';
									// $('#join-form').clearForm();
								} else {
									result = msg;
								}
								
								$(this).fadeOut(200, function() {
									$(this).html(result);
									$(this).fadeIn(600);
								});
							});
						}
					}
		
				});
				return false;
			}
		});
		
		// -------------------------------------------------------------------------------------------------------
		// Protfolio Fade 
		// -------------------------------------------------------------------------------------------------------
		
		if ($.browser.msie && $.browser.version < 7) return;

			$(".portfolio-item-preview img").fadeTo(1, 1);
			$(".portfolio-item-preview img").hover(
			
			function () {
				$(this).fadeTo("fast", 0.80);
			}, function () {
				$(this).fadeTo("slow", 1);
			});

		// -------------------------------------------------------------------------------------------------------
		// Blog links 
		// -------------------------------------------------------------------------------------------------------
		
		$("#blog-older-posts").click(function () {
			$('.older-post').hide(500, function() {
				$('.new-post').show();
			}); 
		})
		
		$("#blog-new-posts").click(function () {
			$('.new-post').fadeOut(500, function() { 
				$('.older-post').fadeIn(500); 
			});
		})
		
	//////////////////////////////////////////////////////////////////////////////////////////////////////////////	
	});
	
	setTimeout("$('.noticemsg').fadeIn(600);", 2000);
	setTimeout("bindTo();", 2000);
	
})(window.jQuery);	

// non jQuery scripts below

function bindTo() {
	
	$( ".send-bulk-sms-to" )
		// don't navigate away from the field on tab when selecting an item
		.bind( "keydown", function( event ) {
			if ( event.keyCode === $.ui.keyCode.TAB &&
					$( this ).data( "autocomplete" ).menu.active ) {
				event.preventDefault();
			}
		})
		.autocomplete({
			source: function( request, response ) {
				$.getJSON( "index.php?autocomplete=to", {
					term: extractLast( request.term )
				}, response );
			},
			search: function() {
				// custom minLength
				var term = extractLast( this.value );
				if ( term.length < 2 ) {
					return false;
				}
			},
			focus: function() {
				// prevent value inserted on focus
				return false;
			},
			select: function( event, ui ) {
				var terms = split( this.value );
				// remove the current input
				terms.pop();
				// add the selected item
				terms.push( ui.item.value );
				// add placeholder to get the comma-and-space at the end
				terms.push( "" );
				this.value = terms.join( ", " );
				return false;
			}
	});
}

function split( val ) {
	return val.split( /,\s*/ );
}
function extractLast( term ) {
	return split( term ).pop();
}

function checkLimit(limitNum) {

	if ($("#message").val().length > limitNum) {
		$("#message").val($("#message").val().substring(0, limitNum));
	} else {
		$("#countdown").html(limitNum - $("#message").val().length);
	}

	var smsCount = 0;

	if ($("#message").val().length > 0)   smsCount = 1;
	if ($("#message").val().length > 160) smsCount = 2;
	if ($("#message").val().length > 306) smsCount = 3;
	if ($("#message").val().length > 459) smsCount = 4;

	$("#messagecount").html(smsCount);
	$("#messagelimit").html(limitNum - $("#message").val().length);
}

function checkLimxit(limit) {
	if ($("#message").val().length > limit) {

	}
}

function checkCost() {
	$("#price_alert").fadeOut(500, function() {
		if (!$('#to').val() || !$('#from').val() || !$('#from').val() || !$('#group').val()) return false;
		
		$("#price_alert").fadeIn(500);
	})
}
    	
