document.root_folder = '';

function trySendComment( id, title ) {
	var ajax = new myAjax();
	ajax.action = 'trySendComment';
	ajax.post( 'title='+title );
	
}

function togglePhoto( linker ) {
	get( 'ph_main' ).src = linker;
}

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}




function saveNewPassword() {
	var op = get( 'old_pass' ).value;
	var np = get( 'new_pass' ).value;
	var npr = get( 'new_pass_repeat' ).value;
	var ajax = new myAjax();
	ajax.action = 'saveNewPassword';
	ajax.post( 'op='+op+'&np='+np+'&npr='+npr );
	ajax.onLoad = function() {
		var d = get( 'passwordMessage' );
		switch( this.response ) {
			case 'ok':
				putHtml( d, 'Zapisano nowe hasło' );
				break;
			case 'not_empty':
				putHtml( d, 'Hasło nie może być puste' );
				break;
			case 'bad_compare':
				putHtml( d, 'Hasło nie zgadza się z powtórzeniem hasła' );
				break;
			case 'bad_pass':
				putHtml( d, 'Stare hasło jest nieprawidłowe' );
				break;
			default:
				putHtml( d, this.response );
				break;
		}
	}
}

function searcher( target ) {
	var t = getSearch( target );
	if( t.length < 3 ) {
		alert( 'Wyszukiwany tekst musi mieć conajmniej 3 znaki' );
	} else {
		document.location = '/szukaj/'+t;//+'/kategoria/'+o;
	}
}

function searchCurrent( target, linker ) {
	var t = getSearch( target );
	if( t.length < 3 ) {
		alert( 'Wyszukiwany tekst musi mieć conajmniej 3 znaki' );
	} else {
		var h = linker;
		var len = h.length;
		var ls = h.substr( len - 1, 1 );
		if( ls == '/' ) {
			h = h.substr( 0, len - 1 );
		}
		document.location.href = h + '/szukaj/'+t;
	}
}

function getSearch( target ) {
	get( target ).value = trim( getValue( target ) );
	return getValue( target ).split( ' ' ).join( '+' );
}

function publicLogin() {
	var l 	= getValue( 'public_login' );
	var p	= getValue( 'public_password' );
	var g = getRadioValueByName( 'save_pass' );
//	get( 'loginMessage' ).value = '';
	var ajax = new myAjax();
	var pstr = 'login='+l+'&pass='+p+'&save_pass='+g+'&cn=Client';
	ajax.action = 'publicLogin';
//	ajax.post ( pstr, 'loginMessage' );
	ajax.post ( pstr );
	ajax.onLoad = function() {
		if( this.response == 'ok' ) {
			document.location.reload();
		} else {
			alert( this.response );
		}
	}
}

function validateEmail( email ) {
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   if( reg.test( email ) == false ) {
      return false;
   }
   return true;
}



$(document).ready( function (){

	
	$( '.map_over' ).bind( 'click', function(){
		$('.mapImage_bg').css( 'display','block' );
    $("#opacityContent").css({
     'background':'#fff',
     'display':'block'
     }).fadeTo('slow','0.8');  
	});
	
	
	$( '.mapImage_closeBg' ).bind( 'click', function(){
		$('.mapImage_bg').css( 'display','none' );
		//$("#opacityContent").fadeTo('slow','0');
		$("#opacityContent").css('display','none');

	});
	
	
	
	
	$( '.paletteBox' ).bind( 'mouseover', function(){
		var idk = this.id + "_box";
		var id = this.id;
		var bgColor = $('#'+idk).css("background-color");
		var idk2 = this.id + "_span";
		var idk3 = this.id + "_span2";
		
    $( '.ccColor' ).css( 'display','none' );
		$( '#'+idk ).css( 'display','block' );
		$('#'+id).css('border-top','3px solid '+bgColor);
		$(this).children( '.seeAllPaletteBase').css( 'display','none' );
		$( '#'+idk2 ).css( 'display','block' );
		$( '#'+idk3 ).css( 'display','block' );
		
  	
	}).bind( 'mouseout', function() {
	  var id = this.id;
		var idk = this.id + "_box";
		var idk2 = this.id + "_span";
		var idk3 = this.id + "_span2";
		
		
		setTimeout(function(){ 
		  $( '#'+idk ).css( 'display','none' );
		  $(this).children( '.seeAllPaletteBase').css( 'display','block' );
		  $( '#'+idk2 ).css( 'display','none' );
		  
		  $( '#'+idk3 ).css( 'display','none' );
		},100);
		$('#'+id).css('border-top','3px solid #fff');
	});
	
	

	$( '.boxBottomColor' ).bind( 'mouseover', function(){
		resetZIndexes();
		$(this).css( 'z-index', '3' );
		$(this).parent().css( 'z-index', '3' );
		$(this).parent().parent().css( 'z-index', '3' );
		$(this).children( '.boxBottomColorOverShodow').css( 'display','block' );
	}).bind( 'mouseout', function() {
		resetZIndexes();
		$(this).children( '.boxBottomColorOverShodow').css( 'display','none' );
	});

	$( '.scroller' ).each( function () {
		var hm = $(this).children().length;
		if( hm > 6 ){
			if( hm%6 > 0 ){
				hm = ( Math.floor(hm / 6) + 1 ) *6;
			}
			var wdh = hm * 160;
			$(this).css( 'width', wdh+'px' );
			checkMovings( this );
		}
	});
	
	
	
	$('.imageFilter').each(function(){
    var imgObj = $(this).find('img');
    grayscale(imgObj);
 
    $(imgObj).css({
      'background':'#2e3360',
      'opacity':'0.4'
    });
    
  }).bind('mouseover',function(){
    var imgObj = $(this).find('img');
    grayscale.reset(imgObj);
    $(imgObj).css({
      'background':'none',
      'opacity':'1'
    }); 
  
  }).bind('mouseout',function(){
    var imgObj = $(this).find('img');
    grayscale(imgObj);
    $(imgObj).css({
      'background':'#2e3360',
      'opacity':'0.4'
    });
  }); 


	$('.rightSideBoxColor').bind( 'click', function(){
		var id = this.id;
		id = id.replace( 'cd', '' );
		openColourPage( id );
	});
});

