var timer;
var current_slide = 1;
var slide_time = 5000;
var slide_trans_time = 2000;
var slide_count = 0;
var max_slides = 10;
var transitioning = false;
var go_to = null;
var trans_to = null;

//var image_width = null;
//var image_height = null;

$navigation_ul = null;
$right_ul = null;

$(document).ready(function() {

	//set right ul
	$right_ul = $("#home div.right ul");
						   
	//set up slides and navigation
	if ($right_ul.children("li").size() > 1) {
		
		//for adjusting image ul width
		//var ul_width = 0;		
		
		//for adjusting navigation ul width
		var nav_ul_width = 0;
							
		//create navigation icons
		$("#footer1").append('<div class="navigation"></div>');
		$navigation = $("#footer1 div.navigation");
		$navigation.append('<ul></ul>'); //<li class="left"><img src="styles/images/home_arrow_left_selected.png" /></li>
				
		//set navigation ul
		$navigation_ul = $navigation.children("ul");
				
		//add to nav ul width
		//nav_ul_width += $navigation_ul.children("li.left").width();
		
		$right_ul.children("li").each(function(index) {
			
			if (index < max_slides) {
			
				index++;
				slide_count++;
			
				//if (image_width == null)
					//image_width = $(this).children("img").width();
				//if (image_height == null)
					//image_height = $(this).children("img").height();
				
				var this_class = getNumberText(index);
				
				//add class to li
				$(this).attr('class',this_class);
				
				//edit position so they will all be in the same place
				$("#home div.right li, #home div.right img").css({ 'position':'absolute','left':'0','top':'0' });
				
				//allow links to show
				$("#home div.right a").css({ 'display':'block' });
				
				// if not current slide, then set opacity to 0
				if (index != current_slide)  {
					
					if ((jQuery.browser.msie) && (jQuery.browser.version == 8))  {
					  $(this).hide();
					}
					else {
						$(this).css({ 'opacity':'0.0' });
					}
					
				}
					
					
				//add to image ul width
				//ul_width += $(this).width();
			
				//add dot to navigation
				$navigation_ul.append('<li class="' + this_class + '"><img src="styles/images/dot_selected.png" /></li>');
				
				//add selected bg to first dot
				if (index == 1) {				
					$navigation_ul.children("li." + this_class).children("img").css({ 'display':'block','opacity':'1.0' });
				}
				else {				
					$navigation_ul.children("li." + this_class).children("img").css({ 'display':'block','opacity':'0.0' });
				}
				
				//add to nav ul width
				nav_ul_width += $navigation_ul.children("li." + this_class).width();
				
			}
			else {
				$(this).remove();
			}
											   
		});
		
		//add right arrow to navigation
		//$navigation_ul.append('<li class="right"><img src="styles/images/home_arrow_right_selected.png" /></li>');
		
		//add to nav ul width
		//nav_ul_width += $navigation_ul.children("li.right").width();
		
		//hide left selected arrow
		//$navigation_ul.children("li.left").children("img").css({ 'display':'block','opacity':'0.0' });
		
		//hide right selected arrow
		//$navigation_ul.children("li.right").children("img").css({ 'display':'block','opacity':'0.0' });
		
		//set nav ul width
		$navigation_ul.css({ 'width':nav_ul_width+'px' });
		
		//set image ul width
		//$right_ul.css({ 'width':ul_width+'px' });
		
		//set loop timer
		timer = setTimeout(loopSlides, slide_time);
		
		
		
		//setup nav hover
        $navigation_ul.children("li").hover(

            function() {
				
				//if (transitioning == false) {
				
                	$(this).css({ 'cursor': 'pointer' });
					$(this).children("img").css({ 'opacity':'1.0' });					
					
				//}
                
            },
            function() {

				var this_digit = getDigit($(this).attr('class'));
	
				if (this_digit != current_slide) {
	
					$(this).css({ 'cursor': 'default' });
					$(this).children("img").css({ 'opacity':'0.0' });
	
				}
				else {
	
					$(this).css({ 'cursor': 'default' });
					$(this).children("img").css({ 'opacity':'1.0' });
	
				}

            }

        );
		
		$navigation_ul.children("li").click(function() {

            var this_digit = getDigit($(this).attr('class'));

            if (this_digit != current_slide) {

                if ((transitioning) && (trans_to == null)) {
                    trans_to = this_digit;
                }
                else if ((transitioning == false) && (go_to == null)) {
                    go_to = this_digit;
                    goToSlide(this_digit);
                }

            }

        });
		
		
	}
	
});

