var myMesArr;
var ImgSmallWidth = new Object();
var mygallerywhich = 0;
var PaddingTopMotion = 80;

function Set_Display_Arr(link){
	// alert(a.checked);
		window.location.href=link;
}

function Hide_Cover_Effect(AllDiv, link){
  // $(AllDiv).fade({ duration: 0.5 });
  // setTimeout("Set_Display_Arr('" + link + "')", 510);
	Set_Display_Arr(link)
}

// KEP MEGJELENITES
function Display_Image(serverurl, func, param, i){
	if(!i)i = 0;
	which = i-1;
	
	if(!param)param = '';
	$('ws_content_prod').innerHTML = '';
	ajaxRequest = new Ajax.Request(
		serverurl + '/functions/ajaxfunc.php',
		{
			method: 'get',
			parameters: 'funcid=' + func + '&amp;img=' + bigphotos[which] + param,
			onComplete: function(request) 
			{
	alert(request.responseText);
				// alert(request.responseText);
				MyZoom_Isset = false;
				if(MySlider){
					MySlider.dispose();
				}
				var myMesArr = request.responseText.split("##");
				mygallerywhich = which;
				Display_Cover_Img(myMesArr[0]);
				mygallerywhich = myMesArr[2];
				ImgSmallWidth = myMesArr[1].split(";");
			}
		}
	)
}

function Display_Cover_Img(request, kat){
	var myDiv = 'ws_content_prod';
	var myLayer = $(myDiv);
	
	var body_dimensions = $('body').getDimensions();  
	Width = body_dimensions.width + 'px';  
	Height = body_dimensions.height + 'px';  
	$('ws_cover').setStyle({width: Width,height: Height,display: 'block'});

	// myLayer.innerHTML = request.responseText;
	myLayer.innerHTML = request;
	// position_layer(myDiv, 500);
	position_layer_IsSize(myDiv, kat);
	// position_layer(myDiv);
	// myLayer.style.display = '';	
	new Effect.Appear(myDiv,
			{
				duration:0.2,
				from:0.0-0.0,
				to:1.0-0.0
			}
		);

	// new Move_Galery_Thumb('motiongallery', 600, 800);
	// alert(request.responseText);
}
// # END - KEP MEGJELENITES

// # KEPET BEFOGLALO DIV MERETEZESE
var myDivMargin = 10;
function position_layer_IsSize(div, kat){
	if(kat)plusCount = CumImgCount[kat];
	else plusCount = 0;
	
  var dim = document.viewport.getDimensions();
  var myLayer = $(div);
  AktTop = document.viewport.getScrollOffsets().top;
  mywidth = parseInt(bigwidth[plusCount+mygallerywhich]);
  myheight = parseInt(bigheight[plusCount+mygallerywhich])+40+PaddingTopMotion;
  // myheight = parseInt(myLayer.getHeight());
	myLayer.style.width = (parseInt(mywidth)+myDivMargin*2) + 'px';
	$('move_thumb').setStyle({width: mywidth + 'px'});
	// myheight += (myheight<50 ? plusheight : 0);
  
  newLeft = (dim.width-mywidth)/2;
	if(myheight > dim.height)newtop = AktTop + 10;
  else newtop = AktTop + (dim.height-myheight)/2;
	if(newLeft < 10)newLeft = 10;
	if(newtop < 10)newtop = 10;

	// alert(newtop + ' -> ' + AktTop + ' - ' + dim.height + ' - ' + myheight + ' - ' + (dim.height-myheight)/2 + ' div.top: ' + myLayer.getHeight());
  // alert(myLayer.style.left + ' - ' + myLayer.style.top);
  myLayer.style.left = newLeft + "px";
  myLayer.style.top = newtop + "px";
}
// # END - KEPET BEFOGLALO DIV MERETEZESE


// var OldMit1;
var showAktiv = 0;
var ShowHideOk = true;
function Show_Hide_Ok(){
	ShowHideOk = true;
}
var lastDate=0;

// # GALLERY MOZGO
var MoveGallery_scrollspeed = 2;	// sebesseg
var MoveGallery_CDTime = 5;				// idozito
var MoveGallery_Sensibility = 150;
var MoveGallery_Border = 2;

