// jQuery extension /
jQuery.fn.center = function () {
    this.css("position","absolute");
    this.css("top", (($(window).height() - this.outerHeight()) / 2) + $(window).scrollTop() + "px");
    this.css("left", (($(window).width() - this.outerWidth()) / 2) + $(window).scrollLeft() + "px");
    return this;
}






// JavaScript Document
function print_r(){
	var obj = arguments[0];
	var depth = arguments[1] || 0;
	var rounds = arguments[2] || 0;
	var _return = arguments[3] || false;
	
	var t = "";	for (var a=0; a<rounds; a++)	t += "\t";
	
	var s = "";
	for (var a in obj){
		if(rounds>0) s += "\n"+t+"----\n";
		else s += "\n----------------------------\n";
		
		s += t+a+" => "+obj[a];
		
		if(depth>rounds && typeof(obj[a])!='string')
			s += print_r(obj[a], depth, (rounds+1), true) + "\n---\n";
			//s += "\n--start-sub--\n" + print_r(obj[a], depth, (rounds+1), true) + "\n-end-sub-\n";
	}
	//alert(s +"\n\n"+ "rounds:"+rounds + "\n depth:"+depth +"\n t:"+t+"." );
	if(_return) return s;
	else alert(s);
}


	function getForm(F){
		var postData = {};
			$(F).find("input[type='text'], select, input[type='hidden'], input[type='password'], textarea, :checked").each(function(){
				if($(this).attr('name') && $(this).attr('name').length>0)
					postData[$(this).attr('name')] = $(this).val();
			});
		return postData;
	}


///////////////////// jQuery dependent functions.
// getParentTag(from, tagName);
function getParentTag(){
	var from = arguments[0];
	var tagName = arguments[1];
	var r = arguments[2] || 0;
	r++;
	
	if(r>50) return false;
	
	if( $(from).is(tagName) ) {
		return from;
	}
	else{
		return getParentTag($(from).parent(), tagName, r);
	}
}



	function popIn(A,name,obj){
		 $.blockUI({ message: $('#'+name), css: {
			 										width: (obj.width)?(obj.width+"px") : '350px',
													height: (obj.height)?(obj.height+"px") : '150px',
													'margin':'0 0 0 100px',
													border: '1px #FFF solid',
													padding: '15px',
													backgroundColor: (obj.bg)?(obj.bg): '#000',
													'-webkit-border-radius': '10px',
													'-moz-border-radius': '10px',
													color: (obj.color)?(obj.color): '#fff',
													cursor: 'normal',
													display: (obj.display) ? (obj.display) :''
													} });
		$('.blockOverlay').attr('title','Click to unblock').click($.unblockUI);
	}
	
/***** Login form handeler ****************/
	function openLoginWin(A){
		 $.blockUI({ message: $('#loginBoxBlock'), css: {
			 										width: '350px',
													height:'150px',
													'margin':'0 0 0 100px',
													border: '1px #FFF solid',
													padding: '15px',
													backgroundColor: '#000',
													'-webkit-border-radius': '10px',
													'-moz-border-radius': '10px',
													color: '#fff',
													cursor: 'normal'
													} });
		$('.blockOverlay').attr('title','Click to unblock').click($.unblockUI);
	}
	function doLogin(F){
		if(F.user_name.value.length<1){ alert("Please provide your Username!"); F.user_name.focus(); return false; }
		if(F.user_password.value.length<1){ alert("Please provide your Password!"); F.user_password.focus(); return false; }
		
		$(F).find('.loader').remove();
		
		/*
		var postData = {};
			$(F).find("input[type='text'], input[type='password'], select").each(function(){
				postData[$(this).attr('name')] = $(this).val();
			});
			postData.ajaxAction = 'login';
		*/
		$(F).prepend("<div class='loader'>Processing your request.. please wait..</div>");
		$(F).find('input[type="submit"]').val('Processing...').attr({'disabled':'disabled'}).get(0).blur();
		
		
		return true;
		//F.submit();
		/*
		$.post('ajax/login.php',postData,function(data,status){
			if(data.indexOf('ERROR')>-1){
				$(F).find('.loader').html(data);
				$(F).find('input[type="submit"]').val('Login').removeAttr('disabled');
			}
			else{
				//var loc = document.location.href.toString();
				//var loc = document.location.toString();
				//document.location = loc;
				//$(F).find('.loader').remove();
				$(F).html("<div>Logging in please wait...</div>"+data);
			}
		}, 'html');
		*/
	}
	function emptyElement(element){
		element.value='';
	}
	
	function validateEmail(address) {
	   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	   if(reg.test(address) == false) {
		  //alert('Invalid Email Address');
		  return false;
	   }
	   return true;
	}
	
	
