/* init images actions 
$(document).ready(
	function(){

		var t = $('#thumbs > a');
		if(t.length){
			$(t[0]).children().addClass('active');
			$(t).click(
				function(){
					changeCatalogPhoto(this.href, '', $(this).children());
					return false;
				}
			);
		}

		$('#big_photo > a').click(
			function(){
				tb_show('', this.href, '');
				return false;
			}
		);
	}
);
*/

function changeCatalogPhoto(src, src2, thumb, bp, th){
	thumb = $(thumb).children();
	$('#' + bp + ' > img').attr('src', src);
	//$('#big_photo > a').attr('href', src2);
	$('#' + th + ' > a > img.active').removeClass('active');
	if(thumb){
		$(thumb).addClass('active');
		$(thumb).parent().blur();
	}
}

/* Favorites */
function addToFavorites(id, link){
	link.blur();
	$.get('/favorites/', {'action': 'add', 'id': id}, function(response){onAfterAddedToFavorites(response, link);});
}

function onAfterAddedToFavorites(response, link){
	if(response != 'auth error'){		
		link.setAttribute('href','/favorites/');
		link.removeAttribute('onclick');
		if($(link).parent().hasClass('to_cart')){
			$(link).parent().removeClass('to_cart');
			$(link).parent().addClass('in_list');
			$(link).html('<img src="/img/inlist.gif"/>');
		}else{
			$(link).addClass('in_list');
			$(link).html('в избранном');
		}
		
		var info = response == '0' ? 'нет проектов' : '<a href="/favorites/">' + response + '</a>';
		$('#wishlist_items_num').html(info);
		
	}else{	
		showPopup('registration_form');
	}
}

/* START PAGE */
function showProjectInfo(lnk){
	$('div', lnk).show();
}

function hideProjectInfo(lnk){
	$('div', lnk).hide();
}

function showTopProject(lnk, pict, paddr){	
	if(!$(lnk).hasClass('expanded')){
		$(lnk).addClass('expanded');		
		$(lnk).siblings().removeClass('expanded');
		$('#tp_photo').attr('src', pict);
		$('#tp_photo').parent().attr('href', paddr);
	}
}

/* popups */
function showPopup(id, link){
	hideAllPopups();
	if(link)
		link.blur();
	showSandBox();
	var t = ($(window).width()-$('#' + id).width())/2;
	$('#' + id).show();//animate({height: 'show'}, 200);
	$('#' + id).css({'left': t});
	$('#' + id).css({'top': $(document).scrollTop() + 50});
	//$(document).scrollTop(0);
}

function hidePopup(id){
	hideSandBox();
	var t = document.getElementById(id);
	if(t){
		t.style.display = 'none';
	}
}

function hideAllPopups(){
	hideSandBox();
	$('div.login_popup').hide();
}

function showSandBox(){
	var s = document.getElementById('sandbox');
	if(!s){
		s = document.createElement('div');
		s.setAttribute('id', 'sandbox');
		$(s).click(function(){hideAllPopups();});
		document.body.appendChild(s);
	}
	s.style.height = $(document).height() + 'px';
	$(s).show();
}

function hideSandBox(){
	var s = document.getElementById('sandbox');
	if(s){
		s.style.display = 'none';
	}
}

/* login */
function doLogin(link){
	link.blur();
	var email = document.forms['login_form'].elements['login'];
	var password = document.forms['login_form'].elements['password'];
	var remember = document.forms['login_form'].elements['remember'];
	var ok = simpleFormCheck('login_form');
	if(ok){
		var s_data = {'login': email.value, 'password': password.value}
		if(remember.checked){
			s_data.remember=1;
		}
		$.post('/login/', s_data, onLoginDone);
	}
}

function onLoginDone(text){
	if(text != ''){
		document.getElementById('login_errors').innerHTML = text;
	}else{
		window.location.reload();
	}
}

function clearLoginForm(){
	var form_name = 'login_form';
	document.getElementById('login_errors').innerHTML = '';
	for(var i=0; i<document.forms[form_name].elements.length; i++){
		if(document.forms[form_name].elements[i].className != 'but' && document.forms[form_name].elements[i].className != 'chb'){
			document.forms[form_name].elements[i].value = '';
			$(document.forms[form_name].elements[i]).removeClass('error');
		}
	}
}

/* registration */
function doRegister(link){
	if(link)
		link.blur();
	var email = document.forms['registration_form'].elements['login'];
	var password = document.forms['registration_form'].elements['password'];
	var password_r = document.forms['registration_form'].elements['password_repeat'];
	var cpt = document.forms['registration_form'].elements['captcha_word'];
	var ok = simpleFormCheck('registration_form');
	if(ok){
		$.post('/registration/', {'_save': 1, 'login': email.value, 'password': password.value, 'password_repeat': password_r.value, 'captcha_word': cpt.value}, onRegisterDone);
	}
}

function onRegisterDone(text){
	if(text != ''){
		document.getElementById('registration_errors').innerHTML = text;
	}else{
		window.location.reload();
	}
}

/* common form functions */
function onFieldFocus(el){
	$(el).removeClass('error');
}

function simpleFormCheck(form_name){
	var isok = true;
	for(var i=0; i<document.forms[form_name].elements.length; i++){
		if(document.forms[form_name].elements[i].value == ''){
			document.forms[form_name].elements[i].className = document.forms[form_name].elements[i].className == '' ? 'error' : document.forms[form_name].elements[i].className + ' error';
			isok = false;
		}
	}
	return isok;
}

function showTooltip(id, lnk){
	var offs = $(lnk).offset();
	if(document.getElementById(id)){
		var h = $('#' + id).height();
		var w = $('#' + id).width();
		
		var left = offs.left - 10;
		var top = offs.top - h;
		
		if(top <= 0){
			top = offs.top + $(lnk).height() + 10;
		}
		
		if(left + w <= $(document.width)){
			left = offs_left - w;
		}
		
		$('#' + id).css({'top': top + 'px', 'left': left + 'px'}).show();
	}
}

function hideTooltip(id){
	$('#' + id).hide();
}

/* init tooltip */
$(document).ready(
	function(){
		$('a.tooltip_control').mouseover(
			function(){
				showTooltip($(this).attr('rel'), this);
			}
		).mouseout(
			function(){
				hideTooltip($(this).attr('rel'));
			}
		).click(
			function(){this.blur(); return false;}
		);
	}
);