/*=======================================================================*\
|| #####################################################################  ||
|| # This script is Made by Brainwaves								   #  ||
|| # ----------------------------------------------------------------- #  ||
|| # Copyright Creativityblast Studios Ltd. All Rights Reserved. 	   #  ||
|| # This Script may not be redistributed in whole or significant part.#  ||
|| # ----------------------------------------------------------------- #  ||
|| # http://creativityblast.com									       #  ||
|| #####################################################################  ||
\*=======================================================================*/
//File Version 1.1

$(document).ready(function(){

	//Call Close button function:
	$('.closeBtn').live('click', function() { 
		$(this).parent().fadeTo(400, 0, function () { // Links with the class "close" will close parent
			$(this).slideUp(400);
		});	
		return false;
	});
	
	// Alternating table rows:
		$('.styleTable tbody tr:even').addClass("alt-row"); // Add class "alt-row" to even table rows
		$('.styleTable tbody tr table tbody tr').removeClass("alt-row");
		$('.styleTable tbody tr table tbody tr:even').addClass("alt-row");
	
    // Initialise Facebox Modal window:
		$('a[rel*=modal]').facebox(); // Applies modal window to any link with attribute rel="modal"
		$(document).bind('reveal.facebox', function() { 
			formModal();
		});

	// Form ajax submit
	var loader = jQuery('<img src="/resources/images/loading.gif" style="vertical-align:bottom;" />')
		.appendTo(".loader")
		.hide();

    var options = { 
        target:        '#ajaxTarget',   // target element(s) to be updated with server response 
        beforeSubmit:  showRequest,  // pre-submit callback 
        success:       showResponse
    }; 
    $('.ajax').ajaxForm(options);
	
	var loaderModal = jQuery('<img src="/resources/images/loading.gif" style="vertical-align:bottom;" />')
		.appendTo(".loaderModal")
		.hide();
		
	var formModal = function()
	{ 
		// Form ajax submit
	    var options = { 
	        target:        '#ajaxTargetModal',   // target element(s) to be updated with server response 
	        beforeSubmit:  showRequest,
	        success:       showResponse  // post-submit callback 
	    }; 
	    $('.ajaxModal').ajaxForm(options);
	}

	// pre-submit callback 
	function showRequest(formData, jqForm, options) {
		//curRequest = jqForm; 
		var form = jqForm[0]; 

		var formID=''; if(form.id) formID="#"+form.id+" ";

		var kill=false;
		$(formID+" input.alert").removeClass('alert');
		$(formID+" input.required").each(function(index) {
			if( !$(this).val() ){ $(this).addClass("alert"); kill=true; }
		});
		if(kill){ $(formID+".loader").html('Please fill the required fields'); return false; }
		
		$(formID+".loader").html('<img src="/resources/images/loading.gif" style="vertical-align:middle;" />');
		
		$("input[type=submit]").attr('disabled','disabled');
	    return true; 
	} 

	// post-submit callback 
	function showResponse(responseText, statusText)  { 
		loader.hide();
		$(".loader").html('');
		$(".loaderModal").html('');
		
		var offset = '';
		offset = ($("#ajaxTarget").offset().top)-70;
		if($('#ajaxTargetModal').length != 0) offset = ($("#ajaxTargetModal").offset().top)-124;
		
	 	$("input[type=submit]").removeAttr('disabled');
		$('html, body').animate({scrollTop:offset}, 'slow');
		
		var pattern = /<div id="ajaxScript"[^>]+>([^Ω]+)ajaxScriptStop<\/div>/;
		var matches = responseText.match(pattern);
		if(matches)
		{
			eval(matches[1]);
		}

	}
	
	$(".ajax.instant").ajaxSubmit(options); //submit right away
	
	$('.confirmFirst').live('click', function() { 
		if( !confirm('Are you sure? Click OK to continue') ) return false;
	});

	$('.num-only').bind('keypress', function(e) { 
		return ( e.which!=8 && e.which!=0 && (e.which<48 || e.which>57)) ? false : true ;
	})

	$('a[title]').qtip({ style: { name: 'dark', border: { width: 3, radius: 5 }, tip: true } })

	//link with rel ajaxLink
	$('a[rel=ajaxLink]').live('click', function() { 
		if( $(this).is(".confirmFirstLink") )
		{
			if( !confirm('Are you sure? Click OK to continue') ) return false;
		}
		
		var link = $(this).attr('href');
		var id = $(this).parent().parent().attr('id');
		
		$.ajax({
			type: "GET",
			url: link,
			beforeSend: function () {
				$("#"+id+" .loader").html('<img src="/resources/images/loading.gif" style="vertical-align:bottom;" />');
			},
			success: function(msg){
				$("#"+id+" .loader").html('');
				$("#ajaxTarget").html(msg);
				
				var offset = '';			
				offset = ($("#ajaxTarget").offset().top)-70;
			 	$("input[type=submit]").removeAttr('disabled');
				$('html, body').animate({scrollTop:offset}, 'slow');
				
				var pattern0 = /<div id="ajaxScript"[^>]+>([^Ω]+)ajaxScriptStop<\/div>/;
				var matches0 = msg.match(pattern0);
				if(matches0[1]) eval(matches0[1]);
	      	}
		});
		return false;
	});

}); 

function limitChars(textid, limit, infodiv)
{
	var text = $(textid).val();	
	var textlength = text.length;
	if(textlength > limit)
	{
		$(infodiv).html('You cannot write more then '+limit+' characters!');
		$(textid).val(text.substr(0,limit));
		return false;
	}
	else
	{
		$(infodiv).html('You have '+ (limit - textlength) +' characters left.');
		return true;
	}
}
