
$( document ).ready( function() { 
	initRefScroll();
	initSlogenPager();
	staticRef();
  $('#ref_pager').show();
} );

var typeList = new Array();
typeList['logo'] = "Logó";
typeList['website'] = "Honlap";

var openFilter_top = "180px";
var openFilter_height = "16px";

/* --- referencia scroll --- */

var actRSItem = 0;
var totalRSItem = 0;
var xml_reference;
var firstRSclick = true;

function initRefScroll() {
	totalRSItem = $( 'li.item' ).length;
	$.ajax({
             type: "GET",
             url: "../references/xml/references.xml",
             dataType: "xml",
             success: function( xml ) {
             		xml_reference = xml;
               }
             }); // $.ajax(
	totalRSItem = $( 'li.item' ).length;
	$( '.ref_prev' ).click( function(){ rightRefScroll() } );
	$( '.ref_next' ).click( function(){ leftRefScroll() } );
  $( '.ref_open_filter' ).click( function(){ openRefFilter() } );
  $( '.ref_filter' ).click( function(){ showFilter( $( this ).attr( 'rel' ) ) } );
	showRSArrows();
}

function leftRefScroll() {
	if ( firstRSclick ){
		firstRSclick = false;
		showRefline( '#references', 3 );
    totalRSItem = $( 'li.item' ).length;
    $( '#references .ref_ul' ).width( ( ( totalRSItem + 1 ) * 330 ) + 5 );
	}
	if ( actRSItem < totalRSItem - 3 ) {
		actRSItem += 1;
		var actPos = actRSItem * -330; 
		$( 'ul.ref_ul' ).animate({left: actPos }, "slow", function(){ showRSArrows() } );
	}
} // if ( firstRSclick )

function rightRefScroll() {
	if ( actRSItem > 0 ) {
		actRSItem -= 1;
		var actPos = actRSItem * -330; 
		$( 'ul.ref_ul' ).animate({left: actPos}, "slow", function(){ showRSArrows() } );
	}
}

function showRSArrows(){
	if ( actRSItem > 0 ) { $( '#ref_pager .ref_prev' ).show(); } else { $( '#ref_pager .ref_prev' ).hide(); } 
	if ( actRSItem < totalRSItem - 3 ) { $( '#ref_pager .ref_next' ).show(); } else { $( '#ref_pager .ref_next' ).hide(); }
}

function openRefFilter() {
  openFilter_top = ( openFilter_top == "140px" ) ? "180px" : "140px"
  openFilter_height = ( openFilter_height == "53px" ) ? "16px" : "53px"
  $( '#ref_filter' ).animate({top: openFilter_top , height: openFilter_height}, "slow" );
}

function showFilter( filter ){
	if ( firstRSclick ){ 
		firstRSclick = false; 
		showRefline( '#references', 3 );
	} // if ( firstRSclick )

	$( 'li.item' ).hide(); 
	$( 'li.' + filter ).show(); 
	totalRSItem = $( 'li.' + filter ).length;
	actRSItem=0;
	var actPos = 0; 
  $( '#references .ref_ul' ).width( ( ( totalRSItem + 1 ) * 330 ) + 5 );
	$( '#references .ref_ul' ).animate({left: actPos }, "slow", function(){ showRSArrows() } );
	
	openRefFilter()
}

/* --- szlogen pager --- */


(function($) {
	$.fn.customFadeIn = function(speed, callback) {
		$(this).fadeIn(speed, function() {
			if(jQuery.browser.msie)
				$(this).get(0).style.removeAttribute('filter');
			if(callback != undefined)
				callback();
		});
	};
	$.fn.customFadeOut = function(speed, callback) {
		$(this).fadeOut(speed, function() {
			if(jQuery.browser.msie)
				$(this).get(0).style.removeAttribute('filter');
			if(callback != undefined)
				callback();
		});
	};
})(jQuery);



