//IP Redirect written for Tasty Bite by Web Development Services - http://www.teggweb.com/

$(document).ready(function() {
	if (!$.cookies.get("tb_aus_stay")) {
		//alert('vinod');
	var cid = 	geoplugin_countryCode();
	//alert(cid);
		if (cid=='US') {
						show_country_lightbox();
						//alert('calling in ')
					}
		
	} else {
		if ($.cookies.get("tb_aus_stay")=="0") {
			window.location = 'http://www.tastybite.com/';
		}
	}
});

function show_country_lightbox() {
	$('body').append('<div id="modal-window"><div id="lightbox"><div id="lightbox-content"></div></div><div id="mask"></div></div>');
	//Get the screen height and width
	var maskHeight = $(document).height();
	var maskWidth = $(window).width();

	//Set height and width to mask to fill up the whole screen
	$('#mask').css({'width':maskWidth,'height':maskHeight});
	$('#mask').css({opacity:0.35});
	$('#mask').click(function() {
		$('#modal-window').remove();
	});
	$(document).keyup(function(e) {
		if(e.keyCode == 27) {   
			$('#modal-window').remove();
		}   
	});
	
	//Get the window height and width
	var winH = $(window).height();
	var winW = $(window).width();
	
	$('#lightbox-content').html('<h1>United States or Australia?</h1><p>It appears that you are accessing this site from the United States. Would you like to view the US Tasty Bite website?</p><ul id="redirect-options"><li class="first"><a href="http://www.tastybite.com/" onclick="return redirect_to_us();">Yes, redirect me</a></li><li><a href="javascript:void(0)" onclick="close_dialog();">No, not at this time</a></li><li class="last"><a href="javascript:void(0)" onclick="set_cookie(\'1\');">No, don\'t ask again</a></li></ul>');
	
	$('#lightbox-content img[align="left"]').css({'margin-right':'15px', 'margin-bottom':'15px'});
	$('#lightbox-content img[align="right"]').css({'margin-left':'15px', 'margin-bottom':'15px'});
	
	//Set the popup window to center
	var lightboxTop = $('body').scrollTop()+((winH-$('#lightbox').height())/2);
	var lightboxLeft = (winW/2)-($('#lightbox').width()/2);
	
	if (lightboxTop<=0) {lightboxTop=10;}
	if (lightboxLeft<=0) {lightboxLeft=10;}
	
	$('#lightbox').css({'left':lightboxLeft,'top':lightboxTop});
	
	$('#mask').fadeIn(400);
	$('#lightbox').fadeIn(400);
	
}

function redirect_to_us() {
	set_cookie('0');
	return true;
}

function close_dialog() {
	$('#modal-window').remove();
}

function set_cookie(val) {
	var expDate = new Date();
	expDate.setYear(expDate.getFullYear() + 1);
	$.cookies.set("tb_aus_stay", val, {expiresAt:expDate});
	close_dialog();
}

function del_cookie() {
	$.cookies.set("tb_aus_stay");
}
