jegtheme

jegbg

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