jQuery(function($) {
	$("#homepage_marquee").css("visibility", "visible");
																
  if(jQuery.support.opacity){       
    ////////////////////////////////////////////////////////////////////////////////////////////////////////
    // VARIABLES
    ////////////////////////////////////////////////////////////////////////////////////////////////////////  
    
    //slider
    var animation_length = 400;
    var animation_type = 'easeOutSine';
    
    //autoplay
    var autoplay_interval_length = 6000;
    var autoplay;
		
		//animation
		var animation_in_progress = false;
    
    //general
    var doc_width = $(document).width();
    var current_slide = $("#slides li").eq(3);
    var image = new Image();
    
    ////////////////////////////////////////////////////////////////////////////////////////////////////////
    // FUNCTION: INITIALIZE SLIDES
    ////////////////////////////////////////////////////////////////////////////////////////////////////////
    function initialize_slides(){
    
			$("#slides li:not(.options)").each(function (i) {
					
					//assign arbitrary data to object      
					$(this).data({
					title: $(this).find("h1").text(),
					href: $(this).find("a:first").attr("href"),
					image: $(this).find("a:last").attr("href"),
					categories: $(this).attr("class")
					});
					
					//position slide and strip href from thumbnail
					$(this).attr("id", "slide_" + i);
					$(this).css("left", i * 126);
					$(this).find("a:first").attr("href", "javascript: void(0);").append('<img src="images/templates/homepage/marquee_slide_border.png" class="border" />');
					
					//add onclick event
					$(this).click(function() {
						//tracking
						//pageTracker._trackEvent('Home Slideshow', 'Click', $(this).data().title);
					
						//check to see if slide is currently active
						if($(this).attr("id") != $(current_slide).attr("id")){
								
								//load image
								load_slide($(this).data().image, $(this).data().href);
								;
						}
					
					});
					
					//add mouseenter event
					$(this).mouseenter(function() {
					if($(this).attr("id") != $(current_slide).attr("id")){
							$(this).find(".border").fadeIn(400);
					}
					});
					
					//add mouseleave event
					$(this).mouseleave(function() {
					if($(this).attr("id") != $(current_slide).attr("id")){
							$(this).find(".border").hide();
					}
					});
					
			});
			
			$("#slides").mouseover(function(){
				clear_interval();
			});
			
			$("#slides").mouseout(function(){
				clear_interval();
			});
        
    }


    
    ////////////////////////////////////////////////////////////////////////////////////////////////////////
    // FUNCTION: LOAD INTERFACE
    ////////////////////////////////////////////////////////////////////////////////////////////////////////
    function load_interface(){
    
    //load slide
    $("#homepage_marquee").prepend('<a id="slide"></a>');
    $("#slides").wrap('<div class="controls" />');
    $("#slides").wrap('<div id="slides_tray" />');
    
    //load mask
    $("#slide").append("<img src=\"images/templates/homepage/marquee_mask.png\" id=\"slide_mask\" />");
    $("#slide_mask").css("z-index","100");
    
    //resize slide
    $("#slide").css("width", Math.min(1095, doc_width/2 + 1095/2) + "px");
    
    $(window).resize(function() {
        $("#slide").css("width", Math.min(1095, doc_width/2 + 1095/2) + "px");      
    });
    
    //load buttons
    $("#homepage_marquee .controls").prepend('<a href="javascript: void(0);" id="homepage_marquee_prev_btn" title="Previous">Previous</a>');
    $("#homepage_marquee .controls").prepend('<a href="javascript: void(0);" id="homepage_marquee_next_btn" title="Next">Next</a>');
    
    }
    
    
    
    ////////////////////////////////////////////////////////////////////////////////////////////////////////
    // FUNCTIONS: AUTOPLAY FUNCTIONALITY
    ////////////////////////////////////////////////////////////////////////////////////////////////////////
    
    function start_interval(){
			clear_interval(autoplay);
      autoplay = setInterval(next_slide, autoplay_interval_length);
    }
    
    function clear_interval(){
      clearInterval(autoplay);
    }
    
    function restart_interval(){
		  start_interval();
    }
    
    
    
    ////////////////////////////////////////////////////////////////////////////////////////////////////////
    // FUNCTION: NEXT SLIDE
    ////////////////////////////////////////////////////////////////////////////////////////////////////////
    function next_slide(){
			animation_in_progress = true;
    
    $(current_slide).find(".border").hide();
    
    //animate slides to left
    $('#slides').animate({
    left: '-=126',
    }, animation_length, animation_type, function() {
			
			  animation_in_progress = false;
        
        //oncomplete, get last slide's position
        var last_x = $('#slides li:last').position().left;
        
        //duplicate first slide and move it to the last position
        $('#slides li:first').appendTo("#slides");
        $('#slides li:last').css("left",(last_x + 126) + "px");
        
        //set current slide to the last slide
        current_slide = $(current_slide).next();
        $(current_slide).find(".border").fadeIn(400);
        load_slide($(current_slide).data().image, $(current_slide).data().href);
        
    });
    }
    
    
    
    ////////////////////////////////////////////////////////////////////////////////////////////////////////
    // FUNCTION: PREVIOUS SLIDE
    ////////////////////////////////////////////////////////////////////////////////////////////////////////
    function previous_slide(){
			
			animation_in_progress = true;
    
			$(current_slide).find(".border").hide();
			
			var last_x = $('#slides li:first').position().left;
			
			//duplicate last slide and move it to the first position
			$('#slides li:last').prependTo("#slides");
			$('#slides li:first').css("left",(last_x - 126) + "px");
									
			//animate slides to right      
			$('#slides').animate({
				left: '+=126',
				}, animation_length, animation_type, function() {
					animation_in_progress = false;  
			});
			
			//set current slide to the last slide
			current_slide = $(current_slide).prev();
			$(current_slide).find(".border").fadeIn(400);
			load_slide($(current_slide).data().image, $(current_slide).data().href);
    
    }
    
    
    
    ////////////////////////////////////////////////////////////////////////////////////////////////////////
    // FUNCTION: LOAD SLIDE
    ////////////////////////////////////////////////////////////////////////////////////////////////////////
    function load_slide(image_src, image_href){
    //load image
    $(image).attr("src", image_src);
    $("#slide").attr("href", image_href);
    }
    
    
    
    ////////////////////////////////////////////////////////////////////////////////////////////////////////
    // FUNCTION: IMAGE ONLOAD
    ////////////////////////////////////////////////////////////////////////////////////////////////////////
    $(image).load(function(){  
    $("#slide").append('<span style="display: none;"><img src="' + $(image).attr("src") + '" /></span>');    
    
    $("#slide span:last").fadeIn('slow', function() {
    if($("#slide span").length > 1){
        $("#slide span:first").remove();
    }
    });
    
    });
    
    
    ////////////////////////////////////////////////////////////////////////////////////////////////////////
    // FUNCTION: ACTIVATE CONTROLS
    ////////////////////////////////////////////////////////////////////////////////////////////////////////
    function activate_controls(){
    //next button
    $('#homepage_marquee_next_btn').click(function() {
				//tracking
				
				if(animation_in_progress == false){
					//pageTracker._trackEvent('Home Slideshow', 'Click', "(arrow) Right");
					restart_interval();
					next_slide();
				}

    });
    
    $('#homepage_marquee_next_btn').mouseenter(function(){
        clear_interval();
    });
    
    $('#homepage_marquee_next_btn').mouseleave(function(){
        start_interval();
    });
    
    //prev button
    $('#homepage_marquee_prev_btn').click(function() {
				//tracking
				if(animation_in_progress == false){
					//pageTracker._trackEvent('Home Slideshow', 'Click', "(arrow) Left");
					restart_interval();
					previous_slide();
				}
    });
    
    $('#homepage_marquee_prev_btn').mouseenter(function(){
        clear_interval();
    });
    
    $('#homepage_marquee_prev_btn').mouseleave(function(){
        start_interval();
    });
    
    //slide mouse events
    $('#slide').mouseenter(function(){
        clear_interval();
    });
    
    $('#slide').mouseleave(function(){
        start_interval();
    });
    }
    
    
    
    ////////////////////////////////////////////////////////////////////////////////////////////////////////
    // INITIALIZE
    ////////////////////////////////////////////////////////////////////////////////////////////////////////
    initialize_slides();
    load_interface();
    activate_controls();
    start_interval();
    
    $(current_slide).find(".border").show()
    load_slide($(current_slide).data().image, $(current_slide).data().href);
  }
});
