/**
 * --------------------------------------------------------------------
 * jQuery-Plugin SuperBGimage - Scaling Fullscreen Backgrounds and Slideshow using jQuery
 * Version: 1.0, 29.08.2009
 *
 * by Andreas Eberhard, andreas.eberhard@gmail.com
 *                      http://dev.andreaseberhard.de/projects/superbgimage/
 *
 * Copyright (c) 2009 Andreas Eberhard
 * licensed under a Creative Commons Attribution 3.0
 *
 *  Inspired by 
 *	  Supersized - Fullscreen Slideshow jQuery Plugin
 *    http://buildinternet.com/project/supersized/
 *	  By Sam Dunn (www.buildinternet.com // www.onemightyroar.com)
 * --------------------------------------------------------------------
 * License:
 * http://creativecommons.org/licenses/by/3.0/
 * http://creativecommons.org/licenses/by/3.0/deed.de
 *
 * You are free:
 *       * to Share - to copy, distribute and transmit the work
 *       * to Remix - to adapt the work
 *
 * Under the following conditions:
 *       * Attribution. You must attribute the work in the manner specified
 *         by the author or licensor (but not in any way that suggests that
 *         they endorse you or your use of the work).
 * --------------------------------------------------------------------
 * Changelog:
 *    29.08.2009 initial Version 1.0
 * --------------------------------------------------------------------
 */
 
