var actTime   = 2000;
var actImage  = 2;
var act_thumb = 0;
var pos_thumb = 0;
var playing = false;
var nodes = new Array();
var displayURL = false;

var MOUSEX  = 0;
var MOUSEY  = 0;
var KEYCODE = 0;

var galleryName = '';
var isLoading = false;
var isAdvert    =  false;
var advert = {};

var IE = document.all;


if ( !IE ) document.captureEvents( Event.MOUSEMOVE );
document.onmousemove = getMouseXY;

if ( !IE ) document.captureEvents( Event.KEYPRESS );
document.onkeypress = getKeyCode;

function getKeyCode( e ) {
  if( e ) {
		 e = e;
	} else {
    e = window.event;
	}

  if( e.which ) {	 
    KEYCODE = e.which;
  } else {
    KEYCODE = e.keyCode;
  }
  keyPress();
}

function keyPress() {
  if( KEYCODE > 0 ) {
    //alert( KEYCODE );
    switch( KEYCODE ) {
      case 43:
        if( actTime < 30000 ) {
          mySlider.setValue( parseInt( actTime ) / 1000 + 1 );
          //set_time( parseInt( actTime ) / 1000 + 1 );
        }
      break;
      case 45:
        if( actTime > 2000 ) {
          mySlider.setValue( parseInt( actTime ) / 1000 - 1 );
          //set_time( parseInt( actTime ) / 1000 - 1 );
        }
      break;
      case 32:
        change_playing();
      break;
      case 49: change_effect( 0 ); break;
      case 50: change_effect( 1 ); break;
      case 51: change_effect( 2 ); break;
      case 52: change_effect( 3 ); break;
    }
  }
}

function getMouseXY( e ) {
  if (IE) {
    MOUSEX = event.clientX + document.body.scrollLeft;
    MOUSEY = event.clientY + document.body.scrollTop;
  } else {
    MOUSEX = e.pageX;
    MOUSEY = e.pageY;
  }
  if ( MOUSEX < 0 ) { MOUSEX = 0; }
  if ( MOUSEY < 0 ) { MOUSEY = 0; }
  mouseMove();
  return true;
}

function mouseMove() {
  $( 'gdesc' ).style.left = MOUSEX - 200 + 'px';
  $( 'gdesc' ).style.top  = MOUSEY + 'px';
  $( 'loading' ).style.left = MOUSEX + 'px';
  $( 'loading' ).style.top  = MOUSEY + 20 + 'px';
}

function show_tooltip( n, d ) {
  if( n != '' || d != '' ) {
    $( 'gdesc' ).innerHTML  = '<center><b>' + n + '</b>' + ( n != '' ? '<br />' : '' ) + d + '</center>';
    $( 'gdesc' ).style.display = 'block';
  }
}

function hide_tooltip() {
  $( 'gdesc' ).style.display = 'none';
  $( 'gdesc' ).innerHTML = '';
}

function change_browser( dir ) {
  actBrowser = !actBrowser;
  if( actBrowser ) {
    $( 'tree_view'     ).style.display = 'block';
    $( 'explorer_view' ).style.display = 'none' ;
    $( 'browser_img'   ).src = tplURI + 'images/header_small_1.gif';
    //$( 'tree_view' ).scrollUpdate();
  } else {
    $( 'tree_view'     ).style.display = 'none' ;
    $( 'explorer_view' ).style.display = 'block';
    $( 'browser_img'   ).src = tplURI + 'images/header_small_2.gif';
    //$( 'explorer_view' ).scrollUpdate();
  }
}

var actNode = '';

function get_node( id, level ) {
  //nodes[ id ] = !nodes[ id ];
  actNode = 'node_' + id;
  if( $( 'node_' + id ).innerHTML + '' != '' ) {
    $( 'node_' + id ).innerHTML = '';
    nodes[ id ] = false;
    refreshScrolls();
  } else {
    if( !isLoading ) {
      nodes[ id ] = true;
      showLoading();
      var myAjaxRequest = new Ajax.Request(
        baseURI + 'node/' + sID + '/' + id + '/' + gID + '/' + ( parseInt( level + 1 ) ) + '/',
        {
          method     : 'GET',
          parameters : '',
          onComplete : setNode
        }
      );
    }
  }
}
//      'node_' + id,
//        parameters : 'sid=' + sID + '&cid=' + id + '&gid=' + gID + '&level=' + ( parseInt( level + 1 ) )

function setNode( obj ) {
  $( actNode ).innerHTML = obj.responseText;
  hideLoading();
}

function refreshScrolls() {
  //$( 'tree_view' ).scrollUpdate();
  //$( 'explorer_view' ).scrollUpdate();
}

