/**
 * Main.js - GXBill Site 
 *
 * created by Vincent Perlerin for GXBill - vperlerin[at]gmail.com
 *
 */
 
 
jQuery.fn.extend({ 

	// Display Ajax loading animation inside an element
	// arg = true: replace the whole content / false: add to the content
	ajaxWait: function(cont, classes)  {
		var loader = $('<img src="/_img/small-loader.gif"  class="wait '+classes+'"/>');
		$("body").addClass("curWait"); 
		if(cont) $(this).html(''); 
		$(this).addClass('waitHolder');
		loader.appendTo($(this));
	},
		
	// Remove Ajax Anim
	ajaxDone: function() {
		$("body").removeClass("curWait"); 	
		$(this).removeClass('waitHolder');
		$('.wait',$(this)).remove();
	},



	/* Toggle faq answers */
	toggleAnswer: function() {
			$(this).next().slideToggle('fast'); 
			return false;
	},
	
	
	/* Open Merchant Login Modal */
	merchantLogin: function() {
		if($('#iframeContainer').length==0) {
			$('<div id="iframeContainer" />').appendTo($('body'));
			$('<iframe id="merchantLoginFrame" src="/merchant_login" frameborder="0" width="500" height="320" />').appendTo($('#iframeContainer'));
		}
		
		$('#iframeContainer').dialog({
                    title: "GXBill Merchant Login",
					autoOpen: true,
                    modal: true,
                    resizable: true,
					autoResize: false,
					width:500,
					heigth:320
                   
        });
		return false;
	}, 
	
	
	/* Display a flag based on "rel" in <a class="flag" */
	flagged: function() {
		var t = $(this), rel = t.attr('rel');
		t.html('<img src="/_img/flags/'+rel+'.png" />');	
	},
	
	
	/* Display/Hide Tabs based on click */
	tab:function() {
		var t = $(this);
		$('.tab_div').hide();
		$('#'+t.attr('rel')).show();
		$('li',t.parent().parent()).removeClass('cur');
		t.parent().addClass('cur');	
		return false;
	}
	
})


function hideErrorFooter() { $('#errorLoginFooter').slideUp(); $('input[name=username]','#login_footer').val("Username");}
 
$(function() { 				

	$('#phone_postit').animate({ opacity: 0, left: '+=20', height: 'toggle'},5).animate({ opacity: 1, left: '-=20', height: 'toggle'},400);

	/* Post-it with phone numbers draggable */
	$( "#phone_postit" ).draggable().dblclick(function() { $(this).animate({ opacity: 0.1, height: 'toggle'},500,function() { $(this).remove() }); }); 
	
	/* Hide/Show answers on FAQ*/
	$('.q').each(function() { $(this).next().hide(); })
	$('.q').click(function() {  $(this).toggleAnswer();  return false;});
	
	/* Flags */
	$('.flag').each(function() { $(this).flagged(); });
	
	/* Login from Footer */
	$('#login_footer').submit(function() {
		
		var mask = $('<div id="mask" class="srd c"/>'), innermask = $('<div class="in"/>') , form = $('#footer form');
		mask.css({"position":"absolute" ,"background":"#fff","width":form.innerWidth()+"px","height":form.innerHeight()+"px","top":0, left:"0"});
		innermask.appendTo(mask);
		mask.appendTo(form);
		innermask.ajaxWait(false,'');
		
		$.post("/api/auth_user/ ",$(this).serialize(),function(data) { 
			if(data=="true") window.parent.location = "/merchants";
			else  $('#errorLoginFooter').show(); 
			mask.ajaxDone();
			mask.remove();
		})
							
		return false;
	});
	
	
	
	/* Forgot Password */
	$('#forgotsingle').submit(function() {
			var t = $(this);
			$('h2',t).ajaxWait(false,'r v_align_mid');
			
			$.post("/api/send_password/",t.serialize(),function(data) { 
				if(data=="true") { $('#confForgot').show(); $('#forgot').hide(); }
				else  			   $('#errorForgot').show(); 
				$('h2',t).ajaxDone();
			})
										
			return false;
	});
		
});

