$(function() {
	$('#search').blur(function() {
		var searchbar = document.getElementById('search');

		if(searchbar.value == '')
		searchbar.value = searchbar.defaultValue; 
	});

	$('#search').focus(function() {
		var searchbar = document.getElementById('search');

		if (searchbar.value == searchbar.defaultValue)
			searchbar.value = '';
	});

	$('#text-5 h2').click(function() { toggleMenu('#text-5 div'); });
	$('#text-4 h2').click(function() { toggleMenu('#text-4 div'); });
	$('#linkcat-2 h2').click(function() { toggleMenu('#linkcat-2 ul'); });
	$('#archives-3 h2').click(function() { toggleMenu('#archives-3 ul'); });

	hideMenu('#text-5 div');
	hideMenu('#text-4 div');
	hideMenu('#linkcat-2 ul');
	hideMenu('#archives-3 ul');


	var basepath = "http://blog.atribecallednext.com/";
	var theme = "wp-content/themes/atcn2/";

	for (i = 0; i < banners.length; i++) {
		Preloader.add(banners[i]);
		$('.anythingSlider .wrapper ul').append(
			'<li style="background-image: url(\'' + banners[i] + '\')"><a href="' + anchrs[i] + '"><div class="feature"><h2>' + descs[i] + '</h2></div></a></li>');
	}
	
	Preloader.onFinish(function(){
		$('.anythingSlider').anythingSlider({
			easing: 'swing',
			autoPlay: true,
			delay: 6000,
			startStopped: false,
			animationTime: 1500,
			hashTags: false,
			buildNavigation: false,
			pauseOnHover: true});
	});
	Preloader.load();
	
	/* preload not necessary */
	$('#text-6 .textwidget').anythingSlider({
		easing: 'swing',
		autoPlay: true,
		delay: 2700,
		startStopped: false,
		animationTime: 1000,
		hashTags: false,
		buildNavigation: false,
		pauseOnHover: true});
		
	swfobject.embedSWF(basepath + theme + "images/banner/mpu_stayingalive_v2.swf", "mtv_swf", "300", "340", "9.0.0");
	swfobject.embedSWF(basepath + theme + "images/banner/rcaclass300x340.swf", "sade_swf", "300", "340", "9.0.0");
});

var active = '';
function showMenu(name) {
	$(name).css('display', 'block');

	if (active != '') {
		$(active).css('display', 'none');
	}
	active = name;
}

function hideMenu(name) {
	$(name).css('display', 'none');
	
	if (active == name) {
		active = '';
	}
}

function toggleMenu(name) {
	if ($(name).css('display') == 'block') {
		$(name).css('display', 'none');
		active = '';
	} else {
		if (active != '') {
			$(active).css('display', 'none');
		}
		$(name).css('display', 'block');
		active = name;
	}
}


var Preloader = {
  callbacks: [],
  images: [],
  loadedImages: [],
  imagesLoaded: 0,

  add: function(image){
    if (typeof image == 'string') this.images.push(image);
    if (typeof image == 'array' || typeof image == 'object'){
      for (var i=0; i< image.length; i++){
        this.images.push(image[i]);
      }
    }
  },
  onFinish: function(func){
    if (typeof func == 'function') this.callbacks.push(func);
    if (typeof func == 'array' || typeof func == 'object'){
      for (var i=0; i< func.length; i++){
        this.callbacks.push(func[i]);
      }
    }
  },
  load: function(){
    for(var i=0; i<this.images.length; i++){
      this.loadedImages[i] = new Image();
      this.loadedImages[i].onload = function(){ Preloader.checkFinished.apply(Preloader) }
      this.loadedImages[i].src = this.images[i];
    }
  },

  checkFinished: function(){
    this.imagesLoaded++;
    if (this.imagesLoaded == this.images.length) this.fireFinish();
  },
  fireFinish: function(){
    for (var i=0; i<this.callbacks.length; i++){
      this.callbacks[i]();
    }
    this.images = [];
    this.loadedImages = [];
    this.imagesLoaded = 0;
    this.callbacks = [];
  }
}