// cookie functions
function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}

// simpleblog character limit
function char_count(textEl,countEl,limit){
	if(textEl.value.length > limit){
		textEl.value = textEl.value.substring(0, limit);
	} else {
		countEl.value = limit - textEl.value.length;
	}
}

// toggle div 
function show(div){
	if(document.getElementById(div).style.display=='block'){
		document.getElementById(div).style.display='none';
	} else {
		document.getElementById(div).style.display='block';
		if(div == 'search'){
			document.searchForm.q.focus();
		}
	}
}

// document ready functions
jQuery(document).ready(function(){
	
	// login menu
	var clicked = false;
	jQuery("a.loginb").click(function(){
		if(clicked != false){
			jQuery('#login .sForm').fadeOut('fast',function(){
				jQuery('#login').slideUp('slow');
				jQuery("a.loginb").text('Login');
				clicked = false;
			});
		} else {
			jQuery('#login').slideDown('slow',function(){
				jQuery("#login .sForm").fadeIn('fast');
				jQuery("a.loginb").text('X Close');
				clicked = true;
			});
		}
	});
	
	// main menu
	var changed = true;
	jQuery("#nav li a").mouseover(function(){
		var itemId = jQuery(this).attr("id");
		if(currMenu!=itemId && changed !=false){
			changed = false;
			jQuery('#'+currMenu+'_nav').fadeOut('fast',function(){ 
				jQuery('#'+currMenu).removeClass('selected');
				jQuery('#'+itemId).addClass('selected');   
				jQuery('#'+itemId+'_nav').fadeIn('fast');
				currMenu = itemId;
				changed = true;
			});
		}
	});
	
	// check subnav state
	var menuCookie = readCookie('subNav');
	if(menuCookie==null || menuCookie==0){
		jQuery('#subNav').show();
		jQuery('#navState').addClass('navToggleOpen');
		showMenu=true;
	} else {
		jQuery('#navState').addClass('navToggleClosed');
		showMenu=false;
	}
	
	// toggle sub nav
	jQuery("a.navToggleOpen,a.navToggleClosed").click(function(){
		if(showMenu != false){
			jQuery('#subNav').slideUp('slow');
			createCookie('subNav','1',365);
			jQuery('#navState').removeClass('navToggleOpen');
			jQuery('#navState').addClass('navToggleClosed');
			showMenu=false;
		} else {
			jQuery('#subNav').slideDown('slow');
			createCookie('subNav','0',365);
			jQuery('#navState').removeClass('navToggleClosed');
			jQuery('#navState').addClass('navToggleOpen');
			showMenu=true;
		}
		return false;
	});
	
	// calender module
	var curr = 'cal_1';
	jQuery(".expandCal").click(function(){
		var div = jQuery(this).attr("title");
		if(div!=curr){
			jQuery("#"+div+" .expandCal").hide();
			 jQuery('#'+div).animate(
				{ width:"263px",queue:false},
				"slow",function(){
					jQuery('#'+div).addClass('cal_open');
				} 
			 );
			 
			 jQuery('#'+curr).animate(
				{ width:"84px",queue:false},
				"fast",function(){
					//alert('test');
				} 
			 );
			 jQuery("#"+curr+" .expandCal").show();
			 jQuery('#'+curr).removeClass('cal_open');
			 jQuery('#'+curr).addClass('cal_closed');
        }
		curr = div;
	});
	

	function vote_article(u,d,uri,c){
		jQuery.post('/scripts/new/vote_post.php',{ doc:d,uri:uri,val:u },
		function(txt){
			var table = '<table cellpadding="0" cellspacing="0"><tr><td width="66%"><span class="rate">Thanks for voting! ('+txt+') Votes</span></td><td><img src="/images/site/thumbsup_grey.gif" title="I Liked This Article" alt="thumbs up" width="20px" height="20px" /></td><td>Thumbs Up &nbsp;&nbsp;</td><td><img src="/images/site/thumbsdown_grey.gif" title="I Did Not Like This Article" alt="thumbs down" width="20px" height="20px" /></td><td>Thumbs Down</td></tr></table>';
			jQuery('#news_rate').html(table);
			createCookie(c, u, 365);
		});
	}
	
	if(document.getElementById('mapContainer')){
		StartYMap();
	}
	
	jQuery('.changeMp3').click(function(){
		var mTitle = jQuery(this).html();
		var mFile = jQuery(this).attr('href');
		var mBlock = jQuery(this).attr('alt');
		jQuery('#'+mBlock+'_head').html(mTitle);
		playerSwf(mBlock,1,mFile);
		return false;
	});
// end document ready
});
