/*
 *	Focus-UI Library
 *
 *	Copyright (c) 2009 Focus Imaging Limited - http://www.focus-global.com
 */


focus_ui = {
	url_root: null,
	langData: null,
	
	init: function(init_param) {
		
		focus_ui.url_root = init_param.url_root;
		
		//focus_ui.day_night.init();
		
		$('img.rollover')
			.bind('mouseover',function(event){ focus_ui.img.mouseover(this); })
			.bind('mouseout',function(event){ focus_ui.img.mouseout(this); });
		
		$('input.rollover')
			.bind('mouseover',function(event){ focus_ui.img.mouseover(this); })
			.bind('mouseout',function(event){ focus_ui.img.mouseout(this); });
	},

	bookmarksite: function(){
		if (window.sidebar) // firefox
		{
			window.sidebar.addPanel(document.title, location.href, "");
		}
		else if(window.opera && window.print) // opera
		{
			var elem = document.createElement('a');
			elem.setAttribute('href',location.href);
			elem.setAttribute('title',document.title);
			elem.setAttribute('rel','sidebar');
			elem.click();
		} 
		else if(document.all) // ie
		{
			window.external.AddFavorite(location.href, document.title);
		}
	},	
	
	clock: {
		current: null,
		init: function( current_time )
		{
			focus_ui.clock.current = new Date(current_time);
			setInterval("focus_ui.clock.refresh()", 1000);
		},
		padZero: function(what){
			return (what.toString().length==1) ? '0' + what : what;
		},
		refresh: function()
		{
			focus_ui.clock.current.setSeconds( focus_ui.clock.current.getSeconds()+1 );
			var s = focus_ui.clock.padZero(focus_ui.clock.current.getHours())+":"+ focus_ui.clock.padZero(focus_ui.clock.current.getMinutes())+":"+ focus_ui.clock.padZero(focus_ui.clock.current.getSeconds());
			$('#focus_ui_clock').html( s );
		}
	},	
	
	img: {
		mouseover: function( el ){
			var el = $(el);
			el.attr('src', el.attr('src').replace('rollout', 'rollover') );
		},
		mouseout: function( el ){
			var el = $(el);
			el.attr('src', el.attr('src').replace('rollover', 'rollout') );
		}
	},
	
	day_night: {
		init: function() {
			$('div.day_night_img').each(function() {
				var img = $(this).find('img.view');
				
				$(this).find('img.change_btn')
					.bind('click', function() { focus_ui.day_night.changeView( img ) });
			});				
		},
		
		changeView: function( target ) {	
			if (target.attr('src').match('day')) {
				target.attr('src', target.attr('src').replace('day', 'night') );
			}
			else {
				target.attr('src', target.attr('src').replace('night', 'day') );
			}			
		}
	},
	
	timepicker: {
		image_icon : 'images_png/date.png',
		hour_val : 0,
		min_val : 0,
		
		init : function( time_selector ) {
			
						
			$(time_selector).each(function() {
				focus_ui.timepicker.initObj( $(this) );

				$(this).bind('keyup', function() {
					focus_ui.timepicker.updateSlider($(this));
				});
			});
		},
		
		initObj : function(obj) {
			var img_code = '<img class="btn focusui-timepicker-icon" src="'+focus_ui.timepicker.image_icon+'"/>';			
			var tp_code = '';

			tp_code += '<table class="focusui-timepicker-parent" style="display:none; width:100%; padding:3px;">';
				tp_code += '<tr>';
					tp_code += '<td class="label" style="width:50px;">Hour</td>';
					tp_code += '<td class="slider">';
						tp_code += '<div class="slider hour" style="width:100%;"></div>';
					tp_code += '</td>';
				tp_code += '</tr>';
				tp_code += '<tr>';
					tp_code += '<td class="label" style="width:20%; margin-right:5px;">Min</td>';
					tp_code += '<td class="slider">';
						tp_code += '<div class="slider min" style="width:100%;"></div>';
					tp_code += '</td>';
				tp_code += '</tr>';				
			tp_code += '</table>';
			
			$(tp_code).insertAfter(obj);
			$(img_code).insertAfter(obj);
			
			var objImg = $(obj).next('img.focusui-timepicker-icon');			
			var objSlider = $(objImg).next('table.focusui-timepicker-parent');
			
			var hour_slider = objSlider.find('div.slider.hour');
			var min_slider = objSlider.find('div.slider.min');
			
			// Bind Obj Actions			
			objImg.bind('click', function() {
				focus_ui.timepicker.showTimePicker($(this));
			});			
			
			hour_slider.slider( {
				min : 8,
				max : 19,
				slide: function(event, ui) {
					focus_ui.timepicker.hour_val = ui.value;
					focus_ui.timepicker.update_timepicker_value(obj);
				}
			});
			
			min_slider.slider( {
				min : 0,
				max : 45,
				step : 15,
				slide: function(event, ui) {
					focus_ui.timepicker.min_val = ui.value;
					focus_ui.timepicker.update_timepicker_value(obj);
				}
			});
		},
		
		showTimePicker : function( imgObj ) {
			var c_obj = $(imgObj).prev(':input');
			var objSlider = $(imgObj).next('table.focusui-timepicker-parent');
						
			objSlider.slideToggle('medium');
		},
		
		updateSlider : function( obj ) {		
			var arr_val = $(obj).val().split(':');
			
			if (arr_val.length >= 2) {				
				var hr = parseInt(arr_val[0]);
				var min = parseInt(arr_val[1]);
				
				var slider_parent = $(obj).nextAll('table.focusui-timepicker-parent');				
				var hour_slider = slider_parent.find('div.slider.hour');			
				var min_slider = slider_parent.find('div.slider.min');
				
				//$('#debug').html(hr+', '+min);
				hour_slider.slider('option', 'value', hr);
				min_slider.slider('option', 'value', min);
			}
			
		},
		
		update_timepicker_value : function( c_obj ) {			
			var tp_value = focus_ui.timepicker.pad_zero(focus_ui.timepicker.hour_val, 2)+
				' : '+focus_ui.timepicker.pad_zero(focus_ui.timepicker.min_val, 2);
			
			$(c_obj).val(tp_value);
		},
		
		pad_zero : function(num, totalChars, padWith) {			
			num = num + '';
			padWith = (padWith) ? padWith : "0";
			
			if (num.length < totalChars) {
				while (num.length < totalChars) {		
					num = padWith + num;
		
				}
			}			
		
			if (num.length > totalChars) { //if padWith was a multiple character string and num was overpadded
				num = num.substring((num.length - totalChars), totalChars);
			}			
		
			return num;	
		}		
	},
	
	autocomplete: {
		images_icon : 'images_png/search.png',
		tbox : null,
		id : null,
		target : null,
		key_code : null,
		
		init : function( selector ) {
			$(selector).each(function() {				
				$(this)
					.bind('blur', function() {						
						focus_ui.autocomplete.blur( $(this) );
					})
					.bind('keyup', function(e) {
						focus_ui.autocomplete.keyup( $(this), e );
					})
					.bind('keydown', function(e) {
						focus_ui.autocomplete.keydown( $(this), e );
					});
					
				focus_ui.autocomplete.setBox( $(this) );
				
				var input_obj = $(this);
				var code = '<img class="btn magnify" src="'+focus_ui.autocomplete.images_icon+'"/>';				
				$(code).insertAfter($(this));
				
				var key_obj = { keyCode : 86 };							
				$(this).next('img.magnify').bind('click', function() {
					focus_ui.autocomplete.keyup( input_obj, key_obj );
					input_obj.focus();
				});
			});
		},
		
		getObj : function (obj) {			
			focus_ui.autocomplete.tbox = $(obj);
			focus_ui.autocomplete.id = focus_ui.autocomplete.tbox.attr('id');
			focus_ui.autocomplete.target = $('body').find('div.hide_list.'+focus_ui.autocomplete.id);
		},
		
		setBox : function ( box ) {
			focus_ui.autocomplete.getObj( box );
			
			focus_ui.autocomplete.target				
				.bind('mouseover', function() {
					$(box).addClass('no_blur');
				})
				.bind('mouseout', function() {
					$(box).removeClass('no_blur');
				});
			
			var tr = focus_ui.autocomplete.target.find('tr');
			for (var t = 0 ; t < tr.length ; t++) {
				tr.eq(t)
					.bind('click', function() {
						$(focus_ui.autocomplete.target).find('tr.current').removeClass('current');
						$(this).addClass('current');
						
						var key_obj = { keyCode : 9 };
						focus_ui.autocomplete.keydown( box, key_obj );						
					});
			}
		},
		
		blur : function(obj) {
			focus_ui.autocomplete.getObj( obj );
			
			if ( !focus_ui.autocomplete.tbox.hasClass('no_blur') ) {				
				focus_ui.autocomplete.target.hide();
			}
		},
		
		keyup : function(obj, key) {			
			focus_ui.autocomplete.getObj( obj );
			key_code = focus_ui.autocomplete.getKeyCode(key);
			
			var num_rec = 0;
			var top = focus_ui.autocomplete.tbox.position().top;
			var left = focus_ui.autocomplete.tbox.position().left;
			
			if ( (key_code != 9) && (key_code != 40) && (key_code != 38) ) {								
				num_rec = focus_ui.autocomplete.filterOption( focus_ui.autocomplete.tbox, focus_ui.autocomplete.target );								
							
				//num_rec = 1;
				
				if (num_rec > 0) {
					focus_ui.autocomplete.target.attr('style', 'height:200px; overflow:auto; z-index:100;')
					focus_ui.autocomplete.target.css('left', left);
					focus_ui.autocomplete.target.css('top', top + 20);
					focus_ui.autocomplete.target.show();
				}
			}
			//$('#debug').html(key_code);
		},
		
		keydown : function(obj, key) {
			focus_ui.autocomplete.getObj( obj );
			key_code = focus_ui.autocomplete.getKeyCode(key);
			
			var c_obj = focus_ui.autocomplete.target.find('tr.current');
			
			// Tab Key
			if (key_code == 9) {
				focus_ui.autocomplete.target.fadeOut('medium');									
				focus_ui.autocomplete.tbox.val( focus_ui.autocomplete.target.find('tr.current td').text() );
			}
			// Down Key
			if (key_code == 40) {
				if (!c_obj.hasClass('last')) {									
					c_obj.removeClass('current');					
					c_obj.nextAll('.show').eq(0).addClass('current');
				}
			}
			// Up Key
			if (key_code == 38) {
				if (!c_obj.hasClass('first')) {
					c_obj.removeClass('current')
					c_obj.prevAll('.show').eq(0).addClass('current');
				}
			}
		},
		
		filterOption: function(obj, target) {
			var value = $(obj).val().toUpperCase();
			var counter = 0;
		
			var x = $(target).find('tr.pool td');
			
			// Reset target attribute			
			$(target).find('tr.first').removeClass('first');
			$(target).find('tr.last').removeClass('last');
			$(target).find('tr.current').removeClass('current');
			$(target).attr('id', '');
			
			//alert(x.length);
			//$('#debug').html(x.length);
			for (var i = 0 ; i < x.length ; i++) {
				var td = x.eq(i);
				
				if (td.text().toUpperCase().indexOf(value) < 0 ) {
					td.parent().hide();
					td.parent().removeClass('show');
				}
				else {
					if ( (target.attr('id') > i) ||
						 (target.attr('id') == '') ) {
						target.attr('id', i);
					}
					td.parent().addClass('show');
					td.parent().show();
					counter++;
				}
			}
			
			$(target).find('tr.show:first').addClass('first');
			$(target).find('tr.show:last').addClass('last');
			$(target).find('tr.show:first').addClass('current');	
			
			return counter;
		},
		
		getKeyCode : function(key) {
			return key.keyCode ? key.keyCode : key.which;
		}
	},
	
	tab: {
		tab_detail : null,
		content_detail : null,	
		desc_detail : null,
		
		
		init : function( tab_obj ) {			
			$(tab_obj.tab).each(function() {
				$(this).find(tab_obj.tab_detail+':first').addClass('current');
				$(this).find(tab_obj.tab_detail+':last').addClass('last');
			});
			
			$(tab_obj.content).each(function() {
				$(this).find(tab_obj.content_detail+':first').addClass('current');
				$(this).find(tab_obj.content_detail+':not(:first)').css('display', 'none');
			});			

			$(tab_obj.tab).each(function() {
				focus_ui.tab.tab_detail = $(this).find(tab_obj.tab_detail+' '+tab_obj.target_dom);

				for (var i = 0 ; i < focus_ui.tab.tab_detail.length ; i++) {							
					focus_ui.tab.tab_detail.eq(i)
						.attr('id', i)
						
						.bind('click', function() {							
							focus_ui.tab.changeTab( tab_obj, $(this) );
							
							$('select.re_list').trigger('change');
							$('select.loc_list').trigger('change');
						});								
				}
			});
			
			$(tab_obj.content).each(function() {
				focus_ui.tab.content_detail = $(this).find(tab_obj.content_detail);
			});
		},
		
		changeTab : function( tab_obj, c_obj ) {
			var this_id = $(c_obj).attr('id');
			
			// Handle Tab
			$(c_obj).parents(tab_obj.tab)				
				.find(tab_obj.tab_detail).removeClass('current');
			$(c_obj).parents(tab_obj.tab)
				.find(tab_obj.tab_detail).eq(this_id).addClass('current');

			// Handle Tab Content
			$(c_obj).parents(tab_obj.parent)
				.find(tab_obj.content_detail).hide().removeClass('current');			
			$(c_obj).parents(tab_obj.parent)
				.find(tab_obj.content_detail).eq(this_id).fadeIn().addClass('current');
		}
	},
		
	scroller: {
		intval: null,
		init: function( selector, speed ){
			if( typeof(speed)=='underfind' )
			{
				speed = 50;
			}
			var parentBox = $(selector);
			parentBox.find('.scroller-forward')
				.bind('mouseover',function(event){ focus_ui.scroller.next( selector, speed ); })
				.bind('mouseout',function(event){ focus_ui.scroller.stop(); });
			parentBox.find('.scroller-backward')
				.bind('mouseover',function(event){ focus_ui.scroller.prev( selector, speed ); })
				.bind('mouseout',function(event){ focus_ui.scroller.stop(); })
				.css('display', 'none');
		}, 
	
		move: function( mode, selector) {		
			var parentBox = $(selector);
			var box = parentBox.find('.scroller-mainbody');
			var max = box.attr('scrollWidth') - box.width();
			
			if (mode == 'next') {
				if ( box.scrollLeft() == max ) {
					parentBox.find('.scroller-forward').css('display', 'none');
				}
				else {
					parentBox.find('.scroller-backward').css('display', '');
					box.scrollLeft(box.scrollLeft() + 1 );
				}										
			}
			else {
				if ( box.scrollLeft() == 0) {
					parentBox.find('.scroller-backward').css('display', 'none');
				}
				else {	
					parentBox.find('.scroller-forward').css('display', '');
					box.scrollLeft(box.scrollLeft() - 1 );										
				}
			}
		},
		
		next: function( selector, speed ){
			focus_ui.scroller.intval = setInterval("focus_ui.scroller.move('next', '"+ selector +"')", speed );
		},
		
		prev: function( selector, speed ){
			focus_ui.scroller.intval = setInterval("focus_ui.scroller.move('prev', '"+selector+"')", speed );
		},
		
		stop: function(){
			window.clearInterval(focus_ui.scroller.intval);
		}
	
	},
	
	vscroller: {
		v_intval: null,
		init: function( selector, speed ){
			if( typeof(speed)=='underfind' )
			{
				speed = 50;
			}
			var parentBox = $(selector);
			parentBox.find('.vscroller-up')
				.bind('mouseover',function(event){ focus_ui.vscroller.up( selector, speed ); })
				.bind('mouseout',function(event){ focus_ui.vscroller.stop(); })
				.css('display', 'none');
			parentBox.find('.vscroller-down')
				.bind('mouseover',function(event){ focus_ui.vscroller.down( selector, speed ); })
				.bind('mouseout',function(event){ focus_ui.vscroller.stop(); });			
		}, 
	
		move: function( mode, selector) {		
			var parentBox = $(selector);
			var box = parentBox.find('.vscroller-mainbody');
			var max = box.attr('scrollHeight') - box.height();
			
			if (mode == 'down') {
				if ( box.scrollTop() == max ) {
					parentBox.find('.vscroller-down').css('display', 'none');
				}
				else {
					parentBox.find('.vscroller-up').css('display', '');
					box.scrollTop(box.scrollTop() + 1 );
				}										
			}
			else {
				if ( box.scrollTop() == 0) {
					parentBox.find('.vscroller-up').css('display', 'none');
				}
				else {	
					parentBox.find('.vscroller-down').css('display', '');
					box.scrollTop(box.scrollTop() - 1 );										
				}
			}
		},
		
		up: function( selector, speed ){
			focus_ui.vscroller.v_intval = setInterval("focus_ui.vscroller.move('up', '"+ selector +"')", speed );
		},
		
		down: function( selector, speed ){
			focus_ui.vscroller.v_intval = setInterval("focus_ui.vscroller.move('down', '"+selector+"')", speed );
		},
		
		stop: function(){
			window.clearInterval(focus_ui.vscroller.v_intval);
		}
	
	},

	lookup: {
		xml: {},
		init: function( urlList, callback ){
			for( var x in urlList ){ 
				focus_ui.lookup.xml[x] = null;
			}
			$.each( urlList, function( name, url ){
				$.get( url, {}, function(data){ 
					focus_ui.lookup.xml[ name ] = $(data); 
					for( var x in focus_ui.lookup.xml ){ 
						if( focus_ui.lookup.xml[x]==null ){ 
							return;
						}
					}
					if( callback!=undefined )
					{
						callback();
					}
				}, 'xml' );
			});
		},
		getValue: function( name, id ){
			return focus_ui.lookup.xml[name].find('[id='+id+']').text();
		},
		match: function( selector, name ){
			$( selector ).each(function(){
				var result = focus_ui.lookup.xml[name].find('[id='+$(this).text()+']');
				if( result.length > 0 )
				{
					$(this).html( result.text() );
				}
			});
		},
		appendOptions: function( selector, name, filter, emptyStr ){
			filter = (filter==undefined)? 'data' : 'data'+filter;
			emptyStr = (emptyStr==undefined)? '-----' : emptyStr;
			var opt = '<option value="">'+emptyStr+'</option>';
			var fData = focus_ui.lookup.xml[name].find(filter);
			for( var i=0; i<fData.length; i++ )
			{
				var data = fData.eq(i);
				opt += '<option value="'+data.attr('id')+'">'+data.text()+'</option>';
			}
			$( selector ).append( opt );
		}
	},
	
	verson: 2
};