var actSPItem = 0;
var totalSPItem = 0;
var timeoutSP = 0;

function initSlogenPager() {
	$( '#slogen_box li.tab' ).click( function() {
		clearTimeout( timeoutSP ); 
		$( '#slogen_box li.tab' ).removeClass( 'activetab' ); 
		$( this ).addClass( 'activetab' );
		$( '#slogen_box li.tab' ).eq( actSPItem ).children( 'ul' ).customFadeOut( 500 );
		$( this ).children( 'ul' ).customFadeIn( 1000 );
		actSPItem = $( '#slogen_box li.tab' ).index( $( this )[0] );
		timeoutSP = setTimeout( "nextSlogenPager()" , 4000 ); 
	} );
	$( '#slogen_box li.tab ul' ).mouseover( function() { clearTimeout( timeoutSP ); } );
	$( '#slogen_box li.tab ul' ).mouseout( function() { timeoutSP = setTimeout( "nextSlogenPager()" , 4000 ); } );

	totalSPItem = $( '#slogen_box li.tab' ).length;
	timeoutSP = setTimeout( "nextSlogenPager()" , 4000 );
}

function nextSlogenPager() {
	clearTimeout( timeoutSP );
	$( '#slogen_box li.tab' ).eq( actSPItem ).children( 'ul' ).customFadeOut( 500 );
	actSPItem = ( actSPItem < totalSPItem - 1 ) ? actSPItem + 1 : 0;
	$( '#slogen_box li.tab' ).removeClass( 'activetab' );	$( '#slogen_box li.tab' ).eq( actSPItem ).addClass( 'activetab' );
	$( '#slogen_box li.tab' ).eq( actSPItem ).children( 'ul' ).customFadeIn( 1000 );
	timeoutSP = setTimeout( "nextSlogenPager()" , 4000 );
}


function staticRef(){
	$('#ref_list ul.ref_ul').empty();
	showRefline( '#ref_list', 0 );
}

function showRefline( target, start ){

	$( xml_reference ).find( 'item' ).each( function( i ){
//			alert( $( this ).find( 'desc' ).text() );
	 		if ( i > start ){
	     var category = $( this ).attr( 'type' );
	     var linktype = $( this ).attr( 'linktype' );
	     var pic = $( this ).find( 'pic' ).text();
	     var desc = $( this ).find( 'desc' ).text();
	     var url = $( this ).find( 'url' ).text();
	     var client = $( this ).find( 'client' ).text();
	     $( target + ' .ref_ul' ).append( '<li class="item ref_' + category + '" id="refitem_' + i + '"></li>' );
	     
	     if ( linktype == 'url' ){ $( '#refitem_' + i ).append( '<a class="ref_open ' + linktype + '" href="' + url + '" title="' + client + ', ' + desc + '"></a>' ); }
	     else { $( '#refitem_' + i ).append( '<a class="ref_open ' + linktype + ' newthickbox" rel="logogallery" href="../references/' + pic + '" title="' + client + ', ' + desc + '"></a>' ); }
			 $( '#refitem_' + i ).append( '<img src="../references/tn/' + pic + '" alt="site03" /><ul class="desc"><li></li></ul>' );
			 $( '#refitem_' + i + ' ul.desc li' ).append( '<span class="ref_label">típus: </span><span class="ref_type">' + typeList[ category ] + '</span>' );
			 $( '#refitem_' + i + ' ul.desc li' ).append( '<span class="ref_label">url: </span><span class="ref_url">' + url + '</span>' );
			 $( '#refitem_' + i + ' ul.desc li' ).append( '<span class="ref_label">munka: </span><span class="ref_desc">' + desc + '</span>' );
			 $( '#refitem_' + i + ' ul.desc li' ).append( '<span class="ref_label">megrendelő: </span><span class="ref_client">' + client + '</span>' );
	    } // $( xml )
	 }); // each

	 tb_init('a.newthickbox');
}

