/* Sliding Caption Start */
$(document).ready(function(){
	$('.grid-3').hover(function(){
		$(".overlay-red", this).stop(true, true).fadeIn({queue:true,duration:200});
	}, function() {
		$(".overlay-red", this).stop(true, true).fadeOut({queue:true,duration:200});  
	});
});


/* Slideshow */ 

// $('img.photo',this).imagesLoaded(myFunction)
// execute a callback when all images have loaded.
// needed because .load() doesn't work on cached images

// mit license. paul irish & andre? hansson. 2010.

// callback function is passed the last image to load
//   as an argument, and the collection as `this`


// Added support for calling the callback on each image load event
// 

// 'fireOne' argument is optional, if set, will invoke the callback once for every
// image in the 'this' collection, thus making 'this' in the callback that element alone
// If it's not used, the callback will be invoked once all the images in the collection has
// been loaded. And 'this' will be the jQuery collection of the filtered 'img' elements.
$.fn.imagesLoaded = function(callback, fireOne) {
  var
    args = arguments,
    elems = this.filter('img'),
    elemsLen = elems.length - 1;
	  elems.bind('load', function(e) {
        if (fireOne) {
            !elemsLen-- && callback.call(elems, e);
        } else {
            callback.call(this, e);
        }
    }).each(function() {
        // cached images don't fire load sometimes, so we reset src.
        if (this.complete || this.complete === undefined){
            this.src = this.src;
        }
    });
}

$(document).ready(function(){
  var images = $(".slideshow img");
  images.parent().addClass("loading");
  images.hide();
  images.imagesLoaded(function() {
		slideShow($(this),5000);
  },true);
});

function slideShow(obj,speed) {
	if (!obj.length) return;
	if (obj.length <= 1) return;
	var array = [];
	var auto = true;
	var direction = "forward";
	var fn = {
		loop: function() {
			if (!auto) return;
			fn.blend();
			setTimeout(fn.loop, speed );
		},
		blend: function() {
			var current = obj.filter(".current").fadeOut("slow").removeClass("current");
			if (direction == "forward") current = (current.next().length) ? current.next() : obj.first();
			if (direction == "back") current = (current.prev().length) ? current.prev() : obj.last();
			current.fadeIn("slow").addClass("current");
		},
		getLargest: function(array){ 
			return Math.max.apply( Math, array ); 
		}
	}
	obj.hide();
	obj.each(function() {
		array.push($(this).height());
	});
	obj.parent().animate({height:fn.getLargest(array)},"fast");		
	obj.parent().removeClass("loading");
	obj.first().fadeIn("slow",function() {
		setTimeout(fn.loop, speed );
	}).addClass("current");

}

/* Styled Dropdown Select */
(function($){
 $.fn.extend({
 
 	customStyle : function(options) {
	  if(!$.browser.msie || ($.browser.msie&&$.browser.version>6)){
	  return this.each(function() {
	  
			var currentSelected = $(this).find(':selected');
			$(this).after('<span class="customStyleSelectBox"><span class="customStyleSelectBoxInner">'+currentSelected.text()+'</span></span>').css({position:'absolute', opacity:0,fontSize:$(this).next().css('font-size')});
			var selectBoxSpan = $(this).next();
			var selectBoxWidth = parseInt($(this).width()) - parseInt(selectBoxSpan.css('padding-left')) -parseInt(selectBoxSpan.css('padding-right'));			
			var selectBoxSpanInner = selectBoxSpan.find(':first-child');
			selectBoxSpan.css({display:'inline-block'});
			selectBoxSpanInner.css({width:selectBoxWidth, display:'inline-block'});
			var selectBoxHeight = parseInt(selectBoxSpan.height()) + parseInt(selectBoxSpan.css('padding-top')) + parseInt(selectBoxSpan.css('padding-bottom'));
			$(this).height(selectBoxHeight).change(function(){
				// selectBoxSpanInner.text($(this).val()).parent().addClass('changed');   This was not ideal
			selectBoxSpanInner.text($(this).find(':selected').text()).parent().addClass('changed');
				// Thanks to Juarez Filho & PaddyMurphy
			});
			
	  });
	  }
	}
 });
})(jQuery);

$(document).ready(function(){
	$('.styled').customStyle();
});

