/* Requires jQuery */

$.preloadImages(
	"/images/home/ibank_big.jpg",
	"/images/home/ibank_mobile_big.jpg",
	"/images/home/ibiz_big.jpg",
	"/images/home/ibiz_pro_big.jpg"
);

$(document).ready(function(){
	// iBank
	$('#products').append('<div id="prod_ibank"><a href="ibank/"><img src="/images/home/ibank_big.jpg" alt="iBank"/></a></div>');
	$('#prod_ibank>a>img').hide();
	$('#prod_ibank').bind("mouseenter", function(){
		$('#prod_ibank>a>img').fadeIn('fast');
	}).bind("mouseleave", function(){
		$('#prod_ibank>a>img').fadeOut('fast');
	});
	
	// iBank Mobile
	$('#products').append('<div id="prod_ibank_mobile"><a href="ibankmobile/"><img src="/images/home/ibank_mobile_big.jpg" alt="iBank Mobile"/></a></div>');
	$('#prod_ibank_mobile>a>img').hide();
	$('#prod_ibank_mobile').bind("mouseenter", function(){
		$('#prod_ibank_mobile>a>img').fadeIn('fast');
	}).bind("mouseleave", function(){
		$('#prod_ibank_mobile>a>img').fadeOut('fast');
	});
	
	// iBiz
	$('#products').append('<div id="prod_ibiz"><a href="ibiz/"><img src="/images/home/ibiz_big.jpg" alt="iBiz"/></a></div>');
	$('#prod_ibiz>a>img').hide();
	$('#prod_ibiz').bind("mouseenter", function(){
		$('#prod_ibiz>a>img').fadeIn('fast');
	}).bind("mouseleave", function(){
		$('#prod_ibiz>a>img').fadeOut('fast');
	});
	
	// iBiz Pro
	$('#products').append('<div id="prod_ibiz_pro"><a href="ibiz/multiuser.php"><img src="/images/home/ibiz_pro_big.jpg" alt="iBiz Pro"/></a></div>');
	$('#prod_ibiz_pro>a>img').hide();
	$('#prod_ibiz_pro').bind("mouseenter", function(){
		$('#prod_ibiz_pro>a>img').fadeIn('fast');
	}).bind("mouseleave", function(){
		$('#prod_ibiz_pro>a>img').fadeOut('fast');
	});

});

function makeClickableLinks(text) {
	text = text.replace(/(((f|ht){1}t(p|ps){1}:\/\/)[-a-zA-Z0-9@:%_\+.~#?&\/\/=;]+)/g,
	 '<a href="$1">$1</a>');
	text = text.replace(/([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_\+.~#?&\/\/=;]+)/g,
	 '$1<a href="http://$2">$2</a>');
	text = text.replace(/([_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3})/g,
	 '<a href="mailto:$1">$1</a>');
	return text;
}

function getFeed(url,el,num,twit){
	if (!num) num = -1;
	if (!twit) twit = false;
	$.ajax({
      type: "GET",
		url: "scripts/proxy.php?url="+url,
		dataType: "xml",
		success: function(xml) {
			var html = "<ul>";
			$(xml).find('item').each(function(){
				if (num != 0) {
					var title = $(this).find('title').text();
					var url = $(this).find('link').text();
					var desc = $(this).find('description').text();
					var date = $(this).find('pubDate').text();
					if (!twit) html += '<li><h1><a href="'+url+'">'+title+'</a></h1><p>'+makeClickableLinks(desc)+'</p></li>';
					else html += '<li><h1><a href="'+url+'">'+date.substr(0,22)+'</a></h1><p>'+makeClickableLinks(title.substr(13))+'</p></li>';
					num--;
				}
			});
			html += "</ul>";
			$('#'+el).hide().html(html).fadeIn();
		},
		error: function(e,status,eObj) { $('#'+el).html("<ul><li>This feed is currently unavailable. Please try again later.</li></ul>"); }
	});
}

function verify(){
	var clean = true;
	var addy = document.getElementById('email').value; // get address
	var addy = addy.replace(/(<([^>]+)>)/ig,""); // remove HTML tags
	var banlist = new Array(
		"free@ringtones.com",
		"cialis@online.com",
		"info@iggsoftware.com",
		"online@casino.com",
		"replica@watches.com",
		"warezona@gmail.com",
		"screebsaver@gmail.com",
		"xzimvl@ngbnoq.com",
		"gay@gmail.com",
		"hanams@mail.com",
		"Carrick@gmail.com",
		"hanamass@mail.com",
		"adkalinsa@gmail.com"); 
	for (var i in banlist) // check for banned addresses
		if (addy.toLowerCase()==banlist[i]) clean = false;
	
	if (!clean) alert('Sorry, the email address you entered is prohibited. Please enter a valid email address.')
	return clean;
}

/* POPUP TUTORIAL VIDEOS */
/* jQuery modal window courtesy of queness.com (http://www.queness.com/post/77/simple-jquery-modal-window-tutorial) */
function setupModals() {	

	//select all the a tag with name equal to modal
	$('a[name=modal]').click(function(e) {
		//Cancel the link behavior
		e.preventDefault();
		
		//Get the A tag
		var id = $(this).attr('href');
	
		//Get the screen height and width
		var maskHeight = $(document).height();
		var maskWidth = $(window).width();
	
		//Set heigth and width to mask to fill up the whole screen
		$('#mask').css({'width':maskWidth,'height':maskHeight});
		
		//transition effect
		$('#mask').css('opacity',0.8);
		$('#mask').fadeIn('fast');
	
		//Get the window height and width
		var winH = $(window).height();
		var winW = $(window).width();
		
		//Get the window scroll offset
		var winS = $(window).scrollTop();
              
		//Set the popup window to center
		$(id).css('top',  winH/2-$(id).height()/2+winS);
		$(id).css('left', winW/2-$(id).width()/2);
		
		//Set the close button to upper left of popup
		$('#closeButton').css('top', winH/2-$(id).height()/2+winS-15);
		$('#closeButton').css('left', winW/2-$(id).width()/2-15);
	
		//transition effect
		$('object').show();
		$(id).fadeIn('fast');
		$('#closeButton').fadeIn('fast');
	
	});
	
	//if close button is clicked
	$('#closeButton').click(function (e) {
		//Cancel the link behavior
		e.preventDefault();
		
		$('object').hide();
		$('#mask').fadeOut('fast');
		$('.window').fadeOut('fast');
		$('#closeButton').fadeOut('fast');
	});		
	
	//if mask is clicked
	$('#mask').click(function () {
		$('object').hide();
		$(this).fadeOut('fast');
		$('.window').fadeOut('fast');
		$('#closeButton').fadeOut('fast');
	});	
}
