jQuery.preloadImages = function()
{
  for(var i = 0; i<arguments.length; i++)
  {
    jQuery("<img>").attr("src", arguments[i]);
  }
}

function hideOverlay() {
	$('#CSSoverlay_content').empty();
	$('#CSStopMenu li.tab').removeClass( 'tab' );
	$('#CSSoverlay').css( 'display' , 'none' );
}



jQuery(document).ready(function($){

	$('#CSStopMenu a[rel="1015"],#CSStopMenu a[rel="1115"],#CSStopMenu a[rel="1215"],#CSStopMenu a[rel="1315"]').hover(function(){
		_obj = $(this);
		hover_intent = setTimeout( function(){
			$('#CSStopMenu li.tab').removeClass( 'tab' );
			_obj.parent().addClass( 'tab' );
			//loadContent( _obj.attr( 'href' ).replace( /\?/ , 'content.php' ), "overlay_content" );
			
			//NEW VERSION WITH REWRITE DOCUMENT URL
			loadContent( _obj.attr( 'href' ).replace( /\?/ , '?document_element_id=ContentBox&amp;' ), "overlay_content" );
			
			$('#CSSoverlay').css( 'display' , 'block' );
		} , 1000 );

	} , function() {
		try{ clearTimeout( hover_intent ); }
		catch(e){};
	} );

	$('#CSSoverlay').mouseleave( function() { hideOverlay(); } );
	$('#CSSoverlay a.close').click( function() { hideOverlay(); } );

	jQuery.fn.setTab = function() {
		if( $(this).hasClass( 'navigator' ) ) {
			$(this).prevAll( 'h2.tab:first' ).addClass( 'parent' );
		} else {
			$( '.mainPage #ContentBox .parent' ).removeClass( 'parent' );
		} //if
		$( '.mainPage #ContentBox .active' ).removeClass( 'active' );
		$( '.mainPage #ContentBox .bkg' ).stop();
		$(this).addClass( 'active' );
		$(this).next( '.content' ).addClass( 'active' );
		//no fadein
		//$( '.mainPage #ContentBox' ).css( { 'background' , 'url("img/' + $(this).attr( 'rel' ) + '") no-repeat left top' } );
		//fadein
		if( $( '.mainPage #ContentBox .bkg:last' ).length ) {
			$( '.mainPage #ContentBox .bkg:last' ).clone().prependTo( '.mainPage>#CSSmainBlock>#ContentBox' );
		} else {
			$( '.mainPage #ContentBox' ).prepend( '<div class="bkg">&nbsp;</div>' );
		}
		$( '.mainPage #ContentBox .bkg:last' ).css( { 'display' : 'none' , 'background' : 'url("/img/' + $(this).attr( 'rel' ) + '") no-repeat left top' } );
		$( '.mainPage #ContentBox .bkg:last' ).fadeTo( 1000 , 1 , function() {
			$( '.mainPage #ContentBox .bkg:not(:last)' ).remove();
		} );
	}

	$( '.mainPage #ContentBox .tab' ).click( function() {
		if( $(this).hasClass( 'active' ) ) {
			document.location.href=$(this).next( '.content' ).attr( 'href' );
		} else {
			window.clearTimeout(timeout_idx);
			$(this).setTab();
		} //if
	} );

	$( '.tabBox .tabFold' ).click( function() { $(this).showTab(); return false; } );

	jQuery.fn.showTab = function() {
		$( '.tabBox .tabFold' ).removeClass( 'active' );
		$(this).addClass( 'active' );
		$( '.tabBox .tab' ).css( { 'display' : 'none' } );
		$( '.tabBox .tab[rel=' + $(this).attr( 'rel' ) + ']' ).css( { 'display' : 'block' } );
	}

	$( 'area' ).hover( function() {
		$(this).parents( '.mapBlock' ).append('<div class="hover" style="background: url(\'/img/layout_main/clickmap/kraj' + $(this).attr( 'rel' ) + '.gif\') no-repeat left top;">&nbsp;</div>');
	} , function() {
		$(this).parents( '.mapBlock' ).find( '.hover' ).remove();
	} );

	$( '.productList.line .unfold a' ).click( function() { $(this).toggleGallery(); return false; } );
	$( '.gallery .unfold a' ).click( function() { $(this).toggleGallery(); return false; } );

	jQuery.fn.toggleGallery = function() {
		//$(this).closest( ".item" ).children( "li[class!=unfold]" ).filter( ":gt(5)" ).toggle();
		$(this).closest( ".item" ).find( "li[class!=unfold]:gt(5)" ).toggle();
		$(this).closest( ".item" ).toggleClass( "open" );
	}

$( '.productList.line li' ).click( function() {
		document.location.href = $(this).children( 'a' ).attr( 'href' );
		return false;
	} );

	$( '.productList.usage .item a[rel!=""]' ).tooltip( { attr: "rel" } );
	$( '.productDetail .icons img' ).tooltip( {
		tipClass: "tooltipAlt"
		,layout: "<div><div class='arrow'>&nbsp;</div></div>"
		,position: "bottom right"
		,offset: [14,-15]
	} );

	// hlavni stranka cykl
	var elements = $( '.mainPage #ContentBox .tab' );
	var length = elements.length;
	var cnt = 0;
	var start_index = 0;

	/*
	function nextTab( index ) {
		index++;
		if( index == length ) { index = 0; }

		elements.eq(index).setTab();
		//setTimeout( "nextTab( "+index+" )" , 2000 );
		setTimeout( "nextTab()" , 2000 );
	}

	nextTab( 0 );
	*/
	
	var last_index = 0;
	
	function nextTab() {
		last_index++;
		if( last_index == length ) { last_index = 0; }

		elements.eq(last_index).setTab();
		timeout_idx = window.setTimeout( nextTab , 3000 );
	}

	var timeout_idx = window.setTimeout( nextTab , 3000 );

});