function search() {
  k = $( 'keyword' ).value;
  if( $( 'date_y' ).value ) {
    y = $( 'date_y' ).value;
    m = $( 'date_m' ).value;
    d = $( 'date_d' ).value;
  } else {
    y = date_y[ date_y.selectedIndex ].value;
    m = date_m[ date_m.selectedIndex ].value;
    d = date_d[ date_d.selectedIndex ].value;
  }
  
  if( k ) {
    var myAjaxUpdater = new Ajax.Updater(
      'searchcontent',
      baseURI + 'search/' + sID + '/' + k + '/' + y + '/' + m + '/' + d + '/',
      {
        method     : 'GET',
        parameters : ''
      }
    );
  } else {
    alert( 'Kérjük, adj meg egy keresendő kifejezést!' );
  }
}
//        parameters : 'sid=' + sID + '&keyword=' + k + '&date_y=' + y + '&date_m=' + m + '&date_d=' + d

function change_playing() {
  if( playing ) {
    playing = false;
    $( 'play_button' ).src = tplURI + 'images/play.gif';
  } else {
    playing = true;
    $( 'play_button' ).src = tplURI + 'images/stop.gif';
    step_image( true, true );
  }
}

function change_effect( index ) {
  if( actEffect != index ) {
    $( 'effect_img_' + actEffect ).src = tplURI + 'images/effect_' + actEffect + '_0.gif';
    actEffect = index;
    $( 'effect_img_' + actEffect ).src = tplURI + 'images/effect_' + actEffect + '_1.gif';
  }
}

var oldHTML = '';

function html_entity_decode(str) {
    var ta=document.createElement("textarea");
    ta.innerHTML=str.replace(/</g,"&lt;").replace(/>/g,"&gt;");
    return ta.value;
}

function haveAdvert( index ) {
	//alert( index );
	advert = {};
	ret_val=false;
	advertList.each( function( a ) {
		if( a.position == index )
		{
			advert = a;
			ret_val = true;
		}
		else return false;
	} );
	return ret_val;
}

function step_image( dir, mode ) {
  if( !mode || ( mode && playing ) ) {
    next_act_thumb = act_thumb;
    next_act_thumb += ( dir ? 1 : -1 );
    if( next_act_thumb < 0 ) next_act_thumb = count_thumbs - 1;
    if( next_act_thumb == count_thumbs ) next_act_thumb = 0;
	haveadv=haveAdvert(next_act_thumb);
  	if(haveadv && !isAdvert ) {
		oldHTML = document.getElementById( 'img_div_' + actImage ).innerHTML;
  		document.getElementById( 'img_div_' + actImage ).innerHTML = html_entity_decode(advert.code);
  		isAdvert = true;
		
  	} else {
  		document.getElementById( 'img_div_' + actImage ).innerHTML = oldHTML;
  		oldHTML = '';
  		isAdvert = false;
  		advert = {};
	    clickthumb( $( 'thumb_img_' + next_act_thumb ), next_act_thumb );
	    act_thumb = next_act_thumb;
	}
    if( playing ) {
      setTimeout( 'step_image( true, true )', actTime );
    }
  }
}

function scroll_thumb( dir ) {
  o = $( 'thumbs' );
  if( o.scrollLeft != o.scrollWidth - parseInt( o.style.width ) || !dir ) {
    pos_thumb += ( dir ? 1 : -1 );
    if( pos_thumb < 0 ) pos_thumb += 1;
    if( pos_thumb == count_thumbs ) pos_thumb -= 1;
    pos_thumb_to( pos_thumb );
  }
}

function overthumb( obj, index ) {
  if( index != act_thumb )
    obj.className = 'thumb_1';
}

function outthumb( obj, index ) {
  if( index != act_thumb )
    obj.className = 'thumb_0';
}

function clickthumb( obj, index ) {
  if( act_thumb != index ) {
    $( 'thumb_img_' + act_thumb ).className = 'thumb_0';
    act_thumb = index;
    obj.className = 'thumb_2';
    /*if( 
      $( 'thumb_img_' + act_thumb ).offsetLeft > ( parseInt( $( 'thumbs' ).style.width ) / 2 ) || 
      $( 'thumb_img_' + act_thumb ).offsetLeft < $( 'thumbs' ).scrollLeft
    )*/
    if( index > 2 || index == 0 )
      pos_thumb_to( index - ( index > 2 ? 2 : 0 ) );
    show_image( index );
  }
}

