$(document).ready(function(){

    if ($.browser.msie){
	  ieFix();
    }
    

    if ($('.index').length > 0){
	  var pictures = $('#gallery ul');
	  pictures.find('li img').each(function(){
		$(this).load(function(e) {
		    $(this).css('margin-top', (60 - parseInt($(this).height() / 2) -2));
		});
	  });
    }
    

    // rolunk modal
    $('#menu a.aboutus').click(function(e){
	  e.preventDefault();
	  $.get('/aboutus', function(data){
		$('body').append(data);
		$('.modal').fadeIn(300);
		activateModal();

		
		$('.modal .close').click(function(e){
		    e.preventDefault();
		    $('.modal').fadeOut(200, function(){
			  $('.modal, .modal .close').unbind().remove();   
			  GUnload();
			  deactModal();
		    });
		});
	  });
    });
    


	// elerhetosegek modal
	$('#menu a.contact').click(function(e){
	  e.preventDefault();
	  $.get('/contacts', function(data){
		$('body').append(data);
		$('.modal').fadeIn(300);
		activateModal();

		initGMAP(); 
		showAddress();  
		
		$('.modal .close').click(function(e){
		    e.preventDefault();
		    $('.modal').fadeOut(200, function(){
			  $('.modal, .modal .close').unbind().remove();   
			  GUnload();
			  deactModal();
		    });
		});
	  });
	});
	
	// hirlevel feliratkozas
	$('.subscribe').click(function(e){
		e.preventDefault();
		$(this).next('form').slideDown(300).find('input').val('');
		$(this).next('form').find('button, input, label').show();
		$(this).next('form').find('p').remove();
	});
	
	$('#header form input').keypress(function(e){
		 if (e.which == 13){
			 e.preventDefault();
			 $('#subscribe-submit').click();
		}
		 
	});
	
	$('#subscribe-submit').click(function(e){
		e.preventDefault();
		var th = $(this).parents('form');
		
		$.post(th.attr('action'), {email: th.find('input').val()}, function(data){
			th.find('p').remove();
                  
                  if (data.error == true){
				th.prepend('<p class="error">'+data.errormsg+'</p>');	
			} else {
				th.find('button, input, label').hide();
				th.prepend('<p>'+data.errormsg+'</p>');
                        setTimeout(function(){
                              $('.subscribe').next('form').slideUp(300);
                        },5000);
                  }
                  
		}, 'json');
	});
	
	$('#subscribe-cancel').click(function(e){
		var th = $(this).parents('form');
		th.slideUp(300);
	});


	// leiratkozas
	$('#unsubscribe-submit').click(function(e){
		e.preventDefault();
		var th = $(this).parents('form');
		
		$.post(th.attr('action'), {email: th.find('input').val()}, function(data){
			th.find('p').remove();
                  
                  if (data.error == true){
				th.append('<p class="error">'+data.errormsg+'</p>');	
			} else {
				th.append('<p>'+data.errormsg+'</p>');
				setTimeout(function(){
					window.location.href='/';
				},10000);
                  }
		}, 'json');
		
	});


    // fooldali galeria
    if($('.pictures').length > 0) {
	  
	  var pictures = $('.pictures ul');
	  var picLeft = 17;
	  pictures.width((pictures.find('li').outerWidth() + parseInt(pictures.find('li').css('margin-right'))) * pictures.find('li').length);
	  
	   $('.pictures').find('a.prevPage').click(function(e){
		e.preventDefault();
		if(picLeft<0){
		    picLeft+=632;
		    pictures.animate({'margin-left':'+=632px'}, 300);
		}
	  });
	  $('.pictures').find('a.nextPage').click(function(e){
		e.preventDefault();
		
		if(picLeft > -parseInt(pictures.width()) + 632 ){
		    picLeft-=632;
		    pictures.animate({'margin-left':'-=632px'}, 300);
		}
	  });
	  
	  pictures.find('li').click(function(e){
		e.preventDefault();
		e.stopPropagation();
		showPic($(this).find('a.zoom').attr('rel'));
	  });
    }
    
    // menu lampa
    if ($('#menu li.sel a').length>0){
	  setLampPos();
    }
    
    
    // kereses
    $('input[name=q]').focus(function(e){
	  if ($(this).val()=='Keresőszó...'){
		$(this).val('');
	  }
    });
    $('input[name=q]').blur(function(e){
	  if ($(this).val()==''){
		$(this).val('Keresőszó...');
	  }
    });
    
    $('.qterms').click(function(e){
	  e.preventDefault();
	  if ($(this).next('fieldset').css('display')=='none'){
		$(this).next('fieldset').show();
	  } else {
		$(this).next('fieldset').hide();
	  }
	  
    });
    
    
    // gugli terkep 
    $('button.map').click(function(e){
	  e.preventDefault();
	  showMap($(this).siblings('.address').html());
    });
    
    
    $('.setorder a:not(.button)').click(function(e){
	  e.preventDefault();
	  $.get('/auction/setsortby/'+$(this).attr('class').replace('up', '').replace('down', ''), function(data){
		if(data.success===true) { window.location.reload(); }
	  }, 'json');
    });
    
    /*$('.setorder .desc, .setorder .asc').click(function(e){
	  e.preventDefault();
	  $.get('/auction/setsortdesc/'+$(this).attr('class').replace('order','').replace(' ',''), function(data){
		if(data.success===true) { window.location.reload(); }
	  }, 'json');
    });*/
    
    
    // aukcio/item
    $('.auction .item').click(function(e){
	  e.preventDefault();
	  showPic($(this).find('img').attr('rel'));
    });
    
    
    // lapozo
    $('.pager select[name=event]').change(function(){
	  window.location.href='/auction/pictures/42/'+$(this).find('option:selected').val();
	  // TODO 42-t atirni
    });
    
    $('.ipp').change(function(){
	  $.get('/auction/setpicperpage/'+$(this).find('option:selected').val(), function(data){
		if(data.success===true) { 
		    window.location.href = data.url; 
		}
	  }, 'json');
    });
    
    
    // osszes arveres
    $('a.allevent').bind('click', function(e){
	  e.preventDefault();
	  if ($('ul.allevent').css('display') == 'none'){
		$('ul.allevent').slideDown(300);
	  } else {
		$('ul.allevent').slideUp(300);
	  }
    });
    
    
    $('.openable').bind('click', function(e){
	  e.preventDefault();
	  var ul = $(this).next('ul');
	  if (ul.css('display') == 'none'){
		ul.slideDown(300);
	  } else {
		ul.slideUp(300);
	  }
    });
    
    if ($.browser.msie){
	  $('.openable').next('ul').hide();
    }
    
    
    
    $('a.study').live('click',function(e){
	  e.preventDefault();
	  
	  if ($('div.study').css('display') == 'none'){
		$(this).parent().siblings(':not(.pic, h2, p.props)').slideUp(300);
		$(this).hide();
		var x = $(this).parent().siblings('div.pic').position();
		var p = x.top;
		$('div.study').css('height', parseInt($(window).height()) - p - 100).slideDown(300);
		$('a.props, p.props').show();
	  } 
    });
    
    $('a.props').live('click',function(e){
	  e.preventDefault();
	  $(this).parent().siblings().slideDown(300);
	  $('div.study').slideUp(300);
	  $(this).hide();
	  $('a.study').show();
    });
    
    $('#header .mod').click(function(e){
	  e.preventDefault();
	  showModal($(this).attr('href'));
    });
    
    
    $('#pic-search').click(function(e){
	  e.preventDefault();
	  
	  var form = $(this).parents('form');
	  var th = $(this);
	  
	  
	  if (form.find('input[name=q]').val()!='' && form.find('input[name=q]').val()!='Keresőszó...'){
		//console.log(form.find('input[type=checkbox]:checked').length);
		if (form.find('input[type=checkbox]:checked').length > 0){
		    form.submit();  
		} else {
		    // TODO hibauzenet
		    alert('Hibás form adatok!')
		}
		
	  } else {
		// TODO hibauzenet
		alert('Hibás form adatok!')
	  }
	  
    });
    
    // galeria/tabolas
    $('.gallery ul.tab a').click(function(e){
	  e.preventDefault();
	  var index = $('.gallery ul.tab li').index($(this).parent()[0]);
	  if (index>0){
		$('.kata').hide();
	  } else {
		$('.kata').show();
	  }
	  
	  $('.gallery ul.tab li').removeClass('sel').eq(index).addClass('sel');
	  $('div.tab').hide().eq(index).show();
    });
    
    // profetak, alkotok
    $('a.cv').click(function(e){
	  e.preventDefault();
//	  console.log('1');
	  $.get($(this).attr('href'), function(data){
		$('div.right').html(data).addClass('cv');
		setGal();
	  });
	  var stimer = setInterval(function(){
		$(window).scrollTop($(window).scrollTop() - $(window).scrollTop()/5);
		if ($(window).scrollTop() == 0){
		    clearInterval(stimer);
		}
		
	  },1);
		    
	  $('a.cv').css('font-weight', 'normal');
	  $(this).css('font-weight', 'bold');
	  
    });
    
    
    // kiallitasok galeria
    function setGal(){
	  
    
	  if($('#gallery').length > 0) {
		
		var pictures = $('#gallery ul');
		var picLeft = 17;
		pictures.width((pictures.find('li').outerWidth() + parseInt(pictures.find('li').css('margin-right'))) * pictures.find('li').length);
		
		 $('#gallery').find('a.prevPage').click(function(e){
		    e.preventDefault();
		    if(picLeft<0){
			  picLeft+=632;
			  pictures.animate({'margin-left':'+=632px'}, 300);
		    }
		});
		$('#gallery').find('a.nextPage').click(function(e){
		    e.preventDefault();
		    
		    if(picLeft > -parseInt(pictures.width()) + 632 ){
			  picLeft-=632;
			  pictures.animate({'margin-left':'-=632px'}, 300);
		    }
		});
		pictures.find('li').click(function(e){
		    showPic($(this).find('a.zoom').attr('rel'));
		});
		
//		  pictures.find('li img').each(function(){
//			h = parseInt($(this).height());
//			$(this).css('margin-top', (60-h/2)-2)
//		  });
		pictures.find('li img').each(function(){
		    $(this).load(function(e) {
			  $(this).css('margin-top', (60 - parseInt($(this).height() / 2) -2));
		    });
		});
	  }
    }
    
    // galeria/koszonto
    $('.gallery .left a img:not(.logo)').click(function(e){
	  e.preventDefault();
	  showPic($(this).parent().attr('href'));
    });
    
    
});


