/* show a popup for user to choose country */

function Country(name, actions){
	this.name = name;
	this.actions = actions;
}

var countries = [ /*add country and url here*/
	new Country('Australia', {
		'login':'index.php?option=com_multivm&url=bG9naW4%3D&source=au',
		'register':'index.php?option=com_multivm&url=c2hvcC1hdS9jcmVhdGUtYW4tYWNjb3VudA%3D%3D&source=au'
	}), //null will use default url
	new Country('New Zealand', {
		'login':'index.php?option=com_multivm&url=bG9naW4%3D&source=au',
		'register':'index.php?option=com_multivm&url=c2hvcC1hdS9jcmVhdGUtYW4tYWNjb3VudA%3D%3D&source=au'
	}),
	new Country('United States', {
		'login':'index.php?option=com_multivm&url=bG9naW4%3D&source=us',
		'register':'index.php?option=com_multivm&url=c2hvcC11cy9jcmVhdGUtYW4tYWNjb3VudA%3D%3D&source=us'
	}),
	new Country('Canada', {
		'login':'index.php?option=com_multivm&url=bG9naW4%3D&source=us',
		'register':'index.php?option=com_multivm&url=c2hvcC11cy9jcmVhdGUtYW4tYWNjb3VudA%3D%3D&source=us'
	})
];
var popupHTML = 
'<div style="display:none;">'+
	'<div id="country-popup" style="min-height: 60px; min-width: 230px;">'+
		'<p>Please select your country:</p>'+
		'<div id="country-popup-links"></div>'+
	'</div>'+
'</div>';

jQuery.noConflict();
jQuery(document).ready(function() {
	jQuery('body').append(popupHTML);
	var currentAction = null;
	var defaultLink = null;
	var fancyboxBackground = jQuery('#fancybox-outer').css('background');
	function initPopup(jLink, action){
		jLink.attr("data-href", jLink.attr("href"));
		jLink.attr("href", "#country-popup");
		jLink.click(function(){
			currentAction = action;
			defaultLink = jQuery(this).attr("data-href");
		});
		jLink.fancybox({
			'speedIn' : 600, 
			'speedOut' : 200,
			'scrolling' : 'no',
			'overlayColor' : '#000',
			'showCloseButton' : false,
			'padding': 0, 
			'onStart' : function(){
				jQuery('#fancybox-outer').css('background', 'transparent');
			},
			'onClosed' : function(){
				jQuery('#fancybox-outer').css('background', fancyboxBackground);
			}
		});
	}
	var jPopupLinks = jQuery('#country-popup-links');
	jQuery(countries).each(function() {
		var thisCountry = this;
		var cLink = jQuery('<a/>');
		cLink.html(this.name);
		cLink.attr('href', 'javascript:void(0);');
		cLink.attr('class', 'button round jbtn greyButton');
		cLink.attr('data-corner', '2px');
		cLink.click(function(){
			if(thisCountry.actions==null){
				window.location = defaultLink;
			}
			window.location = thisCountry.actions[currentAction];
			return false;
		});
		jPopupLinks.append(cLink);
	});
	initPopup(jQuery('ul.menu_loginmenu .item16 a:first'), 'login');
	jQuery('.create_account').each(function(index, element) {
        initPopup(jQuery(this), 'register');
    });
}); 
