function type(o){
    return !!o && Object.prototype.toString.call(o).match(/(\w+)\]/)[1];
}

jQuery(function ($) {

var imgCounter = 0; 
var loaded = false;

function scrollRight() {
  var op = jQuery('#wrap');  
  if ((imgCounter) < ($('div.slide-row div.slide').length-1)) {
    imgCounter += 1;
    var offset = imgCounter*$('body').width();
    op.stop().animate({
      scrollLeft: offset
    }, 300);

		var link = $('#i'+ (imgCounter+1) + ' a.active').attr('href');
		if (link)	window.location.hash = link;
		else window.location.hash = '#/p/' + (imgCounter+1) + '/0';

  } else {
  }                     


}

function scrollLeft() { 
  var op = jQuery('#wrap');
  if (imgCounter > 0) {
    imgCounter -= 1;
    var offset = imgCounter*$('body').width();
    op.stop().animate({
      scrollLeft: offset
    }, 300);    

		var link = $('#i'+ (imgCounter+1) + ' a.active').attr('href');
		if (link)	window.location.hash = link;
		else window.location.hash = '#/p/' + (imgCounter+1) + '/0';

  } else {
  }
}

function gotoImage(i) {
  var op = jQuery('#wrap');
	imgCounter = i;
  if (imgCounter > 0) {
    imgCounter -= 1;
    var offset = imgCounter*jQuery('body').width();
    op.stop().animate({
      scrollLeft: offset
    }, 0);    
  } else {
  }	
}



function openTab(t) {
	t.animate({height: "100%"},300, function() {
  	$('#page-content-top', t).fadeIn(300);	  
	});                                            
  
}
function closeTab(t) {
  $('#page-content-top', t).fadeOut(300, function() {
  	t.animate({height: "20px"},300);    
  });
}

function ajaxLoadImg(ref) {
	// handle the image_thumb_handler
}

var image_thumb_handler = function(e) {
	$('a.active', $(this).parent('.slide-content')).removeClass('active');
	$(this).addClass('active');
	loaded = false;
	if (e.which) {
		var oldObj = $(this).parent('.slide-content').find('img');
		var imgObj = $('<img />').attr('src', $(this).attr('ref')).load(function() {
			if (loaded) {
				oldObj.attr('src', imgObj.attr('src')).animate({opacity: 1});
			} else {
				loaded = true;				
			}
		});
		oldObj.animate({opacity: 0}, function() {
			if (loaded) {
				oldObj.attr('src', imgObj.attr('src')).animate({opacity: 1});
			}
			else {
				loaded = true;
			}
		});
	}
}


var tab_handler = function(c) { // if c => false only check if tab is open or not.
  var t = $('#page-content');
  
	if (c) {
	  if (t.height() > 20) {
	    closeTab(t);
			return false;
	  } else {
	    openTab(t);
	  	return true;
	  }		
	} else if (t.height() > 20) {
		return true;	// TRUE == tab is open
	} 
	return false; // FALSE == tab is closed
}

var image_navigation_handler = function(e) {
  var btnRight = 'mb3';
  var btnLeft = 'mb4';
  var currentTarget = $(this).attr('id');

  if (currentTarget == btnRight) {
    scrollRight();
  } else if (currentTarget == btnLeft) {
    scrollLeft();	
	}
	
  return false;
  
}

var change_page = function(e) {
	var lID = $(this).attr('id').replace('n', '');

	var divs = $('#page-content-text > div');
	var visible_div = $('#page-content-text > div:visible');
	var target_div = $('#c'+lID);
	visible_div.fadeOut(100, function() {
		target_div.fadeIn(100);
	});
	
	return false;
}
   

var keydown = function(e) {
  var keyRight = 39;
  var keyLeft = 37;
 	var keyDown = 40;
	var keyUp = 38;
  
	
  if (e.keyCode == keyRight) {
    scrollRight();
  } else if (e.keyCode == keyLeft) {
    scrollLeft();
  } else if (e.keyCode == keyDown && !tab_handler(false)) {
	  tab_handler(true);
  } else if (e.keyCode == keyUp && tab_handler(false)) {
	  tab_handler(true);
	} else {
    return true;    
  }
  return false;  
}              

var window_resize = function(e) {   
	var b_width = $('body').width();
	// if ($('body').hasClass('opera')) {
		$('.slides').width(b_width*$('.slide').length);
		$('.slide').width(b_width);
	// }
  var offset = imgCounter*b_width;
  $('#wrap').animate({scrollLeft: offset},0);
	
}

var u = 0;
var url_handler = function() {	
	var hash = document.location.hash.replace('#','');
	if (hash) {
		hash = hash.split('/');
		if ((imgCounter+1) != hash[2]) {
			gotoImage(hash[2], hash[3]);
			imgCounter = hash[2]-1;
		}		
	}
	setTimeout(url_handler, 500);
}

/* bindings */

// right and left key navigation
if (jQuery.browser.firefox) {
  $(document).keypress(keydown);
} else {
  $(document).keydown(keydown);  
}

// button click handlers
$('#page-content-navigation a').click(change_page);
$('div#page-menu a').click(image_navigation_handler);
$('a#mt1').click(tab_handler);
$('a.image-thumb').click(image_thumb_handler);

// keeping everything centered on resize
window_resize();
$(window).resize(window_resize);

url_handler();

});
