$(function() {
	if($('.contentForm input[name=your_email]').length>0){
		$('.contentForm input[name=your_email]:eq(0)').focus();
	}
	if($('#slidePicture input[type=hidden]').length>0){
		loadImage($.makeArray($('#slidePicture input[type=hidden]')));
	}
});


//"'.$elem.'|'.$link_val.'|'.$pict->imageURL.'|'.$pict->alttext.'"

function loadImage(images){
	var sliceLength = $('#slidePicture input[type=hidden]').length;
	//var halfLoaded = sliceLength/2+ (sliceLength%2)/2;
	if(images.length == sliceLength-2){
		$('#loading').hide();
	}	
//	if(images.length == halfLoaded){
//		slideXPhotos(halfLoaded-1);
//	}
	if(images.length>0){
		//console.log(images.length);
		var imgItem = images.shift();		
		imgItem = $(imgItem).val().split('|');
		var newImg = $('<img />');
		newImg.attr('src', imgItem[2]);
		if(newImg[0].complete){
			var imgHtml = '<div class="slideElem slideElem'+imgItem[0]+'">';
			if(imgItem[1]!=''){
				imgHtml+='<a href="'+imgItem[1]+'" ><img src="'+imgItem[2]+'" alt="'+imgItem[3]+'"/></a></div>';
			}else{
				imgHtml+='<img src="'+imgItem[2]+'" alt="'+imgItem[3]+'"/></div>';
			}
			if(images.length<sliceLength){
				$(imgHtml).css('left', '864px');
			}
			$("#slidePicture").append(imgHtml);				
			loadImage(images);
		}else{
			newImg.load(function() {
				var imgHtml = '<div class="slideElem slideElem'+imgItem[0]+'">';
				if(imgItem[1]!=''){
					imgHtml+='<a href="'+imgItem[1]+'" ><img src="'+imgItem[2]+'" alt="'+imgItem[3]+'"/></a></div>';
				}else{
					imgHtml+='<img src="'+imgItem[2]+'" alt="'+imgItem[3]+'"/></div>';
				}
				$("#slidePicture").append(imgHtml);				
				loadImage(images);
			});		
		}
	}else{
		$('#slidePicture input[type=hidden]').remove();
		clearTimeout(timeSlide);
		slidePhotos();	
	}
}



//<?php if(is_home()){ ?>afterLoading();<?php } 
//elseif($cat_num==3){ ?> startSlide(); <?php } ?>"
	
var curr_photo = 0;
var start_val = -192;

function showPicture(num_pict){
	if(num_pict!=curr_photo){
		$('#naviPh a').removeClass = 'active';
		curr_photo = num_pict;
		$('#prPhoto').animate({left: (curr_photo*672*(-1))}, 300, "linear" );
		$('#naviPh a:eq('+curr_photo+')').addClass('active');	
	}
}

function showNextPicture(){
	$('#naviPh a').removeClass = 'active';
	curr_photo++;
	if(curr_photo==$('#prPhoto img').length){curr_photo=0;}
	$('#prPhoto').animate({left: (curr_photo*672*(-1))}, 300, "linear" );
	$('#naviPh a:eq('+curr_photo+')').addClass('active');	
}

function showPrevPicture(){
	$('#naviPh a').removeClass = 'active';
	curr_photo--;
	if(curr_photo<0){curr_photo=$('#prPhoto img').length - 1;}
	$('#prPhoto').animate({left: (curr_photo*672*(-1))}, 300, "linear" );
	$('#naviPh a:eq('+curr_photo+')').addClass('active');
}

function changePicture(num_pict){
	if(num_pict!=curr_photo){	
		curr_photo = num_pict;
		$('#prPhoto').animate({left: (curr_photo*864*(-1))+start_val}, 300, "linear" );
	}
}

var timeSlide;
var xSlide = 0;

function startSlide(){
	timeSlide=setTimeout("slidePhotos()",3500);	
}
		     
function slidePhotos(){
	var old = xSlide;
	xSlide++;
	if(xSlide==$('#slidePicture div.slideElem').length){xSlide=0;}
	$('#slidePicture div.slideElem').each(function (i){
		if(i==old){
			$(this).css('z-index', '2');
		}else if(i==xSlide){
			$(this).css('z-index', '3');	
		}else{
			$(this).css('z-index', '1');
		}	
	});	
	$('#slidePicture div.slideElem:eq('+xSlide+')').css('left', '864px');
	$('#slidePicture div.slideElem:eq('+xSlide+')').animate({left: 0}, 500, "linear" );
	timeSlide=setTimeout("slidePhotos()",3500);
}

function slideXPhotos(num){
	var old = xSlide;
	xSlide++;
	var $slideElems = $('#slidePicture div.slideElem:lt('+num+')');
	if(xSlide==$slideElems.length){xSlide=0;}
	$slideElems.each(function (i){
		if(i==old){
			$(this).css('z-index', '2');
		}else if(i==xSlide){
			$(this).css('z-index', '3');	
		}else{
			$(this).css('z-index', '1');
		}	
	});	
	$slideElems.eq(xSlide).css('left', '864px');
	$slideElems.eq(xSlide).animate({left: 0}, 500, "linear" );
	timeSlide=setTimeout("slideXPhotos("+num+")",3500);
}

function slideOnePhoto(num){
	var old = num-1;
	$('#slidePicture div.slideElem').each(function (i){
		if(i==old){
			$(this).css('z-index', '2');
		}else if(i==num){
			$(this).css('z-index', '3');	
		}else{
			$(this).css('z-index', '1');
		}	
	});	
	$('#slidePicture div.slideElem:eq('+num+')').css('left', '864px');
	$('#slidePicture div.slideElem:eq('+num+')').animate({left: 0}, 500, "linear" );
}
