/*** onLOAD ***/

var start_category = 0;
var current_category = 0;

var intrvl = 1500;
var intrvl_fade = 400;
var intrvl_butt = 200;
var intrvl_scroll = 100;
var intrvl_rotation = 4000;

function show_category( category_id, stay, item_id )
{
	if( item_id > 0 )
	{
		jQuery( window ).scrollTo
		(
			jQuery( '#item_' + item_id ),
			{
				duration: intrvl,
				easing: "swing",
				onAfter: doRotation
			}
		);
		
		return false;
	}
	
	if( jQuery( '#category_' + category_id ).hasClass( 'active' ) )
	{
		return false;
	}
	
	current_category = category_id;
	
	_set_category( category_id );
	
	jQuery( '.category .showonover' ).fadeOut( intrvl_fade );
	jQuery( '#category_' + category_id + ' .showonover' ).fadeIn( intrvl_butt );
	jQuery('#items_category_' + category_id ).fadeIn( intrvl_fade );
	
	if( stay != true )
	{	
		jQuery( window ).scrollTo
		(
			jQuery( '#category_' + category_id ),
			{
				duration: intrvl,
				easing: "swing",
				onAfter: doRotation
			}
		);
	}
}

function toggle_category( category_id )
{
	jQuery( '#items_category_' + category_id ).toggle( intrvl );
	
	jQuery( '#category_' + category_id ).removeClass( 'active' );
	jQuery( '#category_' + category_id + ' .showonover' ).fadeOut( intrvl_fade, function(){ doRotation() } );
}

function show_top()
{
	jQuery( '.category .showonover' ).fadeOut( intrvl_fade );
	
	show_category( start_category )
	jQuery( '#category_' + start_category + ' .showonover' ).fadeIn(intrvl_fade);
}

function show_player()
{
	jQuery( 'body' ).css( 'overflow', 'hidden' );
	jQuery( '#flash' ).fadeIn( intrvl_fade);
	jQuery( '#container_h' ).fadeTo( intrvl_fade, 0 );
	jQuery( '#drawing' ).fadeTo( intrvl_fade, 0 );
}

function hide_player()
{
	jQuery( '#flash' ).fadeOut( intrvl_fade );
	jQuery( '#container_h' ).fadeTo( intrvl_fade, 1 );
	jQuery( '#drawing' ).fadeTo( intrvl_fade, 1 );
	jQuery( 'body' ).css( 'overflow', 'auto' );
}

function do_search()
{
	reset_search( false );

	if( jQuery('#elem_search_query').val().length >= 1 )
	{
		var query = jQuery('#elem_search_query').val();
		
		jQuery( '#container_h' ).highlight( query );
		
		var search_count = jQuery('.highlight').length;
		
		if( search_count > 0 )
		{
			jQuery( '#search_results' ).html( search_count + ' results.<br />Scroll down for blue words.' );
		}
		else
		{
			jQuery( '#search_results' ).html( 'No results.<br />Try to redifine search.' );
		}
		
		
		jQuery( '#search_results' ).fadeIn();
		
		/*
		jQuery('html').scrollTo
		(
			jQuery( '.item span.highlight:first' ),
			{
				duration: intrvl,
				easing: "jswing"
			}
		);
		*/
		
	}
}

function reset_search( remove_value )
{
	if( remove_value == true )
	{
		jQuery('#elem_search_query').val('');
	}
	
	jQuery('.highlight').removeClass( 'highlight' );
	
	jQuery('#highlight-plugin').removeHighlight();
	
	jQuery( '#search_results' ).fadeOut( intrvl_fade );
}

var old_rotation = 0;
function doRotation()
{
	var window_offset = jQuery( window ).scrollTop();
	var content_height = jQuery( '#container_h' ).height();
	
	rotation = Math.ceil( window_offset / content_height * 310 );
	
	jQuery( '#drawing #drawing_image' ).rotate( {angle: old_rotation, duration: intrvl_rotation, animateTo: rotation } );
	
	old_rotation = rotation;
}



/*** PRIVATES ***/
function _set_category( category_id )
{
	jQuery(	'.category' ).removeClass('active');
	jQuery(	'.items' ).removeClass('active');
	
	if( category_id > 0 )
	{
		jQuery(	'#category_' + category_id ).addClass('active');
		jQuery(	'#items_category_' + category_id ).addClass('active');
	}
}
function _doOnResize()
{
	var window_height = jQuery( window ).height();
	
	// postavi visinu headera tako da prvi category dodje odmah ispod window-a
	jQuery( '#header' ).css( 'height', jQuery( window ).height() - jQuery('.category_starting').height() );
	
	// u svakoj kategoriji naci najvisi div te ostalima u kategoriji dodijeliti istu visinu
	jQuery( '.category' ).each
	(
		function( category_index )
		{
			var max_height = 0;
		
			var items = '.items:eq(' + parseInt(category_index) + ')';
		
			jQuery( items + ' .items_slider .item' ).each
			(
				function( item_index )
				{
					var item_height = jQuery( items + ' .item:eq(' + parseInt(item_index) + ')' ).height();
					
					if( item_height > max_height )
					{
						max_height = item_height;
						
					}
				}
			);
			
			jQuery( items + ' .item' ).height( max_height );
			
			var pages = Math.ceil( ( jQuery(items).height() + 80 ) / jQuery(window).height() );
			var items_height = Math.max( pages * jQuery(window).height() - 80, jQuery(window).height() );
			jQuery(items).height( items_height );
		}
	);
	
	// na scroll rotiraj drawing
	jQuery( window ).bind
	(
		"scroll",
		function()
		{
			
		}
	);
}


jQuery( document ).ready
(
	function()
	{
		_doOnResize();
		
		/*
		jQuery( ".item .counter" ).bind
        (
            {
                mousedown: function( event )
                {
                    jQuery( this ).parent().not('.show_on_short,.counter').toggleClass('shorted');
                }
            }
        );
		*/
		
		// akcije na mouseover
		/*
		jQuery( "div.category_title" ).bind
        (
            {
                mouseenter: function( event )
                {
                    active_id = this.id;
					
                    jQuery( '.category div#' + active_id + ' .showonover').fadeIn( intrvl_fade );
					jQuery( '.category div#' + active_id + ' parent > parent > div.showonover').fadeIn( intrvl_fade );
                },
                mouseleave: function( event )
                {
					jQuery( '.category:not(.active) div#' + active_id + ' .showonover').fadeOut( intrvl_butt );
					jQuery( '.category:not(.active) div#' + active_id + '').parent().parent().hasClass('showonover').fadeOut( intrvl_butt );
                }
            }
        );
		*/
		
		
		// akcije na kontejneru
		/*
		jQuery( "div.item:not(.active)" ).bind
        (
            {
                mouseenter: function( event )
                {
					active_category = jQuery( this ).parent().parent().attr( 'rel' );
					
                    jQuery( this ).addClass('active');
					jQuery( '#category_' + active_category + ' .category_name' ).addClass( 'active' );
					//jQuery( '#category_' + active_category + '' ).css( {opacity: 0.6} );
					
                },
                mouseleave: function( event )
                {
					jQuery( this ).removeClass('active');
					
					jQuery( '#category_' + active_category + ' .category_name' ).removeClass( 'active' );
					//jQuery( '#category_' + active_category + '' ).css( {opacity: 0.4} );
                }
            }
        );
		*/
		
		
		
		show_top();
	}
);

