/**
 * app.js @ teatraslele.lt
 */

if (typeof($app) == 'undefined'){ var $app = {}; };

jQuery.extend($app, {
	debug: 				false,
	initialized:		false,
	ajax:	{
		timeout:		5000		
	},	
	opts:			{		
		/*guestbook: {
			addContainerId:		'guestbookadd'
		},*/
		highligtsMarquee: {
			containerSel: 		'div.highlights div.content'
		},
		scrollToAnchor: {
			linkClass:	'scroll',
			duration:	300
		},
		lightbox:	{
			selA:		'a[rel*=lightbox]',
			JsUrl:		'js/jquery.lightbox-0.5.min.js',
			CssUrl:		'css/lightbox.css'
		},
		googleMaps:	{
			apiKey:		'ABQIAAAA9Xy9Akliy5zr1pCeDcJBLxQSZ2HgZ_5O3y6fCtGMexJNw3scOhRuvQ4FLf96GgWrLhy0byMGOCO5QQ'
		}
	},
	initialize: function() {		
		var args = arguments;
		if (typeof($app.base) != 'string'){
			return false;
		}
		if (!$app.initialized){			
			jQuery.ajaxSetup({
				timeout: $app.ajax.timeout
			});
			//if(typeof($app.language) == 'string'){
				//$app.log('loading language file');
				//jQuery.ajax({url: $app.base + 'js/app.language_' + $app.language + '.js', type: 'GET', async: false, dataType: 'script', cache: true});				
			//}			
			jQuery(document).ready(function(){						
				jQuery.each(args, function(i,v){					
					if (jQuery.isFunction($app[v+'Setup'])){
						$app.log('init: '+v);
						$app[v+'Setup']();
					}
				});				
			});
			jQuery.extend($app, {initialized: true});
		}
		return true;
	},	
	log: function() {
		if (jQuery.browser.mozilla && $app.debug && window.console && jQuery.isFunction(window.console.log) && arguments.length) window.console.log.apply($app, arguments);			
	},		
	guestbookSetup: function() {
		/*$('#'+$app.opts.guestbook.addContainerId).bind('setAjaxListener',function(){			
			$app.log('guestbook: setAjaxListener');
			var elContainer = $(this);
			elContainer.find('form:first').bind('submit',function(){
				$.ajax({
					url: $(this).attr('action'),
					type: 'post',
					data: $(this).serialize(),
					success: function(data){					
						elContainer.html(data);						
						elContainer.trigger('setAjaxListener');						
					},error: function(){}
				});				
				return false;
			});
		}).trigger('setAjaxListener');*/
	},
	scrollToAnchorSetup: function(){
		$('.'+$app.opts.scrollToAnchor.linkClass).click(function(ev){
			ev.preventDefault();
			var p = this.href.split("#"), offset = $("#"+p[1]).offset();			
			$('html, body').animate({scrollTop:offset.top}, $app.opts.scrollToAnchor.duration);
		});
	},
	
	messageFlashSetup: function() {
		$('div.message.flash,div.error.flash').hide().fadeIn('slow');
	},
	
	highligtsMarqueeSetup: function() {
		
		$($app.opts.highligtsMarquee.containerSel).show();		
		if($app.base != window.location.href.replace(/\?.*?$/,'')) 
			return;
		
		$($app.opts.highligtsMarquee.containerSel)
			.attr({
				'scrollamount':	1,
				'behavior': 	'scroll',
				'direction': 	'left'
			})
			.marquee()
			.hover(
				function(){ $(this).trigger('stop'); },
				function(){ $(this).trigger('start'); }
			)
			.mousemove(function(ev){if ($(this).data('drag') == true) this.scrollLeft = $(this).data('scrollX') + ($(this).data('x') - ev.clientX); })
			.mousedown(function(ev){ $(this).data('drag', true).data('x', ev.clientX).data('scrollX', this.scrollLeft); })
			.mouseup(function(){ $(this).data('drag', false); });
	},
	
	lightboxSetup: function(){
		var elements = $($app.opts.lightbox.selA);
		if (!elements.length) return false;
		
		function scriptOnloadCallback(){
			$(elements).lightBox({ 
				txtImage: '',
				txtOf: ' / ',
				overlayBgColor: '#000000',
				overlayOpacity: 0.85,
				imageLoading:	$app.base + 'img/lightbox/lightbox-ico-loading.gif',
				imageBtnClose:	$app.base + 'img/lightbox/lightbox-btn-close.gif',
				imageBtnPrev:	$app.base + 'img/lightbox/lightbox-btn-prev.gif',
				imageBtnNext:	$app.base + 'img/lightbox/lightbox-btn-next.gif',
				imageBlank:		$app.base + 'img/lightbox/lightbox-blank.gif'
			}).click(function(){
				$('#lightbox-nav-btnPrev').attr('title', '<<');
				$('#lightbox-nav-btnNext').attr('title', '>>');
				$('#lightbox-secNav-btnClose').attr('title', 'x');			
			});
		}
		
		var documentHead = document.getElementsByTagName('head')[0], stylesheet = document.createElement('link'), script = document.createElement('script');
		
		stylesheet.rel = 'stylesheet';
		stylesheet.type = 'text/css';
		stylesheet.media = 'screen';
		
		stylesheet.href = $app.base + $app.opts.lightbox.CssUrl;		
		documentHead.appendChild(stylesheet);	

		script.type = 'text/javascript';
		script.src = $app.base + $app.opts.lightbox.JsUrl;
		
		if (!$.browser.msie) {
			script.onload = function(){ setTimeout(function(){ scriptOnloadCallback() }, 10); };
		}else{
			script.onreadystatechange = function() {
				if (script.readyState == 'loaded' || script.readyState == 'complete'){
					script.onreadystatechange = null;
					scriptOnloadCallback();
				}
			};
		}
		
		documentHead.appendChild(script);		
		return true;
	},
	newsletterPortletSetup: function()
	{
		$('div.portlet.newsletter form').submit(function(){			
			if(!$.trim($(this).find('input.text').attr('value')).length)
				return false;
		});
	},
	googleMapsSetup: function()
	{
		var elMap = $('#google-maps-placeholder0');	
		if(elMap.length) {			
			if (GBrowserIsCompatible()) {
				var objMap = new GMap2(elMap[0]), objMapLatLng = new GLatLng(54.677072, 25.286714);				
				objMap.setCenter(objMapLatLng, 15);
				objMap.addOverlay(new GMarker(objMapLatLng));
				objMap.setUIToDefault();
			}	
		}
	}

});

$app.initialize('highligtsMarquee','scrollToAnchor','messageFlash','lightbox','newsletterPortlet','googleMaps');