document.observe("dom:loaded", function() {

	
	
	
	
	
	if ($$('.author_thumb_action')) {
	
	
		$$('.author_thumb_action').each(function(el){
		
			el.observe("mouseover", function(e){
			
				thumb = el.select('[class=thumb]')[0];
				$('photo_big').src = "/images/" + thumb.innerHTML;
				
			});
			
		});
		
	}

	if($('registration_page')){
		
		$$('.focus').each(function(el){
			el.observe("focus", function(){
				clearChecked();
				el.ancestors()[1].addClassName("checked");
				
			});
		});
	}
	
	if($('submit_type'))
	{
		$('submit_type').observe("click", function(){
			clearSearchBoxes()
		});	
	}
	
	if($('submit'))
	{
		$('submit').observe("click", function(){
			clearSearchBoxes()
		});
	}
	
	if($$('.filter_main'))
	{
		$$('.filter_main').each(function(el){
			
			 done = null;
			el.observe("click", function(){
				
				$$(".t").each(function(elt){
					
					
					if(elt.up() == el.up() && elt.up().hasClassName("open")){
						
						
						Effect.SlideUp(elt, { duration: 3.0 });
							
						if($('zoom_slider')){
							$('zoom_slider').remove();
						}
						
						
							
						done = true;
					}else{
						elt.setStyle({'display':'none'});
						elt.getElementsBySelector('ul.criteria_item')[0].setStyle({
						'bottom': '0px'
						});
						elt.siblings()[0].setStyle({
							
							height: 'auto'
						});
						
						if($('zoom_slider')){
							$('zoom_slider').remove();
						}
						
						if(done==null)
							done=false;
					}
				
					elt.up().removeClassName("open");
				});
				
				ul = el.siblings();

				if (!done) {
					ul[0].getElementsBySelector('ul.criteria_item')[0].setStyle({
						'display': 'block'
					});

				if (ul[0].getHeight() >= 300) {
					if (ul.length == 1) 
						ul[0].setStyle({
							height: '300px'
						});
					
				
					Effect.SlideDown(ul[0], {
						duration: 3.0
					});
					
	//c
					
					s = '<div id="zoom_slider" class="slider"><div id="galka" class="handle">&nbsp;</div>	</div>';
					
					ul[0].setStyle({
						'overflow': 'hidden'
					});
					
					ul[0].innerHTML = s + ul[0].innerHTML;
					
					
					var s2 = new Control.Slider('galka', 'zoom_slider', {
						axis: 'vertical',
						alignY: -143
					});
				
//a

					s2.options.onSlide = function(value){
						values = ul[0].getElementsBySelector('ul.criteria_item')[0].getHeight();
						ul[0].getElementsBySelector('ul.criteria_item')[0].setStyle({
						
							'marginTop':  -1 *(value * (values - 300)) + "px"
						});
					};
					
					s2.options.onChange = function(value){
						values = ul[0].getElementsBySelector('ul.criteria_item')[0].getHeight();
						ul[0].getElementsBySelector('ul.criteria_item')[0].setStyle({
						
							'marginTop': -1 *(value * (values - 300)) + "px"
						});
					};
				}else
				{
					Effect.SlideDown(ul[0], {
						duration: 1.0
					});
					
				}
			
//b

					

					ul[0].up().toggleClassName("open");	
				}
					
				done = false;
				
				
			});
			
		});
		
	}
	


	if($$('.fck_body')){
		
		$$('.fck_body').each(function(el){
			
			id = el.readAttribute('id')
			oFCKeditor = new FCKeditor(id) ;
			oFCKeditor.BasePath = "/fckeditor/" ;
			oFCKeditor.Height = 500;
			oFCKeditor.ReplaceTextarea() ;
			
		});
		
	}	
	
	
	if($("body"))
	{
		var oFCKeditor = new FCKeditor('body') ;
		oFCKeditor.BasePath = "/fckeditor/" ;
		oFCKeditor.Height = 500;
		oFCKeditor.ReplaceTextarea() ;	
	}
	
	if($("body1"))
	{
		var oFCKeditor = new FCKeditor('body1') ;
		oFCKeditor.BasePath = "/fckeditor/" ;
		oFCKeditor.Height = 500;
		oFCKeditor.ReplaceTextarea() ;	
	}
	
	
	if($("static[body]"))
	{
		var oFCKeditor = new FCKeditor('static[body]') ;
		oFCKeditor.BasePath = "/fckeditor/" ;
		oFCKeditor.Height = 500;
		oFCKeditor.ReplaceTextarea() ;	
	}
	
	
	
	
	if($$('.lang_select'))
	{
		
		$$('.lang_select').each(function(el){
			
			el.observe("click", function(){
				
				$$('.lang_select').each(function(elt){
					elt.setStyle({'fontWeight':'normal'});
				});	
				el.setStyle({'fontWeight':'bold'});
				var id = el.readAttribute('id')
				var code = id.split('_')[1]
				

				if($('edit_'+ code))
				{
					$$('.edit_select').each(function(elt){		
						elt.setStyle({'display':'none'});
						$('edit_'+ code).setStyle({'display': 'block'});

					});
					
					
				}
				
			});	

		});
		
		
		
		
		
	}
	
	
	if ($$('.tools')) {
	
		$$('.tools').each(function(el){
		
			el.observe("mouseover", function(e){
			
				var mouseX = Event.pointerX(e) + 20;
				var mouseY = Event.pointerY(e) + 20;
				var artist = el.readAttribute("title");
				var name = el.readAttribute("rel");
				
				$('tool_tip_box').innerHTML = '<p style="font-weight: bold;" >' + artist + '</p><p>' + name + '</p>';
				

				$('tool_tip_box').setStyle({
					'left': mouseX + "px",
					'top': mouseY + "px",
					'display': 'block'
				
				});
				
				
			});
			
			el.observe("mousemove", function(e){
				var mouseX = Event.pointerX(e) + 20;
				var mouseY = Event.pointerY(e) + 20;
				var id = el.readAttribute("id");
				$('tool_tip_box').setStyle({
					'left': mouseX + "px",
					'top': mouseY + "px"
				});
			});	
			
			el.observe("mouseout", function(e){
			
				var id = el.readAttribute("id");
				$('tool_tip_box').setStyle({
					'display': 'none'
				});
				
			});
			
		});
	}	
		
});

