jegtheme

script.js

May 6th, 2013
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.     Author: Jegbagus
  3. */
  4.  
  5. // check if screenwidth is avaliable for animation
  6. var scw = function(i){
  7.     if(jQuery(window).width() >= i) {
  8.         return true;
  9.     } else {
  10.         return false;
  11.     }
  12. };
  13.  
  14. var mediaquerywidth         = 767 + 1;
  15. var iphonewidth             = 480 + 1;
  16. var optioncurtain           = 0;
  17. var defaultexecuted         = 0;
  18.  
  19. /** music **/
  20. var musicplayer             = null;
  21. var mppaused                = false;
  22. jQuery(document).ready(function($){
  23.     var mplist = $("#mplist");
  24.    
  25.     if(mplist.length > 0){     
  26.         $(".openplaylist").click(function(){
  27.             $(mplist).fadeIn();
  28.             return false;
  29.         });
  30.        
  31.         $(".mpcls").click(function(){
  32.             $(mplist).fadeOut();
  33.             return false;
  34.         });
  35.     }    
  36. });
  37.  
  38. var mpnotifbox = function(lang) {
  39.     jQuery(".mpnotif").html(lang);
  40.     jQuery(".mpnotif").fadeIn();
  41.    
  42.     setTimeout(function(){
  43.         jQuery(".mpnotif").fadeOut();
  44.     }, 3000);
  45. };
  46.  
  47. var muteplayer = function() {
  48.     if(typeof mplang != 'undefined') {
  49.         mpnotifbox(mplang.playlistmuted);
  50.         jQuery("#jquery_jplayer").jPlayer("mute");
  51.     }
  52. };
  53.  
  54. var unmuteplayer = function() {
  55.     if(typeof mplang != 'undefined') {
  56.         /* mpnotifbox(mplang.playlistunmuted); */
  57.         jQuery("#jquery_jplayer").jPlayer("unmute");
  58.     }
  59. };
  60. /** music end **/
  61.  
  62. (function($) {
  63.   $.fn.outerHTML = function() {
  64.     return $(this).clone().wrap('<div></div>').parent().html();
  65.   };
  66. })(jQuery);
  67.  
  68.  
  69. var notifbox = function(content, timeout){
  70.     var notiftimeout = undefined;
  71.     var notif   = jQuery(".notification");
  72.     var top     = jQuery(notif).css('top');
  73.     var topdest = 80;
  74.    
  75.     if(jQuery(notif).attr('data-small-menu') == 1) {
  76.         topdest = 40;
  77.     }
  78.    
  79.     /** show notification **/
  80.     jQuery(notif).show();
  81.     jQuery(notif).addClass('unfolded');
  82.    
  83.     // position
  84.     var setpos = function() {
  85.         var leftpos = (jQuery(window).width() - jQuery(notif).width()) / 2;
  86.         jQuery(notif).css({left : leftpos});
  87.     };
  88.    
  89.     setpos();
  90.     jQuery(window).resize(function(){ setpos(); });
  91.    
  92.     var closeNotif = function(callback){
  93.         clearTimeout(notiftimeout);
  94.         jQuery(notif).animate({top : 0}, 'fast', function(){
  95.             jQuery(notif).hide().removeClass('unfolded');
  96.            
  97.             callback = callback || jQuery.noop;
  98.             callback();
  99.         });
  100.     };
  101.    
  102.     var showNotif = function(){
  103.         jQuery('.notification-content',notif).html(content);
  104.         jQuery(notif).show().animate({top : topdest}, 'fast');
  105.         jQuery(notif).addClass('unfolded');
  106.         if(timeout > 0) {
  107.             clearTimeout(notiftimeout);
  108.             notiftimeout = setTimeout(function(){
  109.                 closeNotif();
  110.             }, timeout);
  111.         }
  112.     };
  113.    
  114.     jQuery('.closeme', notif).click(function(){
  115.         closeNotif();
  116.     });
  117.    
  118.     jQuery(window).bind("jmainremove", function(){     
  119.         jQuery(notif).hide();
  120.     });
  121.    
  122.     if(!jQuery(notif).hasClass('unfolded')) {
  123.         showNotif();
  124.     } else {
  125.         closeNotif(showNotif);
  126.     }
  127. };
  128.  
  129. /** header navigation **/
  130. jQuery(document).ready(function($)
  131. {
  132.     var pos;
  133.     var slideHeight     = 45;  
  134.     var increasedMargin = 40;
  135.     var slideDownEle    = undefined;
  136.     var touch           = false;
  137.    
  138.     if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPad/i))) {
  139.         touch = true;
  140.     }
  141.    
  142.     // add haschild
  143.     $("nav li").each(function(){
  144.         var i = $(this);
  145.         $(i).find('li').each(function(){
  146.             if(!$(this).hasClass('haschild')) {
  147.                 var ul = $(this).children('ul');
  148.                 if(ul.length > 0) {
  149.                     $(this).addClass('haschild');
  150.                     $(this).children('a').prepend('<div class="icon-white"></div>');
  151.                 }
  152.             }
  153.         });
  154.     });
  155.    
  156.     var getPos = function(obj) {
  157.         var submenu     = $(obj);              
  158.         var pp          = $(obj).parent().position().left;     
  159.         var pw          = $(obj).parent().width();
  160.         pp = pp + ( pw / 2 ) ;     
  161.         var ww          = $(window).width();       
  162.         var sw          = 0;       
  163.        
  164.         $(obj).children('li').each(function(){
  165.             sw += $(this).width();
  166.         });
  167.        
  168.         pos = pp - (sw / 2);
  169.            
  170.         if(pos < 0) {
  171.             pos = 1;
  172.         } else if( ( pos + sw ) > ww ) {
  173.             pos = ww - sw - 1;
  174.         }      
  175.        
  176.         return pos;
  177.     }; 
  178.    
  179.     var menuSlideDown = function(i){
  180.         var childLength = $(i).children("ul").length;
  181.         if(childLength == 0) {
  182.             if(touch) {
  183.                 var loc = $(i).find('a').attr('href');
  184.                 tourl(loc, true);
  185.             }
  186.         } else {
  187.             // $(i).children("ul").height(slideHeight).css({'padding-left' : pos});
  188.            
  189. //          var c = $(i).children("ul");           
  190. //          var ml = getPos(c);
  191. //          $(c).css({
  192. //              'padding-left'  : ml,
  193. //              'width'         : ($(window).width() - ml)
  194. //          });
  195.            
  196.             $(i).children("ul").stop().slideDown("fast", function(){
  197.                 var ml = getPos(this);
  198.                 $(this).animate({'padding-left' : ml}, function(){
  199.                     $(this).width($(window).width() - ml);
  200.                 });
  201.             });
  202.         }
  203.     };
  204.    
  205.     var parentMenuSlideUp = function(i) {      
  206.         var slideparentmenu = function() {         
  207.             if(i.parent().hasClass('menu')) {
  208.                 $(i).find('.bgmenu').stop().animate({
  209.                     height : 0
  210.                 }, 150, function(){
  211.                     $(i).find('a').removeClass('whitecolor');
  212.                                        
  213.                     // ini HACK PARAH!
  214.                     if(touch) {
  215.                         setTimeout(function(){
  216.                             var nekad = $(i).parent().outerHTML();
  217.                             $(i).parent().parent().html("").append(nekad);
  218.                             bindHoverIntent();                     
  219.                         }, 1200);
  220.                     }
  221.                    
  222.                     $(this).remove();
  223.                 });
  224.             }
  225.         };
  226.        
  227.         if(touch) {
  228.             var ul = $(i).children('ul');
  229.            
  230.             if(ul.length > 0) {
  231.                 slideparentmenu();
  232.             } else {
  233.                 setTimeout(function(){
  234.                     slideparentmenu();
  235.                 }, 500);
  236.             }
  237.            
  238.         } else {
  239.             slideparentmenu();
  240.         }
  241.     };
  242.    
  243.     var menuSlideUp = function(i){
  244.         var child = $(i).children("ul");
  245.         if(child.length > 0) {
  246.             $(i).children("ul").stop().slideUp("fast", function(){
  247.                 parentMenuSlideUp(i);  
  248.                 // fix overflow slideup bug on ipad
  249.                 $(this).css({ "overflow": "", "height" : "auto" });
  250.             });
  251.            
  252.         } else {
  253.             parentMenuSlideUp(i);
  254.         }
  255.     }; 
  256.    
  257.     var bindHoverIntent = function() {
  258.         $("header nav li").hoverIntent({
  259.             over: function(){
  260.                 if(scw(mediaquerywidth)) {
  261.                     var i = $(this);
  262.                    
  263.                     // save last slide down element
  264.                     slideDownEle = i;
  265.                    
  266.                     $(i).find('a').addClass('whitecolor');
  267.                     if(i.parent().hasClass('menu')) {
  268.                         var bgmenu = "<div class='bgmenu'></div>";
  269.                         $(i).prepend(bgmenu);
  270.                        
  271.                         menuSlideDown(i);
  272.                        
  273.                         $(i).find('.bgmenu').css({
  274.                             "width" : $(i).width()
  275.                         }).stop().animate({
  276.                             height : $(i).height()
  277.                         }, 150, function(){});
  278.                        
  279.                     } else {
  280.                         menuSlideDown(i);
  281.                     }
  282.                 }
  283.             },
  284.             out: function(){
  285.                 if(scw(mediaquerywidth)) {
  286.                     var i = $(this);
  287.                     menuSlideUp(i);
  288.                 }
  289.             } ,
  290.             timeout: 250
  291.         });
  292.     };
  293.     bindHoverIntent();
  294.  
  295.    
  296.     var findparent = function (i) {
  297.         if($(i).parent().hasClass('menu')) {
  298.             return $(i);
  299.         } else {
  300.             return findparent($(i).parent());
  301.         }
  302.     };
  303.    
  304.     var slideUpChild = function(i){
  305.         var ul = $(i).children('ul');
  306.        
  307.         if(ul.length > 0) {
  308.             $(ul).slideUp("fast");
  309.             $(ul).children('li').each(function(){
  310.                 slideUpChild($(this));
  311.             });
  312.         }
  313.     };
  314.    
  315.     var slideUpMe = function(i){   
  316.         var parent = findparent(i);
  317.         parentMenuSlideUp(parent);
  318.         slideUpChild(parent);
  319.     };
  320.    
  321.    
  322.     $(window).bind("menuslideup", function(){
  323.         if(slideDownEle !== undefined) {
  324.             slideUpMe(slideDownEle);
  325.         }
  326.     });
  327. });
  328.  
  329.  
  330. /** history **/
  331. //return true kalo dia mau slide keatas
  332. var checktourl = function(url){
  333.    
  334.     var base    = base_url;
  335.     var curloc  = document.URL;
  336.     var nextloc = url;
  337.    
  338.     var index = nextloc.indexOf(base);                 
  339.    
  340.     if(index == 0 ){
  341.         var tempcurloc = curloc.split("#");
  342.         var tempnextloc = nextloc.split("#");      
  343.        
  344.         if(tempcurloc[0] == tempnextloc[0]) {
  345.             var hashindex = nextloc.indexOf("#");
  346.             if(hashindex == -1) {
  347.                 return false;
  348.             } else {
  349.                 return true;
  350.             }
  351.         } else {           
  352.             if(curloc == nextloc) {
  353.                 return true;
  354.             } else {
  355.                 return false;
  356.             }
  357.         }
  358.     } else {
  359.         return true;
  360.     }      
  361. };
  362.  
  363. String.prototype.unescapeHtml = function () {
  364.     var temp = document.createElement("div");
  365.     temp.innerHTML = this;
  366.     var result = temp.childNodes[0].nodeValue;
  367.     temp.removeChild(temp.firstChild);
  368.     return result;
  369. };
  370.  
  371. var ajax_request = undefined;
  372. var tourl = function(i, push){
  373.    
  374.     var cururl = document.URL;
  375.    
  376.     if(!scw(iphonewidth)) {
  377.         window.location = i;
  378.     } else {
  379.         if ( ( Modernizr.history && jcurtain ) != 0) {
  380.             if(!checktourl(i)) {
  381.                 if(push) {
  382.                     history.pushState({ href: i }, '', i);
  383.                 }      
  384.                
  385.                 jQuery("html, body").animate({
  386.                     scrollTop: 0
  387.                 }, function(){});
  388.                
  389.                 var wh = jQuery(window).height();                  
  390.                
  391.                 jQuery(window).trigger("menuslideup");
  392.                
  393.                 unmuteplayer();
  394.                
  395.                 function exeAjax () {
  396.                     /** remove before executing ajax main **/
  397.                     jQuery("#main").remove();
  398.                     jQuery(window).trigger("jmainremove");
  399.                    
  400.                     var replace_body = function (data) {
  401.                         // append data
  402.                         jQuery("div[role=main]").empty().append(data);
  403.                        
  404.                         // check jika page wrapper ga ada, maka ini dia bukan response kita, langsung aja lempar keluar
  405.                         if(jQuery('.pagemetawrapper').length == 0) window.location = i;
  406.                        
  407.                         // change title
  408.                         document.title = jQuery('.pagemetawrapper .pagemetatitle').text().unescapeHtml();
  409.                        
  410.                         // @todo : ganti language nya aja
  411.                         jQuery(".langwrapper").html(jQuery(data).find('.langwrapper').html());                     
  412.                        
  413.                         /** curtain loader **/
  414.                         if(curtainstyle == "fade") {
  415.                             jQuery('.curtain').fadeOut("slow");
  416.                         } else {
  417.                             jQuery(".curtain-loader").fadeOut();
  418.                             jQuery('.curtain').animate({'height'    : 0}, 1500, function(){});
  419.                         }
  420.                     };
  421.                    
  422.  
  423.                     if(ajax_request !== undefined) {
  424.                         ajax_request.abort();
  425.                     }                  
  426.                    
  427.                     ajax_request = jQuery.ajax({
  428.                         url: i,
  429.                         type : "POST",
  430.                         dataType : "html",
  431.                         success: function(data) {
  432.                             replace_body(data);
  433.                         },
  434.                         error : function(xhr, ajaxOptions, thrownError) {
  435.                             if(xhr.status == '404') {
  436.                                 replace_body(xhr.responseText);
  437.                             } else {       
  438.                                 window.location = i;
  439.                             }
  440.                         }
  441.                     });
  442.                    
  443.                 }
  444.                
  445.                 if(curtainstyle == 'fade'){
  446.                     jQuery('.curtain').fadeIn(500, function(){
  447.                         exeAjax();
  448.                     });
  449.                 } else {
  450.                     jQuery('.curtain').animate({
  451.                         'height'    : '100%'
  452.                     }, 300, function(){
  453.                         jQuery(".curtain-loader").fadeIn();                
  454.                         exeAjax();
  455.                     });
  456.                 }
  457.                
  458.                 return false;
  459.             }
  460.             return true;
  461.         } else {
  462.             window.location = i;
  463.         }
  464.     }
  465. };
  466.  
  467. window.tourl = tourl;
  468.  
  469. jQuery(document).ready(function($)
  470. {
  471.     if (Modernizr.history) {
  472.         /** popstate **/
  473.         window.onpopstate = function(e){
  474.             if(e.state !== null) {
  475.                 return tourl(e.state.href, false);
  476.             }
  477.         };
  478.     }
  479.    
  480.     // define curtain position
  481.     if(curtainstyle == 'fade'){
  482.         $(".curtain").addClass('curtainfade');
  483.     }
  484.    
  485.     $('.navselect select').change(function(){
  486.         tourl($(this).val(), true);
  487.     });
  488. });
  489.  
  490. (function($) {
  491.     /** rebinding a href **/
  492.     $.fn.jegdefault = function( options )
  493.     {                      
  494.         var settings = {
  495.             curtain         : false,
  496.             rightclick      : true,
  497.             clickmsg        : ''
  498.         };     
  499.        
  500.         if (options) {
  501.             var options = $.extend(settings, options); 
  502.         } else {
  503.             var options = $.extend(settings);                  
  504.         }
  505.        
  506.         if(!options.rightclick && defaultexecuted == 0) {          
  507.             $(document).mousedown(function(e){               
  508.                 if( e.button == 2 ) {
  509.                       alert(options.clickmsg);
  510.                       return false;
  511.                   }
  512.                   return true;               
  513.             });              
  514.         }
  515.        
  516.         if (Modernizr.history) {
  517.             $('a').not('[data-tourl="false"]').unbind('click');
  518.             if(options.curtain) {
  519.                 $('a').not('[data-tourl="false"]').bind('click', function(){
  520.                     if(!$(this).parents("#wpadminbar").length) {
  521.                         return tourl(this.href, true);
  522.                     } else {
  523.                         return true;
  524.                     }
  525.                 });
  526.             }
  527.         }
  528.        
  529.         /** alert **/
  530.         $('[data-dismiss="alert"]').click(function(){
  531.             $(this).parent().fadeOut();
  532.         });
  533.        
  534.         /** tab **/
  535.         $('[data-tab="tab"]').each(function(){
  536.             var i   = $(this),
  537.             el  = [$("> .nav-tabs > li", i), $("> .tab-content > .tab-pane", i)];
  538.            
  539.             $(el).each(function(){
  540.                 $(this).parent().children(":first").addClass("active");
  541.             });
  542.            
  543.             $("> .nav-tabs li", i).click(function() {
  544.                 if(!$(this).hasClass("active")) {
  545.                     var a = "active";
  546.                     $(el).each(function(){$(this).removeClass(a);});
  547.                     $(this).addClass(a);
  548.                     $(el[1].get(el[0].index(this))).addClass(a);
  549.                    
  550.                     $(window, document).trigger('resize');
  551.                 }
  552.                 return false;
  553.             });
  554.         });
  555.        
  556.         /** colapse **/
  557.         $('[data-accordion="accordion"]').each(function(){
  558.             var i = $(this);
  559.             var acb = "> .accordion-body", acg = "> .accordion-group";         
  560.            
  561.             var showAccordion = function (el) {    
  562.                 if($(el).hasClass("active")) {
  563.                     hideAccordion(el);
  564.                 } else {
  565.                     $(acb, el).slideDown("fast");
  566.                     $(el).addClass("active");                  
  567.                 }
  568.             };
  569.            
  570.             var hideAccordion = function(el) {
  571.                 $(acb, el).slideUp("fast");
  572.                 $(el).removeClass("active");           
  573.             };
  574.            
  575.             showAccordion($(acg , i).get(0));
  576.             hideAccordion( $(acg , i).not(":eq(0)") );         
  577.            
  578.             $(acg , i).click(function(e){              
  579.                 var idx = $(acg , i).index(this);              
  580.                 showAccordion($(acg , i).get(idx));
  581.                 hideAccordion($(acg , i).not(":eq("+idx+")") );
  582.                 return false;
  583.             });
  584.         });
  585.        
  586.         /** tooltips **/
  587.         $('[data-rel="tooltip"]').tipTip({maxWidth: "auto", edgeOffset: 10, defaultPosition: "top", delay : 100});
  588.        
  589.         /** remove me **/
  590.         $('.removeme').remove();
  591.        
  592.        
  593.         /** fix bug when image expanded **/
  594.         $("html").css("overflow-y", "").css("overflow-x", "").css("overflow", ""); 
  595.        
  596.         // default has been executed
  597.         defaultexecuted++;     
  598.     };
  599. })(jQuery);
Advertisement
Add Comment
Please, Sign In to add comment