/* 
 * Copyright (c) 2008 Ralph One
 * $LastChangedDate: 2008-02-20 4:00:00 -0600 (Wed, 20 Feb 2008) $
 *
 * Version: 1.0b
 *
*/


var hl_idx = 0;
var newsid_set = 0;

jQuery.fn.r1_headLiner = function(opts){

	opts = jQuery.extend({
		ajaxUrl: 'home/news',	
		ajaxUrlHeadliner: 'home/headliner',	
		newsPerPage: 5,
		newsScrollElement: '#news-scroll-content',	
		headelineElement: '.headline-container',
		//textElement: '.text-container',
		artistElement: '#artist-gallery',
		loaderElement: '#news-loader',
		defaultTextElementHeight: 100
	},opts||{});
			
	return this.each(function() {
					
		loadDefaultHeadliner = function(){
			
			// load default 0										
			nid = $(opts.newsScrollElement + ' li').eq(0).attr('id');			
			refreshHeadliner(nid);					
			$(opts.newsScrollElement).find('li').eq(0).addClass('news-link-selected'); 

		}
	
		bindMouseEvents = function(){

			$(opts.newsScrollElement + ' li').hover(
				function () {					
		        	$(this).addClass('news-link-hover'); 					
				}, 
				function () {
		            $(this).removeClass('news-link-hover'); 
				}
			);

			$(opts.newsScrollElement + ' li').click(function(){
				
				// remove class from old hl_idx.	
		        $(opts.newsScrollElement + ' li').eq(hl_idx).removeClass('news-link-selected'); 	
		        		        	
				// update hl_idx
				hl_idx = $(opts.newsScrollElement).find('li').index(this);															
				
				refreshHeadliner($(this).attr('id'));												
	
		        $(this).removeClass('news-link-hover').addClass('news-link-selected'); 

			});							
		}

		refreshHeadliner = function(newsid){
						
			if(newsid != newsid_set){							
				
				newsid_set = newsid;
				
				$(opts.headelineElement).ajaxStart(function() {
					$(this).html('<div class="image-container"><img src="media/images/headliner-loading.gif"></div>');				
				});
	
				$.ajax({
					url: opts.ajaxUrlHeadliner + '/' + newsid,
					success: function(data) {
						$(opts.headelineElement).hide().html(data).fadeIn();						
					}		
				});
			}	
															 			
		}
		
		knownNewsCallback = function (page_id, jq){
						
			$.ajax({
				url: opts.ajaxUrl,
				type: 'POST',
				data: { 'limit': opts.newsPerPage, 'page': (page_id*opts.newsPerPage) },
				beforeSend: function(){
					$(opts.newsScrollElement).hide();						
					$(opts.loaderElement).show();						
				},
				success: function(ndata) {
					
					hl_idx = 0; // reset 
					
					$(opts.loaderElement).fadeOut('fast', function(){

						$(opts.newsScrollElement).html(ndata).fadeIn('normal', function(){
							loadDefaultHeadliner();				
							bindMouseEvents();			
						});		
												
					});
				}		
			});
					  						
	    }
	    
	    loadDefaultHeadliner();
		bindMouseEvents();
		
						
	});
	
};


