(function() {
   jQuery.fn['bounds'] = function () {
     var bounds = {left: Number.POSITIVE_INFINITY, top: Number.POSITIVE_INFINITY,
                   right: Number.NEGATIVE_INFINITY, bottom: Number.NEGATIVE_INFINITY};

     this.each(function (i,el) {
                 var elQ = $(el);

                 if (elQ.length == 0)
                   return;

                 var off = elQ.offset();
                 off.right = off.left + $(elQ).width();
                 off.bottom = off.top + $(elQ).height();

                 if (off.left < bounds.left)
                   bounds.left = off.left;

                 if (off.top < bounds.top)
                   bounds.top = off.top;

                 if (off.right > bounds.right)
                   bounds.right = off.right;

                 if (off.bottom > bounds.bottom)
                   bounds.bottom = off.bottom;

               });
     return bounds;
   }
 })();

JF = function(){
	this.ico = [];
	this.icohover = [];
	this.clickCount = 0;
	this.companyObj = [];
	this.stats = [];
};

JF.prototype.confrim = function( url ){
    if (confirm('Usunac wybrany element') == true) {
    	window.location.href = url;
    } else {
    	return false;
    }
};

JF.prototype.prompt = function( url ){
    var txt = prompt( 'Podaj nowe haslo: ' );
	if( /^[a-zA-Z0-9]{6,}/.test( txt ) ) {
		
		var instance = this;
		
		var xhr = new JF.Xhr( 'forumadmin/setpass' );
		xhr.send( { pass : txt }, instance.onSavePass );
		
		
	} else {
		alert( 'Wpisz minimum 6 znaków' );
	}
};

JF.prototype.onSavePass = function( data ) {
	alert( 'Haslo zostalo ustawione' );
};

JF.prototype.hideElement = function( elem ) {

	window.setTimeout(function(){

		$( '#'+elem ).animate({
			opacity : 0,
			width: '500px'
		},
		500,
		'linear',
		function(){ 
			$( '#'+elem ).animate({
				height : '0px'
			});
		});

	}, 3000 );

};

JF.prototype.checkHour = function( elem, form ) {
	if( /^[0-2]{1}[0-4]{1}\:[0-5]{1}[0-9]{1}$/.test( $( '#'+elem )[0].value ) ) {
		$( '#'+form ).submit();
		$( '#'+elem ).css( { border: '1px solid green' } );
	} else {
		$( '#'+elem ).css( { border: '1px solid red' } );
	}
};

JF.prototype.checkPassword = function( params ) {
	
	if( $( '#'+params.ob1 )[0].value ==  $( '#'+params.ob2 )[0].value ) {
		$( '#'+params.form ).submit();
	} else {
		alert( 'podane hasla nie sÄ? identyczne' );
	}
	
};

JF.prototype.checkCommentForm = function( elem, form ) {
	
	if( /^[^\ ].*/.test( $( '#'+elem )[0].value ) ) {
		$( '#'+form ).submit();
		$( '#'+elem ).css( { border: '1px solid green' } );
	} else {
		var handler;
		$( '#'+elem ).click( handler = function(){
			$( '#'+elem )[0].value = '';
			$( '#'+elem ).unbind( 'click', handler );
		} );
		$( '#'+elem ).css( { border: '1px solid red' } );
	}
};

JF.prototype.slide = function(evt) {
	var obj = $(evt).next();
	
	if (obj.is(":hidden")) {
		obj.slideDown("fast");
	} else {
		obj.hide();
	}
};

JF.prototype.CatalogListAdd = function( type, photo ){
	if( type == 'ico' ) {
		this.ico.push( photo );
	} else {
		this.icohover.push( photo );
	}
};