function showMap(address){
    $.get('/statcont/map', function(data){
	  $('body').append(data);
	  $('.modal').fadeIn(300);
	  initGMAP();
	  showAddress(address);
	  activateModal();
	  
	  $('.modal .close').click(function(e){
		e.preventDefault();
		$('.modal').fadeOut(200, function(){
		    $('.modal, .modal .close').unbind().remove();   
		    GUnload();
		    deactModal();
		});
	  });
    });
    
}
function showModal(url){
    $.get(url, function(data){
	  $('body').append(data);
	  $('.modal').fadeIn(300);
	  activateModal();
	  
	  $('.modal .close').click(function(e){
		e.preventDefault();
		$('.modal').fadeOut(200, function(){
		    $('.modal, .modal .close').unbind().remove();   
		    deactModal();
		});
	  });
    });
    
}

function showPic(picID){
    $.get(picID, function(data){

	  if ($('.modal.pic').length>0){
		$('.modal.pic').html($(data).html());
	  } else {
		$('body').append(data);
		$('.modal').fadeIn(300);
		activateModal();
	  }
	  
	  $('.modal .close').click(function(e){
		e.preventDefault();
		$('.modal').fadeOut(200, function(){
		    $('.modal, .modal .close').unbind().remove();   
		    GUnload();
		    deactModal();
		});
	  });
	  
	  $('.modal .next, .modal .prev, .modal .gal a').bind('click',function(e){
		e.preventDefault();
		$('.modal').html('');
		showPic($(this).attr('href'));
	  });
    });
}

function activateModal(){
    $('body').css('overflow','hidden');  
    $('html').css('overflow','hidden');  
}

function deactModal(){
    $('body').css('overflow','visible');  
    $('html').css('overflow','auto');  
}

function setLampPos(){
    lampPos = $('#menu li.sel a').position();
    lampW = $('#menu li.sel a').width();
    if ($('#lamp').length==0){
	  $('body').append('<img id="lamp" alt="" src="/images/bg/lamp.png" />'); 
    }
    $('#lamp').css({left: lampPos.left-70+parseInt(lampW)/2, top: lampPos.top-92});
    $('#menu').css('position', 'relative'); 
}


function ieFix(){
    
    var u = $('.setorder ul');
    
    $('li', u).eq(0).addClass('first');
    $('li', u).eq(1).addClass('sec');
    $('li', u).eq(2).addClass('third');
    $('li', u).eq(3).addClass('fourth');
}

