/**
* Market.hu
*
* @author Szijarto Tamas ( szicsu )
**/

var Market = new function(){
	var self = this;
	
	/**
	* alert funkcio
	* 
	* majd szép alert ablakot fog dobni
	*
	* @param string msg az üzenet
	**/
	this.alert = function( msg ){
		alert( msg );
	}
		
	/**
	* get funkcio
	*
	* leker egy tarlamat a szerverrol
	* 
	* @param string rUrl az url amit szeretnél
	* @param object postData ezt küldi postban
	* @param mixed proc ha fuggveny proc( html ) , ha jQueryObj, akkor proc.html( html );
	**/	
	this.get = function( rUrl , postData , proc ){
		
		postData.ajax='true';
		
		$.ajax({
			type: "POST",
			url: rUrl,
			data: postData,
			dataType: "html",
			success: function( html ){
				
				if( typeof( proc )== 'function' ){
					proc( html );
				}
				else{
					proc.html( html );
					
				}
			},
			error: function(){
				self.alert( 'AJAX ERROR' );
			}	  
		});	
	}	
	
	/**
	* oldal betoltesekor indulás :D
	**/
	$().ready( function(){
		Market.Init.run( PAGE );
	});
}

/**
* Altalanos initek
**/
Market.Init = new function(){
	
	var self = this;
	var scrollBar;
	
	
	/**
	* A scrollbart inicializálja
	**/
	this.initScrollBar = function( obj ){
		if( obj ){
			scrollBar = $('.scroll-pane', obj );
		}
		else{
			scrollBar = $('.scroll-pane');
		}
		scrollBar.jScrollPane({showArrows:false, scrollbarWidth: 6, scrollbarOnLeft: true, scrollbarMargin : 30 }) ;
		
	}
	
		
	/**
	* Alepértelemzett initiket itt kell lefutatni...
	**/
	this.run = function( page ){
		this.initScrollBar();
		Market.Pages.run( page );
	}
}