// Ne modositsd
var MoveGallery_left = 0;
var MoveGallery_aktiv = 0;				// Aktiv idozito
var MoveGallery_aktWay = 1;				// Irany
var MoveGallery_aktwidth = 0;			// 
var MoveGallery_AktivMotion;

var MoveGallery_moveGallery;	// $(GalleryID)
var MoveGallery_DivWidth;			// div szelessege
var MoveGallery_WidthAll;			// a kiskepek szumma szelessege
// END - Ne modositsd

var MoveGallery_ThumbBorderColor_On = '#77160d';			// 
var MoveGallery_ThumbBorderColor_Off = '#ebe2ce';			// 
// var MoveGallery_moveGallery = $('motiongallery');

function show_hide(mit1, mit2, WidthAll, event, othis) {
		
	// if(Position.within($('motioncontainer'), Event.pointerX(event), Event.pointerY(event)) && mit2 != "move_thumb"){
	
		// $(mit1).style.visibility='hidden';
		// $(mit2).style.visibility='visible';
	// }
	// else if(!Position.within($('motioncontainer'), Event.pointerX(event), Event.pointerY(event)) && $('motioncontainer').style.visibility=='visible'){
		// $('motioncontainer').style.visibility='hidden';
		// $('move_thumb').style.visibility='visible';	
	// }
	// else{
		// $('move_thumb').style.visibility='hidden';
		// $('motioncontainer').style.visibility='visible';	
	// }
		
	MoveGallery_DivWidth = $(othis).getWidth();
	MoveGallery_WidthAll = WidthAll;		
	$('motioncontainer').style.width = MoveGallery_DivWidth + 'px';
	MoveGallery_moveGallery = $('motiongallery');
	// alert(MoveGallery_DivWidth);
	
	if(mit2 == "move_thumb"){
		MoveGallery_AktivMotion=false;
		Init_Move_Thumb(event);
		// alert("0 - " + MyZoom_HandleRate);
	}
	// }
}
function show_hide_controll(mit1, mit2){
	$(mit1).style.visibility='hidden';
	$(mit2).style.visibility='visible';
}
function Init_Move_Thumb(event){
// alert('motioncontainer' + ' - ' + $('motioncontainer').style.visibility);
	mouseX = Event.pointerX(event);
	if(parseInt($("ws_content_prod").style.left)+MoveGallery_Sensibility+myDivMargin > mouseX)
		MoveGallery_aktWay = 0;
	else if(MoveGallery_DivWidth+parseInt($("ws_content_prod").style.left)-MoveGallery_Sensibility-myDivMargin < mouseX)
		MoveGallery_aktWay = 1;
	else MoveGallery_aktWay = 2;
	
	MyZoom();
	Move_Thumb();
	// alert('Init_Move_Thumb');
}
function Move_Thumb(){

	if(MoveGallery_DivWidth < MoveGallery_WidthAll && MoveGallery_aktWay < 2){
		MoveGallery_aktwidth = parseInt(MoveGallery_WidthAll)+MoveGallery_left;
		
	
		// # Mozgás jobb oldali jelenletnel -> balra
		if(MoveGallery_aktWay == 1){
			if(MoveGallery_aktwidth > MoveGallery_DivWidth){
				clearTimeout(MoveGallery_aktiv);
				MoveGallery_moveGallery.style.left = parseInt(MoveGallery_moveGallery.style.left)-MoveGallery_scrollspeed + 'px';
				MoveGallery_left = parseInt(MoveGallery_moveGallery.style.left);
				MoveGallery_aktiv = setTimeout("Move_Thumb()", MoveGallery_CDTime);
				if(MySlider){
					if(MyZoom_HandleRate > 1){
						MyZoom_HandleWidth = $('zoom_handle').getWidth();
						MyZoom_HandleRate = (MoveGallery_DivWidth-MyZoom_HandleWidth)/(MoveGallery_WidthAll-MoveGallery_DivWidth);
					}
					MyZoom_HandleLeft = MyZoom_HandleRate*(MoveGallery_WidthAll-MoveGallery_aktwidth);
			// alert(MoveGallery_aktwidth + " - all: " + MoveGallery_WidthAll);
					// MyZoom_HandleLeft += MyZoom_HandleRate*MoveGallery_scrollspeed;
					$('zoom_handle').style.left = MyZoom_HandleLeft + 'px';
					// alert(MyZoom_Rate + ' - ' + MoveGallery_scrollspeed + ' - ' + MyZoom_HandleLeft);
				}

			}	
			else if(MoveGallery_aktiv)clearTimeout(MoveGallery_aktiv);
		}
		// # Mozgás bal oldali jelenletnel -> jobbra
		else if(MoveGallery_aktWay == 0){
			if(MoveGallery_aktwidth < MoveGallery_WidthAll){
				clearTimeout(MoveGallery_aktiv);
				MoveGallery_moveGallery.style.left = parseInt(MoveGallery_moveGallery.style.left)+MoveGallery_scrollspeed + 'px';
				MoveGallery_left = parseInt(MoveGallery_moveGallery.style.left);
				MoveGallery_aktiv = setTimeout("Move_Thumb()", MoveGallery_CDTime);
				if(MySlider){
					if(MyZoom_HandleRate > 1){
						MyZoom_HandleWidth = $('zoom_handle').getWidth();
						MyZoom_HandleRate = (MoveGallery_DivWidth-MyZoom_HandleWidth)/(MoveGallery_WidthAll-MoveGallery_DivWidth);
					}
					MyZoom_HandleLeft = MyZoom_HandleRate*(MoveGallery_WidthAll-MoveGallery_aktwidth);
					// MyZoom_HandleLeft -= MyZoom_HandleRate*MoveGallery_scrollspeed;
					$('zoom_handle').style.left = MyZoom_HandleLeft + 'px';
					// alert(MyZoom_Rate + ' - ' + MoveGallery_scrollspeed + ' - ' + MyZoom_HandleLeft);
				}
			}	
			else if(MoveGallery_aktiv)clearTimeout(MoveGallery_aktiv);
		}
	}
	else{clearTimeout(MoveGallery_aktiv);}
}
// # END - GALLERY MOZGO

