// JavaScript Document (jQuery)

$(function() {
	function beforeSlide(curr, next, opts, fwd) {
		for(var i=0;i<opts.slideCount;i++) {
			$('#item-'+i).removeClass('activeSlide');
		}
		var nextId = next.id.replace(/\D*/,'');
		$('#item-'+nextId).fadeTo(250,0.5);
		$('#item-'+nextId).addClass('activeSlide').fadeTo(250,1);
	}

	var $container = $('#slideshow').cycle({ 
		fx:     'fade', 
		speed:   1500, 
		timeout: 6000,
		before: beforeSlide
	}); 
	var $navigation = $('ul#slideshow-navigation li ul');

	if ($container.children().length == $navigation.children().length) {
		$('ul#slideshow-navigation').mouseleave(function() {
			$container.cycle('resume');
		});
		// add ids and links to the slides
		$container.children().each(function(i) { 
			$(this).attr('id','slide-'+i);
			$(this).click(function() { 
				var url = $(this).metadata({type:'attr',name:'data'}).url;
				top.location = url;
			});
		});
		// initialize the slideshow navigation
		$navigation.children().each(function(i) {
			$(this).attr('id','item-'+i);																			
			$(this).click(function() { 
				var url = $(this).metadata({type:'attr',name:'data'}).url;
				top.location = url;
			});
			$(this).mouseenter(function() {				
				$container.cycle(i);
				$container.cycle('pause');
				return false; 
			});
		});
		$('#item-0').addClass('activeSlide');
	} else {
		console.log('Error: Navigation not initialized. Ensure that the number of navigation items matches the number of slides.');
	}
	
	
        var ie6 = $.browser.msie && parseInt($.browser.version) === 6 && typeof window['XMLHttpRequest'] !== "object";
        if(!ie6)
        {
            $(window).scroll(function () {
                var current = $(window).scrollTop();
                //console.log(current);
                //$("div#slideshow").removeClass('floating');
                var cssAddObj = {
                  'position' : 'fixed',
                  'right' : '50%',
                  'margin-right' : '-463px',
                  'top' : '20px'
                };
                var cssRemoveObj = {
                  'position' : 'relative',
                  'right' : '',
                  'margin-right' : '',
                  'top' : ''
                };

                if(current > 215){ $("div#slideshow").css(cssAddObj);}

                else if(current < 215) {$("div#slideshow").css(cssRemoveObj);}
            });
        }
});