/**
* Page controllers
**/
Market.Pages = new function( page ){
	var self = this;
	/**
	* Index
	**/
	this.magyar = function(){
		
		$('a.popup').bind( 'click', function(){
			
			data = {};			
			data.title = $(this).attr('title');
 			data.text = $( $(this).attr('href') ).html();
			Market.PopUp.open( data );
			return false;
			
		});
		
		//aktialitasok
		Market.get( 'index2.php?modul=aktualitasok', {}, function( html ){
			
			$('#aktualitasok')
				.html( html )
				.find('a').bind('click',function(){
					
					obj = $(this);
					
					data = {};			
					data.title = obj.attr('title');
		 			data.text = $( '#' + obj.attr('href').split('#')[1] ).html();
					data.date = obj.attr('alt').split(' ')[0];
					Market.PopUp.open( data );
				});
			
			Market.Init.initScrollBar( $('#aktualitasok') );
		});		
	}
	
	
	
	/**
	* Ceginformaco
	**/
	this.ceginformacio = function(){

		$('a.subNavLink').bind('click.nav', function(){
			
			obj = $(this);
			hova = $('#subContent');
			param = obj.attr('href').match("modul=(.*)" )[1];	
			
			Market.get( obj.attr('href'), { subContent: param},function(html){
				$('#subContent').html( html );
				Market.Init.initScrollBar( $('#subContent') );
				
			});		
			
			$('a.subNavLink').each(function(){
				$(this).parent().removeClass('activecim');	
			});
			obj.parent().addClass('activecim');
			return false;
		});	
		return false;
	}
	
	
	
	
	/**
	* Szervezet
	**/
	this.szervezet = function(){
		
		$('#szervezetGazdasagiAdatok, #szervezetiFelepitesPopUp').bind('click', function(){
			
			Market.PopUp.open({
				template: 'kepek',
				width: 100,
				height: 100,
				style: 'visibility:hidden;',
				kep: $(this).attr('href')
			},function( win ){					
				win.find('img.kepekImg').bind('load', function(){
					win.css({ 
						width: this.width, 
						height: this.height, 
						marginTop: parseInt( this.height / 2 ) * -1, 
						marginLeft:  parseInt( this.width / 2 ) * -1 
					})
					.find('div.popupKepek').css({visibility: 'visible'});
				});
			});				
			
			return false;
		});
		
	}
	
	
	/**
	* Referenciák
	**/ 
	this.referenciak = function(){
		
		$('a.subNavLink').bind('click.nav', function(){
			
			obj = $(this);
			$('#subContentH1').text( obj.text() );
			Market.get( obj.attr('href'), { subContent: 'true'},function(html){
				$('#subContent').html( html );
				Market.Init.initScrollBar( $('#subContent') );
				self.subReferenciak();
				return false;
			});	
				
			return false;
		});	
		
		return false;
	}
	
	this.subReferenciak = function(){
		$('.backbutton').show();
		$('#subContent')
		
			.find('a.rolldown_open').bind('click',function(){
				$('.rolldown').hide();
				$( '#' + $(this).attr('href').split('#')[1] ).show();					
				return false;				
			}).end()
			
			.find('a.rolldown_close').bind('click',function(){
				$(this).parents('.rolldown').hide();
				return false;
			}).end()

			.find('a.rolldown_popup').bind('click',function(){
				
				Market.PopUp.open({
					template: 'kepek',
					width:100,
					height:100,
					style: 'visibility:hidden;',
					kep: $(this).attr('href')
				},function( win ){					
					win.find('img.kepekImg').bind('load', function(){
						win.css({ 
							width: this.width, 
							height: this.height, 
							marginTop: parseInt( this.height / 2 ) * -1, 
							marginLeft:  parseInt( this.width / 2 ) * -1 
						})
						.find('div.popupKepek').css({visibility: 'visible'});
					});
				});	
				
				return false;
			});		
	}
	

	/**
	* Építkezések
	**/
	this.epitkezesek = function(){
		
		
		$('a.subNavLink').bind('click.nav', function(){
			
			obj = $(this);
			$('.backbutton').show();
			
			Market.get( obj.attr('href'), { subContent: 'true'},function(html){
				
				$('#subContent')
					.html( html )
					.find('h2').text( obj.text() ).end()
					.find('h3').text( obj.attr('title') );
				
				Market.Init.initScrollBar( $('#subContent') );
				self.subEpitkezesek();
				return false;
			});	
				
			return false;
		});
		
	}
	
	/**
	* subÉpitkezések
	**/
	this.subEpitkezesek = function(){
	
		
	
		$('#subContent').find('a.imageView').bind('click',function(){
			
			obj = $(this);
			$('#column2')
				.find('a')
					.attr('href', obj.attr('href') )
					.attr('title', obj.attr('title') )
					.end()
				.find('img')
					.attr('src', obj.attr('href') )
					.end()
				.find('span')
					.text( obj.attr('title') );
			
			return false;
		})
		.eq( '0' ).trigger('click');
	}
	
	
	/**
	* Videok - Befejezett építkezések
	**/
	this.videok = function(){
		$('a.videoLoads').bind('click',function(){
			var so = new FlashObject("/includes/player.swf", "fvp", "320", "271", "8", "#000000");
			//so.addParam("wmode", "transparent");
			so.addVariable("myvideo",  $(this).attr('href') );
			so.write( 'videoPlayerDiv' );
			
			return false;
		}).eq( '0' ).trigger('click');
	}
	
	
	
	/**
	* Események
	**/
	this.esemenyek = function(){
		
		$('#subContent').find('a.subNavLink').bind('click',function(){
			$('.backbutton').show();			
			
			obj = $(this);
			Market.get( obj.attr('href'), { subContent: 'esemenyitem', esemeny: obj.attr('alt') },function(html){
				
				$('#subContent').html( html );
				Market.Init.initScrollBar( $('#subContent') );
				self.subEsemenyek();				
				return false;
			});	
 			
			return false;
		});
		$("a.subNavLink[alt='"+location.hash.replace ('#','')+"']").trigger('click');
	}
	
	
	this.subEsemenyek = function(){
		$('#subContent').find('a.imageView').bind('click', function(){
			
			obj= $(this);
			
			$('#column1')
				.find('img')
					.attr('src', obj.attr('href').replace('_thumb', '' ) )
					.end()
				.find('span')
					.text( $('a.imageView').size() );
					
			
			return false;
		})
		.eq( '0' ).trigger('click');
	}
	
	/**
	* Panorama
	**/	
	this.panorama = function(){
		
		$('a.jsPanoramaItem').bind('click',function(){
			
			var panoramaUrl = $(this).attr('href');
			
			Market.PopUp.open({
				template: 'panorama',
				width: 600,
				height: 400,
				style: 'visibility:hidden;'
			},function( win ){					
								
				$.ajax({
					type: "GET",
					url: panoramaUrl,
					dataType: "html",
					success: function( html ){
						$('div#panoramaLoadedDiv', win).html( html );
						win.find('div.popupKepek').css({visibility: 'visible'});
					}
				});	
			});		
			
			
			return false;
		});
	
	}
	
	
	/**
	* lefutatja a pagenek megfelelot
	**/
	this.run = function( page ){		
		for( i in this){
			if( i == page && typeof( this[i] == 'function' ) ){
				this[i]();
				break;
			}
		}
	}

}


