
$(function() {

	function control_left() {
		currentPosition--;

		if (currentPosition < 0) {
			currentPosition = totalLength-unitWidth; 
		}
			
		var newTo = currentPosition*(aWidth+aMargins);
		$('#pane_test').scrollTo(newTo+"px", 200);
		set_scrubber(newTo);

		return false;
	}
	
	function control_right() {
		if (currentPosition + unitWidth < totalLength) {
			currentPosition++;			
		} else {
			currentPosition = 0;
		}

		var newTo = currentPosition*(aWidth+aMargins);
		$('#pane_test').scrollTo(newTo+"px", 200);
		set_scrubber(newTo);

		return false;
	}
	
	function set_scrubber(newTo) {
		newToScrubber = Math.round(newTo/totalWidth*(770+120+12)+200);
		if (newToScrubber > 770)
			newToScrubber = 850;
		$('#scrubber').css('left', newToScrubber+'px');
	}
	
	$('#pane_test ul li a:last').addClass('last');
	
	var currentPosition = 0;
	var totalLength = $('#pane_test ul li').length;
	
	var aMargins = 
		parseInt($('#pane_test ul li a').css('margin-left')) +
		parseInt($('#pane_test ul li a').css('margin-right'));

	var aWidth = $('#pane_test ul li a').width();

	var aWidths = aWidth + aMargins;		

/* 	var totalWidth = totalLength*aWidths - aMargins; */
	var totalWidth = totalLength*aWidths - aMargins +80;
	
	var viewPortWidth = parseInt($('#pane_test').width());
	var unitWidth = Math.round((viewPortWidth)/(aWidth+aMargins));

	$('#pane_test ul').css('width', totalWidth+'px');

	$('.control_left').click(control_left);
	$('.control_right').click(control_right);	
		
	$('#scrubber').draggable({
		containment: [200, 832, 850, 832],
		drag: function(event, ui) { 							
			scrollPos = ((ui.position.left-200)/(770+120+12))*totalWidth;
			currentPosition = Math.floor(scrollPos/130);
			$('#pane_test').scrollTo(scrollPos+"px", 0);	
		}
	});
	
	var findItems = $('#pane_test ul li a');
	var findItemCounter = 0;
	var foundItem = false;
	
	for(f=0; f<findItems.length; f++) {
		if (findItems[f].id.replace(/item_/g, "")==fd_key) {
			foundItem = true;
		}
		if (!foundItem)
			findItemCounter++;
	}
	
	var newTo = findItemCounter*(aWidth+aMargins);
	$('#pane_test').scrollTo(newTo+"px", 200);
	set_scrubber(newTo);
	currentPosition = findItemCounter;
	
	if (findItems.length<7) {
		$('#scrubber').hide();
		$('.control_left').hide();
		$('.control_right').hide();
	}
})


/* content switching */

var current_page = 'description';
var current_image = 1;


function cart_add() {
	$.post("/ajax/cart_add", { fd_key: fd_key }, function() {	});
}

function set_page(p) {
	$('#'+current_page+'_link').removeClass('active');
	current_page = p;
	$('#'+current_page+'_link').addClass('active');
	
	if (p == 'description') {
		$('#cart_text').hide();
		$('#share_text').hide();
		$('#inquiry_text').hide();
		$('#condition_text').hide();
		$('#description_text').show();
		$("#wrapper #photo").show();
	}
	
	if (p == 'cart') {
	
		if (!in_cart) {
			cart_add();
			$('#nav_cart').fadeIn('slow');
			$('#cart_text').html('<p>This item has been added to your cart.<br><a href="/cart/">View your cart</a> to edit or check out.</p>');
		} else {
			$('#cart_text').html('<p>This item is currently in your cart.<br><a href="/cart/">View your cart</a> to edit or check out.</p>');
		}
	
		$('#cart_text').show();
		$('#share_text').hide();
		$('#inquiry_text').hide();
		$('#condition_text').hide();
		$('#description_text').hide();
		$("#wrapper #photo").show();
	}

	if (p == 'condition') {
		$('#cart_text').hide();
		$('#share_text').hide();
		$('#inquiry_text').hide();
		$('#condition_text').show();
		$('#description_text').hide();
		$("#wrapper #photo").show();
	}
	
	if (p == 'inquiry') {
		$('#cart_text').hide();
		$('#share_text').hide();
		$('#inquiry_text').show();
		$('#condition_text').hide();
		$('#description_text').hide();
		$("#wrapper #photo").hide();
	}
	
	if (p == 'share') {
		the_link = window.location.href.substring(0,window.location.href.length-window.location.hash.length);
		$('#cart_text').hide();
		$('#share_text').html($('#share_text').html().replace(/\*\*link\*\*/, the_link));
		$('#share_text').show();
		$('#inquiry_text').hide();
		$('#condition_text').hide();
		$('#description_text').hide();
		$("#wrapper #photo").hide();
	}
}

function zoom_image(r) {
	var w = screen.availWidth;
	var h = screen.availHeight-100;	
	if (w>800) {
		w -= 100;
	}
	
	if (h>600) {
		h -= 100;
	}
	
	if (h<w) {
		w = h;
	}
	
	window.open ("/helpers/zoom/"+r+"/"+w+"/"+h,"wright_zoom","status=1,toolbar=0,location=0,menubar=0,directories=0,resizable=1,scrollbars=0,left=50,top=25,height="+h+",width="+w);
}

function switch_image(i) {
	$('.caption').hide();
	$('#caption'+i).show();

	$('#image_'+current_image).removeClass('active');
	var new_html = '';
	current_image = i;
	
	$('#image_'+i).addClass('active');
	
	$('#photo').html('');
	
	if (all_images[i-1].zoomable) {
		new_html += '<a href="#" onclick="zoom_image(\''+all_images[i-1].image.replace(/.jpg/g, '')+'\'); return false">';
	}

	new_html += '<img src="/helpers/resize_original/510/'+all_images[i-1].height+'/'+all_images[i-1].image+'" width="510" height="'+all_images[i-1].height+'" style="visibility: hidden" onload="$(this).css(\'display\',\'none\').css(\'visibility\',\'visible\').fadeIn(\'fast\');">';

	if (all_images[i-1].zoomable) {
		new_html += '<img src="/assets/images/core/zoom.png" class="zoom_button" width="15" height="14" /></a>';
	}

	$('#photo').html(new_html);

}

function right_image() {
	if (all_images.length>current_image) {
		switch_image(current_image+1);
	} else {
		switch_image(1);
	}
}

function left_image() {
	if (current_image>1) {
		switch_image(current_image-1);
	} else {
		switch_image(all_images.length);
	}
}