$(function() {	
	$(".external").attr('target', '_blank');
	
	var tmp = {
		lietotajvards : '',
		licence : '',
		parole : '',
		komentars: ''
	}
	
	$('#lietotajvards').focus(function() {
		tmp.lietotajvards = $(this).attr('value');
		$(this).attr('value', '');		
	});				
	$('#lietotajvards').blur(function() {
		if (this.value == '') {
			$(this).attr('value', tmp.lietotajvards);
		}
	});
	
	$('#licence').focus(function() {
		tmp.licence = $(this).attr('value');
		$(this).attr('value', '');
	});				
	$('#licence').blur(function() {
		if (this.value == '') {
			$(this).attr('value', tmp.licence);
		}
	});
	
	$('#parole-c, #parole-u').focus(function() {
		tmp.parole = $(this).attr('value');
		$(this).attr('value', '');
	});
	$('#parole-c, #parole-u').blur(function() {
		if (this.value == '') {
			$(this).attr('value', tmp.parole);
		}
	});
	
	$('#komentars').focus(function() {
		tmp.komentars = $(this).attr('value');
		$(this).html('');
	});
	$('#komentars').blur(function() {
		if ($(this).html() == '') {
			$(this).html(tmp.komentars);
		}
	});
	
	$('#nav ul > li:first-child').addClass('first');
	
	$('#nav .active > a:first-child').hover(function(){
		$(this).next().addClass('show');
	}, function() {
		$(this).next().removeClass('show');
	});

	$('#nav .active > a:first-child').next().hover(function(){
		$(this).addClass('show');
		if($(this).hasClass('sub')) {
			$(this).prev().addClass('hover');
		}
	}, function() {
		$(this).removeClass('show');
		$(this).prev().removeClass('hover');
	});

	
	if($('#nav .active .active ul:not(.sub)').length == 0) {
		$('#nav .active').addClass('nosub');
	}

	$('#nav li').hover(function(){
		if (!$(this).hasClass('active')) {
			$(this).addClass('hover');
		}
		if (this.getElementsByTagName('ul').length != 0) {
			$(this).addClass('hassub');
		}
	}, function() {
			$(this).removeClass('hover');
			$(this).removeClass('hassub');
	});

	$('#usernav ul > li:first-child').addClass('first');
	
	$('#usernav .active > a:first-child').hover(function(){
		$(this).next().addClass('show');
	}, function() {
		$(this).next().removeClass('show');
	});

	$('#usernav .active > a:first-child').next().hover(function(){
		$(this).addClass('show');
		if($(this).hasClass('sub')) {
			$(this).prev().addClass('hover');
		}
	}, function() {
		$(this).removeClass('show');
		$(this).prev().removeClass('hover');
	});

	
	if($('#usernav .active .active ul:not(.sub)').length == 0) {
		$('#usernav .active').addClass('nosub');
	}
	
	$('#usernav li').hover(function(){
		if (!$(this).hasClass('active')) {
			$(this).addClass('hover');
		}		
		if (this.getElementsByTagName('ul').length != 0) {					
			$(this).addClass('hassub');			
		}
	}, function() {
			$(this).removeClass('hover');
			$(this).removeClass('hassub');
	});

	
	$('.tabmenu a').click(function() {
		$('.tabmenu li').removeClass('active');
		$(this).parent().addClass('active');
		$('.startmenucontent').hide();					
		$('.startmenucontent:eq('+$(this).parent().attr('id').replace(/tab-/,'')+')').show();
		return false;	
	});
	
	$('#authorisation ul li a').click(function() {
		$('#authorisation ul li').removeClass('active');
		$(this).parent().addClass('active');
		$('#authorisation form').hide();
		$('#'+$(this).parent().attr('id')+'-form').show();
		return false;	
	});
	
	$('#upload-file-label').bind("click", function() {
      $('#upload-file').click();
    });
	
	$('#myCheckbox0').bind("click", function() {
	  if (!$('.enewsmenu').attr('checked')) {	  	
	  	$('.enewsmenu:enabled').attr('checked', 'checked');
	  }
	  else {
	  	$('.enewsmenu').attr('checked', '');
	  }
    });
	
	if($('#forum .comments').length == 0) {
		$('#forum #comment').addClass('no-border');
	}
	
	var down = false;
	
	$("#uberdropdown_wrap .default").click(function() {
		if(down == false) {
			$(this).addClass("no-border");
		}
		$("#uberdropdown").slideToggle(500, function() {
			if(down == true) {
				$("#uberdropdown_wrap .default").removeClass("no-border");
				down = false;
			} else {
				down = true;
			}
		});
	});
	/*$("#uberdropdown_wrap").mouseout(function() {
		$("#uberdropdown").slideUp(500, function() {
			$("#uberdropdown_wrap .default").removeClass("no-border");
			down = false;
		});
	});*/
	
	/**
	 * Fix bad crosslinks (eg http://fms.lv/http://fmssowtware.lv/lat/xxx)
	 */	 	
	$('a').each(function(index,item) {
		if ( this.href.indexOf('http://',1) !== -1 ) {
			this.href = this.href.substring(this.href.indexOf('http://',1));
		}
	});
});

