﻿$(window).load(function () {
	
	String.prototype.startsWith = 
		function(str){
			return (this.toUpperCase().match("^"+str.toUpperCase())==str.toUpperCase())
		}
		
	/* ------ NETBANK LOGIN ------*/
	/* modal layer */ 

	var netBankShow=function(hash){ 
		//$('#netbankLoginButton').addClass("selected");
		hash.w.show();
		positionNetbankLayer();
		/*hash.w.css('opacity',0.88).show(); */
	};

	var netBankHide=function(hash) { 
		//$('#netbankLoginButton').removeClass("selected");
		hash.o.remove();
		hash.w.hide();
		/*hash.w.fadeOut('2000',function(){ hash.o.remove(); }); */
	};


	$('#netBankDialog').jqm({
		overlay: 60, // 0-100 (int) : 0 is off/transparent, 100 is opaque
		zIndex: 500,
		overlayClass: 'netbankWhiteOverlay',
		onShow: netBankShow,
		onHide: netBankHide,
		toTop: true
	});
	
	/* search */ 
	$("input#netBankSelect").keyup(function (event) {
	
		$("span.netbank").each(function (index){
			if (!$.trim($(this).text()).startsWith(event.target.value)) { //IE kompatibilis
			/*if (!$(this).text().trim().startsWith(event.target.value)) {*/
				$(this).addClass("nonmatch");
			}
			else {
				$(this).removeClass("nonmatch");
			}
		});
		
    });
	
	
	showNetbankChoice();
	
	$("span.netbank a").click(function (event) {
		saveNetbankChoice($(this).text());
		window.open( $(this).attr('href') );
		$('#netBankDialog').jqmHide();
        return false;

	});
	
});

$(window).resize(function () {
	positionNetbankLayer();
});

function showNetbankChoice() {

	var choice = getNetbankChoice();
	if ( choice !== false ) {
		$("#netBankChoiceId").text(getNetbankChoice());
		$("#netBankChoiceId").attr('href', getNetbankUrl(choice));
		$("div#netBankChoice").removeClass("hidden");
	}
}

function getNetbankUrl(choice) {
	var url = '';
	$('span.netbank').each(function(index){
		if ($(this).children('a').text() == choice) {
			url = $(this).children('a').attr('href');
		}
	});
	
	return url;
}

function saveNetbankChoice(netbankId) {
	$.cookie("netbankChoice", null);
	$.cookie("netbankChoice", netbankId, { expires: 0, path: '/' });
}

function getNetbankChoice() {
	if ( $.cookie("netbankChoice") ) {
		return $.cookie("netbankChoice");
	}
	else {
		return false;
	}
}

function positionNetbankLayer() {
	var buttonPos = $('#netbankLoginCont').offset().left;
	var buttonW = $('#netbankLoginCont').outerWidth(); 
	var netbankW = $('#netBankDialog').outerWidth();
	var pos = (buttonPos + buttonW) - netbankW;
	
	//netbank dialog position = netbank button offset - netbank dialog width
	$('#netBankDialog').css('left', pos + 'px');
		
}
