var old = all = 0;
var rotator;
var rotate = true;
var idx = 0;
var duration = 600;
       
$(document).ready(function() {

    all = $('.news_item').size();
    
    rotator = setInterval(spinNews, time);

    $('.news_item').click(function() {
        var curIdx = $('.news_item').index(this);
        showNews1(curIdx);
    });

    $('.newsbox_content_text').click(function() {
		if($('#newsboxcontent').hasClass('newsbox_content'))
		{
        	showNews(idx);
		}
    });      

    $('#showMap').click(function() 
    {       
        ShowLargeNews(false);

        var curIdx = old;
        $('#news_title').html($('#news_item_'+ curIdx +'_title').html());
        $('#news_text').html($('#news_item_'+ curIdx +'_text').html());
        $('.news_item:eq('+ curIdx +')').addClass('active');               
                        
        rotate = true;
        clearInterval(rotator);
        rotator = setInterval(spinNews, time);
        
        var execute = 'showInfoWindow(' + $('#news_item_'+ curIdx).html() + ')';
        setTimeout(execute, 5000);
    });  
    
    $('#expandMap').click(function() 
    { 
        rotate = false;
        clearInterval(rotator);        
       
        $("#newsbox").animate( { "height": "-=220px" }, duration, 
		function(){ 
			$('#newsMenu').attr('class', 'hudden'); 
			$("#newsboxcontent").attr('class', 'hudden'); 
			$('#showNewsd').attr('class', 'showNews');
		});
        		
        $("#map").animate({ "height": "+=220px" }, duration, function() {});
    });

    $('#showNews').click(function() 
    {        
        $('#newsboxcontent').attr('class', 'newsbox_content');
		$("#showNewsd").animate({ "height": "-=25px" }, duration, function() {
			$("#showNewsd").css('height', '100%');
			});
        $("#newsbox").animate( { "height": "+=220px" }, duration,
        function() { 
           	$('#newsMenu').attr('class', '');
		   	$('#showNewsd').attr('class', 'hudden');
			$("#newsbox").css('height', '100%');
        });
        		
        $("#map").animate({ "height": "-=220px" }, duration, function() {});
        
        rotate = true;
        clearInterval(rotator);
        rotator = setInterval(spinNews, time); 
    }); 
    
    if(all == 0)
    {                
        $("#map").css('height', '500px');         
    }
});

function ShowLargeNews(large)
{    
    if(large)
    {
        $("#newsboxcontent").animate( { "height": "+=255px" }, duration,
        function() { 
           $('#newsboxcontent').attr('class', 'newsbox_content_large');                        
        });
                
        $("#map").animate( { "height": "-=250px" }, duration,
        function() {
            $('#map').attr('class', 'hudden');
            $('#showMap').attr('class', 'showMap');
            $('#expandMap').attr('class', 'hudden');
        });
    }
    else
    {    
        $('#map').attr('class', 'map');        
                                        
        $("#newsboxcontent").animate( { "height": "-=255px" }, duration, 
        function() {
            $('#newsboxcontent').attr('class', 'newsbox_content');
        });
        
        $("#map").animate( { "height": "+=250px" }, duration, 
        function() {
            $('#showMap').attr('class', 'hudden');
            $('#expandMap').attr('class', 'expandMap');                        
        });
    }

    $('.newsbox_content_text').css('cursor', (large)?'none':'pointer');
}

function spinNews() {
  var current = (old + 1) % all;
  $('.news_item:eq('+ old +')').removeClass('active');
  $('.news_item:eq('+ current +')').addClass('active');
  idx = $('.news_item').index($('.news_item:eq('+ current +')'));
  
  $('#news_title').html($('#news_item_'+ idx +'_title').html());
  $('#news_text').html($('#news_item_'+ idx +'_text').html());
  $('#news_image > img').attr('src', $('#news_item_'+ idx +'_img').html());  
  old = current;  
}

function showNews(id)
{	
    if(rotate)
    {
        rotate = false;
        clearInterval(rotator);
        ShowLargeNews(true);        
    }    
    idx = id;

    $('#news_title').html($('#news_item_'+ id +'_title').html());
    $('#news_text').html($('#news_item_'+ id +'_textfull').html());
    $('#news_image > img').attr('src', $('#news_item_'+ id +'_img').html());
    
    changePano($('#news_item_'+ id +'_url').html());        
    changeInfoText($('#news_item_'+ id).html());    
    
    $('.news_item:eq('+ old +')').removeClass('active');
    $('.news_item:eq('+ id +')').addClass('active');
    old = id;    
}

function showNews1(id)
{    
	if($('#newsboxcontent').hasClass('newsbox_content'))
	{
		if(rotate)
    	{	
			clearInterval(rotator);
	        rotator = setInterval(spinNews, time);
		}
		
		idx = id;
		
		$('.news_item:eq('+ old +')').removeClass('active');
    	$('.news_item:eq('+ id +')').addClass('active');
	    old = id;    
	
		$('#news_title').html($('#news_item_'+ id +'_title').html());
		$('#news_text').html($('#news_item_'+ id +'_text').html());
		$('#news_image > img').attr('src', $('#news_item_'+ id +'_img').html());  	
		return;	
	}
	
	showNews(id);
}


function changePano(strPage)
{
    panoplace.location.href = strPage;
}    

function changeInfoText(id)
{    
    var url = "info.php?infoId=" + id;    
    $.ajax({
      url: url,
      success: function(data) { $('#info_container').html(''); $('#info_container').html(data); }
    });    
}

function showInfoWindow(id)
{    
    if(id > 0)
    {
        var marker = objects[id].marker;
        if(marker)
        {            
            marker.openInfoWindowHtml(objects[id].desc);
        }
    }
}