// # GALERIA KEPCSERELO
var Image_Replace_Cnt;
function Image_Replace2(kat, ImgCount){
	// myImg.innerHTML = ; 
	Image_Replace_Cnt = '<img src="' + bigphotos[CumImgCount[kat]+mygallerywhich] + '" border="0">';
	c = 0;
	while(thumb = $('thumb' + c)){
		thumb.style.borderColor = MoveGallery_ThumbBorderColor_Off;
		thumb.className = 'img_off';
		c++;
	}
	$('thumb' + mygallerywhich).style.borderColor = MoveGallery_ThumbBorderColor_On;
	$('thumb' + mygallerywhich).className = 'img_on';
	new Effect.Opacity('ws_content_prod', { 
		from: 1, 
		to: 0 ,
		duration: 0.0,
		afterFinish:function(request){
			if(MoveGallery_aktiv)clearTimeout(MoveGallery_aktiv);
			MyZoom_Isset = false;
			if(MySlider){
				MySlider.dispose();
			}
			$('GalleryBig').innerHTML = Image_Replace_Cnt;
			$('GalleryBig').style.width = bigwidth[CumImgCount[kat]+mygallerywhich] + 'px';
			$('GalleryBig').style.height = bigheight[CumImgCount[kat]+mygallerywhich] + 'px';
			
			$('GalleryImgName').innerHTML = kepnev[CumImgCount[kat]+mygallerywhich];
			
			$('move_thumb').style.width = bigwidth[CumImgCount[kat]+mygallerywhich] + 'px';
			// alert($(myDiv).style.width + ' - ' + $('GalleryBig').getWidth());
			$('Gallinside').style.width = $('GalleryBig').getWidth() + 'px';
			$('ws_content_prod').style.width = $('GalleryBig').getWidth() + 20 + 'px';
			MoveGallery_DivWidth = parseInt($('Gallinside').style.width);
			
			$('motioncontainer').style.width = MoveGallery_DivWidth + 'px';

			// Ha kisebb a jelenlegi kep akkor a mozgo resz kicsuszna pl. jobb szelso helyzetben
			if(MoveGallery_aktwidth < MoveGallery_DivWidth && MoveGallery_WidthAll>MoveGallery_DivWidth){
				MoveGallery_aktwidth = MoveGallery_DivWidth;
				pos = Position.cumulativeOffset('GalleryBig');
				MoveGallery_moveGallery.style.left = pos[0]-(MoveGallery_WidthAll-MoveGallery_aktwidth) + 'px';
			}
			
			// Vezerlok megjelenitesenek vizsgalata
			if(mygallerywhich>0) $('gallery_left').style.display = '';
			else $('gallery_left').style.display = 'none';
			if(mygallerywhich<ImgCount-1)$('gallery_right').style.display = '';
			else $('gallery_right').style.display = 'none';
			// $('gallery_left').style.visibility = 'hidden';
			// $('gallery_right').style.visibility = 'hidden';
			// $('gallery_left1').style.visibility = 'visible';
			// $('gallery_right1').style.visibility = 'visible';
			newMiddleDistance = (parseInt(bigheight[CumImgCount[kat]+mygallerywhich])-100)/2+PaddingTopMotion;
			$('gallery_left').setStyle({visibility: 'hidden', top: newMiddleDistance + 'px'});
			$('gallery_right').setStyle({visibility: 'hidden', top: newMiddleDistance + 'px'});
			$('gallery_left1').setStyle({visibility: 'visible', top: newMiddleDistance + 'px'});
			$('gallery_right1').setStyle({visibility: 'visible', top: newMiddleDistance + 'px'});
			
			// alert($(myDiv).style.width);
			// Reteg ujrameretezes
			position_layer_IsSize('ws_content_prod', kat);
			MyZoom();
			
			
			// Scroll kezdo pozicio allitasa
			// alert("2");
			// if(MySlider){$('zoom_handle').style.left = MyZoom_HandleLeft + 'px';}
			// Kep elotunese
			new Effect.Opacity('ws_content_prod', { from: 0, to: 1, duration: 0.2 });
		}
	});
}
// # END - GALERIA KEPCSERELO