function pos_thumb_to( index ) {
  o = $( 'thumbs' );
  //pos = index * ( thumb_width + 10 );
  pos = $( 'thumb_img_' + index ).offsetLeft - 2;
  wait = Math.round( Math.abs( pos - o.scrollLeft ) / 25 );
  if( wait < 25 ) wait = 25;
  left = o.scrollLeft;
  if( left < pos ) {
    left += 20;
    if( left > pos || left > o.scrollWidth ) left = pos;
  } else {
    left -= 20;
    if( left < pos || left < 0 ) left = pos;
  }
  o.scrollLeft = left;
  if( ( left != pos && o.scrollLeft == left ) || ( left < parseInt( o.style.width ) - o.scrollWidth ) ) {
    setTimeout( 'pos_thumb_to(' + index + ')', wait );
  } else {
    if( o.scrollLeft != o.scrollWidth - parseInt( o.style.width ) ) {
      pos_thumb = index;
    }
  }
}

var nI = 2;

function pos_zoom_icon() {
  /*
  $( 'zoom_icon_' + nI ).style.left = Math.round( parseInt( parseInt( $( 'imageview' ).style.width ) - 20 - parseInt( $( 'img_' + nI ).width ) ) / 2 ) + ( parseInt( $( 'img_' + nI ).width  ) - 22 ) + 'px';
  $( 'zoom_icon_' + nI ).style.top  = ( parseInt( $( 'img_' + nI ).height ) - 22 ) + 'px';
  $( 'zoom_icon_' + nI ).style.display = 'block';
  */
}

var statcodeHTML = '';
function show_image( index ) {

  if( actImage == 1 ) {
    var nextImage = 2;
  } else {
    var nextImage = 1;
  }
  
  try {
    $( 'zoom_icon' + actImage ).style.display = 'none';
  } catch(e) {}
  
  nI = nextImage;
  img = new Image();
  img.src = imageList[ index ].normal_uri;
  img.onload = pos_zoom_icon;
  
  var uText = '';
//  if( displayURL )
//    uText = '<br />[A kép linkje: <a style="text-decoration:underline"target="_blank" href="' + baseURI + sID + '/show/' + gID + '/' + imageList[ index ].id + '/' + gallery_talking_name +'">' + baseURI + sID + '/show/' + gID + '/' + imageList[ index ].id + '/' + gallery_talking_name + '</a>]';
  
  if ( sID == 2 )
    $( 'img_div_' + nextImage ).innerHTML = '<img onClick="javascript:playing=true;change_playing();step_image(true)" id="img_' + nextImage + '" name="img_' + nextImage + '" src="' + imageList[ index ].normal_uri +  '" border="0" alt="" /><br/><img onclick="openLargeImage()" src="' + baseURI + 'templates/' + sID + '/images/zoom.gif" style="display:block;position:absolute;z-index:50000;bottom:0px;right:0px;border:0px;" />';
  else    
    $( 'img_div_' + nextImage ).innerHTML = '<img onClick="javascript:playing=true;change_playing();step_image(true)" id="img_' + nextImage + '" name="img_' + nextImage + '" src="' + imageList[ index ].normal_uri +  '" border="0" alt="" />';

  $( 'caption' ).innerHTML = imageList[ index ].caption + uText;
  $( 'gname' ).innerHTML = ( imageList[ index ].name ? imageList[ index ].name : galleryName );
  
  e = 'img_div_' + actImage;
  
  switch( actEffect ) {
    default:
    case 0:
      switch_image( actImage, nextImage );
    break;
    
    case 1:
      $( 'img_div_' + actImage ).style.backgroundColor = '';
      new Effect.Fade( e, { duration : 1, afterFinishInternal : function() { switch_image( actImage, nextImage ); } } );
    break;
    
    case 2:
      $( 'img_div_' + actImage ).style.backgroundColor = '';
      new Effect.Puff( e, { duration : 1, afterFinishInternal : function() { switch_image( actImage, nextImage ); } } );
    break;
    
    case 3:
      $( 'img_div_' + actImage ).style.backgroundColor = '';
      new Effect.DropOut( e, { duration : 1, afterFinishInternal : function() { switch_image( actImage, nextImage ); } } );
    break;
  }
  
  var myStatCodeUpdater = new Ajax.Updater(
    'statcode',
    baseURI + 'statcode.php?sid=' + sID + '&gid=' + gID,
    {
      method      : 'GET',
      parameters  : '',
      evalScripts : true
    }
  );
  
}

function replaceChars( out, add, entry ) {
  temp = "" + entry;
  while( temp.indexOf( out ) > -1 ) {
    pos = temp.indexOf( out );
    temp = "" + ( temp.substring( 0, pos ) + add + temp.substring( ( pos + out.length ), temp.length ) );
  }
  return temp;
}

