// when the DOM is ready...
$(document).ready(function () {

    var $panels = $('#slider .scrollContainer > div');
    var $container = $('#slider .scrollContainer');

	$('.panel').css('height', '360px');
	$('.scroll').css('height', 'auto');

	
	var panelArray = new Array();
	
	for(i=0;i<$('.panel').length;i++){
		  //$('.panel')[i].id  // this is the list of available elements
		  // Create some text  //  $("#divScroll").append(' '+ $('.panel')[i].id);
		  
		  panelArray[i] = $('.panel')[i].id;
		  //i++
	};
    // if false, we'll float all the panels left and fix the width 
    // of the container
    var horizontal = true;

    // float the panels left if we're going horizontal
    if (horizontal) {
        $panels.css({
            'float' : 'left',
            'position' : 'relative' // IE fix to ensure overflow is hidden
        });

        // calculate a new width for the container (so it holds all panels)
        $container.css('width', $panels[0].offsetWidth * $panels.length);
    }

    // collect the scroll object, at the same time apply the hidden overflow
    // to remove the default scrollbars that will appear
    var $scroll = $('#slider .scroll').css('overflow', 'hidden');

    // apply our left + right buttons
    $scroll
        .before('<img class="scrollButtons left" src="img/style/left.gif" />')
        .after('<img class="scrollButtons right" src="img/style/right.gif" />');

    // handle nav selection
    function selectNav() {
        $(this)
            .parents('ul:first')
                .find('a')
                    .removeClass('selected')
                .end()
            .end()
            .addClass('selected');
    }
	var thisItem = panelArray[0];
	var prevItem = panelArray[panelArray.length-1];
	var nextItem = panelArray[1];
	$("#divScroll").text(' '+thisItem);	
	
    $('#slider .navigation ul').find('a').click(selectNav); // "a" to  "li"

    // go find the navigation link that has this target and select the nav
    function trigger(data) {
        var el = $('#slider .navigation ul').find('li a[href$="' + data.id + '"]').get(0);
        selectNav.call(el);
		thisItem = data.id;
		$("#divScroll").text('li');
		
		for(Item in panelArray){
			if(panelArray[Item] == thisItem){
				//$("#divScroll").append(' '+panelArray[Item]);
				prevItem = panelArray[Item-1];
				Item++
				nextItem = panelArray[Item++];
			}; 
		};
	/// PLACE ARROW IF/THEN STATEMENT HERE
	if(prevItem == undefined){
		$("#divScroll").append('undefined:');
		$('.left').css('display', 'none');
	} else {
		$("#divScroll").append('defined:');
		$('.left').css('display', 'block');
	}
		$("#divScroll").append(' '+prevItem);
		$("#divScroll").append(' '+thisItem);
		$("#divScroll").append(' '+nextItem);
		
	if(nextItem == undefined){
		$("#divScroll").append(' :undefined');
		$('.right').css('display', 'none');
	} else {
		$("#divScroll").append(' :defined');
		$('.right').css('display', 'block');
	}
    }
	
    if (window.location.hash) {
        trigger({ id : window.location.hash.substr(1) }); 
    } else {
        $('.navigation ul li:first').click();// "a" to  "li"
    }

    // offset is used to move to *exactly* the right place, since I'm using
    // padding on my example, I need to subtract the amount of padding to
    // the offset.  Try removing this to get a good idea of the effect
    var offset = parseInt((horizontal ? 
        $container.css('paddingTop') : 
        $container.css('paddingLeft')) 
        || 0) * -1;


    var scrollOptions = {
        target: $scroll, // the element that has the overflow

        // can be a selector which will be relative to the target
        items: $panels,

        navigation: '.navigation ul li',// "a" to  "li"

        // allow the scroll effect to run both directions
        axis: 'xy',

        onAfter: trigger, // our final callback

        offset: offset,
		
		force:true,
        // duration of the sliding effect
        duration: 400,
		//do not cycle though items at first and last
		cycle:false,

        // selectors are NOT relative to document, i.e. make sure they're unique
        prev: 'img.left', 
        next: 'img.right',
		//event:'myevent',
        // easing - can be used with the easing plugin: 
        // http://gsgd.co.uk/sandbox/jquery/easing/
        easing: 'swing'
    };

    // apply serialScroll to the slider - we chose this plugin because it 
    // supports// the indexed next and previous scroll along with hooking 
    // in to our navigation.
    $('#slider').serialScroll(scrollOptions);

    // now apply localScroll to hook any other arbitrary links to trigger 
    // the effect
    $.localScroll(scrollOptions);

    // finally, if the URL has a hash, move the slider in to position, 
    // setting the duration to 1 because I don't want it to scroll in the
    // very first page load.  We don't always need this, but it ensures
    // the positioning is absolutely spot on when the pages loads.
    scrollOptions.duration = 1;
    $.localScroll.hash(scrollOptions);
	
	
	/// my clutter ------>
	
	
    $("#slider")
        .bind('mousewheel', function(event, delta) {
			
            var dir = delta > 0 ? 'up' : 'dn',
                vel = Math.abs(delta);
            $("#divScroll").text(dir + ' at ' + vel);
			if (dir == "up"){
				if (vel <= 3){
				 $('#slider .navigation ul').find('a[href$="#'+prevItem+'"]').click();
				} else {
				 $('.navigation ul li:first').click();
				}
			} else {
				if (vel <= 3){
				 $('#slider .navigation ul').find('a[href$="#'+nextItem+'"]').click();
				} else {
				 $('.navigation ul li:last').click();					
				}
			}
            return false;
        }); // "a" to  "li"

/*

*/

});
