var ImageLoader = new Class({

	initialize: function(el) {
		this.element = $(el);
		this.image = (this.element.get('tag') == 'img')
			? this.element
			: this.element.getElement('img');
		this.originalSrc = this.image.src;
		this.element.addClass('loading');
		this.loader = new Image();
		this.loader.onload = this.onLoad.bind(this);
		this.loader.onerror = this.onError.bind(this);
		this.loader.src = this.originalSrc;
	},
	
	complete: function() {
		this.image.src = this.loader.src;
		this.element.removeClass('loading');
	},
	
	onError: function() {
		this.retry.delay(5000, this);
	},
	
	onLoad: function() {
		this.complete();
	},
			
	retry: function() {
		this.loader.src = this.originalSrc + (this.originalSrc.indexOf('?') < 0 ? '?' : '&') + 'ts=' + (new Date()).getTime();
	}
	
});


Element.implement({

	enableImageLoader: function() {
		return new ImageLoader(this);
	},
	show: function() {
		this.setStyle('display', 'block');
		return this;
	},
	hide: function() {
		this.setStyle('display', 'none');
		return this;
	},
	toggle: function() {
		if (this.visible()) {
			this.hide();
		}
		else {
			this.show();
		}
	},
	visible: function() {
		return this.getStyle('display', 'none') != 'none';
	}

});


window.addEvent('domready', function() {

/*  $$('.product-card-item p.thumbnail img').enableImageLoader();
*/

  $$('.product-card-item p.thumbnail').enableImageLoader();

  $$('ul.tabs li').each(function(el) {
	
/*	var rel = $(el.get('rel'));
	
    if(el.match('.selected')) {
	  rel.show();
    }
    else {
      rel.hide();
    }*/
	
	el.adopt(new Element('span'));
	
  /*el.addEvent('click', function() {
		var siblings = $$(this.getAllPrevious(), this.getAllNext());
		siblings.each(function(el) {
			$(el.get('rel')).hide();
		});
		rel.show();
		siblings.removeClass('selected');
		this.addClass('selected');
	});*/
  });
  
  $$('#popup .cancel').addEvent('click', function() {
	$$('#dim, #popup').hide();
	$$('#popup-content').empty();
  });
  
  $$('table.item-list tr:even').addClass('even');
  $$('table.item-list tr:odd').addClass('odd');
  
  var rows = $$($$('table.item-list tr').slice(1));
  
  rows.addEvent('click', function(e) {
	
    if(!$(e.target).match('input')) {
      $(this).getElements('input[type=checkbox]').each(function() {
        if(this.checked) {
          this.checked = false;
        }
        else {
          this.checked = true;
        }
      });
    }
	
  });
  
  
  /**
   * Quicksearch
   */
  
  $$('.quick-search input').addEvent('keyup', function() {
	
    var search_word = this.get('value').toLowerCase();
    
    $$('#thumbnails .product-card-item').each(function(el) {
      if(el.get('text').toLowerCase().indexOf(search_word) != -1) {
        el.show();
      }
      else {
        el.hide();
      }
    });

    $$('#thumbnails tr').each(function(el) {
	  if (el.match(':first')) return;
      if(el.get('text').toLowerCase().indexOf(search_word) != -1) {
        el.show();
      }
      else {
        el.hide();
      }
    });
	
  });
  
  
  /**
   * Advanced menu
   */
  
  var hideMenus = function() {
	$$('.dropdown > ul').hide();
    $$('.dropdown > a').removeClass('hover');
  }
  
  hideMenus();
  
  $$('.advanced-menu .dropdown').each(function(el) {
	
    el.getElement('a').addEvent('click', function(e) {
		
	  hideMenus();
		
	  closeInfo();
      
	  $(this).getNext('ul').toggle();
	  
      if($(this).getNext('ul').visible()) {
        $(this).addClass('hover');
      }
      else {
        $(this).removeClass('hover');
      }
	  
	  e.stop();
	  
    });
	
	$$('.advanced-menu li').addEvents({
		mouseenter: function() {
			$(this).addClass('hover');
		},
		mouseleave: function() {
			$(this).removeClass('hover');
		}
	});
    
    el.getElements('ul li').addEvent('click', function(e) {
	  hideMenus();
    });
	
  });
  
  $$('body').addEvent('click', function(e) {
	hideMenus();
  });
  
});

function openInfo(section) {
  /*$('section-info').show();*/
  var tool = $('tool-' + section);
  var siblings = $$(tool.getAllPrevious('div'), tool.getAllNext('div'));
  $('tool-' + section).show();
  siblings.hide();
  $('main').setStyle('right', 341);
}




function showDynamicTree(val,org) {
  $('operations-packet').hide();
  $('operations-remove').hide();
   $('folderselector').show();
   if (val==1){
    $('movebutton').hide();
    $('copybutton').show();
   }
    else{
    $('copybutton').hide();
    $('movebutton').show();
    }
/*  alert(parent.frames['selector']);
   if (undefined==parent.frames['selector']){
  
   }*/
/*   else {*/
     window.frames[0].document.location='folders?ac=folderlist&folder_id=0&org='+org+'&mode='+val;
/*   }*/
 
}
function hideDynamicTree() {
    $('copybutton').hide();
    $('movebutton').hide();
  $('folderselector').hide();
}

function goView(jump,view){
document.forms['files'].actionsel.value=0;
document.forms['files'].jump.value=jump;
document.forms['files'].view.value=view;
document.forms['files'].submit();
}

function closeInfo() {
/*  $('section-info').hide();*/
  $('main').setStyle('right', 0);
}

function reloadLeftMenu( company )
{
	var leftMenu = window.parent.menu;
	if( leftMenu != null )
	{
		leftMenu.location = '../digtator/campaigns?ac=listcampaigns&org=1&selectedcompany=' + company ;
	}
}


function selectAll (){

  daFiles = document.forms['files'].elements;

    for (i = 0; i < daFiles.length; i++)
   {
   elemName = daFiles[i].name;
         if (elemName.substring(0, 8) == 'filesel_')
	       {
	               if (daFiles[i].checked == false)
		               {
			           daFiles[i].checked = true;
		           }
         }
   }
}

function selectNone (){

  daFiles = document.forms['files'].elements;


    for (i = 0; i < daFiles.length; i++)
   {
   elemName = daFiles[i].name;
   if (elemName.substring(0, 8) == 'filesel_'){
              if (daFiles[i].checked == true)  {
	           daFiles[i].checked = false;
	      }
  }
  }
}

function showfl(jump){
document.forms['files'].jump.value=0;
document.forms['files'].submit();
}



function showRemoveFiles() {
    $('copybutton').hide();
    $('movebutton').hide();
  $('folderselector').hide();
  $('operations-packet').hide();
  $('operations-remove').show();
}
function hideRemoveFiles() {
  $('operations-remove').hide();
}

function showPackage() {
  $('copybutton').hide();
  $('movebutton').hide();
  $('folderselector').hide();
  $('operations-remove').hide();
  $('operations-packet').show();
}
function hidePackage() {
  $('operations-packet').hide();
}