(function($){
 $.fn.extend({
 	customStyleSmall : function(options) {
	  if(!$.browser.msie || ($.browser.msie&&$.browser.version>6)){
	  return this.each(function() {
	  
			var currentSelected = $(this).find(':selected');
			$(this).after('<span class="customStyleSelectBox-small"><span class="customStyleSelectBoxInner-small">'+currentSelected.text()+'</span></span>').css({position:'absolute', opacity:0,fontSize:$(this).next().css('font-size')});
			var selectBoxSpan = $(this).next();
			var selectBoxWidth = parseInt($(this).width()) - parseInt(selectBoxSpan.css('padding-left')) -parseInt(selectBoxSpan.css('padding-right'));			
			var selectBoxSpanInner = selectBoxSpan.find(':first-child');
			selectBoxSpan.css({display:'inline-block'});
			selectBoxSpanInner.css({width:selectBoxWidth, display:'inline-block'});
			var selectBoxHeight = parseInt(selectBoxSpan.height()) + parseInt(selectBoxSpan.css('padding-top')) + parseInt(selectBoxSpan.css('padding-bottom'));
			$(this).height(selectBoxHeight).change(function(){
				// selectBoxSpanInner.text($(this).val()).parent().addClass('changed');   This was not ideal
			selectBoxSpanInner.text($(this).find(':selected').text()).parent().addClass('changed');
				// Thanks to Juarez Filho & PaddyMurphy
			});
	  });
	  }
	}
 });
})(jQuery);

$(document).ready(function(){
	$('.styled-small').customStyleSmall();
});

// Custom example logic
$(function() {
  if (typeof plupload != 'undefined' && $('#pickfiles').html() != null) {

  var uploader = new plupload.Uploader({
    runtimes : 'html5,flash,gears,silverlight,browserplus',
    browse_button : 'pickfiles',
    container : 'container',
    max_file_size : '500mb',
    url : '../php/upload.php',
    flash_swf_url : '../js/plupload.flash.swf',
    silverlight_xap_url : '../js/plupload.silverlight.xap',
    filters : [
      {title : "Erlaubte Dateien", extensions : "pdf,tif"}
    ]
  });
	 
  $('#uploadfiles').click(function(e) {
    uploader.start();
    e.preventDefault();
  });
 
  uploader.init();
	 
  uploader.bind('FilesAdded', function(up, files) {
    $.each(files, function(i, file) {
      $('#filelist').append('<div id="' + file.id + '">' + file.name + ' (' + plupload.formatSize(file.size) + ') <b></b>' + '</div>');
	  });
	  
	  if (uploader.files.length > 1) {
	    $('h2 .multi').show();
	  } else {
	    $('h2 .multi').hide();
	  }
	  
	  // Reposition Flash/Silverlight
	  up.refresh(); 
  });
	 
	uploader.bind('UploadProgress', function(up, file) {
	  $('#' + file.id + " b").html(file.percent + "%");
	});
	 
	uploader.bind('Error', function(up, err) {
	  $('#filelist').append("<div>Error: " + err.code + ", Message: " + err.message + (err.file ? ", File: " + err.file.name : "") + "</div>");
    
    // Reposition Flash/Silverlight
    up.refresh(); 
  });
	 
	uploader.bind('FileUploaded', function(up, file) {
	  $('#' + file.id + " b").html("100%");
	});
	
	uploader.bind('BeforeUpload', function(up, file) {
	  // Display a message to the user
	  $('#notice').show();
	});
	
  uploader.bind('FileUploaded', function(up, file, response) {
    var obj = eval('(' + response.response + ')');
    
    // Append file path to form
    var files_array = $('input[name=files]').val();
    files_array = files_array.split(',');
    
    if (!in_array(obj.result.path, files_array)) {
      files_array.push(obj.result.path);
    }
    
    $('input[name=files]').val(files_array.join(','));
  });
	
	uploader.bind('UploadComplete', function(up, files) {
	  // Submit form
	  if ($('input[name=files]').val() != '') {
	    $('form.upload').submit();
	  }
	});
	
	}
	
	if ($('#art').val() == 'Hardcover') {
	  $('#us-grammatur').parent().hide();
	}
	
	$('#art').change(function() {
	  if ($(this).val() == 'Hardcover') {
	    $('#us-grammatur').parent().hide();
	  } else {
	    $('#us-grammatur').parent().show();  
	  }
	});
});

function in_array(needle, haystack) {
  var length = haystack.length;
  for (var i = 0; i < length; i++) {
    if(haystack[i] == needle) return true;
  }
  return false;
}