function clearText(field){

    if (field.defaultValue == field.value) field.value = '';
    else if (field.value == '') field.value = field.defaultValue;

}


var count = 1;

function addFileField(){
	var p = new Element("p")
	var input = new Element("input", {'name':'image_'+ count, 'id': 'image_' + count, 'type': 'file'})
//	input.insert($('product_images'));
    p.insert(input);
	input.observe("change", function(){
		addFileField();
	});
	
	$('product_images').insert(p);
	$('imageCount').value = count + 1
	count++;
}

function addImageToGallery(image_name, token, image_id, product_id)
{
	setTimeout( function(){
		var img = new Element("img", {
			'alt': "",
			'src': '/images/small_' + image_name
		});
		var a = new Element("a", {
			"onclick": "new Ajax.Updater('product_gallery', '/image/delete/" + window.frames[0].document.body.textContent + "?product=" + product_id + "', {asynchronous:true, evalScripts:true, parameters:'authenticity_token=' + encodeURIComponent('" + token + "')}); return false;"
		}).update("Usuń");
		$('product_gallery').insert(img);
		$('product_gallery').insert(a);
	}
	,1000);
}
var changedMail = false;
function changeTestMail(){
	
	if (changedMail) {
		$('text_mail').setStyle({
			'display': 'none'
		});
		changedMail = false;
	}
	else {
		$('text_mail').setStyle({
			'display': 'block'
		});
		changeMail = true;
	}
}

function clearSearchBoxes()
{
	if($$('.searchbox')){
		
		$$('.searchbox').each(function(el){
			el.setStyle({'display': 'none'})
			
		});
		
	}	
}

