var Istyle = {
	Frontpage : {},
	Article   : {}
};

Istyle.RegisterCallback = Class.create({
  initialize: function(tracks){
    this.tracks  = [];
    this.options = arguments[1] || { };
    this.addTracks(tracks);
  }
});
//new Istyle.RigisterClass('isa',{'preGoto','postGoto','onPause','onPlay','onStop'})
Istyle.RegisterClass = Class.create({
  initialize: function(obj){
    this.tracks  = [];
    this.options = arguments[1] || { };
  }
});
Istyle.Article.Pager = Class.create({
  initialize: function(obj){
    this._current = 0;
	this._max = 0;
  },
  gotoPage:	function(nr)
  {
	$('link_'+this._current).removeClassName('current');
	$('page_'+nr).show();
	$('page_'+this._current).hide();
	$('link_'+nr).addClassName('current');
	this._current = nr;
  },
  next: function()
  {
	if(this._current+1 != this._max)
	  this.gotoPage(this._current+1);
  },
  previous: function()
  {
  if(this._current-1 !=-1 )
	gotoPage(this._current-1);
  }
});
Istyle.Article.ImageText = Class.create({
  initialize: function(holder)
  {
	var e = document.getElementsByClassName(holder);
	for(i=0; i < e.length; i++)
	{
	  var imgs = e[i].getElementsByTagName('img');
	  imgs = imgs[0];

      var text = Builder.node('p', {
        style: ('width:'+imgs.width+'px;')
      });

      text.appendChild(Builder.node('strong', {},imgs.title));
      text.appendChild(Builder.node('br'));
	  text.appendChild(Builder.node('span',imgs.alt));
	  e[i].appendChild(text);
	}
  }
});
var _istyle_article_slideshow = null;
var _ias = null;
var _istyle_article_slideshow_ = null;
var _istyle_article_slideshow_t = null;
var __nr_of_slideshows = 0;
var __q;
Istyle.Article.Slideshow = Class.create({
  initialize: function(cl)
  {
  	var nr = __q = (++__nr_of_slideshows);

	var e = document.getElementsByClassName(cl);
	if(e.length!=0){
		var items = Gallery.Parser(e[0]);
	    var imgs = e[0].getElementsByTagName('img');
      	var container = $('gallery_pic'+nr);
      	if (container===null)
      	{
          var img = Builder.node('img', {
            id: 'gallery_pic'+nr,
            src: '#',
            width:imgs[0].width
          });
      	}

		var attr = e[0].className.split(' ');
		var scroll = 0;
		var move = function()
		{
			moveTo(scroll+50);
		}
		var movep = function()
		{
			moveTo(scroll-50);
		}
		var moveTo = function(pos)
		{
//			console.log(pos);
			var em = $('gallery_thumbs'+nr);
			//em.scrollLeft = scroll;
			scroll = em.scrollLeft;
			new Effect.Tween(em, scroll, pos, function(p){em.scrollLeft = p;});
			//em.Tween(scroll, pos);
			scroll = pos;
		}
		if(attr.indexOf('t') != -1)
		{
	      var thumbs = Builder.node('div', {
	        id: 'gallery_thumbs'+nr
	      });
	      var thumbsn = Builder.node('div', {
	        id: 'gallery_thumbs'+nr+"next",
	        style: 'float:right;'
	        },' >');
	      var thumbsp = Builder.node('div', {
	        id: 'gallery_thumbs'+nr+"prev",
	        style: 'float:left;'
	        },'< ');
			e[0].appendChild(thumbsp);
			e[0].appendChild(thumbsn);
			e[0].appendChild(thumbs);
			Event.observe('gallery_thumbs'+nr+"next",'mousedown',move);
			Event.observe('gallery_thumbs'+nr+"prev",'click',movep);
			Event.observe('gallery_thumbs'+nr+"next",'click',move);
		}



		e[0].appendChild(img);
		e[0].show();
		var c = null;
		var options = {
	  		pictureId : 'gallery_pic'+nr,
	  		thumbHolderId : 'gallery_thumbs'+nr,
	  		autostart : true,
	  		interval  : 5,
	  		onLoad : function(items){
	  			$('gallery_pic'+nr).src = items[0].image;
	  		},
	  		beforChange: function(id, current, items){
			  if(attr.indexOf('t') != -1){
			  	var x = 60*id;
			  	if(id==0)
			  	    moveTo(0);
			  	if(id > 0)
			  		moveTo(x-(370/4));

	    		c.getThumbElement(current).removeClassName('gallery_highlight');
	  			c.getThumbElement(id).addClassName('gallery_highlight');
	  		  }
	  		},
	  		onClick : function(event) {
	  		  if(attr.indexOf('t') != -1){
		  		var id = Event.findElement(event,'img');
		  		if(!Object.isUndefined(id)){
		  			id = id.id.split('_');
				  	c.resetTimer();
		  			c.gotoId(id[id.length-1]);
		  		}
	  		  }
	  		}
		};
	  	if(attr.indexOf('t') != -1)
	  	{
		  c =  new Gallery.SlideshowWithThumbnail(options);
	  	}
	  	else
	  	{
	      c =  new Gallery.Slideshow(options);
	  	}
		c.load(items.items);
		var img = new Image();
		img.src = items.items[0].thumb;
		e[0].style.width = img.width+'px';
	  	if(attr.indexOf('t') != -1)
	  	{
	  	  var width = $(options.thumbHolderId).offsetWidth;
	  	//  $(options.thumbHolderId).style="overflow: hidden; width: 400px; height: 55px; white-space: nowrap;";
	      $(options.thumbHolderId).style.overflow = 'hidden';
	  	  $(options.thumbHolderId).style.height= '55px';
	  	  $(options.thumbHolderId).style.width= (img.width-30)+'px';
	  	  $(options.thumbHolderId).style.whiteSpace='nowrap';
	//  	  console.log(width);
	  //	  console.log($(options.thumbHolderId));
	  	}
	//	console.log(img.width);
		//console.log(e[0]);
	}
  }
});