/* Form Tool Tip Function */
function addTT(field,message,type){
	var fName = $(field).attr('name');
	var TT_name = 'TT_'+fName;
	var TT = $(field).parent().find('#'+TT_name).get(0);
	if(TT){
		$(TT).html('<div class="TT_tip tip_'+type+'">'+message+'</div>');
	}
	else{
		$(field).after('<div id="'+TT_name+'" class="tip_wrapper"><div class="TT_tip tip_'+type+'">'+message+'</div></div>');
	}
	
	var callerTopPosition = $(field).offset().top;
	var callerleftPosition = $(field).offset().left;
	
	var callerWidth =  $(field).width();
	//var inputHeight = $(divFormError).height();
			
	$(field).parent().find('#'+TT_name).css({top:callerTopPosition, left:callerleftPosition+callerWidth+10});

	$(field).parent().find('#'+TT_name).fadeIn('slow');
	field.focus();
	/*
	$(field).blur(function() {
		$(this).parent().find('#'+TT_name).remove();
	});
	*/
}
function removeTT(field){
	var fName = $(field).attr('name');
	var TT_name = 'TT_'+fName;
	var TT = $(field).parent().find('#'+TT_name).get(0);
	if(TT){
		$(field).parent().find('#'+TT_name).remove();
	}
}


//blockUiPop(blockID, url, opt)/
function blockUiPop(){
	var blockID = arguments[0] ? arguments[0] : 'voucherpop_new_01';
	var url = arguments[1] ? arguments[1] : false;
		if(!url){ alert("No pop url found"); return false; }
		
	var postVars = {arguments0:blockID, arguments1:url};
		//postVars.arguments0 = blockID;
		//postVars.arguments1 = url;
	/*if(url.indexOf('?')>-1){
		var parts = url.split("?");
		parts = parts[1].split('&');
	}*/
		
	var opt = arguments[2] || {};
		if(!opt.width) opt.width = '571px';
		//if(!opt.height) opt.height = '350px';
		//if(!opt.margin) opt.margin = '0 0 0 100px';
		//if(!opt.padding) opt.padding = '0px';
		if(!opt.bg) opt.bg = '#FFF';
		if(!opt.border) opt.border = '10px #282a25 solid';
		//if(!opt.radious) opt.radious = '10px';
		if(!opt.color) opt.color = '#000';
		if(!opt.cursor) opt.cursor = 'normal';
		
	var css = {};
		if(opt.width)     css.width=opt.width;
		if(opt.height)    css.height=opt.height;
		if(opt.margin)    css.margin=opt.margin;
		if(opt.padding)   css.padding=opt.padding;
		if(opt.bg)        css.bg=opt.bg;
		if(opt.border)    css.border=opt.border;
		if(opt.radious)   css.radious=opt.radious;
		if(opt.color)     css.color=opt.color;
		if(opt.cursor)    css.cursor=opt.cursor;
	
	if(window.console) console.log("opt.width: "+ opt.width);
	
	$.blockUI({ 'css':css,
				 message: $('<div id="'+blockID+'">Loading please wait..</h1>') 
			});
	$('.blockOverlay').attr('title','Click to unblock').click($.unblockUI);
	centerObj('.blockUI.blockMsg.blockPage');
	
	$('#'+blockID).load(url, postVars, function(data){
	});
}