function openLargeImage() {
  myimg = new Image();
  myimg.src = imageList[ act_thumb ].large_uri;
  uri = replaceChars( '/', 'per_per', imageList[ act_thumb ].large_uri );
  uri = replaceChars( ':', 'dbldot_dbldot', uri );
  uri = replaceChars( '.', 'dot_dot', uri );
  //alert( baseURI + 'popup_image/' + sID + '/' + uri + '/' );
  myimg.onload = function() {
    window.open( baseURI + 'popup_image/' + sID + '/' + uri + '/', 'viewerwindow', 'width=' + this.width + ',height=' + this.height + ',menu=0,menubar=0,status=0,scrollbars=0' );
  }
}

function switch_image( a, n ) {

  $( 'img_div_' + a ).style.zIndex = 10;
  $( 'img_div_' + n ).style.zIndex = 20;  
  $( 'img_div_' + a ).style.backgroundColor = '#FFFFFF';
  
  actImage = n;
  
}

function set_time( v ) {
  if( v == '' || parseInt( v ) ) {
    if( v != '' ) {
      if( v < 2 ) v = 2;
      if( v > 30 ) v = 30;
      actTime = v * 1000;
    }
  } else {
    v = actTime / 1000;
  }
  $( 'time_sec' ).value = v;
}

var mySlider = null;

function runGallery( photoID, dURL ) {

  if( dURL ) 
    displayURL = true;


  
  try {
    img = new Image();
    img.src = imageList[ 0 ].normal_uri;
    img.onload = pos_zoom_icon;
    $( 'zoom_icon_' + actImage ).style.display = 'block';
    
    galleryName = $( 'gname' ).innerHTML;
    
    var thumbs = document.getElementById( 'thumbs' );
    thumbs = thumbs.getElementsByTagName('table');
    thumbs = thumbs[0];

    
    thumbs.style.overflow = 'hidden';
    thumbs.style.width = '10px';
    thumbs.style.width = $( 'thumbs' ).scrollWidth + 'px';
    
    // Scrollbar
    //CSBfleXcroll( 'browsercontent' );
    //CSBfleXcroll( 'tree_view' );
    //CSBfleXcroll( 'explorer_view' );
    //CSBfleXcroll( 'explorer_view' );
    // Time slider
    mySlider = new Control.Slider(
      'time_handle',
      'time_track',
      { 
        range   : $R( 2, 30 ),
        values  : [ 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30 ], 
        onSlide : function( v ) { set_time( v ) },
        onChange: function( v ) { set_time( v ) }
      }
    );

    
   if ( sID != 2 )
      get_explorer( cID );

    if( photoID ) {
      for( i = 0; i < imageList.length; i++ )
        if( photoID == imageList[ i ].id ) {
          pos_thumb_to( i );
          clickthumb( $( 'thumb_img_' + i ), i );
          break;
        }
   }
        
  } catch(e) {}
  
}

function get_explorer( cid ) {
  
    if( !isLoading ) {
      if( actBrowser == 2 ) showLoading();
      var myAjaxUpdater = new Ajax.Updater(
        'explorer_view',
        baseURI + 'explorer/' + cid + '/' + sID + '/' + gID + '/',
        {
          method     : 'GET',
          parameters : '',
          onComplete : hideLoading()
        }
      );
    }
    
}
//        parameters : 'cid=' + cid + '&sid=' + sID + '&gid=' + gID

function showLoading() {
  $( 'loading' ).style.display = 'block';
  shBrowser( false );
  isLoading = true;
}

function hideLoading() {
  $( 'loading' ).style.display = 'none';
  shBrowser( true );
  isLoading = false;
}

function showPopmenu() {
  $( 'popmenu' ).style.display = 'block';
  $( 'popmenu' ).style.left = MOUSEX - 5 + 'px';
  $( 'popmenu' ).style.top  = MOUSEY - 5 + 'px';
}

function hidePopmenu() {
  $( 'popmenu' ).style.display = 'none';
}

function shBrowser( mode ) {
  o = $( 'browser' );
  t = $( 'tree_view' );
  e = $( 'explorer_view' );
  if( mode ) {
    setOpac( t, 0.99 );
    setOpac( e, 0.99 );
  } else {
    setOpac( t, 0.5 );
    setOpac( e, 0.5 );
  }
}

function setOpac( obj, opac ) {
  o.style.opacity = opac;
  o.style.mozOpacity = opac;
  o.style.khtmlOpacity = opac;
  o.style.filter = 'alpha(opacity=' + ( opac * 100 ) + ')';
}

function sbp_onLeftClick(code, evt)
{
var mbtn = (window.Event) ? evt.which-1 : evt.button;
if (mbtn==0) eval(code);
return true;
}
