$(document).ready(function() { 
	$('#header h1').click( function() {
		window.location = '';
	});
	
	try{
		$('.sumulatelb').each(function(){
			var id = $(this).get(0).className.replace(/\D*/, '');
			$('a.gllbitem'+id).lightBox();
			
			$(this).click(function(){
				$('a.gllbitem'+id).trigger('click');
				return false;
			})
		})
	} catch (e) {
		
	}
	
	$('#user_change_territory').change(function(){
		$.post('/' , 
				{ action: 'changeterr', id: $(this).val() }, 
				function(data) {
			window.location.reload()
			
		});
	})
	
	function calendarLayout() {
		$('#calendarlayout').load('/users/calendar/month:'+month+'/year:'+year,  function(){
			attachCalendarEvents();
		});
	}
	
	function nextMonth() {
		if( month < 12 ) month++;
		else if( month == 12 ) { month = 1; year++; }
		calendarLayout();
		return false;
	}
	function prevMonth() {
		if( month > 1 ) month--;
		else if( month == 1 ) { month = 12; year--; }
		calendarLayout();
		return false;
	}
	calendarLayout();
	//attachCalendarEvents();
	
	function attachCalendarEvents(){
		$('#nextmonth').bind('click', nextMonth );
		$('#prevmonth').bind('click', prevMonth );
		$('.loadtodayitems a').click(function(){
			$('.selected_day').removeClass('selected_day');
			$(this).parents('td:first').addClass('selected_day');
			$('#todayeventslayout').load('/users/todayitems/month:'+month+'/year:'+year+'/day:'+Number( $(this).text() ) );
			return false;
		})
	}
	
	$('.forgotten').click(function(){
		$('#resetpassfrm').slideDown('fast');
	})
	
	$('form#signup').submit(function(){
		var ret = true;
		if( $('form#signup #name').val().length < 5 ) {
			$('form#signup #name').addClass('error');
			ret = false;
		} else {
			$('form#signup #name').removeClass('error');
		}

		if( $('form#signup #action').val() != 'update' ) {
		
			if( $('form#signup #password').val().length < 5 || $('form#signup #password').val() != $('form#signup #password1').val()) {
				$('form#signup #password').addClass('error');
				$('form#signup #password1').addClass('error');
				ret = false;
			} else {
				$('form#signup #password').removeClass('error');
				$('form#signup #password1').removeClass('error');
			}
			
		}
		if( !Validate.email( $('form#signup #email').val() ) ) {
			$('form#signup #email').addClass('error');
			ret = false;
		} else {
			$('form#signup #email').removeClass('error');
		}
		
		var cnt = 0;
		$('input.territories.query').each(function(i){
			if( $(this).get(0).checked ) cnt++;
		})
		
		if( cnt < 1 ) {
			alert('Select atleast one territory!');
			ret = false;
		}
		
		return ret;
	})
	
	$('#check_not, #check_glob_not').click(function(){
		$.post('/users', 
					{'notification': $('#check_not').get(0).checked.toString(), 
					'global_notification': $('#check_glob_not').get(0).checked.toString(),
					'action': 'medupdate'
				}, function(){});
	})
	
	$('div.press a.lightbox').each(function(i){
		$(this).lightBox();
	})
	
	$('div.press .larger').click(function(){
		$(this).siblings('.lightbox').trigger('click');
	})
	
	function prevLibr1(){
		$(this).unbind( 'click' );
		$(this).bind( 'click', noop );
		var el = this;
		var currPage = parseInt( $(el).parents('span:first').siblings('span.currentpage:first').text() );
		if( currPage > 1 ) {
			var newPage = currPage - 1;
			$(el).parents('h1:first').siblings('div.listing:first').children('a.page'+currPage).fadeOut('fast', function(){
				
				$(el).parents('span:first').siblings('span.currentpage:first').text( newPage );
				
				$(el).parents('h1:first').siblings('div.listing:first').children('a.page'+newPage).fadeIn('fast');
				$(el).unbind( 'click' );
				$(el).bind( 'click', prevLibr1 );
			});
		}
		return false;
	}
	function nextLibr1(){
		$(this).unbind( 'click' );
		$(this).bind( 'click', noop );
		var el = this;
		var currPage = parseInt( $(el).parents('span:first').siblings('span.currentpage:first').text() );
		var newPage = currPage + 1;
		if( $(el).parents('h1:first').siblings('div.listing:first').children('a.page'+newPage).length > 0 ) {
			
			$(el).parents('h1:first').siblings('div.listing:first').children('a.page'+currPage).fadeOut('fast', function(){
				$(el).parents('span:first').siblings('span.currentpage:first').text( newPage );
				$(el).parents('h1:first').siblings('div.listing:first').children('a.page'+newPage).fadeIn('fast');
				$(el).unbind( 'click' );
				$(el).bind( 'click', nextLibr1 );
			});
		
		}
		return false;
	}
	
	function noop(){
		return false;
	}
	
	$('div.box.librs a.prev').bind('click', prevLibr1 );
	$('div.box.librs a.next').bind( 'click', nextLibr1 );
	
	$('div.box.cstud a.prev').bind('click', prevCStud );	
	$('div.box.cstud a.next').bind( 'click', nextCStud );
	
	function prevCStud(){
		$(this).unbind( 'click' );
		$(this).bind( 'click', noop );
		var el = this;
		var currPage = parseInt( $(el).parents('span:first').siblings('span.currentpage:first').text() );
		if( currPage > 1 ) {
			var newPage = currPage - 1;
			$(el).parents('h1:first').siblings('div.listing:first').children('ul:first').children('li.page'+currPage).fadeOut('fast', function(){
				
				$(el).parents('span:first').siblings('span.currentpage:first').text( newPage );
				
				$(el).parents('h1:first').siblings('div.listing:first').children('ul:first').children('li.page'+newPage).fadeIn('fast');
				$(el).unbind( 'click' );
				$(el).bind( 'click', prevCStud );
			});
		}
		return false;
	}
	function nextCStud(){
		
		$(this).unbind( 'click' );
		$(this).bind( 'click', noop );
		var el = this;
		var currPage = parseInt( $(el).parents('span:first').siblings('span.currentpage:first').text() );
		var newPage = currPage + 1;
		if( $(el).parents('h1:first').siblings('div.listing:first').children('ul:first').children('li.page'+newPage).length > 0 ) {
			
			$(el).parents('h1:first').siblings('div.listing:first').children('ul:first').children('li.page'+currPage).fadeOut('fast', function(){
				$(el).parents('span:first').siblings('span.currentpage:first').text( newPage );
				$(el).parents('h1:first').siblings('div.listing:first').children('ul:first').children('li.page'+newPage).fadeIn('fast');
				$(el).unbind( 'click' );
				$(el).bind( 'click', nextCStud );
			});
		
		}
		return false;
	}
	
	$('.filtercats').click(function(){
		var catId = $(this).get(0).id.toString().replace(/\D*/, '');
		$('.filtercats').removeClass('active');
		$(this).addClass('active');
		if( catId == 0 ) {
			$(this).parents('div.box:first').children('div.listing').fadeIn();
			return false;
		}
		$(this).parents('div.box:first').children('div.listing').each(function(i){
			if( $(this).get(0).id.toString() != 'divCat'+catId ) {
				$(this).fadeOut();
			} else {
				$(this).fadeIn();
			}
		});
		return false;
	})
	
	
});



