var pages = new Array('Nyheter', 'VideoNyheter', 'SenasteBilder', 'LiveRadio');

var demo_path = baseurl;

function showFooterPage(page, param, restore)
{
	//alert(param);
	var NyheterDisplay = jQuery('#' + page + 'Block').css('display');
	
    if(typeof(restore) == 'undefined')
    {
		
	   closeFooterPages(page);
    }
	
	url = demo_path+'footer/'+page+'Show.php';
	
	if(param != null)
	{
		url += '?' + param;
	}
	
	if (NyheterDisplay=='none' || param != null)
	{
		jQuery.ajax({url:url, async: false,
			success: function(html) {
				//Clear the text currently inside the results div.		
					jQuery('#' + page + 'Show').html(html);
					//otherEl.erase('style');
					jQuery('#' + page + 'Block').css('display', 'block');
					jQuery('#t' + page).css('display', 'block');
			}
		});
	}
	else jQuery('#' + page + 'Block').css('visibility','visible');
	
	jQuery.cookie('footer-' + page, 'visible');
	jQuery.cookie('footer-' + page + '-param', param);
	
	//reset_z('#' + page + 'Block');
	if(typeof(restore) == 'undefined')
    {
        jQuery('#t' + page).attr('class', 'inactive'); 
    	jQuery('#at' + page).attr('class', 'blt_active'); 
    	jQuery('#span_topent' + page).css('display', 'none');     
    }
	
}

function closeFooterPages(p)
{
	for(j = 0; j < pages.length; j++)
	{
		page = pages[j];
		if(page != p && jQuery('#' + page + 'Block').css('display') != 'none')
		{
			jQuery('#' + page + 'Block').css('visibility','hidden');
			jQuery('#t' + page).attr('class', 'noactive'); 
			jQuery('#at' + page).attr('class', 'blt_mactive'); 
			jQuery('#span_topent' + page).css('display', 'inline');	
            
            jQuery.cookie('footer-' + page, 'minimize');
		}
	}
}

function minFooter(page)
{
    jQuery('#' + page + 'Block').css('visibility','hidden');
	jQuery('#t' + page).attr('class', 'noactive'); 
	jQuery('#at' + page).attr('class', 'blt_mactive'); 
	jQuery('#span_topent' + page).css('display', 'inline');
    jQuery.cookie('footer-' + page, 'minimize');
}

function reset_z(a) {
	//var z_index = $(a).getStyle('z-index');
	b = eval(jQuery('#SenasteBilderBlock').css('z-index')-1);
	jQuery('#SenasteBilderBlock').css('z-index', b);
	
	b = eval(jQuery('#VideoNyheterBlock').css('z-index')-1);
	jQuery('#VideoNyheterBlock').css('z-index', b);
	
	b = eval(jQuery('#NyheterBlock').css('z-index')-1);
	jQuery('#NyheterBlock').css('z-index', b);
	
	b = eval(jQuery('#LiveRadioBlock').css('z-index')-1);
	jQuery('#LiveRadioBlock').css('z-index', b);
	
	
	jQuery(a).css('z-index', '5');
}
	
jQuery(document).ready(function($){	

	//if (!window.demo_path) window.demo_path = '';
	// --
		
	//We can use one Request object many times.
	
	var i = 3;
    
	
	for(i = 0; i < pages.length; i++)
	{
		var p = pages[i];
		
		// restore previous state from cookie
		if($.cookie('footer-' + p) == 'visible')
		{
			showFooterPage(p, $.cookie('footer-' + p + '-param'));
            //alert('visible: ' + p);
		}
        else if ($.cookie('footer-' + p) == 'minimize')
        {
            showFooterPage(p, $.cookie('footer-' + p + '-param'), false);
            minFooter(p);
            //alert('minimize: ' + p);
        }
		
		//open
		$('#'+p).click(function() {			
			var page = $(this).attr('id');
			showFooterPage(page);
		});
		//close
		$('#close' + p).click(function() {
			var page = $(this).attr('id').replace('close', '');			
			// save state
			$.cookie('footer-' + page, 'closed');
			
			$('#' + page + 'Show').html('');
			$('#' + page + 'Block').css('display', 'none'); 
			$('#t' + page).css('display', 'none');
		});
		//minimize
		$('#min' + p).click(function() {		
			var page = $(this).attr('id').replace('min', '');
            
            // save state
			$.cookie('footer-' + page, 'minimize');
									 
			minFooter(page);		
		});		
		
		$('#tc_' + p).click(function() {
			var page = $(this).attr('id').replace('tc_', '');
			
			$('#' + page + 'Show').html('');
			$('#' + page + 'Block').css('display', 'none');
			$('#t' + page).css('display', 'none');
			$('#' + page + 'Block').css('visibility','visible');
            jQuery.cookie('footer-' + page, 'closed');
		});	
		
		$('#at' + p).click(function() {
			var page = $(this).attr('id').replace('at', '');
            
            // save state
			$.cookie('footer-' + page, 'visible');
			
			$('#' + page + 'Block').css('visibility','visible');
			reset_z('#' + page + 'Block');
			closeFooterPages(page);
			
			$('#t' + page).attr('class', 'inactive'); 
			$('#at' + page).attr('class', 'blt_active'); 
			$('#span_topent' + page).css('display', 'none');		
		});		
	}
    
    //showFooterPage('Nyheter', '', true);
    /*showFooterPage('VideoNyheter', '', true);*/
    //minFooter('Nyheter');
    
    // code for showing one news by MARUF
    
    $("div.latestnews-news li a, #related a").each(function(){

       $(this).click(function(){
           // alert(this);
            var id      = "",
                page    = "Nyheter";
                id = $(this).attr('id').split("_")[1];
                               
            //jQuery.cookie('content-' + id, 'ok');
            /*$.ajax({url:demo_path+'footer/NyheterShow.php?id=' + id, 
                    
					success: function(html) {					   
						//Clear the text currently inside the results div.		
							$('#' + page + 'Show').html(html);
							//otherEl.erase('style');
							$('#' + page + 'Block').css('display', 'block');
							$('#t' + page).css('display', 'block');							
					}
				});*/
			
			showFooterPage(page, 'id=' + id)
            
            return false;
       }); 
    });
	
	function min_box(a){
		$(a).attr('class','noactive');		
	}
    
    //code for handling the increase the hits of news / articles
    $('a.readon_').each(function(){        
       $(this).bind("click", function(event){            
            
            var $id = $(this).attr('id');
            
            if($id){
                $article_id = $id.replace("article_readmore_link_", "");
                
                $.ajax({url:'index.php?option=com_content&task=update_hit&id=' + $article_id, 
                    
					success: function(html) {					   
						//Clear the text currently inside the results div.		
						//console.log(html);
                        //$(this).siblings("#comment").children('.comments-read-count').html(html + 'ggr');						
					}
				});
                
            }            
            
          event.preventDefault();
        });
    });

});