var interval;
function centerObj(o){
	if(!interval)  interval = setInterval(  "centerObj('"+o+"')", 1  );
	if($(o).get(0)){
		$(o).center();
		//interval = setInterval(  "centerObj('"+o+"')", 1  );
	}else{
		clearInterval(interval);
		interval=false;
	}
}

/*function getPopInfo(obj){
		$.post('<?=WEBPATH?>ajax/getPopInfo.php',obj,function(data,status){
			$('#voucherpop_02').html(data);
		}, 'html');
	}*/

function openMyPop(popID, fields, optons){
	
		if(!$(popID).get(0)){ alert("Popup not found"); return false; }
		
		var opt = optons || {};
		if(!opt.width) opt.width = '520px';
		if(!opt.height) opt.height = '350px';
		if(!opt.margin) opt.margin = '0 0 0 100px';
		if(!opt.padding) opt.padding = '0px';
		if(!opt.bg) opt.bg = '#FFF';
		if(!opt.border) opt.border = '10px #282a25 solid';
		if(!opt.radious) opt.radious = '10px';
		if(!opt.color) opt.color = '#000';
		if(!opt.cursor) opt.cursor = 'normal';
		// replacing field values /
		for (var h in fields){
			$(popID).find('#'+h).val(fields[h]);
		}
		
		$.blockUI({ message: $(popID), css: {
			 										width: opt.width,
													height:+opt.height,
													'margin':opt.margin,
													border: opt.border,
													padding: opt.padding,
													backgroundColor: opt.bg,
													//'-webkit-border-radius': opt.radious,
													//'-moz-border-radius': opt.radious,
													color: opt.color,
													cursor: opt.cursor
													} });
		$('.blockOverlay').attr('title','Click to unblock').click($.unblockUI);
}




function refferToFriend(F){
	if($(F).get(0).ref_email.disabled){ return false; }
	//if(F.login.value.length<1){ addTT(F.login, 'Please provide a Username!', 'error'); return false; }
	//	else{ removeTT(F.login); }
	//if(!validateEmail(F.ref_email.value)){ addTT(F.ref_email, 'Invalid email address!', 'error'); return false; }
	//	else{ removeTT(F.ref_email); }
	if(!validateEmail(F.ref_email.value)){ alert('Invalid email address!'); return false; }

	
	var postData = {};
		$(F).find("input, select").each(function(){
			if($(this).attr('name').length>0)
				postData[$(this).attr('name')] = $(this).val();
		});
		postData.ajaxAction = 'refer_to_friend';
	
	$(F).get(0).ref_email.value = 'Processing request, please wait..';
	$(F).get(0).ref_email.disabled = true;
		
		
	$.post($(F).attr('action'),postData,function(data,status){
		$(F).get(0).ref_email.disabled = false;
		if(data.message) $(F).get(0).ref_email.value = data.message;
		//if(data.message) $(F).html(data.message);
		if(data.alert) alert(data.alert);
	}, 'json');
		
	return false;
}




/* jQuery DD menu */
	function megaHoverOver(){
		$(this).find(".sub").stop().fadeTo('fast', 1).show();
			
		//Calculate width of all ul's
		(function($) { 
			jQuery.fn.calcSubWidth = function() {
				rowWidth = 0;
				//Calculate row
				$(this).find(".wrapper").each(function() {					
				//$(this).find("ul").each(function() {					
					rowWidth += $(this).width(); 
				});	
			};
		})(jQuery); 
		
		if ( $(this).find(".row").length > 0 ) { //If row exists...
			var biggestRow = 0;	
			//Calculate each row
			$(this).find(".row").each(function() {							   
				$(this).calcSubWidth();
				//Find biggest row
				if(rowWidth > biggestRow) {
					biggestRow = rowWidth;
				}
			});
			//Set width
			$(this).find(".sub").css({'width' :biggestRow});
			$(this).find(".row:last").css({'margin':'0'});
			
		} else { //If row does not exist...
			
			$(this).calcSubWidth();
			//Set Width
			$(this).find(".sub").css({'width' : rowWidth});
			
		}
	}
	function megaHoverOut(){ 
	  //$(this).find(".sub").stop().fadeTo('fast', 0, function() {
	  $(this).find(".sub").stop().hide();
	}
	function executeDDmenu(){
		var config = {    
			 sensitivity: 2, // number = sensitivity threshold (must be 1 or higher)    
			 interval: 0, // number = milliseconds for onMouseOver polling interval    
			 over: megaHoverOver, // function = onMouseOver callback (REQUIRED)    
			 timeout: 50, // number = milliseconds delay before onMouseOut    
			 out: megaHoverOut // function = onMouseOut callback (REQUIRED)    
		};
	
		$("ul.jqNavBar li .sub").css({'opacity':'0'});
		$("ul.jqNavBar li").hoverIntent(config);
	}