function getFilterValues()
{
	var result = ""
	var first = true
	// TYPES
	var result = ""
	
	
	
	
	$('filter_typ').select('[type=checkbox]').each(function(el){
		
		if(el.checked==true)
		{
			if(first){
				result = result + "types[]=" + el.value
				first = false;
			}
			else{
				result = result + "&types[]=" + el.value;
			}
		}
	});
	
	
	// ARTISTS
	
	$('filter_artist').select('[type=checkbox]').each(function(el){
		
		if(el.checked==true)
		{
			if(first){
				result = result + "artists[]=" + el.value
				first = false;
			}
			else{
				result = result + "&artists[]=" + el.value;
				
			}
			
		}
		
		
		
	});	
	
	//COLOR
	
	/*$('filter_colour').select('[type=checkbox]').each(function(el){
		
		if(el.checked==true)
		{
			if(first){
				result = result + "colors[]=" + el.value
				first = false;
			}
			else{
				result = result + "&colors[]=" + el.value;
				
			}
			
		}
		
		
		
	});	*/
	
	//TECHNIQUES
	
	$('filter_technique').select('[type=checkbox]').each(function(el){
		
		if(el.checked==true)
		{
			if(first){
				result = result + "techniques[]=" + el.value
				first = false;
			}
			else{
				result = result + "&techniques[]=" + el.value;
				
			}
			
		}
		
		
		
	});	
	
	//PRICE
	
	var price_from = $('price_from').value;
	var price_to = $('price_to').value;
	
	result = result + "&price_from=" + price_from;
	result = result + "&price_to=" + price_to;
	
	return result;
}

function clearChecked()
{
	if($('registration_page'))
	{
		s = $('registration_page').select('tr.checked')
		if(s.size()==1)
		{
			s[0].removeClassName("checked");
		}
	}
}

function commit_filter(box, input)
{
	/*
	clearSearchBoxes();
	
	var result = ""
	var first = true
	
	$(box).select("[type='checkbox']").each(function(el){
		if(el.checked)
		{
			if(first)
			{
				
				result = result + el.readAttribute('name');
				first=false;
			}
			else{
				result = result + "," + el.readAttribute('name');
			}
		}
		
	});
	

	$(input).value = result;*/
}

function regulaminTest(){
	
	if($('regulamin_test').checked){
		$('register_form').submit();
	}else{
		$('regulamin_info').setStyle(
		{color: 'red'}
		);

	}
	
	
}

function overl(){
	if ($('overlay2')) {
		$('overlay2').setStyle({
			display: "block",
		});
		width = ($('wrapperBox').getDimensions().width / 2 * -1)
		height = ($('wrapperBox').getDimensions().height / 2 * -1)
		$('wrapperBox').setStyle({
			display: "block",
			height: "10px",
			width: "30px"
		});
		
		$('overlay2').fade({
			duration: 2.0,
			from: 0,
			to: 0.5
		});
		$('wrapperBox').fade({
			duration: 1.0,
			from: 0,
			to: 1
		});
		new Effect.Morph('wrapperBox', {
			style: 'height: 280px; width: 450px; margin-left: ' + width + 'px; margin-top: ' + height + 'px;',
			duration: 0.8
		});
		$('close_ol').observe("click", function(){
			$('overlay2').fade({
				duration: 2.0,
				from: 0.5,
				to: 0
			});
			$('wrapperBox').fade({
				duration: 1.0,
				from: 1,
				to: 0
			});
			setTimeout(function(){
				$('overlay2').setStyle({
					display: "none"
				});
				$('wrapperBox').setStyle({
					display: "none"
				});
			}, 2000);
		});
		
		$('overlay2').observe("click", function(){
			$('overlay2').fade({
				duration: 2.0,
				from: 0.5,
				to: 0
			});
			$('wrapperBox').fade({
				duration: 1.0,
				from: 1,
				to: 0
			});
			setTimeout(function(){
				$('overlay2').setStyle({
					display: "none"
				});
				$('wrapperBox').setStyle({
					display: "none"
				});
			}, 2000);
		});
		
	}
}