(function(a){jQuery.fn.superbgimage=function(c){var b=a.extend(a.fn.superbgimage.defaults,a.fn.superbgimage.options,c);a.superbg_inAnimation=false;a.superbg_slideshowActive=false;a.superbg_imgIndex=1;a.superbg_imgActual=1;a.superbg_imgLast=-1;a.superbg_imgSlide=0;a.superbg_interval=0;a.superbg_preload=0;a.superbg_direction=0;a.superbg_max_randomtrans=7;a.superbg_lasttrans=-1;a.superbg_isIE6=false;a.superbg_firstLoaded=false;a.superbg_saveId=a(this).attr("id");if(a("#"+b.id).length===0){a("body").prepend('<div id="'+b.id+'" style="display:none;"></div>')}else{a("body").prepend(a("#"+b.id))}a("#"+b.id).css("display","none").css("overflow","hidden").css("z-index",b.z_index);if(b.inlineMode===0){a("#"+b.id).css("position","fixed").css("width","100%").css("height","100%").css("top",0).css("left",0)}if(b.reload){a("#"+b.id+" img").remove()}a("#"+b.id+" img").hide().css("position","absolute");a("#"+b.id).children("img").each(function(){a(this).attr("rel",a.superbg_imgIndex++);if(!b.showtitle){a(this).attr("title","")}});a(this).children("a").each(function(){a(this).attr("rel",a.superbg_imgIndex++).click(function(){a(this).superbgShowImage();return false}).addClass("preload")});a.superbg_imgIndex--;a(window).bind("load",function(){a(this).superbgLoad()});a(window).bind("resize",function(){a(this).superbgResize()});a.superbg_isIE6=/msie|MSIE 6/.test(navigator.userAgent);if(a.superbg_isIE6&&(b.inlineMode===0)){a("#"+b.id).css("position","absolute").width(a(window).width()).height(a(window).height());a(window).bind("scroll",function(){a(this).superbgScrollIE6()})}if(b.reload){a(this).superbgLoad()}return this};jQuery.fn.superbgScrollIE6=function(){var b=a.extend(a.fn.superbgimage.defaults,a.fn.superbgimage.options);a("#"+b.id).css("top",document.documentElement.scrollTop+"px")};jQuery.fn.superbgLoad=function(){var b=a.extend(a.fn.superbgimage.defaults,a.fn.superbgimage.options);if((a("#"+b.id).children("img").length>0)||(a("#"+a.superbg_saveId).children("a").length>0)){a("#"+b.id).show()}if((typeof b.showimage!="undefined")&&(b.showimage>=0)){a.superbg_imgActual=b.showimage}if(b.randomimage===1){a.superbg_imgActual=(1+parseInt(Math.random()*(a.superbg_imgIndex-1+1),10))}a(this).superbgShowImage(a.superbg_imgActual)};jQuery.fn.superbgimagePreload=function(){var b=a.extend(a.fn.superbgimage.defaults,a.fn.superbgimage.options);clearInterval(a.superbg_preload);if(!a.superbg_firstLoaded&&(a("#"+a.superbg_saveId).children("a").length>0)){a.superbg_preload=setInterval("$(this).superbgimagePreload()",111);return}a("#"+a.superbg_saveId).children("a.preload:first").each(function(){var d=a(this).attr("rel");var c=a(this).attr("title");var e=new Image();a(e).load(function(){a(this).css("position","absolute").hide();if(a("#"+b.id).children("img[rel='"+d+"']").length===0){a(this).attr("rel",d);if(b.showtitle===1){a(this).attr("title",c)}a("#"+b.id).prepend(this)}e.onload=function(){}}).error(function(){e.onerror=function(){}}).attr("src",a(this).attr("href"));a.superbg_preload=setInterval("$(this).superbgimagePreload()",111)}).removeClass("preload")};jQuery.fn.startSlideShow=function(){var b=a.extend(a.fn.superbgimage.defaults,a.fn.superbgimage.options);a.superbg_imgSlide=a.superbg_imgActual;if(a.superbg_interval!==0){clearInterval(a.superbg_interval)}a.superbg_interval=setInterval("$(this).nextSlide()",b.slide_interval);a.superbg_slideshowActive=true;return false};jQuery.fn.stopSlideShow=function(){var b=a.extend(a.fn.superbgimage.defaults,a.fn.superbgimage.options);clearInterval(a.superbg_interval);a.superbg_slideshowActive=false;return false};jQuery.fn.nextSlide=function(){var b=a.extend(a.fn.superbgimage.defaults,a.fn.superbgimage.options);if(a.superbg_inAnimation){return false}if(a.superbg_slideshowActive){clearInterval(a.superbg_preload)}a.superbg_direction=0;a.superbg_imgSlide++;if(a.superbg_imgSlide>a.superbg_imgIndex){a.superbg_imgSlide=1}if(b.randomimage===1){a.superbg_imgSlide=(1+parseInt(Math.random()*(a.superbg_imgIndex-1+1),10));while(a.superbg_imgSlide===a.superbg_imgLast){a.superbg_imgSlide=(1+parseInt(Math.random()*(a.superbg_imgIndex-1+1),10))}}a.superbg_imgActual=a.superbg_imgSlide;return a(this).superbgShowImage(a.superbg_imgActual)};jQuery.fn.prevSlide=function(){var b=a.extend(a.fn.superbgimage.defaults,a.fn.superbgimage.options);if(a.superbg_inAnimation){return false}a.superbg_direction=1;a.superbg_imgSlide--;if(a.superbg_imgSlide<1){a.superbg_imgSlide=a.superbg_imgIndex}if(b.randomimage===1){a.superbg_imgSlide=(1+parseInt(Math.random()*(a.superbg_imgIndex-1+1),10));while(a.superbg_imgSlide===a.superbg_imgLast){a.superbg_imgSlide=(1+parseInt(Math.random()*(a.superbg_imgIndex-1+1),10))}}a.superbg_imgActual=a.superbg_imgSlide;return a(this).superbgShowImage(a.superbg_imgActual)};jQuery.fn.superbgResize=function(){var d=a.extend(a.fn.superbgimage.defaults,a.fn.superbgimage.options);var g=a("#"+d.id+" img.activeslide");var f=a(this).superbgCalcSize(a(g).width(),a(g).height());var b=f[0];var c=f[1];var h=f[2];var e=f[3];a(g).css("width",b+"px");a(g).css("height",c+"px");if(a.superbg_isIE6&&(d.inlineMode===0)){a("#"+d.id).width(b).height(c);a(g).width(b);a(g).height(c)}a(g).css("left",h+"px");if(d.vertical_center===1){a(g).css("top",e+"px")}else{a(g).css("top","0px")}};jQuery.fn.superbgCalcSize=function(c,l){var m=a.extend(a.fn.superbgimage.defaults,a.fn.superbgimage.options);var b=a(window).width();var e=a(window).height();if(m.inlineMode===1){b=a("#"+m.id).width();e=a("#"+m.id).height()}var g=l/c;var k=0;var h=0;if((e/b)>g){k=e;h=Math.round(e/g)}else{if(g>1){var j=e/l;h=Math.round(c*j);k=e}else{k=Math.round(b*g);h=b}}var d=Math.round((b-h)/2);var f=Math.round((e-k)/2);var i=[h,k,d,f];return i};jQuery.fn.superbgShowImage=function(c){var d=a.extend(a.fn.superbgimage.defaults,a.fn.superbgimage.options);a.superbg_imgActual=a(this).attr("rel");if(typeof c!=="undefined"){a.superbg_imgActual=c}if(a("#"+d.id+" img.activeslide").attr("rel")===a.superbg_imgActual){return false}if(a.superbg_inAnimation){return false}else{a.superbg_inAnimation=true}var e="";var b="";if(a("#"+d.id).children("img[rel='"+a.superbg_imgActual+"']").length===0){e=a("#"+a.superbg_saveId+" a[rel='"+a.superbg_imgActual+"']").attr("href");b=a("#"+a.superbg_saveId+" a[rel='"+a.superbg_imgActual+"']").attr("title")}else{e=a("#"+d.id).children("img[rel='"+a.superbg_imgActual+"']").attr("src")}if((typeof d.onHide==="function")&&(d.onHide!==null)&&(a.superbg_imgLast>=0)){d.onHide(a.superbg_imgLast)}a("#"+d.id+" img.activeslide").superbgLoadImage(e,b);a("#"+a.superbg_saveId+" a").removeClass("activeslide");a("#"+a.superbg_saveId).children("a[rel='"+a.superbg_imgActual+"']").addClass("activeslide");a.superbg_imgSlide=a.superbg_imgActual;a.superbg_imgLast=a.superbg_imgActual;return false};jQuery.fn.superbgLoadImage=function(g,b){var d=a.extend(a.fn.superbgimage.defaults,a.fn.superbgimage.options);if(a("#"+d.id).children("img[rel='"+a.superbg_imgActual+"']").length===0){var c=new Image();a(c).load(function(){a(this).css("position","absolute").hide();if(a("#"+d.id).children("img[rel='"+a.superbg_imgActual+"']").length===0){a(this).attr("rel",a.superbg_imgActual);if(d.showtitle===1){a(this).attr("title",b)}a("#"+d.id).prepend(this)}var i=a("#"+d.id).children("img[rel='"+a.superbg_imgActual+"']");var h=a(this).superbgCalcSize(c.width,c.height);a(this).superbgTransition(i,h);if(!a.superbg_firstLoaded){if(d.slideshow===1){a(this).startSlideShow()}if((d.preload===1)&&(a("#"+a.superbg_saveId).children("a").length>0)){a.superbg_preload=setInterval("$(this).superbgimagePreload()",250)}}a.superbg_firstLoaded=true;c.onload=function(){}}).error(function(){a.superbg_inAnimation=false;c.onerror=function(){}}).attr("src",g)}else{var f=a("#"+d.id).children("img[rel='"+a.superbg_imgActual+"']");var e=a(this).superbgCalcSize(a(f).width(),a(f).height());a(this).superbgTransition(f,e);if(!a.superbg_firstLoaded){if(d.slideshow===1){a(this).startSlideShow()}if((d.preload===1)&&(a("#"+a.superbg_saveId).children("a").length>0)){a.superbg_preload=setInterval("$(this).superbgimagePreload()",250)}a.superbg_firstLoaded=true}}};jQuery.fn.superbgTransition=function(d,b){var m=a.extend(a.fn.superbgimage.defaults,a.fn.superbgimage.options);var k=b[0];var l=b[1];var h=b[2];var j=b[3];a(d).css("width",k+"px").css("height",l+"px").css("left",h+"px");if((typeof m.onClick==="function")&&(m.onClick!==null)){a(d).unbind("click").click(function(){m.onClick(a.superbg_imgActual)})}if((typeof m.onMouseenter==="function")&&(m.onMouseenter!==null)){a(d).unbind("mouseenter").mouseenter(function(){m.onMouseenter(a.superbg_imgActual)})}if((typeof m.onMouseleave==="function")&&(m.onMouseleave!==null)){a(d).unbind("mouseleave").mouseleave(function(){m.onMouseleave(a.superbg_imgActual)})}if((typeof m.onMousemove==="function")&&(m.onMousemove!==null)){a(d).unbind("mousemove").mousemove(function(n){m.onMousemove(a.superbg_imgActual,n)})}if(m.randomtransition===1){var e=(0+parseInt(Math.random()*(a.superbg_max_randomtrans-0+1),10));while(e===a.superbg_lasttrans){e=(0+parseInt(Math.random()*(a.superbg_max_randomtrans-0+1),10))}m.transition=e}if(m.vertical_center===1){a(d).css("top",j+"px")}else{a(d).css("top","0px")}var c=m.transitionout;if((m.transition===6)||(m.transition===7)){c=0}if(c===1){a("#"+m.id+" img.activeslide").removeClass("activeslide").addClass("lastslide").css("z-index",0)}else{a("#"+m.id+" img.activeslide").removeClass("activeslide").addClass("lastslideno").css("z-index",0)}a(d).css("z-index",1);m.transition=parseInt(m.transition,10);a.superbg_lasttrans=m.transition;var f="";var g="";if(m.transition===0){a(d).show(1,function(){if((typeof m.onShow==="function")&&(m.onShow!==null)){m.onShow(a.superbg_imgActual)}a.superbg_inAnimation=false;if(a.superbg_slideshowActive){a("#"+m.id).startSlideShow()}}).addClass("activeslide")}else{if(m.transition===1){a(d).fadeIn(m.speed,function(){if((typeof m.onShow==="function")&&(m.onShow!==null)){m.onShow(a.superbg_imgActual)}a("#"+m.id+" img.lastslideno").hide(1,null).removeClass("lastslideno");a.superbg_inAnimation=false;if(a.superbg_slideshowActive){a("#"+m.id).startSlideShow()}}).addClass("activeslide")}else{if(m.transition===2){f="slide";g="up"}if(m.transition===3){f="slide";g="right"}if(m.transition===4){f="slide";g="down"}if(m.transition===5){f="slide";g="left"}if(m.transition===6){f="blind";g="horizontal"}if(m.transition===7){f="blind";g="vertical"}if(m.transition===90){f="slide";g="left";if(a.superbg_direction===1){g="right"}}if(m.transition===91){f="slide";g="down";if(a.superbg_direction===1){g="up"}}a(d).show(f,{direction:g},m.speed,function(){if((typeof m.onShow==="function")&&(m.onShow!==null)){m.onShow(a.superbg_imgActual)}a("#"+m.id+" img.lastslideno").hide(1,null).removeClass("lastslideno");a.superbg_inAnimation=false;if(a.superbg_slideshowActive){a("#"+m.id).startSlideShow()}}).addClass("activeslide")}}if(c===1){var i=m.speed;if(m.speed=="slow"){i=600+200}else{if(m.speed=="normal"){i=400+200}else{if(m.speed=="fast"){i=400+200}else{i=m.speed+200}}}if(m.transition===0){a("#"+m.id+" img.lastslide").hide(1,null).removeClass("lastslide")}else{if(m.transition==1){a("#"+m.id+" img.lastslide").fadeOut(i).removeClass("lastslide")}else{if(m.transition===2){f="slide";g="down"}if(m.transition===3){f="slide";g="left"}if(m.transition===4){f="slide";g="up"}if(m.transition===5){f="slide";g="right"}if(m.transition===6){f="";g=""}if(m.transition===7){f="";g=""}if(m.transition===90){f="slide";g="right";if(a.superbg_direction===1){g="left"}}if(m.transition===91){f="slide";g="up";if(a.superbg_direction===1){g="down"}}a("#"+m.id+" img.lastslide").hide(f,{direction:g},i).removeClass("lastslide")}}}else{a("#"+m.id+" img.lastslide").hide(1,null).removeClass("lastslide")}};jQuery.fn.superbgimage.defaults={id:"superbgimage",z_index:0,inlineMode:0,showimage:1,vertical_center:1,transition:1,transitionout:1,randomtransition:0,showtitle:0,slideshow:0,slide_interval:5000,randomimage:0,speed:"slow",preload:1,onShow:null,onClick:null,onHide:null,onMouseenter:null,onMouseleave:null,onMousemove:null}})(jQuery);