/* Favorite functions*/
	function SubmitFavorite(E,func){
		var F = getParentTag(E, 'form');
		var postData = getForm(F);
		var url = $(F).attr('action');
		
		$.post(url, postData, function(data,status){
			if(func){ func(); }
			else{
				$(E).after(data);
				$(E).remove();
			}
		}, 'html');
	}
	
	function refreshFavBlock(urlBase){
		$('.favContainer').html("Refreshing...");
		$.post(urlBase+'ajax/fav_merchants.php', {fav_merchants:'yes'}, function(data,status){
			//$('#myFavoriteHeading').after(data);
			$('.favContainer').html(data);
		}, 'html');
	}



/************************* TABBED DATA ***************************/
/* USAGE /
$(function(){
	setTabbedData('#tabbedDataset01');
});
<div class="tabbed_data" id="tabbedDataset01">
	<div class="tab_row"><ul>
			<li><a href="#" class="active" rel="tab01">Tab 1</a></li><li><a href="#" rel="tab02">Tab 2</a></li>
	</ul></div>
	<div class="data_row">
		<div class="tab01 tab_data">tab01</div>
		<div class="tab02 tab_data">tab02</div>
	</div>
</div>
.tabbed_data{ display:inline-block; width:100%; }
.tabbed_data .tab_row{ display:inline-block; width:100%; background-color:#585858; }
	.tabbed_data .tab_row ul{ list-style:none; margin:5px 10px 0 10px; padding:0; }
	.tabbed_data .tab_row ul li{ display:inline; margin:0 10px; padding:0; }
	.tabbed_data .tab_row ul li a{ text-transform:uppercase; display:inline-block; background-color:#414141; color:#FFF; padding:5px 15px; font-weight:bold; text-decoration:none; }
	.tabbed_data .tab_row ul li a:hover, .tabbed_data .tab_row ul li a.active, .tab_row ul li.active a{ background-color:#FFF; color:#414141; }
.tabbed_data .data_row{ display:inline-block; width:100%; }
	.tabbed_data .data_row .tab_data{ display:none; width:100%; background-color:#0F0; }
	.tabbed_data .data_row .tab_data.active();
*/
function setTabbedData(id){
	$(id+' .tab_row ul li a').click(function(){
		handelTabbedDataClick(this, id);
		return false;
	});
	if( $(id+' .tab_row ul li a.active').get(0) ){
		var p = getParentTag($(id+' .tab_row ul li a.active'), 'DIV.tabbed_data');
		handelTabbedDataClick(  $(id+' .tab_row ul li a.active'), p  );
	}
}
function handelTabbedDataClick(e, id){
		if(typeof(id)=='string'){
			$(id+' .tab_row ul li a').removeClass('active');
			$(e).addClass('active');
			var rel = $(e).attr('rel');
			$(id+' .data_row .tab_data').removeClass('active').hide();
			$(id+' .data_row .tab_data.'+rel).addClass('active').show();
		}else{
			$(id).find('.tab_row ul li a').removeClass('active');
			$(e).addClass('active');
			var rel = $(e).attr('rel');
			$(id).find('.data_row .tab_data').removeClass('active').hide();
			$(id).find('.data_row .tab_data.'+rel).addClass('active').show();
		}
}