function goToSlide(go_to_slide) {

	transitioning = true;

    //clear timeout, will restart after slide change
    clearTimeout(timer);

    //get current and go_to slide
    var current_class = getNumberText(current_slide);
    var go_to_class = getNumberText(go_to_slide);
    
    // 1) move go_to slide before/after current slide
    $go_to = $right_ul.children("li." + go_to_class);	
	//$right_ul.children("li." + current_class).after( $go_to );
		
		
    // 2) loop each li (except go_to and current) and move to beginning/end of line
    $right_ul.children("li").each(function(index) {

        var this_class = $(this).attr('class');
        var digit = getDigit(this_class);

        if ((digit != current_slide) && (digit != go_to_slide)) {

            var prev_slide = 0;
			prev_slide = digit - 1;
           	if (prev_slide < 1) {
           		prev_slide = slide_count;
			}
			
            //then move to the end of the line (because if we put after current, it would get in the way of go_to)
            if (prev_slide == current_slide) {

                $this_slide = $right_ul.children("li." + this_class);                
				//$right_ul.append($this_slide);

            }
            //move to after previous slide
            else {

                var prev_class = getNumberText(prev_slide);

                $this_slide = $right_ul.children("li." + this_class);                
				//$right_ul.children("li."+ prev_class).after($this_slide);

            }

        }

    });

    //start loop
    loopSlides();
    
}

function loopSlides() {

	transitioning = true;

    //prev slide
    var prev_slide = current_slide - 1;
   	if (prev_slide < 1) {
    	prev_slide = slide_count;
	}
	
    var prev_class = getNumberText(prev_slide);
        
    //current slide
    var current_class = getNumberText(current_slide);
    $this_slide = $right_ul.children("li." + current_class);
    
    //next slide
    var next_slide = current_slide + 1;
	
	if (go_to != null) {
        next_slide = go_to;
        go_to = null;        
    }
	else if (trans_to != null) {
        next_slide = trans_to;
        trans_to = null;        
    }
    
	if (next_slide > slide_count) {
   		next_slide = 1;
    }
	else if (next_slide < 1) {
		next_slide = slide_count;
	}
    
    var next_class = getNumberText(next_slide);
	
	
	if ((jQuery.browser.msie) && (jQuery.browser.version == 8))  {
	
		//hide current slide
		$right_ul.children("li." + current_class).hide();
		
		//show next slide
		$right_ul.children("li." + next_class).show();
		
		//hide current dot
		$navigation_ul.children("li." + current_class).children("img").hide();
		
		//show next dot
		$navigation_ul.children("li." + next_class).children("img").show();
		
		transDone(next_slide);
		
	}
	else {
			
		//fade out current slide
		$right_ul.children("li." + current_class).animate({
			opacity: "0.0"
		}, slide_trans_time, function() {
			
		});
		
		//fade in next slide
		$right_ul.children("li." + next_class).animate({
			opacity: "1.0"
		}, slide_trans_time, function() {
		
			transDone(next_slide);
						
		});
		
		//fade out current dot
		$navigation_ul.children("li." + current_class).children("img").animate({
			opacity: '0.0'
		}, {
			duration: slide_trans_time,
			complete: function() {
	
			}
		});
		
		//fade in next dot
		$navigation_ul.children("li." + next_class).children("img").animate({
			opacity: '1.0'
		}, {
			duration: slide_trans_time,
			complete: function() {
	
			}
		});
       
	}

}

function transDone(next_slide) {
	
	transitioning = false;
	
	//change current_slide
	current_slide = next_slide;
			
	if (trans_to != null) {
		goToSlide(trans_to);			
	}
	else {
		//set timeout for next loop
		timer = setTimeout(loopSlides, slide_time);
	}
			
}

function getNumberText(index) {
	
	if (index == 0)
		return "zero";
	else if (index == 1)
		return "one";
	else if (index == 2)
		return "two";
	else if (index == 3)
		return "three";
	else if (index == 4)
		return "four";
	else if (index == 5)
		return "five";
	else if (index == 6)
		return "six";
	else if (index == 7)
		return "seven";
	else if (index == 8)
		return "eight";
	else if (index == 9)
		return "nine";
	else if (index == 10)
		return "ten";
	else
		return null;

}

function getDigit(text) {

    if (text == "zero")
        return 0;
    else if (text == "one")
        return 1;
    else if (text == "two")
        return 2;
    else if (text == "three")
        return 3;
    else if (text == "four")
        return 4;
    else if (text == "five")
        return 5;
    else if (text == "six")
        return 6;
    else if (text == "seven")
        return 7;
    else if (text == "eight")
        return 8;
    else if (text == "nine")
        return 9;
    else if (text == "ten")
        return 10;
	else
		return null;

}
