$(document).ready(function() {
	var who = (readCookie('menu_left'));
	$("a[href^="+who+"]").next('ul').css('display','block');
    $('a[href^=#]').click(function(){
    	if ($(this).next('ul').is(':hidden')){
    		$('a[href^=#]').next('ul').slideUp('fast');
    		$(this).next('ul').slideDown('fast');
    		createCookie('menu_left', $(this).attr('href'), 10);
    	}
    	else{
    		$(this).next('ul').slideUp('fast');
    		if ($(this).next('ul').length>0){eraseCookie('menu_left');}
    	}
    });
    
    // news popup
    
    var pn_visible = false;
    var pn_recalculate = function () {
      var st = $(window).scrollTop();
      var ww = $(window).width();
      var wh = $(window).height();
      $('#news_popup').css({
        'top' : ((wh-500) / 2)+st+'px',
        'left': ((ww-900) / 2)+'px'
      });
    };
    
    $(window).scroll(function () {
      if (pn_visible) pn_recalculate();  
    });
    
    $('.env_news_know_more').click(function (e) {
      e.preventDefault();
      var href = $(this).attr('href');
      var st = $(window).scrollTop();
      var ww = $(window).width();
      var wh = $(window).height();
      var l = href.length;
	  var id = href.charAt(l-1);
      
      $.ajax({
      	type: 'get',
      	url: 'http://www.enviprime.com/template/enviprime/js/ajax.php',
      	data: 'id='+id,
      	dataType: 'json',
      	async: false,
      	success: function(data) {
      		$.each(data, function(i,item){
      			$('#news_popup .'+i).html(item);
      		});
      		      		
      		$('#news_popup').css({
		        'top' : ((wh-500) / 2)+st+'px',
		        'left': ((ww-900) / 2)+'px',
		        'width' : '900px',
		        'height' : '500px'
		    }).show('normal');
	      
	        pn_visible = true;
      	  }
      });
    });
    
    $('#news_popup .close').click( function() {
      if (pn_visible) {
        $('#news_popup').hide('normal');
      }
    });
    
});

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 var 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;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}
