$(document).ready(function() {

	fakeSelectHandler.init();
	fakeSelectHandler.setDefaults();		

	$('#roaming-print').click(function(){		
		popUp(_var.roaming_print_url, 'PrintRoaming', {width: 800, height: 600});		
	});	

});

var fakeSelectHandler = {
	code: '',
	loaded_country: false,
	currentCountryID: '',
	currentCountryValue: '',
	translated: {ch: '', alt: '', ctrl: '', shift: '', meta: ''},
	init: function() {
		$('body').append('<div id="fakeselect-drop" class="hidden"><ul id="fakeselect-items"></ul></div>');
		$('#fakeselect option').each(function(i,elem){
			var country_id = $(elem).attr('value');										
			$('#fakeselect-items').append('<li><a id="' +country_id+ '" href="#' + country_id + '" onmouseover="fakeSelectHandler.mouseHover(true, \'' +country_id+ '\');" onmouseout="fakeSelectHandler.mouseHover(false, \'' +country_id+ '\');" style="background-image: url(' + _var.links_url + 'UserFiles/Roaming/flags/' + country_id + '.gif);">' + $(elem).text() + '</a></li>');
			$('#' + country_id).click(function(){
				fakeSelectHandler.currentCountryID = country_id;	
				fakeSelectHandler.currentCountryValue = this.innerHTML;
				$('#fakeselect-current div').html(this.innerHTML);
				$('#fakeselect-current').css('background-image',this.style.backgroundImage);											
				jQuery.get(js_site_url+'static/Roaming/ajaxRoaming/&country_id='+country_id+'&lang=' + _var.active_lang + '','',function(responce){
					$('#provider_table').html(responce);
				});
				fakeSelectHandler.unbindHandler(document, true);									    
			});										
		});
		$('#fakeselect').append('<div id="fakeselect-current" style="background-image: url(' + _var.links_url + 'UserFiles/Roaming/flags/' + $('#fakeselect option:selected').attr('value') + '.gif);"><div>' + $('#fakeselect option:selected').text() + '</div></div>');
		$('#fakeselect-current').click(function(){									
			if($('#fakeselect-drop').hasClass('hidden')){											
				$('#fakeselect-drop').removeClass('hidden').css({
					left: $(this).offset()['left'],
					top: $(this).offset()['top'] + $(this)[0].offsetHeight
				});										
				fakeSelectHandler.bindHandler(document);
			} else {
				fakeSelectHandler.unbindHandler(document, true);
				$('#fakeselect-drop').addClass('hidden');											
			}
		});									
		$('#fakeselect SELECT, #fakeselect INPUT').addClass('hidden');
	},				
	mouseHover: function(mtype, elem) {
		if (mtype == true) {
			fakeSelectHandler.removeActives(); 
		}
		$('a#' + elem).toggleClass('sel_hover');
	},
	bindHandler: function(element) {																				
		$(element).bind("keydown", fakeSelectHandler.listenEvent);
		$(element).bind("keyup", fakeSelectHandler.cancelEvent);
		$(element).bind("keypress", fakeSelectHandler.cancelEvent);	
		$(element).bind("mouseup", function(e){ fakeSelectHandler.cancelEvent(e); fakeSelectHandler.unbindHandler(e); });
	},
	unbindHandler: function(element, forceTrue) {	
		if (element.target)	{
			var currentTarget = element.target.toString();
			if (currentTarget=='[object HTMLUListElement]' || currentTarget.substr(0, 4)=='http') {
				return false;
			} else {
				fakeSelectHandler.unbindHandler(document, true);
			}
			return false;
		}																		
		if (forceTrue == true)
		{
			$('#fakeselect-drop').addClass('hidden');
			$(element).unbind("keydown");
			$(element).unbind("keyup");
			$(element).unbind("keypress");	
			$(element).unbind("mouseup");
		}									
	},
	listenEvent: function(e) {		
		if (e.returnValue == false) {
			return fakeSelectHandler.cancelEvent(e);
		}
		e = e || window.event;
		if (e.keyCode) {
			fakeSelectHandler.code = e.keyCode;
		} else if (e.which) {
			fakeSelectHandler.code = e.which;
		}									
		fakeSelectHandler.translateKey(e);									
		fakeSelectHandler.doSomething();									
		return fakeSelectHandler.cancelEvent(e);
	},
	cancelEvent: function(e) {																	
		e = e || window.event;
		e.cancelBubble = true;
		e.returnValue = false;
		if (e.stopPropagation) e.stopPropagation();
		if (e.preventDefault) e.preventDefault();	
		return false;
	},
	translateKey: function(e) {
		fakeSelectHandler.translated.ch = String.fromCharCode(fakeSelectHandler.code);
		fakeSelectHandler.translated.alt = e.altKey ? "A" : "";
		fakeSelectHandler.translated.ctrl = e.ctrlKey ? "C" : "";
		fakeSelectHandler.translated.shift = e.shiftKey ? "S" : "";
		fakeSelectHandler.translated.meta = e.metaKey ? "M" : "";		
	},
	doSomething: function() {													
		if (fakeSelectHandler.code == '38' || fakeSelectHandler.code == '40') {																		
			var currentItem = fakeSelectHandler.currentCountryValue ? fakeSelectHandler.currentCountryValue : $('#fakeselect-current div').html();
			$('#country_select_id').find("option").each(function(i, k){
				if (k.text == currentItem)
				{
					if (fakeSelectHandler.code == '38') {
						fakeSelectHandler.changeItemTo($(k).prev());
					} else if (fakeSelectHandler.code == '40') {
						fakeSelectHandler.changeItemTo($(k).next());	
					} 
				}										
			});
			return true;
		}
		if (fakeSelectHandler.code == '13' && fakeSelectHandler.currentCountryID)
		{
			jQuery.get(js_site_url+'static/Roaming/ajaxRoaming/&country_id='+fakeSelectHandler.currentCountryID+'&lang=' + _var.active_lang + '','',function(responce){
				$('#provider_table').html(responce);
			});
			fakeSelectHandler.unbindHandler(document, true);
			$('#fakeselect-drop').addClass('hidden');										
			return true;
		}
		
		$('#country_select_id').find("option").each(function(i, k){
			if (k.text.substr(0, 1) == fakeSelectHandler.translated.ch)
			{
				fakeSelectHandler.changeItemTo($(k));
			}
		});									
	},
	changeItemTo: function(changeItem) {	
		if (!changeItem.get(0)) {
			return false;
		}
		fakeSelectHandler.currentCountryID = changeItem.get(0).value;
		fakeSelectHandler.currentCountryValue = changeItem.get(0).text;
		$('#fakeselect-items li a').removeClass('sel_hover');
		try {
			$('#fakeselect-items li a#' + changeItem.get(0).value).get(0).focus();	
		}catch(e){}		
		$('#fakeselect-items li a#' + changeItem.get(0).value).addClass('sel_hover');									
		$('#fakeselect-current div').html(changeItem.get(0).text);
		$('#fakeselect-current').css('background-image', $('a#'+changeItem.get(0).value).css('background-image'));									
	},
	removeActives: function() {
		$('#fakeselect-items li a').removeClass('sel_hover');
	},
	setDefaults: function() {
		if (window.location.hash.length > 1)
		{
			if (fakeSelectHandler.loaded_country = (window.location.hash.split('#')[1].length >= 2 ? window.location.hash.split('#')[1] : false))
			{
				if (fakeSelectHandler.loaded_country != 'plane' && fakeSelectHandler.loaded_country != 'satellite' && fakeSelectHandler.loaded_country != 'maritime')
				{
					fakeSelectHandler.loaded_country = fakeSelectHandler.loaded_country.substr(0, 2);
				}				

				$('#country_select_id').find("option").each(function(i, k){
					if (k.value == fakeSelectHandler.loaded_country)
					{
						fakeSelectHandler.changeItemTo($(k));
					}
				});	

				jQuery.get(js_site_url+'static/Roaming/ajaxRoaming/&country_id='+fakeSelectHandler.loaded_country+'&lang=' + _var.active_lang + '','',function(responce){
					$('#provider_table').html(responce);
				});
				fakeSelectHandler.unbindHandler(document, true);
				$('#fakeselect-drop').addClass('hidden');										
				return true;
			}
		}
	}
};

