var showing_id, rotation_list, rotations, rotation_intv;

function init_window()
{
	var main_page_position = $('#main_page_image_'+ showing_id).position();
	var home_page_image_position = $('#home_page_image').position();
	
	//$("[id^='coupon_slider_']").css({'top':main_page_position.top+1+'px', 'left':main_page_position.left+1+'px'});
	//$("[id^='main_page_image_']").css({'top':main_page_position.top+'px', 'left':main_page_position.left+'px'});
	//$("[id^='home_page_image_']").css({'top':home_page_image_position.top+'px', 'left':home_page_image_position.left+'px'});
	
	//$('.coupon_slider, .coupon_rotate').css({'position':'absolute', 'top':main_page_position.top+1+'px', 'left':main_page_position.left+1+'px'});
	//$('.main_page_rotate').css({'top':main_page_position.top+'px', 'left':main_page_position.left+'px'});
	//$('.main_page_header_rotate').css({'top':home_page_image_position.top+'px', 'left':home_page_image_position.left+'px'});
	
	$("div[id='left_home_container']").find("div[id='buttons_right']").each(function() {
		if ($(this).attr('button_id') != undefined){
			var position = $(this).position();
			//.css({'top':'px'}) 86(70) 136(120) 138//	alert(position.top)
			$(this).attr('title','').find('a').attr('title','')
			$(this).hover(
				function() { 
					this_h = $('#call_out_right_'+$(this).attr('button_id')).height()
					$('#call_out_right_'+$(this).attr('button_id')).css({'top':position.top-this_h+16+'px', 'left':position.left+100+'px'}).show() 
				},
				function() { $('#call_out_right_'+$(this).attr('button_id')).hide() }
			) 
		}
	});
	$('.home_bottom_link').hover(
		function() {$(this).addClass('hover')},
		function() {$(this).removeClass('hover')}
	)
	$("div[id='right_home_container']").find("div[id='buttons_right']").each(function() {
		if ($(this).attr('button_id') != undefined){
			var position = $(this).position();
			//alert(position.top)
			//.css({'top':'px'}) 86(70) 136(120) 138//	alert(position.top) 303(280) 289 296 position.left=1307
			$(this).attr('title','').find('a').attr('title','')
			$(this).hover(
				function() { 
					this_h = $('#call_out_right_'+$(this).attr('button_id')).height()
					this_w = $('#call_out_right_'+$(this).attr('button_id')).width()
			
					$('#call_out_right_'+$(this).attr('button_id')).css({'top':position.top-this_h+16+'px', 'left':position.left-this_w+23+'px'}).show() 
				},
				function() { $('#call_out_right_'+$(this).attr('button_id')).hide() }
			) 
		}
	});

	$('.home_bottom_link').each(function() {
		$(this).click(function(event){
			event.stopPropagation();
			if ($(this).attr('site_page_id') != undefined) {
				$.post("/global_includes/get_page_link.php", { SitePageID:$(this).attr('site_page_id') }, function(data){
				 document.location = $.trim(data)
					event.stopPropagation();
				});
					
			} else {
			}
		});
	});

	$('.home_bottom_image').each(function() {
		$(this).click(function(event){
			event.stopPropagation();
			if ($(this).attr('site_page_id') != undefined) {
				$.post("/global_includes/get_page_link.php", { SitePageID:$(this).attr('site_page_id') }, function(data){
				 document.location = $.trim(data)
					event.stopPropagation();
				});
					
			} else {
			}
		});
	});
	
}

$(window).resize(init_window);

$(document).ready(function(){
	init_window();

	var full_w = $('#coupon_rotate_'+showing_id).attr('full_w');
	$('#coupon_rotate_'+showing_id).css({'width':'0px','z-index':100}).show().delay(2000).animate({'width':full_w+'px'}, 1000 )

	rotation_intv = window.setInterval(function() { 
		if ( !rotations )
		{
			if ( !rotation_list )
			{
				clearInterval(rotation_intv);
				rotation_intv = null;
				return;
			} else
				rotations = rotation_list.split('|');
		}
		var next_id, full_w;
		var i, len = rotations.length;
		for ( i = 0; i < len; i++ )
		{
			if ( rotations[i] == showing_id )
			{
				if ( i == len - 1 )
					next_id = rotations[0];
				else
					next_id = rotations[i + 1];

				$('.main_page_header_rotate, .coupon_rotate, .tag_line_container').hide();

				$('#coupon_rotate_'+ next_id).css({width:'0px', 'z-index':rotations.length + 1}).show().delay(3000).animate({width:$('#coupon_rotate_'+ next_id).attr('full_w')+'px'}, 1000);
				$('#main_page_image_'+ next_id).css({'z-index':rotations.length}).fadeIn('slow');
				$('#main_page_header_'+ next_id).fadeIn('slow');
				$('#main_page_text_'+ next_id).fadeIn('fast');
				
				$('#main_page_image_'+ showing_id).fadeOut('slow');
				
				showing_id = next_id;

				break;
			}
		}
	}, 18000);
});