Istyle.Frontpage.Slideshow = Class.create({
  initialize: function(file)
  {
  	//SlideshowWithThumbnail
  	var c =  new Gallery.SlideshowWithThumbnail({
  		pictureId : 'gallery_pic',
  		thumbTag  : 'li',
  		thumbHolderId : 'gallery_thumbs',
  		autostart : true,
  		interval  : 10,
  		onLoad : function(items){
  			$('gallery_pic').src = items[0].image;
  		},
  		beforChange: function(id, current, items){
    		c.getThumbElement(current).removeClassName('gallery_highlight');
  			c.getThumbElement(id).addClassName('gallery_highlight');
  		},
  		onClick : function(event) {
	  		var id = Event.findElement(event,'img');
	  		if(!Object.isUndefined(id)){
	  			id = id.id.split('_');
	  			c.gotoId(id[id.length-1]);
	  		}
  		},
  		onChange : function(id, current, items)
  		{
  		    //console.log('cpasd');
            if(typeof($('text_title').getElementsByTagName("a")[0]) != "undefined")
            {
                var x = $('text_title').getElementsByTagName("a")[0];
                x.parentNode.removeChild(x);
            }
            var a = Builder.node('a', {
                href: items[id].url
              },items[id].title
            );
            if(typeof($('gallery_text').getElementsByTagName("a")[0]) != "undefined")
            {
                var x = $('gallery_text').getElementsByTagName("a")[0];
                x.parentNode.removeChild(x);
            }
            var aT = Builder.node('a', {
                href: items[id].url
              },items[id].text
            );

          	$('text_title').appendChild(a);
          	$('text_author').innerHTML = items[id].author;
          	$('text_date').innerHTML = items[id].date;
          	$('gallery_text').appendChild(aT);
          	/*$('gallery_text').innerHTML = items[id].text;*/
          	$('text_more').href = items[id].url;
            $('text_date').appear();
            $('text_author').appear();
            $('text_title').appear();
            $('text_more').appear();
            $('gallery_text').appear();
  		}
  	 });
	new Ajax.Request('/'+file+'.json', {
        method:'get',
        requestHeaders: {Accept: 'application/json'},
        onSuccess: function(transport){
        	var t = transport.responseText.evalJSON().items;
          c.load(t);
        }
    });
  }
});