JF.prototype.CatalogList = function( root ) {
	var instance = this;

	$(document).ready(function(){
		var elems = $( '#' + root ).find( 'a' );
		
		var could = $('<div></div>').css({
			position: 'absolute',
			top: '0px',
			left: '0px',
			width: '180px',
			height: '350px',
			background: '#fff',
			opacity: 0,
			textAlign: 'left'
		}).append( 
		'<div style="padding:6px;"><b>Wybierz województwo</b><br /><br />' + 
			'<a id="region/all"><b>wszystkie</b></a> &raquo; <br />' +
			'<a id="region/dolnośląskie">dolnośląskie</a> &raquo; <br />' +
			'<a id="region/kujawsko-pomorskie">kujawsko-pomorskie</a> &raquo; <br />' +
			'<a id="region/lubelskie">lubelskie</a> &raquo; <br />' +
			'<a id="region/lubuskie">lubuskie</a> &raquo; <br />' +
			'<a id="region/łódzkie">łódzkie</a> &raquo; <br />' +
			'<a id="region/małopolskie">małopolskie</a> &raquo; <br />' +
			'<a id="region/mazowieckie">mazowieckie</a> &raquo; <br />' +
			'<a id="region/opolskie">opolskie</a> &raquo; <br />' +
			'<a id="region/podkarpackie">podkarpackie</a> &raquo; <br />' +
			'<a id="region/podlaskie">podlaskie</a> &raquo; <br />' +
			'<a id="region/pomorskie">pomorskie</a> &raquo; <br />' +
			'<a id="region/śląskie">śląskie</a> &raquo; <br />' +
			'<a id="region/świętokrzyskie">świętokrzyskie</a> &raquo; <br />' +
			'<a id="region/warmińsko-mazurskie">warmińsko-mazurskie</a> &raquo; <br />' +
			'<a id="region/wielkopolskie">wielkopolskie</a> &raquo; <br />' +
			'<a id="region/zachodniopomorskie">zachodniopomorskie</a> &raquo;' +	
		'</div>' ).click( function(){
			$(this).animate({ opacity : 0, height: '0px' });
			
		} );
		
		$( document.body ).append( could );
		
		for( var i = 0, len = elems.length; i < len; i++ ) {
			$( elems[i] ).hover(function(){
				$( this ).css( { background : 'url(' + instance.icohover[$( this ).attr( 'nr' )] + ')' } );
			},function(i){
				$( this ).css( { background : 'url(' + instance.ico[$( this ).attr( 'nr' )] + ')' } );
			}).click(function(){
				
				var regions = could.find( 'a' );
				for( var i = 0, len = regions.length; i < len; i++ ) {
					$( regions[i] ).attr({
						href : this.href + '/' + regions[i].id
					});
					
				}
				
				var bounds = $( this ).bounds();
				
				could.animate({
					left: parseInt( bounds.left - 30, 10 ) + 'px',
					top: parseInt( bounds.top - 70, 10 ) + 'px',
					opacity: 0.9,
					height: '350px'
				});
			});
		}
	});
}

JF.prototype.CatalogSelectedCount = function( root ) {
	var instance = this;

	this.clicked = {};
	
	$(document).ready(function(){
		var elems = $( '#' + root ).find( 'input' );
		
		var obj, fn;
		for( var i = 0, len = elems.length; i < len; i++ ) {
			obj = $( elems[i] );
			
			if( obj[0].checked ) {
				instance.clicked[obj[0].value] = true;
				instance.clickCount++;
			}
			
			obj.click( fn = function(e){	
				if( !instance.clicked[e.currentTarget.value] ) {
					if( instance.clickCount <= 1 ) {
						instance.clicked[e.currentTarget.value] = true;
						instance.clickCount++;
					} else {
						alert('Możesz wybrać max. 2 branże')
						return false;
					}
				} else {
					instance.clicked[e.currentTarget.value] = false;
					instance.clickCount--
				}
			});
		}
	});
};

JF.prototype.CompanyObjAdd = function( data ) {
	//this.companyAdd = JSON.decode( data );
	this.companyAdd = eval('(' + data + ')');
	//document.title = JSON.decode( data );
};

