/*
 javascripts and variables used for horizontal scrolling div
*/


var scrollfx = {};
var scrolldivs = {};
var scrolldivwidths = {};
var scrollwidths = {};
var scrollpages = {};
var scrollanzahl = {};
var scrollpositions = {};
var scrollmaxbreite = {};
var scrollloaded = {};
var increment = {};



window.addEvent('domready', 
	function(){
	    var totIncrement    = 0;
	    scrolldivs = $$('.scrolldiv');
	    scrolldivs.each(function(el,elidx) {
		    scrollfx[getIdFromString(el.id)] = new Fx.Tween(el.id, {duration: 1200,transition:'sine:in:out'});
			scrollpositions[getIdFromString(el.id)] = 0;
			scrollmaxbreite[getIdFromString(el.id)] = Math.ceil(scrollwidths[getIdFromString(el.id)]/increment);
			scrollloaded[getIdFromString(el.id)] = {1:1};
	    }
	    );
	    $$('.productspage').each(function(el) {
	    	if (!$chk(el.html)) {
	    		el.set('html',emptydiv);
			    }});
		$$('.pfeillinks').each(function(el) { 
				if (scrollwidths[getIdFromString(el.id)] <= increment[getIdFromString(el.id)]) { el.addClass('versteckt'); }
				el.addClass('pfeiloff') 
			}); 
		$$('.pfeilrechts').each(function(el) { 
			if (scrollwidths[getIdFromString(el.id)] <= increment[getIdFromString(el.id)]) el.addClass('versteckt') 
			}); 
	    $$('.pfeillinks').each(function(el) { 
	    	el.addEvent('click', 
	    	function(ev) {
	    		//doScroll(this,0);
    			switchToPrevTab();
				window.clearInterval(nextTabTimeout);
				nextTabTimeout = window.setInterval('switchToNextTab();',autotabdelay);
                return;
			}
			)
	    	}
		);
	    $$('.pfeilrechts').each(function(el, eldidx) { 
	    	el.addEvent('click', 
	     	function(ev) {
                //doScroll(this,1);
	    		switchToNextTab(0);
				window.clearInterval(nextTabTimeout);
				nextTabTimeout = window.setInterval('switchToNextTab();',autotabdelay);
                return;
			}
	    )
	    }
		);
	}
);

function doScroll(el,dir, position,offset) {
	el = $(el);
	eId = getIdFromString(el.id);
	
	scrollMiddle = (scrolldivwidths[eId]-increment[eId])/2;
	if ($chk(offset)) {
		scrollMiddle = offset-increment[eId]/2;
	}
	if (scrollMiddle < 0) {
		scrollMiddle = 0;
	}
	if (scrollMiddle > (scrolldivwidths[eId]-increment[eId])) {
		scrollMiddle = (scrolldivwidths[eId]-increment[eId]);
	}

	if ($chk(position)) {
		scrollpages[eId] = position-1;
	}
	else if (dir == 0) {
		if(scrollpositions[eId] < 0) {
		    scrollpages[eId] = scrollpages[eId] - 1;
		}
	}
	else if (dir == 1) {
		if(scrollpositions[eId] > -scrollwidths[eId]+scrolldivwidths[eId]){
		    scrollpages[eId] = scrollpages[eId] + 1;
		}
	}
	scrollpositions[eId] = -1 * ( scrollpages[eId] * increment[eId] - scrollMiddle);
    if (scrolldivwidths[eId] < scrollwidths[eId]) {
	    //$('next_'+eId).removeClass('npfeiloff');			
	    //$('previous_'+eId).removeClass('pfeiloff'); 
    }
	if (scrollpositions[eId] >= 0) {
		scrollpositions[eId] = 0;
	    //$('previous_'+eId).addClass('pfeiloff'); 
	}
    if (scrollpositions[eId] <= -scrollwidths[eId]+scrolldivwidths[eId] ) {
    	scrollpositions[eId] = -scrollwidths[eId]+scrolldivwidths[eId];
    	//$('next_'+eId).addClass('npfeiloff'); 
    }
    scrollfx[eId].cancel();
	scrollfx[eId].start('margin-left',scrollpositions[eId]);		
}
var last_selected_tab = '';
var tab_ids = Array();
var manual_selected = 0;
var nextTabTimeout;
var autotabdelay = 5000;
function switchToTab(tab_id) {
	if (tab_id != last_selected_tab) {
		var lastTab = $(last_selected_tab);
		if (lastTab) {
			lastTab.fade('out');
		}
		last_selected_tab = tab_id;
		$(tab_id).fade('in');
	}
	var eId = '1';
	if (tab_ids.length == 1) {
    	$('next_'+eId).addClass('npfeiloff');			
	    $('previous_'+eId).addClass('pfeiloff'); 
	}
	else {
		if (tab_id == tab_ids[0]) {
		    $('previous_'+eId).addClass('pfeiloff');
		}
		else {
	    	$('previous_'+eId).removeClass('pfeiloff');			
		}
		if (tab_id == tab_ids[tab_ids.length-1]) {
		    $('next_'+eId).addClass('npfeiloff'); 
		}
		else {
	    	$('next_'+eId).removeClass('npfeiloff');			
		}
	}			
}

function switchToNextTab(jump) {
	var first_tab = '';
	var next_tab = 0;
	var select_tab = '';
	var last_tab = '';
	var do_jump = 1;
	if (typeof jump != 'undefined') {
		do_jump = jump;
	}
	if (manual_selected == 0) {
		for (var index = 0; index < tab_ids.length; ++index) {
			if (next_tab == 1) {
				select_tab = tab_ids[index];
				next_tab = 0;
			}
			if (first_tab == '') {
				first_tab = tab_ids[index];
			}
			//alert (tab_ids[index]+' == '+last_selected_tab);
			if (tab_ids[index] == last_selected_tab) {
				next_tab = 1;
			}
			last_tab = tab_ids[index];
		}
		if (next_tab == 1 && select_tab == '') {
			if (do_jump) {
				select_tab = first_tab;
			}
			else {
				select_tab = last_tab;
			}
		}
		selectTab(select_tab.replace('c',''));
		//alert(select_tab);
	}
}

function switchToPrevTab() {
	var first_tab = '';
	var prev_tab = 0;
	var select_tab = '';
	var last_tab = '';
	if (manual_selected == 0) {
		for (var index = 0; index < tab_ids.length; ++index) {
			if (tab_ids[index] == last_selected_tab) {
				prev_tab = 1;
			}
			if (prev_tab == 1 && last_tab != '') {
				select_tab = last_tab;
				prev_tab = 0;
			}
			last_tab = tab_ids[index];
			if (first_tab == '') {
				first_tab = tab_ids[index];
			}
			//alert (tab_ids[index]+' == '+last_selected_tab);
		}
		if (prev_tab == 1 && select_tab == '') {
			select_tab = first_tab;
		}
		selectTab(select_tab.replace('c',''));
		//alert(select_tab);
	}
}

function selectTab(el,ev) {
	el = $(el);
	$$('.tab_selector').each(function (el) {el.removeClass('tab_selector_hover');} );
	$$('.tab_button').each(function (el) {el.removeClass('tab_button_hover');} );
	$(el).addClass('tab_selector_hover');
	$('tab_button_'+getIdFromString(el.id)).addClass('tab_button_hover');
	offset = 0;
	if ($chk(ev)) {
		posi = $('maske_1').getPosition();
		offset = ev.page.x - posi.x;
		//el.innerHTML = 'Offset '+offset+'px';
	}
	//doScroll($('next_1'),0,getIdFromString(el.id),offset);
	switchToTab(el.id+'c');
}

