/**
 * @author Daniel
 */


$(function () {
	$(".statusbox").hide();
	$("#dopplrlogo").hover(function() { $("#dopplrdiv").show(); }, function() { $("#dopplrdiv").hide(); } );
	$("#club").hover(function() { $("#club>ul").slideDown(); }, function() { $("#club>ul").slideUp(); } );
	$("#people").hover(function() { $("#people>ul").slideDown(); }, function() { $("#people>ul").slideUp(); } );
	$("#conference").hover(function() { $("#conference>ul").slideDown(); }, function() { $("#conference>ul").slideUp(); } );
	$("#debate").hover(function() { $("#debate>ul").slideDown(); }, function() { $("#debate>ul").slideUp(); } );
	$("#admin_content, #sidebar a").click(function() { $(".menu-content").slideUp(); });
}); 

$(document).ready(function() {
	
	function formatItem(row) {
		return row[0] + " (<em>" + row[1] + "</em>)";
	}
	function formatResult(row) {
		return row[0].replace(/(<.+?>)/gi, '');
	}

	
	item = $("#item").val()
	$('#termlist').load('http://dwms.eu/api.php?terms='+item);

	$('.collapse-container').
  		 click(function() {
  			$(this).children().fadeIn('slow');
		});
	
	$('#fb-toggle').
  		 toggle(function() {
  			$('.facebook').fadeOut('slow');
		}, function() {
  			$('.facebook').fadeIn('slow');
		});
	$('#flickr-toggle').
  		 toggle(function() {
  			$('.flickr').fadeOut('slow');
		}, function() {
  			$('.flickr').fadeIn('slow');
		});
	$('#twitter-toggle').
  		 toggle(function() {
  			$('.twitter').fadeOut('slow');
		}, function() {
  			$('.twitter').fadeIn('slow');
	});
	$('#lastfm-toggle').
  		 toggle(function() {
  			$('.lastfm').fadeOut('slow');
		}, function() {
  			$('.lastfm').fadeIn('slow');
	});
	
	
      // set the image hidden by default    
      //$('#icon-bar').fadeIn(3000);
  $.fn.infiniteCarousel = function () {

    function repeat(str, num) {
        return new Array( num + 1 ).join( str );
    }
  
    return this.each(function () {
        var $wrapper = $('> div', this).css('overflow', 'hidden'),
            $slider = $wrapper.find('> ul'),
            $items = $slider.find('> li'),
            $single = $items.filter(':first'),
            
            singleWidth = $single.outerWidth(), 
            visible = Math.ceil($wrapper.innerWidth() / singleWidth), // note: doesn't include padding or border
            currentPage = 1,
            pages = Math.ceil($items.length / visible);            


        // 1. Pad so that 'visible' number will always be seen, otherwise create empty items
        if (($items.length % visible) != 0) {
            $slider.append(repeat('<li class="empty" />', visible - ($items.length % visible)));
            $items = $slider.find('> li');
        }

        // 2. Top and tail the list with 'visible' number of items, top has the last section, and tail has the first
        $items.filter(':first').before($items.slice(- visible).clone().addClass('cloned'));
        $items.filter(':last').after($items.slice(0, visible).clone().addClass('cloned'));
        $items = $slider.find('> li'); // reselect
        
        // 3. Set the left position to the first 'real' item
        $wrapper.scrollLeft(singleWidth * visible);
        
        // 4. paging function
        function gotoPage(page) {
            var dir = page < currentPage ? -1 : 1,
                n = Math.abs(currentPage - page),
                left = singleWidth * dir * visible * n;
            
            $wrapper.filter(':not(:animated)').animate({
                scrollLeft : '+=' + left
            }, 500, function () {
                if (page == 0) {
                    $wrapper.scrollLeft(singleWidth * visible * pages);
                    page = pages;
                } else if (page > pages) {
                    $wrapper.scrollLeft(singleWidth * visible);
                    // reset back to start position
                    page = 1;
                } 

                currentPage = page;
            });                
            
            return false;
        }
        
        $wrapper.after('<a class="arrow back">&lt;</a><a class="arrow forward">&gt;</a>');
        
        // 5. Bind to the forward and back buttons
        $('a.back', this).click(function () {
            return gotoPage(currentPage - 1);                
        });
        
        $('a.forward', this).click(function () {
            return gotoPage(currentPage + 1);
        });
        
        // create a public interface to move to a specific page
        $(this).bind('goto', function (event, page) {
            gotoPage(page);
        });
    });  
};

$(document).ready(function () {
 // $('.infiniteCarousel').infiniteCarousel();
});

                         
	
});
function initOverLabels () {
  if (!document.getElementById) return;      

  var labels, id, field;

  // Set focus and blur handlers to hide and show 
  // labels with 'overlabel' class names.
  labels = document.getElementsByTagName('label');
  for (var i = 0; i < labels.length; i++) {

    if (labels[i].className == 'overlabel') {

      // Skip labels that do not have a named association
      // with another field.
      id = labels[i].htmlFor || labels[i].getAttribute('for');
      if (!id || !(field = document.getElementById(id))) {
        continue;
      } 

      // Change the applied class to hover the label 
      // over the form field.
      labels[i].className = 'overlabel-apply';

      // Hide any fields having an initial value.
      if (field.value !== '') {
        hideLabel(field.getAttribute('id'), true);
      }

      // Set handlers to show and hide labels.
      field.onfocus = function () {
        hideLabel(this.getAttribute('id'), true);
      };
      field.onblur = function () {
        if (this.value === '') {
          hideLabel(this.getAttribute('id'), false);
        }
      };

      // Handle clicks to label elements (for Safari).
      labels[i].onclick = function () {
        var id, field;
        id = this.getAttribute('for');
        if (id && (field = document.getElementById(id))) {
          field.focus();
        }
      };

    }
  }
};

function hideLabel (field_id, hide) {
  var field_for;
  var labels = document.getElementsByTagName('label');
  for (var i = 0; i < labels.length; i++) {
    field_for = labels[i].htmlFor || labels[i].getAttribute('for');
    if (field_for == field_id) {
      labels[i].style.textIndent = (hide) ? '-1000px' : '0px';
      return true;
    }
  }
}
window.onload = function () {
  setTimeout(initOverLabels, 50);
};

// AJAX Data
function createTerm(){
		$.ajax
		({
			type: "POST",
			url: "http://dwms.eu/api.php?maketerm=1",
			data: {
				parent: $("#relationship").val(),
				type: $("#type").val(),
				item: $("#item").val(),
				name: $("#term-name").val()
			},
			success: function(){
				$('#termlist').load('http://dwms.eu/api.php?terms=' + item);
				$("#term-name").val('');
				$("#relationship").val('');
			}
		});
	
}
function linkTerm(){
	$.ajax({
		type: "POST",
		url: "http://dwms.eu/api.php?link=1",
		data: {
			item: $("#item").val(),
			term: $("#term").val()
		},
		success: function(){
			$('#termlist').load('http://dwms.eu/api.php?terms=' + item);
			$("#term").val('');
		}
	});
}

