$(document).ready(function() {
	//$(".selectList select").selectbox().bind("change", function(){
		//alert($(this).val());
	//});
	//$(".checkbox input").ezMark();
	
/*
	$(".prodPhotos .smallPhotos a").each(function(){
		$(this).attr("rel", $(this).attr("href"));
		$(this).removeAttr("href");
	});
	$(".prodPhotos .smallPhotos a").mouseover(function(){
		$(this).parents(".prodPhotos").eq(0).find(".photo img").attr("src", $(this).attr("rel"));									   
	});
*/
	
	$(".slideshowBlock").each(function(){
		initSlideshow($(this));
	});
	$("a[rel=img_group]").fancybox({
	    'transitionIn'      : 'elastic',
	    'transitionOut'     : 'elastic',
	    'titlePosition'     : 'over',
	    'titleFormat'       : function(title, currentArray, currentIndex, currentOpts) {
					return '<span id="fancybox-title-over">Фото ' +  (currentIndex + 1) + ' / ' + currentArray.length + '</span>';
					}
	});
});

var idTimeout;
function initSlideshow(slides){
	var items = slides.find(".slideshowList li");
	var photos = slides.find("._photo");
	var cur = slides.find(".slideshowList li.active").index();
	var count = items.size();
	photos.removeClass("active");
	photos.css("display", "block");
	
	var overed = false;
	
	showPhoto();
	function showPhoto(){
		photos.css("z-index", 4);
		photos.css("opacity", 0);
		photos.eq(cur).css("z-index", 5);
		photos.stop();
		photos.eq(cur).animate({
			opacity: 1
		}, 500, function() {
			if(!overed){
				idTimeout = setTimeout(function(){
					if(!overed){
						hidePhoto();
					}
				}, 3000);
			}
		});	
	}
	function hidePhoto(num){
		var prevCur = cur;
		items.removeClass("active");
		if(!num){
			if(cur + 1 < count){
				cur++;	
			}
			else{
				cur = 0;	
			}
		}
		else{
			cur = num;
		}
		items.eq(cur).addClass("active");
		showPhoto();
		photos.eq(prevCur).animate({
			opacity: 0
		}, 500, function() {
			
		});		
	}
	
	items.click(function(e){
		e.stopPropagation();
		cur = $(this).index();
		clearTimeout(idTimeout);
		photos.css("z-index", 4);
		photos.css("opacity", 0);
		photos.eq(cur).css("z-index", 5);
		photos.stop();
		photos.eq(cur).animate({
			opacity: 1
			}, 1000, function() {
				});
		items.removeClass("active");
		items.eq(cur).addClass("active");
		return false;
	});
	
	slides.mouseenter(function(e){
		overed = true;
		clearTimeout(idTimeout);
		e.stopPropagation();
		e.preventDefault();
		return false;
	});
	slides.mouseleave(function(e){
		overed = false;
		e.stopPropagation();
		e.preventDefault();
		clearTimeout(idTimeout);
		idTimeout = setTimeout(function(){
			if(cur + 1 < count){
				cur++;	
			}
			else{
				cur = 0;	
			}
			items.removeClass("active");
			items.eq(cur).addClass("active");
			photos.stop();
			showPhoto();
		}, 3000);
		return false;
	});
}

function login_me() {
	var link = '/login/?check=1';
	$.ajax({
		url: '/' + link,
		type: "GET",
		processData: false,
		cache: false,
		success: onSuccessLogin,
		error: errorFunction,
		dataType: "json"
	});
}

function errorFunction(XMLHttpRequest, textStatus, errorThrown){
//    alert(textStatus);
}

function onSuccessLogin(oReq){
	var json = oReq;
	$('#login_content').html(json.login);

//alert(json.cookie);
//alert(json.login);
}