// # Kep lepteto
function Image_Step(ImgCount, way){
	var gallery_right;
	var gallery_left;
	// Vissza
	if(way){
		if(mygallerywhich>0)	mygallerywhich-=1;
	}
	// ELore
	else{
		if(mygallerywhich<ImgCount-1) mygallerywhich=parseInt(mygallerywhich)+1;
	}
	
}

// # GALERIA SCROLLBAR
var MyZoom_Isset = false;
var MyZoom_Left;
var MyZoom_Rate;
var MyZoom_HandleLeft  = 0;
var MyZoom_HandleWidth  = 0;
var MyZoom_HandleRate  = 0;
var MyZoom_HandleRateSlider = 1;
var MySlider;

function ImgSmall_Center(){
	// if(!MyZoom_Isset){
		// # KISKEP IGAZITASA KOZEPRE
		// # CSAK AKKOR, HA VAN GORGETO
		// Az aktualis kepet kozepre helyezem a motion-ban
		var cumWidth = 0;
		var aktWidth = 0;
		// alert(which);
		for(var d=0;d<mygallerywhich;d++){
			cumWidth += parseInt(ImgSmallWidth[d])+2*MoveGallery_Border;
			// alert(myMesArr + ' - ' + ImgSmallWidth[d] + ' - ' + MoveGallery_left);
		}
		aktWidth2 = parseInt((parseInt(ImgSmallWidth[mygallerywhich])+2*MoveGallery_Border)/2);
		eleje = aktWidth2+cumWidth;
		maradek = MoveGallery_WidthAll-aktWidth2-cumWidth;
		if(MoveGallery_WidthAll > MoveGallery_DivWidth){
			// alert(eleje + ' - ' + aktWidth2 + ' - ' + maradek);
			MoveGallery_DivWidth2 = parseInt(MoveGallery_DivWidth/2);
			// Kozepre lehet igazitani, mert van tavolsag mindket oldalon
			if(maradek > MoveGallery_DivWidth2 && eleje > MoveGallery_DivWidth2){
				MoveGallery_aktwidth = MoveGallery_WidthAll-cumWidth+MoveGallery_DivWidth2-aktWidth2;
			}
			// Az eleje rovidebb ezert 0-ba pozicionalom
			else if(eleje < MoveGallery_DivWidth2){
				MoveGallery_aktwidth = MoveGallery_WidthAll;
			}
			// A kep a kozeptol jobbra helyezkedik el, ezert a poziciot teljesen a jobb szelre teszem (amennyire lehet)
			else MoveGallery_aktwidth = MoveGallery_DivWidth;		
			
			pos = Position.cumulativeOffset('GalleryBig');
			MoveGallery_moveGallery.style.left = pos[0]-(MoveGallery_WidthAll-MoveGallery_aktwidth) + 'px';
			MoveGallery_left = parseInt(MoveGallery_moveGallery.style.left);
		}
		// MyZoom();
	// }
}
function MyZoom() {
	if(!MyZoom_Isset){
		if(MoveGallery_WidthAll > MoveGallery_DivWidth){
			ImgSmall_Center();
			$('zoom_slider').style.display = '';
			
			// MyZoom_HandleLeft = (MoveGallery_WidthAll-MoveGallery_aktwidth);
			$('zoom_slider').style.width = MoveGallery_DivWidth + 'px';
			$('zoom_handle').style.width = parseInt(MoveGallery_DivWidth*MoveGallery_DivWidth/MoveGallery_WidthAll) + 'px';
			MyZoom_HandleWidth = $('zoom_handle').getWidth();
			MyZoom_HandleRate = (MoveGallery_DivWidth-MyZoom_HandleWidth)/(MoveGallery_WidthAll-MoveGallery_DivWidth);
			MyZoom_HandleRateSlider = (MoveGallery_WidthAll-MoveGallery_DivWidth)/MoveGallery_DivWidth;
			// alert("1 - " + $('zoom_slider').getWidth() + " - rate: " + MyZoom_HandleRate);
			MyZoom_HandleLeft = MyZoom_HandleRate*(MoveGallery_WidthAll-MoveGallery_aktwidth);
			$('zoom_handle').style.left = MyZoom_HandleLeft + 'px';

			pos = Position.cumulativeOffset('motioncontainer');
			MyZoom_Left = pos[0];
			// alert(MyZoom_HandleRateSlider);
			// MyZoom_Rate = ((MoveGallery_WidthAll-MoveGallery_DivWidth)/MoveGallery_DivWidth);
			
			var zoom_slider = $('zoom_slider'),
					// rgb_slider = $('rgb_slider'),
					box = $('motiongallery');

			MySlider = new Control.Slider(zoom_slider.down('.handle'), zoom_slider, {
				range: $R(0, MoveGallery_DivWidth),
				// mycounter: 0,
				sliderValue: MyZoom_HandleLeft+MyZoom_HandleLeft*(MyZoom_HandleWidth/(MoveGallery_DivWidth-MyZoom_HandleWidth)),
				// sliderValue: MyZoom_HandleLeft,
				onSlide: function(value) {
					box.setStyle({ left: MyZoom_Left-value*MyZoom_HandleRateSlider + 'px' });
					MoveGallery_left = parseInt(MoveGallery_moveGallery.style.left);
					// this.mycounter += parseInt(value*MyZoom_Rate);
					// alert(MyZoom_Left-parseInt(value*MyZoom_Rate));
				},
				onChange: function(value) { 
					box.setStyle({ left: MyZoom_Left-value*MyZoom_HandleRateSlider + 'px' });
					MoveGallery_left = parseInt(MoveGallery_moveGallery.style.left);
					// pos = Position.cumulativeOffset('motioncontainer');
					// MyZoom_Left = pos[0];
					// alert(value + 'handlewidth: ' + MyZoom_HandleWidth + ' - rateSlider: ' + MyZoom_HandleRateSlider +  ' - rate: ' + MyZoom_HandleRate + ' - left: ' + MyZoom_HandleRate*MoveGallery_left + ' - ' + box.style.left);
				}
			});
			MyZoom_Isset = true;
		}
		else{
			// $('zoom_slider').style.visibility = 'hidden';
			$('zoom_slider').style.display = 'none';
			$('motiongallery').style.left = '0px';
		}
	}
};
// # END - GALERIA SCROLLBAR
