var filter = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@(([^-]\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([^-][a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,4}))$/;

var stop_loop = 0;
var time_interval = 4000; //ms
var last_index = '';

$(document).ready(function()
{
	var lastImageSrc = null;
	var lastLinkHtml = $('#images .info a').html();
	var lastLinkHref = $('#images .info a').attr('href');;

	$('#menu_left li.main').bind('mouseover', function()
	{
		if(!$(this).hasClass('selected'))
		{
			$('#menu_left li.main').each(function(){
				$(this).removeClass('selected');
			});
			$(this).addClass('selected');
			changeImage($(this).children().attr('href'));
			
			$('#images .info a').attr('href', $(this).children('a').attr('href'));
			$('#images .info a').html($(this).children('a').html());
			return false;
		}
	});


	$('#menu_left li.main').bind('mouseout', function()
	{
//		if(!$(this).hasClass('green'))
//		{
//			$(this).removeClass('selected');
//		}
		return false;
	});


	$('#gallery img').click(function()
	{
		var img_src = $(this).attr('id');
		if($('#gallery li').length > 1)
		{
			$('#gallery #img_div img').fadeOut("fast");
			setTimeout(function(){$.ajax({
				url: 'ajax.php',
				type: 'POST',
				async: false,
				data: {gallery: 1, image: img_src, module: module_name},
				success: function(result){
					$('#gallery #img_div').html(result);
				}
			});
			}, 150);
		}
	});


	$('#gallery img').bind('mouseover', (function()
	{
		$(this).addClass('low');
	}));
	$('#gallery img').bind('mouseout', (function()
	{
		$(this).removeClass('low');
	}));


	//if(jQuery.browser.version == '')
	$('#content_center a img').bind('mouseover', (function()
	{
		$(this).addClass('img_hover');
	}));
	$('#content_center a img').bind('mouseout', (function()
	{
		$(this).removeClass('img_hover');
	}));

	// bulletin subscriotn
	$('#newsletter_form .newsletter_subscription').bind('click', (function()
	{
		$.ajax({
				url: 'ajax.php',
				type: 'POST',
				async: false,
				data: {newsletter: 1, email: $('#newsletter_subscription').val(), lang: $('#lang').val()},
				success: function(result){
					$('#newsletter_submit_msg').html(result);
					setTimeout("$('#newsletter_submit_msg').html('&nbsp;');", 5000);
				}
			});
	}));

	$('#newsletter_subscription').bind('blur', function(){
		if ($(this).val()=='')
		{
			$(this).val('email');
		}
	});

	$('#newsletter_subscription').bind('focus', function(){
		if ($(this).val() == 'email')
		{
			$(this).val('');
		}
	});


	// send to friend
	$('#options .send_to_mail').bind('click', function()
	{
		var status = $('#send_page').css('display');
		if(status == 'none')
		{
			$.ajax({
					url: 'ajax.php',
					type: 'POST',
					//async: false,
					data: {send_page: 1},
					success: function(result){
						$('#send_page').html(result);
						$('#send_page').css({/*display: 'block', */top: '-'+$('#send_page').width(), left: '0', background: '#fff'});
						var left_pos = ($(window).width()/2) - ($('#send_page').width()/2);
						var top_pos = ($(window).height()/2) - ($('#send_page').height()/2);

						$('#send_page').css({top: top_pos, left: left_pos});
						$('#send_page').fadeIn();
					}
				});
		}
	});



	// gallery
	var speed = 6;
	var stop_loop = 1;
	var ul_length = $('#container li').length * 91;
	var div_length = $('#pictures').width();
	var dif = ul_length - div_length;
	$('#container').css('width', ul_length+$('#container li').width());

	$('#button_left').bind('mouseover', function(){
		setStop(0);
		moveGallery(dif, speed, 'left');
	});
	$('#button_left').bind('mouseout', function(){
		setStop(1);
	});

	$('#button_right').bind('mouseover', function(){
		setStop(0);
		moveGallery(dif, speed, 'right');
	});
	$('#button_right').bind('mouseout', function(){
		setStop(1);
	});

	// gallery hover
	$('#pictures img').bind('mouseover', function(){
		$(this).addClass('low');
	});
	// gallery hover
	$('#pictures img').bind('mouseout', function(){
		$(this).removeClass('low');
	});
	// gallery hover
	$('#pictures a').bind('click', function(){
		$(this).lightbox({start:true,events:false});
		return false;
	});



	$('#q').bind('blur', function(){
		if ($(this).val()=='')
		{
			$(this).val('Търсене');
		}
	});

	$('#q').bind('focus', function(){
		if ($(this).val() == 'Търсене')
		{
			$(this).val('');
		}
	});

	$('#q2').bind('blur', function(){
		if ($(this).val()=='')
		{
			$(this).val('Търсене');
		}
	});

	$('#q2').bind('focus', function(){
		if ($(this).val() == 'Търсене')
		{
			$(this).val('');
		}
	});



	$('#calendar_btns .up').bind('click', function(){
		$('#year').val( parseInt( $('#year').val() ) + 1 );
		getCalendar();
	});
	$('#calendar_btns .down').bind('click', function(){
		$('#year').val( parseInt( $('#year').val() ) - 1 );
		getCalendar();
	});



bindCalendarButtons();

});
// ----------------------------------------------

function bindCalendarButtons()
{

	$('#calendar_btns .up').bind('click', function(){
		$('#year').val( parseInt( $('#year').val() ) + 1 );
		getCalendar();
	});
	$('#calendar_btns .down').bind('click', function(){
		$('#year').val( parseInt( $('#year').val() ) - 1 );
		getCalendar();
	});

	$('#months').bind('change', function(){
		getCalendar();
	});
}

function getCalendar()
{
		$.ajax({
				url: 'ajax.php',
				type: 'POST',
				async: false,
				data: {calendar: 1, year: $('#year').val(), month: $('#months').val()},
				success: function(result){
					$('#calendar').html(result);
					bindCalendarButtons();
					return false;
				}
			});

}



function moveGallery(dif, speed, pos)
{
	if(dif > 0 && stop_loop == 0)
	{
		var current_pos = parseInt($('#container').css('margin-left'));
		if(pos == 'left'){
			var go_to = current_pos-speed;
			if(-(current_pos) <= (dif+17)){
				var move = true;
			}else{
				var move = false;
			}
		}
		else{
			var go_to = current_pos+speed;
			if(current_pos <= 0){
				var move = true;
			}else{
				var move = false;
			}
		}

		if(move == true)
		{
			$('#container').css('margin-left', go_to);
			setTimeout("moveGallery("+dif+", "+speed+", '"+pos+"');", 5);
		}

	}
}

function setStop(val)
{
	stop_loop = val;
}

function changeImage(pageUrl)
{
	if($('#images').html())
	{
		$('#slide_img').attr('src', load_images[getImgSrc(pageUrl)]);
	}
}


function getImgSrc(url)
{
	if(url)
	{
		var parts = url.split('/');
		var last_part = parts[parts.length - 1];
		if( last_part.indexOf('?') > 0 ){
		
			last_part = last_part.split("?lg=en").join("");
			last_part = last_part.split("?lg=bg").join("");
		}
		return last_part;
	}
}



function validate_email(email, form)
{
	if(form == '')
	{
		var form = '';
	}
	else
	{
		var form = form;
	}

	if(!$(email).value.match(filter))
	{
		$(email).focus();
		return false;
	}

	if(form != '')
	{
		$(form).submit();
	}
	else
	{
		return true;
	}
}