function checkMovings( elem ){
	var lefto = $( elem ).css( 'left' );
	lefto = parseInt( lefto.replace( 'px', '' ) );
	var widtho = $( elem ).css( 'width' );
	widtho = parseInt( widtho.replace( 'px', '' ) );

	var toLeft = false;
	var toRight = false;

	if( lefto == 0 ){
		toLeft = false;
		toRight = true;
	}
	if( lefto < 0 && (lefto*-1) < widtho ){
		toLeft = true;
		toRight = true;
	}
	if( lefto < 0 && (lefto*-1 + 960 ) >= widtho ){
		toLeft = true;
		toRight = false;
	}
	if( toLeft ){
		$( elem ).closest( '.scrollerBox' ).find( '.leftArrow a' ).unbind( 'click' ).bind( 'click', {scr: elem}, function( event ){
			event.preventDefault();
			$(event.data.scr).animate({left: '+=960'}, 700, function(){
				checkMovings( event.data.scr );
			} );
		});
	}
	else {
		$( elem ).closest( '.scrollerBox' ).find( '.leftArrow a' ).unbind( 'click' ).bind( 'click', function( event ){
			event.preventDefault();
			return true;
		});
	}

	if( toRight ){
		$( elem ).closest( '.scrollerBox' ).find( '.rightArrow a' ).unbind( 'click' ).bind( 'click', {scr: elem}, function( event ){
			event.preventDefault();
			$(event.data.scr).animate({left: '-=960'}, 700, function(){
				checkMovings( event.data.scr );
				} );
			});
	}
	else {
		$( elem ).closest( '.scrollerBox' ).find( '.rightArrow a' ).unbind( 'click' ).bind( 'click', function( event ){
			event.preventDefault();
			return true;
		});
	}
}

function showImage(imageId,show){
  if (show == "1"){
    document.getElementById(imageId).style.display = 'block';
  } else {
      document.getElementById(imageId).style.display = 'none';
  }
}

function resetZIndexes(){
	$( '#leftSideBox .boxLeft' ).css( 'z-index', '2' );
	$( '#leftSideBox .boxCenter' ).css( 'z-index', '2' );
	$( '#leftSideBox .boxRight' ).css( 'z-index', '2' );
	$( '.boxBottomColor' ).css( 'z-index', '2' );
}

$(window).load(function(){
    $("a.lightbox").colorbox({title: false});
	$( '.limited' ).each( function(){
		//var hgh2 = $(this).css( 'height' );
		var hgh = $(this).height();
		//alert(hgh+' - '+hgh2);
		//hgh = parseInt( hgh.replace( 'px', '' ) );
		if( (hgh - 77)%80 > 0 ){
		  var nhgh = (( Math.floor( ( hgh - 77) / 80 ) ) + 1)*80 + 77;
		}
		else {
			//nhgh = 77;
		}
		$(this).css( 'height', nhgh +'px' );
	});
});
