$(document).ready(function(){

	var transitionDelay = 800;

	//  carusel -> fce-slider 			navigation_bar -> switcher-box			act ->  white

	var imgCounter = 0;
	var globalBlock = '';
	function tabSlide(items) {
		interval = 4000;
		setTimeout(function () {
			if($(items[imgCounter]).hasClass( 'act' ) && !$('.fce-slider .switcher-box').hasClass('locked') && items.length>1 ) {
			                $(items).removeClass( 'act' );
			                globalBlock = $(items[imgCounter]).attr('href').split('#')[1];
							$('#'+$(items[imgCounter]).attr('href').split('#')[1]).fadeOut(transitionDelay,removeFilter);
			                if(items[imgCounter+1]) {
			                    $(items[imgCounter+1]).addClass( 'act' );
								$('#'+$(items[imgCounter+1]).attr('href').split('#')[1]).fadeIn(transitionDelay);
								imgCounter ++;
			                } else {
			                    imgCounter = 0;
			                    $(items[imgCounter]).addClass( 'act' );
								$('#'+$(items[imgCounter]).attr('href').split('#')[1]).fadeIn(transitionDelay);
			                }
			} else if($('.fce-slider .switcher-box').hasClass('locked')) {
				//
			} else if(imgCounter < items.length) {
				imgCounter ++;
			} else {
				imgCounter = 0;
			}
			//alert(imgCounter);
			tabSlide(items);
		}, interval);

	}
	//stop(this.setTimeout(),'mouseover');
	//start(this.setTimeout(),'mouseout');
	/*
	if(!(jQuery.browser.msie)) {
		$('#mainMenu').simplemenu();
	}
	*/
//	$('.carusel .navigation_bar > ul').tabs({ fx: { opacity: 'toggle' } }).tabs('rotate', 5000);

	$('.fce-slider .switcher-box').idTabs(function(id,list,set){
		if($(id).css('display') == 'none') {
			for(i in list)
				$(list[i]).hide();
			$(id).fadeIn(transitionDelay);
			$('a',set).removeClass('act');
			$('a',set).filter("[href='"+id+"']",set).addClass('act');
			$('a',set).filter("[href='home.html"+id+"']",set).addClass('act');
		}
		return false;
	});
	/*
	$('.fce-slider .switcher-box').hover(function() {
				$(this).toggleClass('locked');
	},function() {
				$(this).toggleClass('locked');
	});
	*/

	$('.fce-slider').hover(function() {
				$('.fce-slider .switcher-box').toggleClass('locked');
	},function() {
				$('.fce-slider .switcher-box').toggleClass('locked');
	});

	if ($('.fce-slider .switcher-box').length){
		tabSlide($('.fce-slider .switcher-box a'));
		$('.close').click(function() {
				$(this).parent().parent().find('.shader').hide();
				$(this).parent().hide();
		});
	}
	enableOpenFolder = true;

	function removeFilter() {
	  $('#' + globalBlock).removeAttr("filter");
	}

});

/*
 * This function is called from the flash, and gives a articleId as parameter.
 * Based on the articleId, we need to show the articleAddToCart popup.
 *
 * If the function is called in a iframe, it will try to break out to the normal screen.
 * This JS should be included in both screens ofcourse.
 */
function addArticle(articleId) {
	if (parent != window) {
		parent.addArticle(articleId);
		// hide article until we get a signal to show it again? (else the flash will be forced-on-top)
	} else {
		// hide iframe?
		// show popup (addToCart), when done with addToCart, show the flash again (so revert back)
		$('#tx_impkreko_pi1-popup-uid').val(articleId);
		$('#tx_impkreko_pi1-product_popup-quantity').val('');
		$('#tx_impkreko_pi1-cart_reload').trigger('goSingle');
	}
}


/*
 *	This function opens the lightbox, from the carrousel container.
 */
var enableOpenFolder = false;
function openFolder(url) {
	if (url == null || url == '') return;
	if (!enableOpenFolder) return false;

	// Init the FolderFrame, so we have a clean Div.
	var newDialog = true;
	if (!document.getElementById('folderFrame')) jQuery('#mainContainer').after('<div id="folderFrame" title="Kreko Folder"></div>');
	else newDialog = false;

	// Add the IFrame containing the flash folder (this is a normal TYPO3 page)
	if (!document.getElementById('folderIFrame')) {
		jQuery('#folderFrame').html('<iframe id="folderIFrame" src="'+url+'" height="600" width="960" frameborder=0></iframe>');
	} else {
		jQuery('#folderIFrame').attr('src', url);
	}

	// init the Dialog and show the lightbox
	if (newDialog) {
		// open lightbox here.
		jQuery('#folderFrame').dialog({
			bgiframe: true,
			resizable: false,
			height: 650,
			width: 980,
			modal: false,
			overlay: {
				backgroundColor: '#000',
				opacity: 0.5
			},
			draggable: true,
			position: [20, 10],
			closeOnEscape: false
		});
	} else {
		jQuery('#folderFrame').dialog('open');
	}
}
