$(document).ready(function(){
	rollOver('.model_more_button', '#ffffff');
	$('.model_more_button').click(showModelDetails);
	$('.tab_selector').live('click', changeTab);
	if (isiPad()) {
		$('.tab_selector').css({backgroundColor: '#666'});
	}
});

function models() {
	$('#models').fadeIn();
}

function showModelDetails() {
	var div = $(this).parent().find('.details').html();
	$('#overlay_model_content').find('.content').html(div);
	var overlay_src = $(this).parent().find('.overlay_src').html();
	$('#model_image_overlay').html('<img src="'+overlay_src+'" border="0">');
	$('#overlay_model').fadeIn('normal', function(){
		$(this).find('.tab_selector').each(function(){
			if ($(this).attr('rel') == 0) {
				$(this).trigger('click');
			}
		});
	});
}

function changeTab() {
	$('.tab_selector').removeClass('tab_selector_selected');
	$(this).addClass('tab_selector_selected');
	var id = $(this).attr('rel');
	$('#overlay_model_content').find('.tab_content').each(function(){
		if ($(this).attr('id') == 'tab_content_'+id) {
			$(this).stop().fadeIn();
		} else {
			$(this).stop().fadeOut();
		}
	});
	var text_height = parseInt($('#overlay_model_content').find('#tab_content_'+id).find('.tab_content_text p').height(), 10);
	var p_height = parseInt($('#overlay_model_content').find('#tab_content_'+id).find('.tab_content_text').css('height'), 10);
	if (text_height > p_height) {
		checkTextScroll(id);
	} else {
		$('#overlay_model_content').find('#tab_content_'+id).find('.scrollbar_small').hide();
	}
}

function checkTextScroll(id) {
	var text_height = parseInt($('#overlay_model_content').find('#tab_content_'+id).find('.tab_content_text p').height(), 10);
	var p_height = parseInt($('#overlay_model_content').find('#tab_content_'+id).find('.tab_content_text').css('height'), 10);
	$('#overlay_model_content').find('#tab_content_'+id).find('.scrollbar_small').show();
	$('#overlay_model_content').find('#tab_content_'+id).find('.scroller_small').draggable({ containment: "parent", scroll: false, axis: 'y' });
	$('#overlay_model_content').find('#tab_content_'+id).find('.scroller_small').mousedown(function(){ $(this).stop(); });
	$('#overlay_model_content').find('#tab_content_'+id).find('.scroller_small').bind("drag", function(event, ui) {
		var top = 0;
		var differenza = text_height - p_height;
		top = parseInt($(this).css('top'),10) * differenza / (parseInt($('#overlay_model_content').find('#tab_content_'+id).find('.scrollbar_small').height(),10) - parseInt($(this).height(),10));
		$('#overlay_model_content').find('#tab_content_'+id).find('.tab_content_text p').css({marginTop: '-'+top+'px'});
	});
	
	if (isiPad()) {
		$('#overlay_model_content').find('#tab_content_'+id).find('.scroller_small').live("touchstart", function(event){
			if (event.originalEvent.targetTouches.length != 1) return false;
			this.startY = event.originalEvent.targetTouches[0].clientY;
		})
		$('#overlay_model_content').find('#tab_content_'+id).find('.scroller_small').live("touchmove", function(event){
			
			event.originalEvent.preventDefault();
			
			var scroller_top = event.originalEvent.targetTouches[0].pageY - this.startY;
			if (scroller_top < 0) scroller_top = 0;
			if (scroller_top > parseInt($('#overlay_model_content').find('#tab_content_'+id).find('.scrollbar_small').height(),10) - parseInt($(this).height(),10)) scroller_top = parseInt($('#overlay_model_content').find('#tab_content_'+id).find('.scrollbar_small').height(),10) - parseInt($(this).height(),10);
			$(this).css({top: scroller_top});
			
			var top = 0;
			var differenza = text_height - p_height;
			top = parseInt($(this).css('top'),10) * differenza / (parseInt($('#overlay_model_content').find('#tab_content_'+id).find('.scrollbar_small').height(),10) - parseInt($(this).height(),10));
			$('#overlay_model_content').find('#tab_content_'+id).find('.tab_content_text p').css({marginTop: '-'+top+'px'});
			
		});
		$('#overlay_model_content').find('#tab_content_'+id).find('.scroller_small').live("touchend", function(event){
			var top = 0;
			var differenza = text_height - p_height;
			top = parseInt($(this).css('top'),10) * differenza / (parseInt($('#overlay_model_content').find('#tab_content_'+id).find('.scrollbar_small').height(),10) - parseInt($(this).height(),10));
			$('#overlay_model_content').find('#tab_content_'+id).find('.tab_content_text p').css({marginTop: '-'+top+'px'});
		});
	}
	
}
