var tOut;
var searchDef = 'Введите фамилию или её часть...';
var totalAdd = 1;

function do_modal (mode, msg) {
	var mode, msg, html = '';
	$('#matte').hide();
	$('#modal').empty().hide();
	var contW = $('#holder').width();
	var contH = $('#holder').height();
	if (mode == 0) {
		$('.alert').hide();
		return;
	}
	switch (mode) {
		case 1: html = $('#enter').html(); break;
		case 2: html = $('#registration').html(); break;
		case 3: html = $('#remind').html(); break;
		default: html = $('#mailto').html();
	}
	location.hash = 'top';
	$('#matte').show().css('height', $(document).height()).css('opacity', '0.5');
	$('#modal').show().css('left', (contW / 2 - $('#modal').width() / 2) + 'px');
	$('#modal').css('top', '150px');
	$('#modal').html(html);
	$('#modal img[@id^=capt]').attr('src', '/inc/captcha.php?r=' + Math.round(Math.random() * 100000));
}

function hider () {
    $('#alert').empty().hide();
    clearTimeout(tOut);
}

function default_search () {
    var curVal = $.trim( $('#search').val() );
    if (curVal == '' || curVal == searchDef) $('#search').val(searchDef).css('color', '#bababa');
}

function tog_add_text () {
	$('#add_div').toggle();
	$('#add_text').toggle();
}

function clone_field (id, method) {
	var id, method, html;
	if (method) {
		if (totalAdd > 9) {
			alert('Нельзя добавить больше 10 фотографий за один раз!');
			return;
		}
		var strict = $('input[@name=name]').val();
		if (strict.length < 5) {
			alert('Сначала заполните название галереи!\nНазвание должно быть не менее 5 символов.');
			return;
		}
		html = '<div id="add_' + (totalAdd + 1) + '" style="padding-left:350px;">';
		html += $('#' + id + ' div').html() + '</div>';
		$('#' + id).append(html);
		totalAdd++;
	} else {
		if (totalAdd < 2) return;
        $('#add_' + totalAdd).remove();
        totalAdd--;
	}
}

$(document).ready( function () {

    tOut = window.setTimeout('hider()', 8000);
    default_search();

    $('#menu a img').hover( function() {
        var src = $(this).attr('src');
        $(this).attr('src', src.replace('.png', '_h.png'));
    }, function() {
        var src = $(this).attr('src');
        $(this).attr('src', src.replace('_h.png', '.png'));
    });

    $('.field, .row').hover( function () {
        $(this).css('background-color', '#eeeeee');
    }, function () {
        $(this).css('background-color', '');
    });

    $(this).keyup( function(e) {
    	var keynum;
        if (window.event) keynum = e.keyCode;
        else if(e.which) keynum = e.which;
        if (keynum == 27) {
            $('#matte').hide();
	        $('#modal').empty().hide();
        }
    });

    $('#search').focus( function () {
        $(this).css('color', '#000000');
        if ($(this).val() == searchDef) $(this).val('');
    });
    $('#search').blur( function () {
        default_search();
    });

    /*$('#reset_btn').click( function () {
        $('#captcha').attr('src', '/inc/captcha.php?r=' + Math.round(Math.random() * 100000));
        return false;
    });*/

});



function del_gal (id) {
	var id;
	if (confirm('Вы действительно хотите удалить галерею?\nВсе данные будут утеряны БЕЗВОЗВРАТНО!')) {
		$.get(
		    '/inc/ajax.php',
            {id: id, method: 'gal'},
            function (data) {
            	var data;
                if (data == 'TRUE') {
                	$('#gal_' + id).remove();
                } else {
                	alert('Невозможно удалить галерею! Попробуйте в другой раз.')
                }
            }
        );
	}
}

function del_img (path) {
	var path;
	if (confirm('Вы действительно хотите удалить фото?\nЭта операция НЕОБРАТИМА!')) {
		$.get(
		    '/inc/ajax.php',
            {path: path, method: 'img'},
            function (data) {
            	var data;
                if (data == 'TRUE') {
                	$('img[@src=' + path + ']').parents('.gal_photo').remove();
                } else {
                	alert('Невозможно удалить фото! Попробуйте в другой раз.')
                }
            }
        );
	}
}