/**
* Az oldalon hasznalt popUpok mind ezen keresztül kellene megvalósítani
**/
Market.PopUp = new function(){
	var self = this; 
	var overLayId = 'PU_overlay';
	var windowId = 'PU_widow';
	var closeId = 'PU_close';
	
	function initOverLay(){
		$('body').append('<div id="'+ overLayId +'">&nbsp;</div>');
	}
	
	function removeOverlay(){
		$('#'+overLayId).remove();
	}
	
	function initWindow( html, param ){
		$('<div id="'+windowId+'"></div>')
			.appendTo('body')
			.css({ width: param.width, height: param.height, marginTop: parseInt( param.height / 2 ) * -1, marginLeft:  parseInt( param.width / 2 ) * -1 })
			.html( html )
			.find('#'+closeId).bind('click', self.close);
			
		$(document).bind('keydown.popup', function( e ){
			(e.keyCode) ? keyCode=e.keyCode : keyCode=e.which;
			if(keyCode == 27){ 
				self.close();
			}
		});
		
		Market.Init.initScrollBar( $('#'+windowId) );
	}
	
	function removeWindow(){
		$('#'+windowId).remove();
	}
	
	function getTemplate( param ){
		
		switch( param.template ){
			
			case 'kepek':
				html = new Array(
					'<div class="popupKepek" style="{style}">',
						'<span class="popupKepek_close">',
								'<a href="#" id="'+closeId+'" title="{close}"><img src="/pictures/popup_close_bg.gif" alt="{close}" /></a>',
						'</span>',
						'<img class="kepekImg" src="{kep}" alt="" />',
					'</div>' 
				);			
			break;
			case 'panorama':
				html = new Array(
					'<div class="popupKepek" style="{style}">',
						'<span class="popupKepek_close">',
								'<a href="#" id="'+closeId+'" title="{close}"><img src="/pictures/popup_close_bg.gif" alt="{close}" /></a>',
						'</span>',
						'<div id="panoramaLoadedDiv"></div>',
					'</div>' 
				);			
			break;
			default:
				html = new Array(
					'<div class="popup" >',
						'<div class="popup_inner">',
							'<span class="popup_title">{title}</span>',
							'<span class="popup_date">{date}</span>',
							'<span class="popup_close">',
								'<a href="#" id="'+closeId+'" title="{close}"><img src="/pictures/popup_close_bg.gif" alt="{close}" /></a>',
							'</span>',
							'<div class="clr">&nbsp;</div>',
							'<div class="popup_text scroll-pane">{text}</div>',
							'<div class="clr">&nbsp;</div>',
						'</div>',
					'</div>'
					);			
			break;
		}
		
		return $.template( html.join('') ).apply( param );
	}
	

	this.open = function( param , callBack){
		removeWindow();
		initOverLay();
		
		param = param || {};
		
		/** default beállítások **/
		param.template= param.template || 'index';
		param.title = param.title || 'title :)';
		param.close = param.close || '';
		param.width = param.width || 452;
		param.height = param.height || 400;
		
		html = getTemplate( param ); 
		initWindow( html , param );
		
		if( typeof( callBack ) == 'function'){
			callBack( $('#' + windowId ) );
		}
	
	}
	
	this.close = function(){
		$(document).unbind('keydown.popup');
		removeWindow();
		removeOverlay();
		return false;
	}
}


