window.addEvent('domready', function () {
	var section = 'summary';
	if (location.hash) {
		section = location.hash.substring(1);
	}
	displaySection(section);
});

function displaySection (section) {
	try {
		$$('.productSection').setStyle('display', 'none');
		$$('.productNav a').removeClass('selected');
		$(section).setStyle('display', 'block');
		$$('.' + section).addClass('selected');
	} catch (e) {
		$('summary').setStyle('display', 'block');
	}
}

function loadExample (src, obj) {
	document.getElement('.productImage img').src = src;
	document.getElement('.caption').set('html', obj.getAttribute('description'));
	var link = obj.getAttribute('link');
	if (link) {
		document.getElement('.caption').setStyles({'cursor': 'pointer'});
		document.getElement('.caption').addEvent('click', function (link) {
			location.href = link;
		}.pass(link));
		document.getElement('.productImage').setStyles({'cursor': 'pointer'});
		document.getElement('.productImage').addEvent('click', function (link) {
			location.href = link;
		}.pass(link));
	} else {
		document.getElement('.caption').setStyles({'cursor': 'default'});
		document.getElement('.productImage').setStyles({'cursor': 'default'});
	}
}var ts_item_width = 124*7;
var ts_items_shown = 7;

function init_tab () {
	
	// Import tabs defined in the specific product views
	$$('.productTab').each(function (item) {
		var tmp = new Element('div', {
			'class': 'ts_tab', 
			'tab': item.getProperty('id'), 
			'text': item.getProperty('label')
		});
		if ($$('#ts_tabs .ts_tab').length > 0) {
			tmp.inject(document.getElement('#ts_tabs .ts_tab'), 'after');
		} else {
			tmp.inject($('ts_tabs'), 'top');
		}
		var tmp = new Element('div', {
			'class': 'ts_sliderContainer', 
			'tab': item.getProperty('id'), 
			'style': {display: 'none'}
		}).adopt(
			new Element('div', {
				'class': 'ts_item_container ts_item_container_ns', 
				'html': item.get('html')
			})
		).adopt(new Element('div', {'class': 'clear'}));
		tmp.inject($('ts_slidersContainer'), 'top');
	});
	
	$$('#ts_tabs .ts_tab').each(function (item, num) {
		item.addClass('ts_tab' + (num+1));
	});
	
	// Select requested tab
	var tab = location.search.substring(1).parseQueryString();
	if (!tab['tab'] || !$$('div.ts_tab[tab=' + tab['tab'] + ']').length) {
		tab['tab'] = $$('div.ts_tab').getLast().getProperty('tab');
	} else {
		$('ts_Container').scrollIntoView(true);
	}
	try {
		$$('div.ts_tab').removeClass('selected');
		$$('div.ts_tab[tab=' + tab['tab'] + ']').addClass('selected');
		$$('div.ts_sliderContainer').each(function(item, index) {
			if (item.getProperty('tab') == tab['tab']) {
				item.setStyle('display', 'block');
			} else {
				item.setStyle('display', 'none');
			}
		});
	} catch (e) {
	}
}

function ts_activate_tab () {
	var tab = this.getProperty('tab');
	location.href = '?tab=' + tab;
}

function ts_scroll (direction) {
	var tab = this.getProperty('tab');

	div = $$('div.ts_item_container2[tab='+ tab +']');
	var current_position = div.getStyle('margin-left');
	current_position = parseInt(current_position);
	
	var max_position = 0;
	$$('div.ts_item_container2[tab='+tab+'] a.ts_item').each(function (item) {
		max_position -= item.getSize().x;
	});
	
	if (max_position == 0) {
		$$('div.ts_item_container2[tab='+tab+'] a.ts_item2').each(function (item) {
			max_position -= item.getSize().x;
		});
	}
		
	if (this.hasClass('ts_next')) {
		if (current_position <= max_position) {
			var new_position = max_position;
		} else {
			var new_position = current_position - ts_item_width;
		}
		var next_position = current_position - ts_item_width;
	} else {
		if (current_position >= 0) {
			var new_position = 0;
		} else {
			var new_position = current_position + ts_item_width;
		}
		var next_position = current_position + ts_item_width;
	} 
	
	if (next_position <= max_position + ts_item_width) {
		new_position = max_position + ts_item_width;
	}
	
	// scrolling
	if (next_position >= 0) {
		new_position = 0;
	}
	
	if (new_position >= 0) {
		new_position = 0;
	}
	
	// fix arrow visibility
	if (new_position >= 0) {
		$$('div.ts_previous[tab='+tab+']').setStyle('opacity', 0.1);
	} else {
		$$('div.ts_previous[tab='+tab+']').setStyle('opacity', 1);
	}
	
	if (max_position == 0 || (next_position + max_position) == 0 || max_position == next_position || next_position <= (max_position + ts_item_width) || (next_position + max_position) > 0) {
		$$('div.ts_next[tab='+tab+']').setStyle('opacity', 0.1);
	} else {
		$$('div.ts_next[tab='+tab+']').setStyle('opacity', 1);
	}
	
	div.set('tween', {duration: 800});
	div.tween('margin-left', new_position + 'px');
}

window.addEvent('domready', function () {
	init_tab();
	$$('div.ts_tab').addEvent('click', ts_activate_tab);
	$$('div.ts_next').addEvent('click', ts_scroll);
	$$('div.ts_previous').addEvent('click', ts_scroll);
	
	$$('div.ts_previous').fireEvent('click');
	
	// $$('div.ts_tab[tab=' + $('ts_tabs').getFirst().getProperty('tab') + ']').fireEvent('click');
});