JF.prototype.CatalogMap = function( root ) {
	var instance = this;
	
	$(document).ready(function(){
	
		var cont = $( '#' + root );

		var woj = [];

		//document.title = instance.stats;

		//zachodniopomorskie
		woj.push( $( '<div id="zachodniopomorskie">'+instance.stats['zachodniopomorskie'][0]['cnt']+'</div>' ).css({ 
			position: 'absolute',
			top: '30px',
			left: '18px',
			fontSize : '10px',
			opacity : 0,
			width : '20px',
			cursor: 'pointer'
		}) );
		//pomorskie
		woj.push( $( '<div id="pomorskie">'+instance.stats['pomorskie'][0]['cnt']+'</div>' ).css({ 
			position: 'absolute',
			top: '16px',
			left: '56px',
			fontSize : '10px',
			opacity : 0,
			cursor: 'pointer'
		}) );
		//warmińsko-mazurskie
		woj.push( $( '<div id="warmińsko-mazurskie">'+instance.stats['warmińsko-mazurskie'][0]['cnt']+'</div>' ).css({ 
			position: 'absolute',
			top: '26px',
			left: '99px',
			fontSize : '10px',
			opacity : 0,
			cursor: 'pointer'
		}) );
		//podlaskie
		woj.push( $( '<div id="podlaskie">'+instance.stats['podlaskie'][0]['cnt']+'</div>' ).css({ 
			position: 'absolute',
			top: '42px',
			left: '132px',
			fontSize : '10px',
			opacity : 0,
			cursor: 'pointer'
		}) );
		//lubuskie
		woj.push( $( '<div id="lubuskie">'+instance.stats['lubuskie'][0]['cnt']+'</div>' ).css({ 
			position: 'absolute',
			top: '70px',
			left: '13px',
			fontSize : '10px',
			opacity : 0,
			cursor: 'pointer'
		}) );
		//wielkopolskie
		woj.push( $( '<div id="wielkopolskie">'+instance.stats['wielkopolskie'][0]['cnt']+'</div>' ).css({ 
			position: 'absolute',
			top: '62px',
			left: '40px',
			fontSize : '10px',
			opacity : 0,
			cursor: 'pointer'
		}) );
		//kujawsko-pomorskie
		woj.push( $( '<div id="kujawsko-pomorskie">'+instance.stats['kujawsko-pomorskie'][0]['cnt']+'</div>' ).css({ 
			position: 'absolute',
			top: '43px',
			left: '62px',
			fontSize : '10px',
			opacity : 0,
			cursor: 'pointer'
		}) );
		//mazowieckie
		woj.push( $( '<div id="mazowieckie">'+instance.stats['mazowieckie'][0]['cnt']+'</div>' ).css({ 
			position: 'absolute',
			top: '62px',
			left: '104px',
			fontSize : '10px',
			opacity : 0,
			cursor: 'pointer'
		}) );
		//lubelskie
		woj.push( $( '<div id="lubelskie">'+instance.stats['lubelskie'][0]['cnt']+'</div>' ).css({ 
			position: 'absolute',
			top: '90px',
			left: '126px',
			fontSize : '10px',
			opacity : 0,
			cursor: 'pointer'
		}) );
		//łódzkie
		woj.push( $( '<div id="łódzkie">'+instance.stats['łódzkie'][0]['cnt']+'</div>' ).css({ 
			position: 'absolute',
			top: '80px',
			left: '76px',
			fontSize : '10px',
			opacity : 0,
			cursor: 'pointer'
		}) );
		//dolnośląskie
		woj.push( $( '<div id="dolnośląskie">'+instance.stats['dolnośląskie'][0]['cnt']+'</div>' ).css({ 
			position: 'absolute',
			top: '95px',
			left: '31px',
			fontSize : '10px',
			opacity : 0,
			cursor: 'pointer'
		}) );
		//opolskie
		woj.push( $( '<div id="opolskie">'+instance.stats['opolskie'][0]['cnt']+'</div>' ).css({ 
			position: 'absolute',
			top: '105px',
			left: '55px',
			fontSize : '10px',
			opacity : 0,
			cursor: 'pointer'
		}) );
		//śląskie
		woj.push( $( '<div id="śląskie">'+instance.stats['śląskie'][0]['cnt']+'</div>' ).css({ 
			position: 'absolute',
			top: '110px',
			left: '74px',
			fontSize : '10px',
			opacity : 0,
			cursor: 'pointer'
		}) );
		//świętokrzyskie
		woj.push( $( '<div id="świętokrzyskie">'+instance.stats['świętokrzyskie'][0]['cnt']+'</div>' ).css({ 
			position: 'absolute',
			top: '101px',
			left: '100px',
			fontSize : '10px',
			opacity : 0,
			cursor: 'pointer'
		}) );
		//małopolskie
		woj.push( $( '<div id="małopolskie">'+instance.stats['małopolskie'][0]['cnt']+'</div>' ).css({ 
			position: 'absolute',
			top: '125px',
			left: '90px',
			fontSize : '10px',
			opacity : 0,
			cursor: 'pointer'
		}) );
		//podkarpackie
		woj.push( $( '<div id="podkarpackie">'+instance.stats['podkarpackie'][0]['cnt']+'</div>' ).css({ 
			position: 'absolute',
			top: '121px',
			left: '120px',
			fontSize : '10px',
			opacity : 0,
			cursor: 'pointer'
		}) );

		var wojName = '';
		
		var bounds = cont.bounds();
		
		var category = $( '<div style="padding:6px;text-align:left;"><b>Wybierz kategorie</b><br /><br /></div>' );
		
		var elAll = $( '<a href="javascript:void(0)" id="category/all/cat/1"><b>wszystkie</b></a> &raquo; <br />' );
		elAll.click(function(e){
			setTimeout(function()
			{
			    window.location = '/pcatalog/list/category/all/region/' + wojName + '/cat/1';
			}, 0);
		});
		category.append( elAll );
	
		//document.title = instance.companyAdd;

		for( var i = 0, len = instance.companyAdd.length; i < len; i++ ) {
			el = $('<a href="javascript:void(0)" id="category/' + instance.companyAdd[i].id + '">' + instance.companyAdd[i].name + '</a> &raquo; <br />');
			el.click(function(e){
				setTimeout(function()
				{
				    window.location = '/pcatalog/list/' + e.target.id + '/region/' + wojName + '/cat/1';
				}, 0);
			});
			category.append( el );
		}

		var could = $('<div></div>').css({
			opacity : 0,
			display : 'none',
			position : 'absolute',
			top: parseInt( bounds.top, 10 ) + 'px',
			left : parseInt( bounds.left - 20, 10 ) + 'px',
			width: '200px',
			height: '0px',
			background : '#fff',
			overflow : 'hidden'
		}).append( category ).click(function(){
			could.css({ display : 'none', height : '0px', opacity : 0 });
			if( $.browser.msie ) {
				$(document.body).find( 'select' ).css({ visibility : 'visible' });
			}
		});
			
		$( document.body ).append( could );

		for( var i = 0, len = woj.length; i < len; i++ ){
			cont.append( woj[i][0] )
			woj[i].animate({ 
				opacity : 1
			}).click(function(){
				could.css({ display : 'block' });
				if( $.browser.msie ) {
					$(document.body).find( 'select' ).css({ visibility : 'hidden' });
				}
				could.animate({
					opacity : 0.9,
					height : '480px'
				});
				wojName = this.id;
			});
		}

	});

};

JF.prototype.CompanyStats = function( data ) {
	var instance = this;

	$(document).ready(function(){
		//instance.stats = JSON.decode( data );
		instance.stats = eval('(' + data + ')');
	});
};

$$JF = new JF();

