jegtheme

jegbg.js

Nov 6th, 2013
137
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.                         'vq'        : 'hd720',
  355.                         'hd'        : 1
  356.                     };
  357.                 alert(playervar);
  358.             } else {
  359.                 playervar = {
  360.                         'autoplay'  : 1,
  361.                         'controls'  : 0,
  362.                         'showinfo'  : 0,
  363.                         'rel'       : 0,
  364.                         'wmode'     : 'opaque',
  365.                         'vq'        : 'hd720',
  366.                         'hd'        : 1
  367.                     };
  368.             }
  369.                
  370.             var loadYoutube = function() {     
  371.                 player = new YT.Player('ytmedia', {
  372.                     height      : $(container).height(),
  373.                     width       : $(container).width(),
  374.                     videoId     : youtubeid,
  375.                     playerVars : playervar,
  376.                     events: {
  377.                           'onReady'         : youtube_player_ready,
  378.                           'onStateChange'   : updateytplayerInfo
  379.                     }
  380.                 });
  381.                
  382.                 $("div#ytmedia").remove();
  383.             };
  384.            
  385.             var checkYoutubeLoaded = function() {
  386.                 setTimeout(function(){
  387.                     if(typeof YT === 'undefined') {
  388.                         checkYoutubeLoaded();
  389.                     } else {
  390.                         loadYoutube();
  391.                     }
  392.                 }, 1000);
  393.             };
  394.            
  395.             checkYoutubeLoaded();          
  396.         };
  397.        
  398.         var flashplayer_video = function(source)
  399.         {
  400.            
  401.         };
  402.        
  403.         var create_video_tag = function(source, priority)
  404.         {          
  405.             if(priority[0] == true) {
  406.                 return {
  407.                     tagbehaviour    : 0,
  408.                     videotag        : html5_video(source)
  409.                 };
  410.             } else if(priority[1] == true) {
  411.                 return {
  412.                     tagbehaviour    : 1,
  413.                     videotag        : youtube_video(source)
  414.                 };
  415.             } else if(priority[3] == true){
  416.                 return {
  417.                     tagbehaviour    : 3,
  418.                     videotag        : flashplayer_video(source)
  419.                 };
  420.             }
  421.         };
  422.        
  423.    
  424.         // priority : video tag, youtube, vimeo own player     
  425.         var video_tag = function(source)
  426.         {
  427.             var priority = new Array();
  428.            
  429.             for(var i = 0; i < 4; i++) priority[i] = false;        
  430.            
  431.             // build priority nya dulu
  432.             for(var i = 0 ; i < source.length ; i++)
  433.             {
  434.                 var type = source[i].videotype;
  435.                 var videotag = check_video_tag_support(source[i].videotype);
  436.                
  437.                 if( ( type == "mp4" || type == "webm" || type == "ogg" ) && videotag) {
  438.                     if(videotag) priority[0] = true;
  439.                 } else if(type == "youtube") {
  440.                     priority[1] = true;
  441.                 } else if(type == "vimeo") {
  442.                     priority[2] = true;
  443.                 } else { // fallback
  444.                     if(type == "mp4" || type == "flv") {
  445.                         priority[3] = true;
  446.                     }
  447.                 }
  448.             }
  449.            
  450.             return create_video_tag(source, priority);
  451.         };
  452.        
  453.         var display_video = function(idx)
  454.         {
  455.             var videoobj    = video_tag($(elementlist[idx].source));
  456.             var video       = videoobj.videotag;
  457.             var priority    = videoobj.tagbehaviour;
  458.            
  459.             hide_prev_media_loader();
  460.            
  461.             if(priority == 0) {
  462.                 append_media(video);
  463.                 $(video).bind("ended", function() {
  464.                     start_show();
  465.                 });
  466.             } else if(priority == 1){
  467.                 $("#ytmedia").addClass("curmedia");
  468.             } else if(priority == 3){
  469.                 $("#ytmedia").addClass("curmedia");
  470.             }
  471.            
  472.             // relase any lock                     
  473.             release_lock();
  474.         };
  475.        
  476.         var set_image_size = function(image, hide, pos, nocrop)
  477.         {
  478.             var size = get_image_size(image, nocrop);
  479.             $(image).css('height', size[0])
  480.                 .css('width',size[1])
  481.                 .css('left', size[2])
  482.                 .css('max-width', '200%');         
  483.            
  484.             if(hide) {
  485.                 $(image).css({'display' : 'none'});
  486.             }
  487.            
  488.             if(pos == 'top') {
  489.                 $(image).css('top', 0);
  490.             } else if(pos == 'bottom'){
  491.                 $(image).css('bottom', 0);
  492.             } else if(pos == 'center'){
  493.                 $(image).css('top', size[3]);
  494.             }
  495.            
  496.             $(image).addClass('nextmedia');
  497.            
  498.             return image;
  499.         };
  500.  
  501.         var resize_image = function()
  502.         {
  503.             set_image_size($('#bgcontainer .curmedia'), false, elementlist[curIdx].pos, elementlist[curIdx].nocrop);                   
  504.         };
  505.        
  506.         var resize_video = function()
  507.         {
  508.             $('#bgcontainer .curmedia').width($(container).width()).height($(container).height());
  509.         };
  510.        
  511.         var resize_window_bg = function()
  512.         {
  513.             if(elementlist[curIdx] !== undefined) {
  514.                 if(elementlist[curIdx].type == IMAGE) {
  515.                     resize_image();
  516.                 } else if(elementlist[curIdx].type == VIDEO) {
  517.                     resize_video();
  518.                 }
  519.             }
  520.         };
  521.        
  522.         var increase_idx = function()
  523.         {
  524.             if(options.random == false) {
  525.                 curIdx = ( curIdx + 1 ) % ( elementlist.length );
  526.             } else {
  527.                 var oldIdx = curIdx;
  528.                 curIdx = Math.floor(Math.random() * ( elementlist.length ));   
  529.                 if(oldIdx == curIdx && elementlist.length > 1) {                       
  530.                     increase_idx();
  531.                 }
  532.             }
  533.         };
  534.        
  535.         var reduce_idx = function()
  536.         {
  537.             curIdx = ( curIdx - 1 );
  538.             if(curIdx < 0) {
  539.                 curIdx = elementlist.length - 1;
  540.             }
  541.         };     
  542.        
  543.         var stop_show       = function()
  544.         {
  545.             // todo : check if current video is youtube, than stop those player
  546.             if(is_curently_youtube()) {
  547.                 youtube_ended();
  548.             }
  549.             clearTimeout(timerTimeout);
  550.         };
  551.        
  552.         var pause_show      = function ()
  553.         {
  554.             if(is_curently_youtube()) {            
  555.                 pause_youtube();
  556.             } else if(is_html5_video()) {
  557.                 player = document.getElementById("html5video");
  558.                 player.pause();
  559.             }
  560.             clearTimeout(timerTimeout);
  561.            
  562.             return true;
  563.         };
  564.        
  565.         var replay_show     = function()
  566.         {
  567.             paused = false;        
  568.             if(is_curently_youtube()) {
  569.                 play_youtube();
  570.             } else if(is_html5_video()) {
  571.                 player = document.getElementById("html5video");
  572.                 player.play();             
  573.             } else {
  574.                 clearTimeout(timerTimeout);
  575.                 timerTimeout = setTimeout(function(){
  576.                     start_show();
  577.                 }, options.delay);
  578.             }
  579.             return false;
  580.         };
  581.        
  582.         var start_show      = function(direction) {        
  583.             show_loader();
  584.             var oldIdx = curIdx;
  585.             paused = false;                    
  586.            
  587.             if(typeof direction == 'undefined') {
  588.                 increase_idx();
  589.             } else if(direction == NEXT) {
  590.                 increase_idx();
  591.             } else if(direction == PREV) {
  592.                 reduce_idx();
  593.             }
  594.            
  595.             if(curIdx >= 0) endCallback.call(this, elementlist[oldIdx], elementlist[curIdx]);
  596.                                    
  597.             if(elementlist[curIdx].type == IMAGE) {
  598.                 // unmute player when normal begin  
  599.                 unmuteplayer();
  600.                 // display image
  601.                 display_image(curIdx);
  602.             } else if(elementlist[curIdx].type == VIDEO) {
  603.                 muteplayer();
  604.                 setTimeout(function(){
  605.                     display_video(curIdx);
  606.                 }, 3000);              
  607.             }          
  608.         };
  609.        
  610.         var load_prev_next = function() {
  611.            
  612.             var nextid = curIdx + 1;
  613.             if(nextid <= elementlist.length - 1) {
  614.                 if(elementlist[nextid].type == IMAGE) {
  615.                     var img = new Image();
  616.                     $(img).attr('src', elementlist[nextid].source);
  617.                 }
  618.             }
  619.            
  620.             var previd = curIdx - 1;
  621.             if(previd >= 0) {
  622.                 if(elementlist[previd].type == IMAGE) {
  623.                     var img = new Image();
  624.                     $(img).attr('src', elementlist[previd].source);
  625.                 }
  626.             }
  627.            
  628.            
  629.         };
  630.        
  631.         // recursive function for loading image
  632.         var load_all = function(i)
  633.         {
  634.             if(i <= elementlist.length - 1) {
  635.                 if(elementlist[i].type == IMAGE) {
  636.                     var img = new Image();
  637.                     $(img).load(function(){
  638.                         load_all(++i);
  639.                     }).attr('src', elementlist[i].source);
  640.                 }
  641.             } else {
  642.                 start_show();
  643.             }
  644.         };
  645.        
  646.         var control_bg = function (direction)
  647.         {
  648.             if(!is_locked()) {
  649.                 stop_show(); // clear interval first
  650.                 start_show(direction);
  651.             }
  652.         };
  653.        
  654.         var hit_pause = function()
  655.         {          
  656.             paused = (paused == false) ? pause_show() : replay_show();
  657.             return false;
  658.         };
  659.        
  660.         var set_start_callback = function(start_callback)
  661.         {
  662.             startCallback = start_callback;
  663.         };
  664.        
  665.         var set_end_callback = function(end_callback)
  666.         {
  667.             endCallback = end_callback;
  668.         };
  669.        
  670.         // initialize
  671.         var init = function() {
  672.             var eleIdx = 0;
  673.             build_container();
  674.             elementlist = options.content;
  675.            
  676.             if(elementlist.length == 1) {
  677.                 singleMode = true;
  678.                 $(".navleft", container).hide();
  679.                 $(".navright", container).hide();
  680.             }
  681.            
  682.             if(options.partial_load) {
  683.                 start_show();
  684.                
  685.                 $(window).resize(function(){
  686.                     resize_window_bg();
  687.                 });
  688.             } else {
  689.                 $(window).load( function() {
  690.                     load_all(0);
  691.                 });
  692.             }
  693.            
  694.             $(window).resize(function(){
  695.                 resize_window_bg();
  696.             });
  697.            
  698.             // kalo ini ya global
  699.             $(window).stop().keydown(function(e) {
  700.                 if(e.keyCode == 37) { // left      
  701.                     if(!singleMode) {
  702.                         control_bg(PREV);
  703.                         return false;
  704.                     }
  705.                 }
  706.                 else if(e.keyCode == 39) { // right
  707.                     if(!singleMode) {
  708.                         control_bg(NEXT);
  709.                         return false;
  710.                     }
  711.                 }  
  712.                 else if(e.keyCode == 32) { // right
  713.                     hit_pause();
  714.                 }
  715.             });
  716.                        
  717.             if(!singleMode) {
  718.                 $(".navleft" , container).click(function(){
  719.                     control_bg(PREV);
  720.                 });
  721.                
  722.                 $(".navright" , container).click(function(){
  723.                     control_bg(NEXT);
  724.                 });
  725.             }
  726.            
  727.             if(!singleMode) {
  728.                 $("#bgcontainer" , container).touchwipe({
  729.                     wipeLeft: function(e) {                
  730.                         control_bg(NEXT);
  731.                         return false;
  732.                     },
  733.                     wipeRight: function() {
  734.                         control_bg(PREV);                  
  735.                         return false;
  736.                     },
  737.                     min_move_x: 20,
  738.                     min_move_y: 20,
  739.                     preventDefaultEvents: true
  740.                 });
  741.             }
  742.            
  743.             // listen to destroyed element
  744.             $(window).bind("jmainremove", function(){
  745.                 stop_show();
  746.                 $(window).unbind('keydown');
  747.                 $(window).unbind('resize');
  748.                 delete $.fn.jegbg;
  749.             });
  750.            
  751.         };
  752.        
  753.         var next = function(){
  754.             control_bg(PREV);
  755.         };
  756.        
  757.         var prev = function() {
  758.             control_bg(NEXT);
  759.         };
  760.        
  761.         if(options.autostart) {
  762.             init();
  763.         }
  764.        
  765.         return {
  766.             next                : next,
  767.             prev                : prev,
  768.             start               : init,
  769.             pause               : hit_pause,
  770.             restart             : replay_show,
  771.             set_start_callback  : set_start_callback,
  772.             set_end_callback    : set_end_callback
  773.         };
  774.     };
  775. })(jQuery);
Advertisement
Add Comment
Please, Sign In to add comment