Advertisement
jegtheme

jegbg.full.js

Jul 26th, 2014
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /** Jeg bg **/
  2.  
  3. (function($) {
  4.     $.fn.jegbg = function( options , startCallback, endCallback)
  5.     {
  6.         // global variable
  7.         var elementlist     = Array();
  8.         var curIdx          = -1;
  9.         var timerTimeout    = 0;
  10.         var timer           = 0;           
  11.         var paused          = false;
  12.         var container       = $(this);
  13.         var instance        = Math.random();
  14.         var ytplayer        = null;
  15.         var touch           = false;
  16.         var singleMode      = false;
  17.        
  18.         if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPad/i))) {
  19.             touch = true;
  20.         }
  21.        
  22.         // lock any button table background image still in load
  23.         var locked          = true;
  24.         VIDEO               = 'video';
  25.         IMAGE               = 'image';
  26.         NEXT                = 'next';
  27.         PREV                = 'prev';
  28.        
  29.         // setting & extends options
  30.         var settings = {
  31.             autoplay                    : 1,
  32.             show_video_control          : 0,
  33.             delay                       : 6000,
  34.             random                      : false,
  35.             fade_speed                  : 1500,
  36.             loader_fade                 : 1000,
  37.             partial_load                : true,
  38.             autostart                   : true,
  39.             content                     : null,
  40.             nocrop                      : false
  41.         };
  42.  
  43.         startCallback = startCallback || $.noop;
  44.         endCallback = endCallback || $.noop;
  45.        
  46.         if (options) {
  47.             var options = $.extend(settings, options); 
  48.         } else {
  49.             var options = $.extend(settings);                  
  50.         }
  51.        
  52.        
  53.         /** create youtube tag  (youtube script move to header)
  54.         {
  55.             var tag = document.createElement('script');
  56.             tag.src = "http://www.youtube.com/player_api";
  57.             var firstScriptTag = document.getElementsByTagName('script')[0];
  58.             firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
  59.         }  
  60.         end youtube tag **/
  61.        
  62.         var build_container = function() { $(container).prepend('<div id="bgcontainer"></div>').prepend('<div class="lio-loader"></div>'); };
  63.         var hide_loader     = function() {
  64.             $('.lio-loader').stop().fadeOut(options.loader_fade, function(){
  65.                 $(this).css({
  66.                     height      : '',
  67.                     margin      : '',
  68.                     opacity     : '',
  69.                     overflow    : '',
  70.                     padding     : '',
  71.                     width       : '',
  72.                     display     : 'none'
  73.                 });
  74.             });
  75.         };
  76.         var show_loader     = function() {
  77.             $('.lio-loader').stop().show(options.loader_fade);
  78.         };
  79.         var apply_lock      = function() { locked = true; };
  80.         var release_lock    = function() {
  81.             locked = false;
  82.             load_prev_next();
  83.         };
  84.         var is_locked       = function() { return locked; };
  85.        
  86.         var get_image_size = function (img, nocrop)
  87.         {
  88.             var nh, nw, nt, nl;
  89.             var h = $(img).height();
  90.             var w = $(img).width();
  91.            
  92.             if(h == 0) {
  93.                 h = img.height;
  94.                 w = img.width;
  95.             }
  96.            
  97.             var r = ( h / w ).toFixed(2);
  98.             var wh = $(container).height();
  99.             var ww = $(container).width();
  100.             var wr = wh/ww.toFixed(2);
  101.  
  102.             var resizeWidth = function()
  103.             {              
  104.                 nw = ww;
  105.                 nh = ww * r;
  106.                 nl = ( ww - nw )  / 2;
  107.                 nt = ( wh - nh )  / 2;
  108.                                
  109.                 return new Array(nh, nw, nl, nt);
  110.             };
  111.  
  112.             var resizeHeight = function()
  113.             {                  
  114.                 nw = wh / r;
  115.                 nh = wh;
  116.                 nl = ( ww - nw )  / 2;
  117.                 nt = ( wh - nh )  / 2;
  118.                 return new Array(nh, nw, nl, nt);
  119.             };
  120.            
  121.             if(nocrop)
  122.             {
  123.                 if(wr > r) {
  124.                     return (0 && r <= 1 )  ? resizeHeight() : resizeWidth();
  125.                 } else {
  126.                     return (0 && r > 1 )  ? resizeWidth() : resizeHeight();
  127.                 }
  128.             } else {
  129.                 if(wr > r) {
  130.                     return (0 && r <= 1 )  ? resizeWidth() : resizeHeight();
  131.                 } else {
  132.                     return (0 && r > 1 )  ? resizeHeight() : resizeWidth();
  133.                 }
  134.             }
  135.            
  136.         };
  137.        
  138.         var hide_prev_media_loader = function()
  139.         {
  140.             hide_media();
  141.             hide_loader();
  142.         };
  143.  
  144.         var append_media = function(media)
  145.         {          
  146.             startCallback.call(this, elementlist[curIdx], media);
  147.             $("#bgcontainer").append(media);
  148.                        
  149.             $(media).fadeIn(options.fade_speed, function(){            
  150.                 $(this).addClass("curmedia");
  151.             });
  152.         };
  153.  
  154.         var display_image = function(idx)
  155.         {
  156.             // locked any attempt to next / prev slide
  157.             apply_lock();
  158.             var img = new Image();
  159.            
  160.             $(img).load(function(){
  161.                 var image = new Image();                               
  162.                 image.src = elementlist[idx].source;
  163.                
  164.                 image = set_image_size(image, true, elementlist[idx].pos, elementlist[idx].nocrop);
  165.                 hide_prev_media_loader();
  166.                 append_media(image);
  167.                
  168.                 // relase any lock
  169.                 release_lock();
  170.                
  171.                 if(!singleMode) {
  172.                     clearTimeout(timerTimeout);
  173.                     timerTimeout = setTimeout(function(){
  174.                         start_show();
  175.                     }, options.delay);
  176.                 }
  177.             }).error(function(){
  178.                 start_show();
  179.             }).attr('src', elementlist[idx].source);
  180.         };
  181.  
  182.         var hide_media = function()
  183.         {
  184.             if($('#bgcontainer .curmedia').length > 0)
  185.             {
  186.                 if(is_curently_youtube()) {
  187.                     youtube_ended();
  188.                 } else {
  189.                     $('#bgcontainer .curmedia').stop().fadeOut(options.fade_speed, function(){
  190.                         $(this).remove();
  191.                     });
  192.                 }
  193.             }
  194.         };
  195.        
  196.         var check_video_tag_support = function(type)
  197.         {
  198.             if(type == "mp4" || type == "webm" || type == "ogg") {
  199.                 if(Modernizr.video){
  200.                     if(Modernizr.video["h264"] && type == "mp4") {
  201.                         return "video/mp4";
  202.                     } else if(Modernizr.video["webm"] && type == "webm") {
  203.                         return "video/webm";
  204.                     } else if(Modernizr.video["ogg"] && type == "ogg") {
  205.                         return "video/ogg";
  206.                     } else {
  207.                         return false;
  208.                     }
  209.                 } else {
  210.                     return false;
  211.                 }
  212.             } else {
  213.                 return false;
  214.             }
  215.         };
  216.        
  217.         var get_video_type = function (type) {
  218.             if(type == "mp4") {
  219.                 return "video/mp4";
  220.             } else if(type == "webm") {
  221.                 return "video/webm";
  222.             } else if(type == "ogg") {
  223.                 return "video/ogg";
  224.             } else {
  225.                 return false;
  226.             }
  227.         };
  228.        
  229.         var html5_video = function(source)
  230.         {
  231.             var video = "<video id=\"html5video\" style=\"display : none;\" width=\"" + $(container).width() + "\" height=\"" + $(container).height() + "\" autoplay=1></video>";
  232.             var videosource = "";
  233.             for(var i = 0 ; i < source.length ; i++)
  234.             {
  235.                 var videotype = get_video_type(source[i].videotype);
  236.                 videosource = videosource + "<source src=\"" + source[i].src + "\" type=\"" + videotype + "\" />";                             
  237.             }
  238.             return $(video).append(videosource);
  239.         };
  240.        
  241.         var is_html5_video = function()
  242.         {
  243.             if($("#bgcontainer video").length > 0){
  244.                 return true;
  245.             } else {
  246.                 return false;
  247.             }
  248.         };
  249.        
  250.         var youtube_parser = function (url)
  251.         {
  252.             var regExp = /^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#\&\?]*).*/;
  253.             var match = url.match(regExp);
  254.            
  255.             if ( match && match[7].length == 11 ) {
  256.                 return match[7];
  257.             } else {
  258.                 alert("Url Incorrect");
  259.             }
  260.         };
  261.        
  262.         var updateytplayerInfo = function(e)
  263.         {
  264.             if (e.data == YT.PlayerState.ENDED ) {
  265.                 start_show();
  266.                 youtube_ended();
  267.             }
  268.         };
  269.        
  270.         var stop_youtube = function()
  271.         {
  272.             // stop video
  273.             if(typeof player.stopVideo == 'function') {
  274.                 player.stopVideo();
  275.             }
  276.         };
  277.        
  278.         var pause_youtube = function()
  279.         {
  280.             // pause video
  281.             if(typeof player.pauseVideo == 'function') {
  282.                 player.pauseVideo();   
  283.             }
  284.         };
  285.        
  286.         var play_youtube = function()
  287.         {
  288.             // play video
  289.             if(typeof player.playVideo == 'function') {
  290.                 player.playVideo();
  291.             }
  292.         };
  293.        
  294.         var youtube_ended = function()
  295.         {
  296.             // need to handle it separately again :(               
  297.             stop_youtube();
  298.            
  299.             setTimeout(function(){
  300.                 $("iframe#ytmedia").remove();
  301.                 player = null;
  302.             }, options.loader_fade);
  303.            
  304.         };
  305.        
  306.         var youtube_player_ready = function ()
  307.         {
  308.             // we cannot call any of default method, so we need to call it here, its ugly hack         
  309.             startCallback.call(this, elementlist[curIdx], $("#curmedia"));
  310.             // force to start youtube player
  311.             if(touch) {
  312.                 play_youtube();
  313.             }
  314.         };
  315.        
  316.  
  317.         var is_curently_youtube = function()
  318.         {
  319.             if($("#ytmedia").length > 0){
  320.                 if($("#ytmedia").hasClass("curmedia")) {
  321.                     return true;
  322.                 } else {
  323.                     return false;
  324.                 }
  325.             } else {
  326.                 return false;
  327.             }
  328.         };
  329.        
  330.         var youtube_video = function (source)
  331.         {
  332.             var youtubesrc;
  333.             for(var i = 0 ; i < source.length ; i++)
  334.             {
  335.                 var type = source[i].videotype;
  336.                 if(type == 'youtube') {
  337.                     youtubesrc = source[i].src;
  338.                     break;
  339.                 }
  340.             }
  341.            
  342.             // create youtube api tag
  343.             $("#bgcontainer").append("<div id=\"ytmedia\"></div>");        
  344.             var youtubeid = youtube_parser(youtubesrc);                
  345.             var playervar = null;
  346.            
  347.             if(touch) {
  348.                 playervar = {
  349.                         'autoplay'  : 0,
  350.                         'controls'  : 1,
  351.                         'showinfo'  : 0,
  352.                         'rel'       : 0,
  353.                         'wmode'     : 'opaque',
  354.                         'hd'        : 1
  355.                     };
  356.                 alert(playervar);
  357.             } else {
  358.                 playervar = {
  359.                         'autoplay'  : 1,
  360.                         'controls'  : 0,
  361.                         'showinfo'  : 0,
  362.                         'rel'       : 0,
  363.                         'wmode'     : 'opaque',
  364.                         'hd'        : 1
  365.                     };
  366.             }
  367.                
  368.             var loadYoutube = function() {     
  369.                 player = new YT.Player('ytmedia', {
  370.                     height      : $(container).height(),
  371.                     width       : $(container).width(),
  372.                     videoId     : youtubeid,
  373.                     playerVars : playervar,
  374.                     events: {
  375.                           'onReady'         : youtube_player_ready,
  376.                           'onStateChange'   : updateytplayerInfo
  377.                     }
  378.                 });
  379.                
  380.                 $("div#ytmedia").remove();
  381.             };
  382.            
  383.             var checkYoutubeLoaded = function() {
  384.                 setTimeout(function(){
  385.                     if(typeof YT === 'undefined') {
  386.                         checkYoutubeLoaded();
  387.                     } else {
  388.                         loadYoutube();
  389.                     }
  390.                 }, 1000);
  391.             };
  392.            
  393.             checkYoutubeLoaded();          
  394.         };
  395.        
  396.         var flashplayer_video = function(source)
  397.         {
  398.            
  399.         };
  400.        
  401.         var create_video_tag = function(source, priority)
  402.         {          
  403.             if(priority[0] == true) {
  404.                 return {
  405.                     tagbehaviour    : 0,
  406.                     videotag        : html5_video(source)
  407.                 };
  408.             } else if(priority[1] == true) {
  409.                 return {
  410.                     tagbehaviour    : 1,
  411.                     videotag        : youtube_video(source)
  412.                 };
  413.             } else if(priority[3] == true){
  414.                 return {
  415.                     tagbehaviour    : 3,
  416.                     videotag        : flashplayer_video(source)
  417.                 };
  418.             }
  419.         };
  420.        
  421.    
  422.         // priority : video tag, youtube, vimeo own player     
  423.         var video_tag = function(source)
  424.         {
  425.             var priority = new Array();
  426.            
  427.             for(var i = 0; i < 4; i++) priority[i] = false;        
  428.            
  429.             // build priority nya dulu
  430.             for(var i = 0 ; i < source.length ; i++)
  431.             {
  432.                 var type = source[i].videotype;
  433.                 var videotag = check_video_tag_support(source[i].videotype);
  434.                
  435.                 if( ( type == "mp4" || type == "webm" || type == "ogg" ) && videotag) {
  436.                     if(videotag) priority[0] = true;
  437.                 } else if(type == "youtube") {
  438.                     priority[1] = true;
  439.                 } else if(type == "vimeo") {
  440.                     priority[2] = true;
  441.                 } else { // fallback
  442.                     if(type == "mp4" || type == "flv") {
  443.                         priority[3] = true;
  444.                     }
  445.                 }
  446.             }
  447.            
  448.             return create_video_tag(source, priority);
  449.         };
  450.        
  451.         var display_video = function(idx)
  452.         {
  453.             var videoobj    = video_tag($(elementlist[idx].source));
  454.             var video       = videoobj.videotag;
  455.             var priority    = videoobj.tagbehaviour;
  456.            
  457.             hide_prev_media_loader();
  458.            
  459.             if(priority == 0) {
  460.                 append_media(video);
  461.                 $(video).bind("ended", function() {
  462.                     start_show();
  463.                 });
  464.             } else if(priority == 1){
  465.                 $("#ytmedia").addClass("curmedia");
  466.             } else if(priority == 3){
  467.                 $("#ytmedia").addClass("curmedia");
  468.             }
  469.            
  470.             // relase any lock                     
  471.             release_lock();
  472.         };
  473.        
  474.         var set_image_size = function(image, hide, pos, nocrop)
  475.         {
  476.             var size = get_image_size(image, nocrop);
  477.             $(image).css('height', size[0])
  478.                 .css('width',size[1])
  479.                 .css('left', size[2])
  480.                 .css('max-width', '200%');         
  481.            
  482.             if(hide) {
  483.                 $(image).css({'display' : 'none'});
  484.             }
  485.            
  486.             if(pos == 'top') {
  487.                 $(image).css('top', 0);
  488.             } else if(pos == 'bottom'){
  489.                 $(image).css('bottom', 0);
  490.             } else if(pos == 'center'){
  491.                 $(image).css('top', size[3]);
  492.             }
  493.            
  494.             $(image).addClass('nextmedia');
  495.            
  496.             return image;
  497.         };
  498.  
  499.         var resize_image = function()
  500.         {
  501.             set_image_size($('#bgcontainer .curmedia'), false, elementlist[curIdx].pos, elementlist[curIdx].nocrop);                   
  502.         };
  503.        
  504.         var resize_video = function()
  505.         {
  506.             $('#bgcontainer .curmedia').width($(container).width()).height($(container).height());
  507.         };
  508.        
  509.         var resize_window_bg = function()
  510.         {
  511.             if(elementlist[curIdx] !== undefined) {
  512.                 if(elementlist[curIdx].type == IMAGE) {
  513.                     resize_image();
  514.                 } else if(elementlist[curIdx].type == VIDEO) {
  515.                     resize_video();
  516.                 }
  517.             }
  518.         };
  519.        
  520.         var increase_idx = function()
  521.         {
  522.             if(options.random == false) {
  523.                 curIdx = ( curIdx + 1 ) % ( elementlist.length );
  524.             } else {
  525.                 var oldIdx = curIdx;
  526.                 curIdx = Math.floor(Math.random() * ( elementlist.length ));   
  527.                 if(oldIdx == curIdx && elementlist.length > 1) {                       
  528.                     increase_idx();
  529.                 }
  530.             }
  531.         };
  532.        
  533.         var reduce_idx = function()
  534.         {
  535.             curIdx = ( curIdx - 1 );
  536.             if(curIdx < 0) {
  537.                 curIdx = elementlist.length - 1;
  538.             }
  539.         };     
  540.        
  541.         var stop_show       = function()
  542.         {
  543.             // todo : check if current video is youtube, than stop those player
  544.             if(is_curently_youtube()) {
  545.                 youtube_ended();
  546.             }
  547.             clearTimeout(timerTimeout);
  548.         };
  549.        
  550.         var pause_show      = function ()
  551.         {
  552.             if(is_curently_youtube()) {            
  553.                 pause_youtube();
  554.             } else if(is_html5_video()) {
  555.                 player = document.getElementById("html5video");
  556.                 player.pause();
  557.             }
  558.             clearTimeout(timerTimeout);
  559.            
  560.             return true;
  561.         };
  562.        
  563.         var replay_show     = function()
  564.         {
  565.             paused = false;        
  566.             if(is_curently_youtube()) {
  567.                 play_youtube();
  568.             } else if(is_html5_video()) {
  569.                 player = document.getElementById("html5video");
  570.                 player.play();             
  571.             } else {
  572.                 clearTimeout(timerTimeout);
  573.                 timerTimeout = setTimeout(function(){
  574.                     start_show();
  575.                 }, options.delay);
  576.             }
  577.             return false;
  578.         };
  579.        
  580.         var start_show      = function(direction) {        
  581.             show_loader();
  582.             var oldIdx = curIdx;
  583.             paused = false;                    
  584.            
  585.             if(typeof direction == 'undefined') {
  586.                 increase_idx();
  587.             } else if(direction == NEXT) {
  588.                 increase_idx();
  589.             } else if(direction == PREV) {
  590.                 reduce_idx();
  591.             }
  592.            
  593.             if(curIdx >= 0) endCallback.call(this, elementlist[oldIdx], elementlist[curIdx]);
  594.                                    
  595.             if(elementlist[curIdx].type == IMAGE) {
  596.                 // unmute player when normal begin  
  597.                 unmuteplayer();
  598.                 // display image
  599.                 display_image(curIdx);
  600.             } else if(elementlist[curIdx].type == VIDEO) {
  601.                 muteplayer();
  602.                 setTimeout(function(){
  603.                     display_video(curIdx);
  604.                 }, 3000);              
  605.             }          
  606.         };
  607.        
  608.         var load_prev_next = function() {
  609.            
  610.             var nextid = curIdx + 1;
  611.             if(nextid <= elementlist.length - 1) {
  612.                 if(elementlist[nextid].type == IMAGE) {
  613.                     var img = new Image();
  614.                     $(img).attr('src', elementlist[nextid].source);
  615.                 }
  616.             }
  617.            
  618.             var previd = curIdx - 1;
  619.             if(previd >= 0) {
  620.                 if(elementlist[previd].type == IMAGE) {
  621.                     var img = new Image();
  622.                     $(img).attr('src', elementlist[previd].source);
  623.                 }
  624.             }
  625.            
  626.            
  627.         };
  628.        
  629.         // recursive function for loading image
  630.         var load_all = function(i)
  631.         {
  632.             if(i <= elementlist.length - 1) {
  633.                 if(elementlist[i].type == IMAGE) {
  634.                     var img = new Image();
  635.                     $(img).load(function(){
  636.                         load_all(++i);
  637.                     }).attr('src', elementlist[i].source);
  638.                 }
  639.             } else {
  640.                 start_show();
  641.             }
  642.         };
  643.        
  644.         var control_bg = function (direction)
  645.         {
  646.             if(!is_locked()) {
  647.                 stop_show(); // clear interval first
  648.                 start_show(direction);
  649.             }
  650.         };
  651.        
  652.         var hit_pause = function()
  653.         {          
  654.             paused = (paused == false) ? pause_show() : replay_show();
  655.             return false;
  656.         };
  657.        
  658.         var set_start_callback = function(start_callback)
  659.         {
  660.             startCallback = start_callback;
  661.         };
  662.        
  663.         var set_end_callback = function(end_callback)
  664.         {
  665.             endCallback = end_callback;
  666.         };
  667.        
  668.         // initialize
  669.         var init = function() {
  670.             var eleIdx = 0;
  671.             build_container();
  672.             elementlist = options.content;
  673.            
  674.             if(elementlist.length == 1) {
  675.                 singleMode = true;
  676.                 $(".navleft", container).hide();
  677.                 $(".navright", container).hide();
  678.             }
  679.            
  680.             if(options.partial_load) {
  681.                 start_show();
  682.                
  683.                 $(window).resize(function(){
  684.                     resize_window_bg();
  685.                 });
  686.             } else {
  687.                 $(window).load( function() {
  688.                     load_all(0);
  689.                 });
  690.             }
  691.            
  692.             $(window).resize(function(){
  693.                 resize_window_bg();
  694.             });
  695.            
  696.             // kalo ini ya global
  697.             $(window).stop().keydown(function(e) {
  698.                 if(e.keyCode == 37) { // left      
  699.                     if(!singleMode) {
  700.                         control_bg(PREV);
  701.                         return false;
  702.                     }
  703.                 }
  704.                 else if(e.keyCode == 39) { // right
  705.                     if(!singleMode) {
  706.                         control_bg(NEXT);
  707.                         return false;
  708.                     }
  709.                 }  
  710.                 else if(e.keyCode == 32) { // right
  711.                     hit_pause();
  712.                 }
  713.             });
  714.                        
  715.             if(!singleMode) {
  716.                 $(".navleft" , container).click(function(){
  717.                     control_bg(PREV);
  718.                 });
  719.                
  720.                 $(".navright" , container).click(function(){
  721.                     control_bg(NEXT);
  722.                 });
  723.             }
  724.            
  725.             if(!singleMode) {
  726.                 $("#bgcontainer" , container).touchwipe({
  727.                     wipeLeft: function(e) {                
  728.                         control_bg(NEXT);
  729.                         return false;
  730.                     },
  731.                     wipeRight: function() {
  732.                         control_bg(PREV);                  
  733.                         return false;
  734.                     },
  735.                     min_move_x: 20,
  736.                     min_move_y: 20,
  737.                     preventDefaultEvents: true
  738.                 });
  739.             }
  740.            
  741.             // listen to destroyed element
  742.             $(window).bind("jmainremove", function(){
  743.                 stop_show();
  744.                 $(window).unbind('keydown');
  745.                 $(window).unbind('resize');
  746.                 delete $.fn.jegbg;
  747.             });
  748.            
  749.         };
  750.        
  751.         var next = function(){
  752.             control_bg(PREV);
  753.         };
  754.        
  755.         var prev = function() {
  756.             control_bg(NEXT);
  757.         };
  758.        
  759.         if(options.autostart) {
  760.             init();
  761.         }
  762.        
  763.         return {
  764.             next                : next,
  765.             prev                : prev,
  766.             start               : init,
  767.             pause               : hit_pause,
  768.             restart             : replay_show,
  769.             set_start_callback  : set_start_callback,
  770.             set_end_callback    : set_end_callback
  771.         };
  772.     };
  773. })(jQuery);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement