/* highslide*/
hs.showCredits = false;
hs.graphicsDir = '/js/highslide/graphics/';
hs.outlineType = 'rounded-white';
hs.captionEval = 'this.thumb.alt';
hs.loadingText = 'Зареждане...';
hs.restoreTitle='';
hs.align = 'center';
hs.outlineType = 'rounded-white';
hs.transitions = ['fade'];
hs.fadeInOut = true;
hs.addSlideshow({
	interval: 5000,
	repeat: false,
	useControls: true,
	fixedControls: 'fit',
	overlayOptions: {
		opacity: 0.75,
		position: 'bottom center',
		hideOnMouseOut: true
	}
});
/* !!! */



$(document).ready(
	function(){
	
		
		/* Sub menu*/
		
		function menuFade(){
			if(menuOver == false){
				$('#subMenu ul').css('display','none');
				$('#'+sMenu).css('display','block');
				menuSleep = false;
			}
					
		}
		
		var menuTime;
		var menuSleep = false;
		var menuOver = false;
		
		$('#mainMenu li a').mouseenter(function(){
			menuOver = true;
			$('#subMenu ul').css('display','none');
			menuSleep = false;
			clearTimeout(menuTime);
		}).mouseout(function(){
			menuOver = false;
			if(menuSleep) return;
			menuSleep = true;
			
			menuTime = setTimeout(function(){
				if(menuOver == false){
					$('#subMenu ul').css('display','none');
					$('#'+sMenu).css('display','block');
					menuSleep = false;
				}
					
			}, 1000);
		});
		
		$('#subMenu').mouseenter(function(){
			menuOver = true;
			menuSleep = false;
			clearTimeout(menuTime);
		}).mouseleave(function(){
			$('#mainMenu li a').trigger('mouseout');
		});
		
		
		$('#mNews').mouseenter(function(){
			
			$('#sNews').css('display','block');
			
		});
		
		$('#mSport').mouseenter(function(){
			$('#sSport').css('display','block');
			
		});
		
		$('#mEditorial').mouseenter(function(){
			$('#sEditorial').css('display','block');
			
		});
		
		$('#mVideo').mouseenter(function(){
			$('#sVideo').css('display','block');
			
		});
		
		
		$('#pageSearch').focus(
			function(){
				if($(this).val()=='търси...') $(this).val('');
			}
		).focusout(
			function(){
				if($(this).val()=='') $(this).val('търси...');
			}
		);
		
		$('.lenti')
			.mouseenter(function(){$('.border',this).css('border-color','#d33');})
			.mouseleave(function(){$('.border',this).css('border-color','#414141');});
		
		$('.shadow')
			.mouseenter(function(){$(this).css('border-color','#999');})
			.mouseleave(function(){$(this).css('border-color','#ccc');});
		
		
	
		$('#pageNav li').mouseenter(
			function(){
				$('ul',this).stop().animate({'height': $('ul',this).attr('height')},parseInt($('ul',this).attr('height'))*6);
			}
		).mouseleave(
			function(){
				$('ul',this).stop().animate({'height': 0},parseInt($('ul',this).attr('height'))*5);
			}
		);
		var slideSleep = false;
		$('.slider, .rte-slider').each(function(){
			var thisSlider = this;
			
			if($('.screen, .screenp',this).attr('speed')) var slideSPeed = parseInt($('.screen, .screenp',this).attr('speed'));
			
			$('.next',this).click(function(){ slide(-1, thisSlider) });
			$('.prev',this).click(function(){ slide(1, thisSlider) });
			
			function slide(offset, obj){
				if(slideSleep) return false;
				if($(thisSlider).hasClass('rte-slider')){
					rte_cimg -= offset;
					
					if(rte_cimg >= rte_images) $('.next',thisSlider).fadeOut(0);
					else $('.next',thisSlider).fadeIn(0);
					
					if(rte_cimg <= 1) $('.prev',thisSlider).fadeOut(0);
					else $('.prev',thisSlider).fadeIn(0);
					
					$('#imgNum, #imgNump').html(rte_cimg+'/'+rte_images);
					$('#rtdTxt').html(rtdTxt[rte_cimg+1]);
				}
				var screenWidth = parseInt($('.screen, .screenp', obj).width());
				var offset      = offset*screenWidth;
				var filmWidth   = parseInt($('.film-width', obj).width());
				var marginLeft  = parseInt($('.film', obj).css('marginLeft'));
		
				var cLeft       = marginLeft + offset;
				var maxOffset   = screenWidth -  filmWidth;
			
				if(cLeft < maxOffset){
					screenWidth -= maxOffset - cLeft;
					cLeft = maxOffset;
					
				}
				else if(cLeft > 0){
					screenWidth -= cLeft;
					cLeft = 0;
				}
				if(cLeft == marginLeft) return false;
				$('.film', obj).animate({'marginLeft': cLeft},(slideSPeed ? slideSPeed : screenWidth*2.5),function(){ slideSleep = false; });
				slideSleep = true;
				
			}
		
		});
		
		$('#login-popup').dialog({ 
			title: 'вход', 
			autoOpen: false,
			modal: true,
			resizable: false,
			width: 350,
			height: 'auto'
		});
		
		
		
		$('#writeCom textarea').bind('keypress keydown keyup',function(){
			if($(this).val().length > 700) $(this).val($(this).val().substring(0, 700));
			else $('#countdown').html(700 - $(this).val().length);
		});
		
		
		
		
		
		//short news
		$('#shortNews a').mouseover(function(){
			$('#shortNews a').removeClass('hover');
			$(this).addClass('hover');
			$('#shortNewsImg')
				.attr('src',$(this).attr('img'))
				.attr('onclick','window.location = "/short:'+$(this).attr('uid')+'"');
		});
		
		
		
		$('.comm-vote a').mouseup(function(){
			var thisBox = $(this).parent();
			var vote = $(this).attr('vote');
			$.post("/ajax.php?cvote",{
				'id' : $(this).attr('uid'),
				'vote' : (vote == 0 ? 0 : 1)
			},function(data){
				if(data == 'OK'){
					$('a',thisBox).unbind('mouseup');
					
					if(vote == 1){
						var pl = parseInt($('.pl',thisBox).text());
						$('.pl',thisBox).text((isNaN(pl) ? 0 : pl) + 1);
						$('.sp-minus',thisBox).css({
							'background-position': '-160px 0',
							'cursor' : 'default'
						});
						
						$('.sp-plus',thisBox).css({
							'background-position': '-180px -20px',
							'cursor' : 'default'
						});
						
					}
					else{
						var mn = parseInt($('.mn',thisBox).text());
						$('.mn',thisBox).text((isNaN(mn) ? 0 : mn) + 1);
						$('.sp-minus',thisBox).css({
							'background-position': '-180px 0',
							'cursor' : 'default'
						});
						
						$('.sp-plus',thisBox).css({
							'background-position': '-160px -20px',
							'cursor' : 'default'
						});
					}
					
				}
				else alert(data);
		})
});


		
	}
	
);

function seekFor(){
	if($('#pageSearch').val() != 'търси...' && $('#pageSearch').val() != '') window.location = '/search:'+$('#pageSearch').val();
	return false;
};




function openPopup(params){
	if($('#popup').length == 0) $('body').append('<div id="popup"></div>');

	var options = {
		autoOpen: true,
		modal: true,
		resizable: params.resizable,
		width: params.width,
		minHeight: (params.height == undefined ? 'auto' : params.height),
		close: function(){
			$(this).dialog('destroy');
			$(this).remove();
		},
		buttons: { },
		title: params.title
	};


	$('#popup').html(params.html).dialog(options);
	return false;
}


function addComment(uid, rtd){
	$.post("/ajax.php?addComment",{
		'text': $('#writeCom textarea').val(),
		'uid': uid,
		'rtd': rtd
	}, function(data){ 
		if(data == 'OK'){
			id = 'com'+Math.ceil(Math.random()*1000000);
			$('#comment-box').html(
				'<div style="display:none" id="'+id+'">'+
					'<strong>'+username+'</strong>'+
					$('#writeCom textarea').val()+
				'</div>' + 
				$('#comment-box').html()
			);
			var targetOffset = $('#'+id).offset().top;
			$('html,body').animate({scrollTop: targetOffset+200}, 700, function(){ 
				$('#'+id).slideDown('slow'); 
			});
				
			
			$('#writeCom textarea').val('');

		}
		else alert(data);
	});
}



/* SLIDE */
var slideSleep = false;
function testSlide(offset,images, e, parent){
	if(slideSleep) return false;
	var mLeft = parseInt($('.slide', parent).css('margin-left'))+offset;
	if(mLeft > 0 || mLeft < (1 - images)*Math.abs(offset)) return false;
	$('.arrows i',parent).removeClass('inactive');
	if(mLeft < (2 - images)*Math.abs(offset)) $(e).children('i').addClass('inactive');
	if(mLeft > -Math.abs(offset)) $(e).children('i').addClass('inactive');
	$('#testDriveHome .slide').animate({'margin-left': mLeft},700,function(){ slideSleep = false; });
	slideSleep = true;
}



var submitLogin=false;
function login(){
	$.post("/ajax.php?login",{ 
		"username": $("#username").val(),
		"password": $("#password").val()
	}, function(data){
		if(data == "OK"){
			submitLogin = true;
			document.form_login.submit();			
		}
		else $(".error-text").html(data).parents(".ui-widget").fadeIn(0) });
}

var error=false;
function register(){
	var user = $("#reg-username").val();
	var pwd  = $("#reg-password").val();
	var pwd2 = $("#reg-password2").val();
	var email = $("#reg-email").val();
	var born = $("#reg-born").val();
	
	if(!user || !pwd || !pwd2 || !email || born==0) error = 'Моля попълнете всички полета';
	else if($("#obshti_usloviya:checked").length == 0) error = 'Не сте приели общите условия.';
	else if(user.length < 4) error = 'Невалидна потребител ( мин. 4 символа )';
	else if(pwd.length < 4) error = 'Невалидна парола ( мин. 4 символа )';
	else if(pwd != pwd2) error = 'Паролите не съвпадат';
	
	
	else{
		$.post("/ajax.php?register",{ 
			"username": user,
			"password": pwd,
			"password2": pwd2,
			"email": email,
			"daily": $('#daily').val(),
			"born": born
		}, function(data){
			if(data == "OK"){
				window.location.reload(true);		
			}
			else $(".error-text").html(data).parents(".ui-widget").fadeIn(0); });
	}
	if(error) $(".error-text").html(error).parents(".ui-widget").fadeIn(0);
	

}

