$(document).ready(function() {
	// change href for language selector link
	var href = $('#lng-select-handle').attr('href');
	var regular = /(^(http|https){1}:\/\/[\w\.-]+\/([a-z]{2}-[a-z]{2}\/)?[\w-]+)/;
	
	if (href) {
		result = href.match(regular);
		
		if (array_key_exists(0, result)) {
			href = result[0] + '-box';
			$('#lng-select-handle').attr('href', href);
		}

	    $('#lng-select-handle').facebox({
	        loadingImage    : location.protocol + '//' + location.host + '/stc/tpl/crp/img/facebox/loading.gif',
	        closeImage      : location.protocol + '//' +location.host + '/stc/tpl/crp/img/facebox/closelabel.gif',
	        opacity         : 0.3
	    }); 
	    
	    $(document).bind('reveal.facebox', function(){
	        $('#fb-close').live('click', function(event){
	            event.preventDefault();
	        
	            $(document).trigger('close.facebox');
	        });
	    }); 
	    
	    $(document).bind('reveal.facebox', function(){
	        $('#fb-change-region a').live('click', function(event){
	            event.preventDefault();
	        
	            $('#fb-lng-container').hide();
	            $('#fb-rgn-container').show();
	        });
	    }); 
	    
	    $(document).bind('reveal.facebox', function(){
	        $('.lng').live('click', function(event){
	            event.preventDefault();
	                    
	            var href = $(this).attr('href');
	            
	            $(document).trigger('close.facebox');
	            window.location.href = href; 
	        });
	    });
	    
	    $(document).bind('reveal.facebox', function(){
	        $('.rgn').live('click', function(event){
	            event.preventDefault();
	            
	            // get locale from url
	            var path = location.pathname;
	            var result = path.split("/");
	            
	            // if no locale is found, use default
	            if (array_key_exists(1, result) && result[1].length <= 5 ) {
	            	var locale = result[1];
	            } else {
	            	var locale = 'ww-en';
	            }
	            
	            id = parseInt($(this).attr('id'));
	            href = $('#lng-select-handle').attr('href');
	            
	            $.ajax ({
	        		type: 'POST',
	        		url: href,
	        		dataType: 'html',
	        		timeout: 7000,
	        		data: 'langSelectorHelper=' + $('#langSelectorHelper').val() + '&langSelectorHelper2=' + $('#langSelectorHelper2').val() + '&rgn=' + id,
	        		success: function(data) {
		            	$('#fb-lang-sel-wrapper').html(data); 
		                $('#fb-your-rgn').hide();
		                $('#fb-rgn-container').hide();
		                $('#fb-lng-container').show(); 
	        		},
	        		error: function(objAJAXRequest, strError){
	        			$(document).trigger('close.facebox');
	        			
	                    var regular = /((-box{1})$)/;
	        			var result = href.split(regular);

	        			if (array_key_exists(0, result)) {
	        				window.location.href = result[0];
	        			} else {
	        				window.location.href = location.protocol + '//' + location.host + '/change-language';
	        			}
	        		}
	        	});
	        });
	    });
	}
});

function array_key_exists ( key, search ) {
    if (!search || (search.constructor !== Array && search.constructor !== Object)){
        return false;
    }

    return key in search;
}