function popUp(url, name, settings) {
	var name = (name == null) ? 'popup' : name;
	default_settings = {
		height:450, // sets the height in pixels of the window.
		width:450, // sets the width in pixels of the window.
		toolbar:0, // determines whether a toolbar (includes the forward and back buttons) is displayed {1 (YES) or 0 (NO)}.
		scrollbars:1, // determines whether scrollbars appear on the window {1 (YES) or 0 (NO)}.
		status:0, // whether a status line appears at the bottom of the window {1 (YES) or 0 (NO)}.
		resizable:1, // whether the window can be resized {1 (YES) or 0 (NO)}. Can also be overloaded using resizable.
		left:0, // left position when the window appears.
		top:0, // top position when the window appears.
		center:1 // should we center the window? {1 (YES) or 0 (NO)}. overrides top and left
	};
	if(settings !== null) {
		settings = jQuery.extend(default_settings, settings);
	}
	// center the window
	if (settings.center == 1) {
		settings.top = (screen.height-(settings.height + 110))/2;
		settings.left = (screen.width-settings.width)/2;
	}
	var parameters = "height=" + settings.height + ",width=" + settings.width + ",toolbar=" + settings.toolbar + ",scrollbars=" + settings.scrollbars  + ",status=" + settings.status + ",resizable=" + settings.resizable + ",left=" + settings.left  + ",screenX=" + settings.left + ",top=" + settings.top  + ",screenY=" + settings.top;
	window.open(url, name, parameters).focus();
}$(document).ready(function() {
	$('DIV.expandable').each(function(i,a){
		$('H2', a).css('cursor', 'pointer');
		$('H2', a).click(function(){
			$('DIV.expander A', a).click();
		});
		$('DIV.expander A', a).click(function(){
			if($(this).hasClass('closed')){
				$(this).removeClass('closed');
				$('DIV.more', a).removeClass('hidden');
				var el = $('DIV.main', a);
				if (el.hasClass('cb02-b')) {
					el.removeClass('cb02-b').addClass('cb02-c');
				} else if (el.hasClass('cb03-b')) {
					el.removeClass('cb03-b').addClass('cb03-c');
				}
			} else {
				$(this).addClass('closed');
				$('DIV.more', a).addClass('hidden');
				$('DIV.main', a).addClass('cb02-b').removeClass('cb02-c');
				var el = $('DIV.main', a);
				if (el.hasClass('cb02-c')) {
					el.removeClass('cb02-c').addClass('cb02-b');
				} else if (el.hasClass('cb03-c')) {
					el.removeClass('cb03-c').addClass('cb03-b');
				}
			}
			return false;
		});

		// Init
		$('DIV.expander A', a).addClass('closed');
		$('DIV.more', a).addClass('hidden');
		$('DIV.main.cb02-c', a).addClass('cb02-b').removeClass('cb02-c');
		$('DIV.main.cb03-c', a).addClass('cb03-b').removeClass('cb03-c');
	});
});