var imgwidth = 0;

var anim = new Object();


var fxs = "";

var expanding = false;

var shrinking = false;

	jQuery(function(){

		
		var plusdiv = "<div class='plusbar' style='display:none; height:140px'><div class='plus'>+</div></div>";
		jQuery('#headlines .headline').find('h1').before(plusdiv);
		jQuery('#headlines .headline').css('text-decoration','none');	//love ie
		
		jQuery('#headlines .headline').each(function(){
			
			if(jQuery(this).find('.pics_section .image_block').length == 2 ){
				jQuery(this).find('.pics_section').addClass('two');
			}
			else{
				jQuery(this).find('.pics_section').addClass('three');
			}
			
		});
		
		jQuery('#headlines .headline .links_section a').append(" <span></span>");
		
		
		var ss = document.styleSheets;
		var home_style = ss[ss.length-1];
		var dark_color = jQuery('#footernav a').css('color');
		var light_color = jQuery('#navblock').css('backgroundColor');
		
		
				
			css_init = {
				'width':'360px',
				'height':'140px',
				'left':imgwidth+'px',
				'marginLeft':'0px',
				'marginTop':'0'
			};
			

			css_hover = {
				'width':'360px',
				'height':'140px',
				'left':imgwidth+'px',
				'marginLeft':'0px',
				'marginTop':'0px'
			};
			
			css_exp = {
				'width':'740px',
				'height':'323px',
				'right':'75px',
				'marginLeft':'0',
				'marginTop':'4px'
			};
			
			
			
			css_init_color = {
				'backgroundColor':'white'
			};
			
			css_focus_color = {
				'backgroundColor':dark_color
			};
		
			

		if(home_style.addRule){
			
			home_style.addRule("#headlines .headline.focused","background-color:"+dark_color, home_style.rules.length);
			home_style.addRule("#headlines .headline.focused","border-top:"+light_color, home_style.rules.length);
			home_style.addRule("#headlines .headline.focused","border-bottom:"+light_color, home_style.rules.length);
			
			home_style.addRule("#headlines .headline h2","color:"+light_color, home_style.rules.length);
			
			home_style.addRule("#headlines .headline .pics_section .image_block","background-color:"+light_color, home_style.rules.length);
			home_style.addRule("#headlines .headline .plusbar","background-color:"+light_color, home_style.rules.length);
		}
		else{
			cssNode = document.createElement('style');
			cssNode.type = 'text/css';
			cssNode.rel = 'stylesheet';
			cssNode.media = 'screen';
			cssNode.title = 'dynamicSheet';
			document.getElementsByTagName("head")[0].appendChild(cssNode);
			
			var new_style = ss[ss.length-1];
			new_style.insertRule("#headlines .headline.focused{background-color:"+ dark_color +";}",0);
			new_style.insertRule("#headlines .headline.focused{border-color:"+ light_color +";}",0);
			
			new_style.insertRule("#headlines .headline h2{color:"+ light_color +";}",0);
					
			new_style.insertRule("#headlines .headline .pics_section .image_block{background-color:"+ light_color +";}",0);	
			new_style.insertRule("#headlines .headline .plusbar{background-color:"+ light_color +";}",0);
		}
		
		
		jQuery.fn.extend({
			
		init_state: function(speed){
			fxs += "(" + jQuery(this).attr('id') +"|init_state)";
			jQuery(this).removeClass('focused');
			jQuery(this).removeClass('extended');
			var pbar = jQuery(this).find('.plusbar');
			var headers = jQuery(this).find('h1, h2');
			
			pbar.hide();
			
			jQuery(this).stop();
			headers.stop();
			css_init_extend = jQuery.extend(true,{}, css_init);
			if(jQuery(this).hasClass('mini')){
				css_init_extend.height = '60px';
			}
			jQuery(this).animate(css_init_extend, speed);
			jQuery(this).css('z-index', '2');

			headers.animate({'marginLeft':'0'},speed);
			
			jQuery(".shadow").hide();
			jQuery("span.shadow").remove();
		
		},
		
		
		init_slide: function(){
			fxs += "(" + jQuery(this).attr('id') +"|init_slide)";
			jQuery(this).stop(true,true);
			jQuery(this).find('h1, h2').stop(true,true);
			
			jQuery(this).addClass('focused');
			
			var css_hover_extend =	jQuery.extend(true,{'z-index':( 50 + jQuery(this).hasClass('mini') ) }, css_hover);
			if(jQuery(this).hasClass('mini')){
				css_hover_extend.height = '60px';
				css_hover_extend.marginTop = '2px';
			}
			

			jQuery(this).css(css_hover_extend);
			jQuery(this).find('.plusbar').height(jQuery(this).height()).css('display','inline');	
			
			jQuery(this).css('margin-left','-20px').animate({'marginLeft':'0px'},150);
			jQuery(this).find('h1').css('margin-left','15px').animate({'marginLeft':'30px'},200);
			jQuery(this).find('h2').css('margin-left','15px').animate({'marginLeft':'30px'},200);

			
			/*
			var pos = $(this).position();
			$('.shadow').css({'top': ((pos.top)-30)+'px', 'left':(575-37)+'px','display':'inline-block','z-index':'3','position':'absolute', 'width':'446px', 'height':'160px'});
			$(document).pngFix();
			*/
			
		},
		
		expand: function(top, speed){
			fxs += "(" + jQuery(this).attr('id') +"|expand)";
			var pbar = jQuery(this).find('.plusbar');
			
			pbar.css('height','100%');
			pbar.find('.plus').text('-');
			
			if(jQuery(this).hasClass('mini')){
				jQuery(this).find('h2').fadeIn(speed/2);
				jQuery(this).removeClass('mini');
			}
			
			expanding = true;
			jQuery(this).addClass('expanded');
			var cur_id = jQuery(this).attr('id');
			var css_exp_extend = jQuery.extend(true, {'top':top}, css_exp);
			jQuery(this).animate(css_exp_extend, speed, 'swing',function(){
				
				jQuery("#headlines .headline:not('#"+cur_id+"')").stop(true,true);
				jQuery("#headlines .headline h2").stop(true,true);
				
				
				var pics = jQuery(this).find('.pics_section');
				var links = jQuery(this).find('.links_section');
				jQuery(this).find('.body_text').show();
					pics.slideDown(300, function(){
						links.slideDown(300);
					});

				expanding = false;
			});
			
			jQuery(this).find('h1,h2').animate({'marginLeft':'30px'},speed/4);
			jQuery(this).find('h2').css('display','none');
	
		},
		
		
		shrink_max: function(top, speed){
			fxs += "(" + jQuery(this).attr('id') +"|shrink_max)";
			jQuery(this).find('.plusbar .plus').text('+');	
			
			jQuery(this).find('.body_text').hide();
			jQuery(this).find('.pics_section').stop(true,true).hide();
			jQuery(this).find('.links_section').stop(true,true).hide();
			
			var extend = {'top':top};
			var css= jQuery.extend(true, extend , css_hover);

			var cur_id = jQuery(this).attr('id');
			shrinking = true;
			jQuery(this).animate(css, speed,'swing', function(){
				
				jQuery(this).removeClass('expanded');
				
				if(!jQuery(this).hasClass('hovering')){
					jQuery(this).init_state(0);
				}	
				
				jQuery("#headlines .headline:not('#"+cur_id+"')").stop(true,true);
				jQuery("#headlines .headline h2").stop(true,true);
				

				shrinking = false;
			});
			
			jQuery(this).find('h1,h2').animate({'marginLeft':'30px'},speed/4);
				
		},
		
		shrink_min: function(top){
			fxs += "(" + jQuery(this).attr('id') +"|shrink_min)";
			
			var pbar = jQuery(this).find('.plusbar');
			pbar.find('.plus').text('+');
			pbar.hide();
			
			jQuery(this).find('.body_text').hide();
			jQuery(this).find('.pics_section').stop(true,true).hide();
			jQuery(this).find('.links_section').stop(true,true).hide();
			
			jQuery(this).find('h2').hide();
			jQuery(this).find('h1, h2').css('margin-left','0px');
			
			jQuery(this).removeClass('focused');
			jQuery(this).removeClass('expanded');
			jQuery(this).addClass('mini');
			
			css_init_extend = jQuery.extend(true,{'top':top}, css_init);
			css_init_extend.height = '60px';
			jQuery(this).css(css_init_extend);
			jQuery(this).css('z-index', '2');
			
		},
		
		minimize: function(top,speed){
			
			fxs += "(" + jQuery(this).attr('id') +"|minimize)";
			
			jQuery(this).stop(false, true);
			jQuery(this).find('h1, h2').stop(false,true);
			
			jQuery(this).removeClass('expanded');
			jQuery(this).addClass('mini');
			css_init_extend = jQuery.extend(true,{'top':top}, css_init);
			css_init_extend.height = '60px';
			jQuery(this).animate(css_init_extend,speed,'swing',function(){
				
			
			});
			jQuery(this).find('h2').fadeOut(speed/2);
			
		},
		
		maximize: function(top,speed){
			
			fxs += "(" + jQuery(this).attr('id') +"|maximize)";	
			
			jQuery(this).stop(false, true);
			jQuery(this).find('h1, h2').stop(false,true);
			
			jQuery(this).removeClass('mini');
			css_init_extend = jQuery.extend(true,{'top':top}, css_init);
			jQuery(this).animate(css_init_extend,speed,'swing',function(){
				
			
			});
			jQuery(this).find('h2').fadeIn(speed);
		}
		
		});
		
		jQuery('.index_hover_action').hover(
						function()
						{
							jQuery('.index_hover_show').show();	
						}
					);

		jQuery('#headlines .headline').hover(
			function(){	
				jQuery(this).addClass('hovering');
				
				if(!jQuery(this).hasClass('expanded')){

					jQuery(this).init_slide();
				}
			
			},
			function(){
				jQuery(this).removeClass('hovering');
				
				if(!jQuery(this).hasClass('expanded')){
					
					jQuery(this).init_state(0);
				}
		
			}
		);
		
		jQuery('#headlines .headline').click(function(){
			
			if(expanding || shrinking) return;
			
			var pbar = jQuery(this).find('.plusbar');
			
			var temp = jQuery(this).attr('id').split('_');
			var exp_no = temp[1] -1;
			if(!jQuery(this).hasClass('expanded')){		
				var there_is_expanded =  jQuery('#headlines .expanded').length;
				jQuery('#headlines .headline').each(function(i){
					var top = -5;
					if(i > exp_no ){
						top += (i-1)*56 + 328;
					}
					else{
						top += i*56;
					}
					
					if(i == exp_no){ 
						jQuery(this).expand(top, 500);
					}
					else{
						if(jQuery(this).hasClass('expanded')){
							if(there_is_expanded){
								jQuery(this).shrink_min(top);
							}
							else{
								jQuery(this).shrink_max(top,500);
							}
						}
						else{
							jQuery(this).minimize(top, 500);
						}
					}
				
				});
			}else{
			var pbar = jQuery(this).find('.plusbarx');
			pbar.click();
/*
				$('#headlines .headline').each(function(i){
					var top = 6 + i*106;
					
					if(i == exp_no){ 
						$(this).shrink_max(top,500);
					}
					else{
						$(this).maximize(top, 500);
					}

				});
*/
			}
			
		});
		
		jQuery('#headlines .headline .plusbar').live('click', function(){
			if(expanding || shrinking) return;
			
			var temp = jQuery(this).closest('.headline').attr('id').split('_');
			var exp_no = temp[1] -1;
		
			jQuery(".headline").find('h2').css('display','block');
			jQuery('#headlines .headline').each(function(i){
				var top = i*140;
				
				if(i == exp_no){ 
					jQuery(this).shrink_max(top,500);
				}
				else{
					jQuery(this).maximize(top, 500);
				}

			});
			
		});
		
		jQuery('#headlines .headline .plusbarx').click(function(){
			if(expanding || shrinking) return;
			
			var temp = jQuery(this).closest('.headline').attr('id').split('_');
			var exp_no = temp[1] -1;
		
			jQuery(".headline").find('h2').css('display','block');
			jQuery('#headlines .headline').each(function(i){
				var top = i*140;
				
				if(i == exp_no){ 
					jQuery(this).shrink_max(top,500);
				}
				else{
					jQuery(this).maximize(top, 500);
				}

			});
			
		});
		
		jQuery('.image_block').hover(
				function(){
					//$(this).animate({'backgroundColor':'#aaaaaa'},200);
				},
				function(){
					//$(this).stop(true,true);
					//$(this).css('background-color','');
					
				}
				);

		

	});
	