function js_changetown( obj )
{ 
   var town = document.getElementsByName("townlist")[0];
	document.location = '/'+town.value;
}

function js_seltown()
{ 
	$("#ztown").css('display', 'block');
//   var town = document.getElementsByName("townlist")[0];
//	document.location = '/'+town.value;
}

function js_over()
{
	$('#ztown').css('display','block');
}

function js_out()
{
	$('#ztown').css('display','none');
}
var razd_tip = 0;

function js_razdel( dislok )
{
//	var tip = $(this).find('.razdlist');
//	$("#razd"+dislok).css( 'display', 'block' );
//	tip.show();
//   alert( dislok);
/*	if ( $("#razd"+dislok).html() =='' )
	{
		$("#razd"+dislok).html('Идет загрузка...');
		$.get("/inc/ajax_town.php", { dislok: dislok },
   	function(data){
   		$("#razd"+dislok).html( data );
         tip = razd_tip;
//         alert( razd_tip );
         var tipHeight = tip.height(); //Find height of tooltip
         var offset = tip.offset();
         var mousey = offset.top - tipHeight + 15; 
//        alert( offset.left );
        //Absolute position the tooltip according to mouse position
        tip.css({  top: mousey });
   	});
   }*/
//   		$("#razd"+dislok).html( data );
//         tip = razd_tip;
//         alert( razd_tip );
/*         var tipHeight = tip.height(); //Find height of tooltip
         var offset = tip.offset();
         var mousey = offset.top - tipHeight + 15; 
//        alert( offset.left );
        //Absolute position the tooltip according to mouse position
        tip.css({  top: mousey });
  */ 
	return false;
}

$(document).ready(function() {
    //Tooltips
    $(".tip_trigger").hover( function(){},
	    function() {
        tip = $(this).find('.razdlist');
		  tip.hide();
//		  $(this).find('.razdlist').css( 'display', 'none' );
//		  $("#razd"+dislok).css( 'display', 'none' );
	 });
    $(".tip_trigger").click(function( e ){
        tip = $(this).find('.razdlist');
        razd_tip = tip;
//         alert( 'ok' + razd_tip );
        var mousex = e.pageX + 0; //Get X coodrinates
        var mousey = e.pageY + 0; //Get Y coordinates
        var tipWidth = tip.width(); //Find width of tooltip
        var tipHeight = tip.height(); //Find height of tooltip

        //Distance of element from the right edge of viewport
        var tipVisX = $(window).width() - (mousex + tipWidth);
        //Distance of element from the bottom of viewport
        var tipVisY = $(window).height() - (mousey + tipHeight);

        if ( tipVisX < 20 ) { //If tooltip exceeds the X coordinate of viewport
            mousex = e.pageX - tipWidth - 20;
        } if ( tipVisY < 20 ) { //If tooltip exceeds the Y coordinate of viewport
            mousey = e.pageY - tipHeight - 20;
        }
        var offset = $("#rdiv").offset();
        mousex = mousex - parseInt(offset.left);
//        alert( mousex + ' ' + mousey );
        //Absolute position the tooltip according to mouse position
        tip.css({  top: mousey, left: mousex });

//         var tipHeight = tip.height(); //Find height of tooltip
/*         alert( tip + tip.offset() );
         var offset = tip.offset();
         var mousey = offset.top - tipHeight + 15; 
//        alert( offset.left );
        //Absolute position the tooltip according to mouse position
        tip.css({  top: mousey });
*/
//        alert( tip );
//		  tip.css('display', 'block');        
        tip.show(); //Show tooltip
    })
	 
/*	 , function() {
        tip.hide(); //Hide tooltip
    }).mousemove(function(e) {
    });*/
});

function js_linktown( elem )
{
	document.location = $(elem).attr( 'href' );
	return true;
}
