$(function() {
// flash zindex fix
$("td object").prepend("<param name='wmode' value='transparent'>").hide().show();
$("td embed").attr({wmode: "transparent"}).hide().show();		
$("td iframe").hide().show();

$.scrollTo.defaults.axis = 'xy';
$.scrollTo( 0 );//reset the screen to (0,0)

var current = 0; // current counter
var next = $('td').size(); // number of items on page
var next_url = $("#right a").attr("href"); // next page
var prev_url = $("#left a").attr("href"); // prev page

// set what we are scrolling for ie workaround
var scrollTarget = "";
if (jQuery.browser.msie) {
  if(parseInt(jQuery.browser.version) == 6) {
     var scrollTarget = "#stuff";
  }
} 

// padding variable
var screenPadding = $(window).width() - ($("td:last").outerWidth()-30);  
//add padding
$("td:last").css({paddingRight: screenPadding});

// add right hand margin
$(window).resize(function(){
	var screenPadding = $(window).width() - ($("td:last").outerWidth()-30);    	  	
	$("td:last").css({paddingRight: screenPadding});
	
	 jQuery.each(jQuery.browser, function(i, val) {
  	if(i=="msie" && jQuery.browser.version.substr(0,3)=="6.0") {
	  
	var windowHeight = $(window).height();
	$("div#left a").css("marginTop",(windowHeight/2) + "px").css("background-position","top right");
	$("div#right a").css("marginTop",(windowHeight/2) + "px").css("background-position","top left");	
	$("div#header").width($(window).width()-16);
	$("div#stuff").width($(window).width());
	$("div#stuff").height(windowHeight);
 	 }
  });
	
});

//intially hide left control
if (prev_url == null) {$("#left a").hide();}

// post controls
$("a.top-button").css({display:'inline'});
$("a.next-button").css({display:'inline'});
$("a.next-button").click(function(e){$(scrollTarget).scrollTo($('#' + $(this).attr("title")), 800, {axis:'yx', queue:true,offset: {top:-160, left:-10} });});
$("a.top-button").click(function(e){$(scrollTarget).scrollTo($('#' + $(this).attr("title")), 800, {axis:'y',offset: {top:-160, left:-10}});});


$("#left a").click(function(e){
	if (current > 0) {current = current -1;	}	// decrement count
	$(scrollTarget).scrollTo($('td:eq(' + current + ')'), 800, {axis:'yx', queue:true,offset: {top:-160, left:-20} });		
	// next link
	if (current == 0 && prev_url > "") {$(this).attr({ href:prev_url});return true}		
	else {return false}
});	

$("#right a").click(function(e){
	current ++; // increment count
	$(scrollTarget).scrollTo($('td:eq(' + current + ')'), 800, {axis:'yx', queue:true,offset: {top:-160, left:-20} });	
	if (current == next) { $(this).attr({ href:next_url}); return true } // next link		
	else {return false}
});


// when someone manually scrolls update counter
 $(window).scroll(function () { 
    var position = $(window).scrollLeft();   			
  	current = 0;
	var pixelcount = 0;
	var tempcounter = 0;
	while (position > pixelcount && tempcounter < 11 ) {
				pixelcount = pixelcount + $("td:eq(" + tempcounter +")").width();
				pixelcount = pixelcount + 100 // add padding
				if (pixelcount > position ) break;
				if (position == pixelcount) break;
				if (position == undefined ) break;
				if (position == 0) break;
				tempcounter++;
				}
			if (position  > pixelcount ) {tempcounter = 9};		
			current = tempcounter;				
			// prev arrow
			if (current == 0) {
				if (prev_url >"") {$("#left a").addClass("prev");}
				else
				{$("#left a").fadeOut(1000);}
			}
	  if (current+1 == next) { // next arrow
			if (next_url > "") {$("div#right a").addClass("next");}
				else
				{$("div#right a").fadeOut(1000);}		
	  };
	  if (current <= 8) {$("div#right a").removeClass("next");}// reset next arrow
	  if (current > 0) {$("div#left a").removeClass("prev");}// reset prev arrow
	  if (current >= 1) {$("div#left a").fadeIn(1000);}			
	  if (current < next-1) {$("div#right a").fadeIn(1000);}	
  });

// ie 6 is shit sort fixed positioning
 jQuery.each(jQuery.browser, function(i, val) {
  if(i=="msie" && jQuery.browser.version.substr(0,3)=="6.0") {
	  
	var windowHeight = $(window).height();
	$("div#left a").css("marginTop",(windowHeight/2) + "px").css("background-position","top right");
	$("div#right a").css("marginTop",(windowHeight/2) + "px").css("background-position","top left");	
	$("div#header").width($(window).width()-16);
	$("div#stuff").width($(window).width());
	$("div#stuff").height(windowHeight);

 	 }
  });

 // focus on item if anchor in url
 var asset = jQuery.url.attr("anchor");
 if (asset > "" ) {
	   $(".kithkin").animate({opacity: 1.0}, 1000,function(){
        $(scrollTarget).scrollTo($('#stuff' + asset), 800, {axis:'yx', queue:true,offset: {top:-160, left:-10} });
		$('div#stuff' + asset).addClass("highlight");
    }); 	
 }
});

