var page = {
        tr: null, ts: null, Tracker: null, o: new Object,
        
        init: function() {
          if ( $("#slider").length ) {
            page.tr = $("#slider").fadeTransition({pauseTime: 7000, transitionTime: 500});
            $("div.navigation").each(function() {
              $(this).children().each( function(idx) {
                if ($(this).is("a"))
                  $(this).click(function() { page.tr.showItem(idx); return false; })
              });
            });
          }
          
          if ( $("div#right_column").length ) {
            $('.tab_container').each(function(){ page.lists($(this).attr('id')); });
            page.facebookConnect();
          };
          
          if ($('div#social').length) {
            page.social();
          };
          

          if ($("div#gallery").length) {
            page.gallery();
          };
          
          if ($('#gallery_period').length) {
            $('select#gallery_period').bind('change', function() {
              var q = $(this).val().split(",");
              if (q[1]) {
                $.get('/d/'+q[0]+'/'+q[1], function(data){
                 $('#gals ul').html(data);
                });
                
              };
            });
          }
          
          if ($("div#galerija").length){
            
            $(window).load(
              function(){
               var current = 1;
                $('div.roll').each(function(index) {
                  $(this).css('visibility','visible');
                  var img = $(this).find('img');
                  var w = img.width();
                  var h = img.height();
                  
                  img.css({'left':(900-w)/2});
                  img.css({'top':(600-h)/2});
                  
                  if (index == 0) { 
                    $(this).css({'display':'block'});
                  } else {
                    $(this).css({'display':'none'});
                  }
                });
                
                $(window).keydown(function(event){
                    var key = event.keyCode;
                    if (key == 37) {  $("#galerija .right").trigger('click'); };
                    if (key == 39) {  $("#galerija .left").trigger('click'); };
                });
                
                $("#galerija .left").bind('click', function(){
                  list('p');
                } );
                $('#galerija .right').bind('click', function(){
                  list('n');
                } );

                function list(q){
                  q == 'p' ? c=current+1 : c=current-1;
                  var x = $('div.roll:nth-child('+current+')');
                  var y = $('div.roll:nth-child('+c+')');
                  if (y.length != 0) {
                    current = c;
                  } else {
                    if (current == 1) {
                      var y = $('div.roll:nth-child('+$('div.roll').length+')');
                      current = $('div.roll').length;
                    } else {
                      var y = $('div.roll:nth-child(1)');
                      current = 1;
                    }
                  }
                  x.fadeOut();
                  y.fadeIn();
                }
              }
          );
          };
          
          page.googleAnalyze();
          
        },
        
        social: function(){
          
          var social = $('div#social'),
              t = document.title,
              u = document.URL,
              short_t = $('p#short_description').text();
              
          services = { 'digg':{     'url'   : "http://digg.com/submit",
                                    'link'  : "url",
                                    'title' : "title" },
                      'facebook': { 'url'   : "http://www.facebook.com/share.php",
                                    'link'  : "u",
                                    'title' : "t" },
                      'twitter': {  'url'   : "http://twitter.com/home",
                                    'title' : "status" },
                      'google': {   'url'   : "http://www.google.com/bookmarks/mark",
                                    'link'  : "op=edit&bkmk",
                                    'title' : "title",
                                    'desc'  : "annotation" },
                      'delicious': {'url'   : "http://delicious.com/post",
                                    'link'  : "url",
                                    'title' : 'title',
                                    'desc'  : 'notes'
                        
                      },
          };
          
          $.each(services, function(i, v) {
            var link = document.createElement('a');
                if (v.link) {
                  q = v.link+"="+u+"&"+v.title+"="+t;
                  v.desc ? q+="&"+v.desc+"="+short_t : null;
                } else {
                  q = v.title+"="+t;
                  v.desc ? q+=v.desc+"="+short_t : null;
                  
                }
                
              
                link.href = v.url+"?"+q;
                $(link).addClass('social').addClass(i).text(i).appendTo(social);
          });
          
           function shortUrl(){
             this.href += ' → http://url.ba/vd4n';
           }
        },
        
        gallery: function(){
          
          //Preload images in gallery

          $("div#gallery ol li a").each(function(){
            var img = new Image;
                img.src = this.href;
          });
          
          $("div.left").bind('click', function(){
            $("div#gallery ol li:last").prependTo("div#gallery ol");
            $("div#gallery ol").css({"left":-115});
            $("div#gallery ol").animate({"left":0}, "fast");
            $("div#gallery ol li a").trigger('click');
          });
          
          $("div.right").bind('click', function(){
            $("div#gallery ol").animate({"left":-115}, "fast", function(){
              $("div#gallery ol").css({"left":0});
              $("div#gallery ol li:first").appendTo("div#gallery ol");
            });
            $("div#gallery ol li").next().find('a').trigger('click');
          });
          
          var img = $(document.createElement('img'));
          
          img.bind('click', function(){

              var expose = $(document.createElement('div'));
              var overlay = $(document.createElement('div'));
                  img_c = img.clone();
                  overlay
                    .html(img_c)
                    .addClass('overlay')
                    .bind('click', function(){
                      overlay.fadeOut('fast', function(){ $(this).remove(); });
                      expose.fadeOut('fast', function(){ $(this).remove(); });
                  })
                  .prependTo('body')
                  .fadeIn('slow')
                  .css({
                          "top":($(window).height()/2)-(img_c.height()/2)-20+"px",
                          "left":($(window).width()/2)-(img_c.width()/2)-20+"px"
                  });
                  

                  
                  expose
                    .addClass('expose')
                    .fadeIn('fast')
                    .bind('click', function(){
                      overlay.fadeOut('fast', function(){ $(this).remove(); });
                      expose.fadeOut('fast', function(){ $(this).remove(); });
                    })
                    .prependTo('body');
          });
          
          $("div#gallery ol li a").live('click', function(){
            var src = $(this).attr('href');
            var alt = $(this).attr('title');
                      
                      var new_image = new Image;
                          new_image.src = src;
                          $(new_image).load(function(){
                           img.parent().height(this.height+'px');
                          });
                      
            img.fadeOut('fast', function(){
              img.attr({'src':src, 'alt':alt});
              img.fadeIn('fast');
              img.parent().height(img.height()+10);
            });
            return false;
          });
          
          if ( !$("#picture_preview img").length ) {
            img.attr({'src':$("div#gallery ol li a").attr('href'), 'alt':$("div#gallery ol li a").attr('title')});
            $("#picture_preview").html(img);
          };
        },
        
        googleAnalyze: function(){
          var host = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
          var src = host + 'google-analytics.com/ga.js';
          $.ajax({
            url: src,
            type: 'GET',
            dataType: 'script',
            cache: true,
          
            success: function() {
              page.Tracker = _gat._getTracker('UA-12383803-1');
              page.Tracker._trackPageview();
            },   
            error: function() { throw "Unable to load ga.js; _gat has not been defined."; }
          });
        },

        show: function(idx) {
          if (page.tr.timer) clearTimeout(page.tr.timer);
          page.tr.showItem(idx);
        },
        
        facebookConnect: function() {   
          var loaded = null;
          var api_key = 'a9d92ba216c544f61a752bf756df9a10';
          var xd = '/media/xd_receiver.htm';
          $.ajax({
            url: 'http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php/bs_BA',
            type: 'GET',
            dataType: 'script',
            cache: "cache",
            async: true,
            
            complete: function(){
              if (loaded == 1) {
                  var fb = $(document.createElement('fb:fan')).attr('profile_id', 242909460046).attr('stream', 0).attr('connections', 10).attr('width', 300);
                  $('div#right_column').append(fb);
              };
            },
          
            success: function() {
              FB.init(api_key, '/media/xd_receiver.htm');
              if ( FB.HiddenContainer.get() == null ) {
                var hc = $(document.createElement('div')).attr('id', 'FB_HiddenContainer').css('display', 'none').appendTo('body');
              }
              loaded = 1;
            },
          
            error: function() {
              throw "Unable to load FeatureLoader.";
            }
          });
        },
        
        lists: function(q) {
          var t = new Yetii({ id: q, interval: 20 });
        }
        
      };

jQuery(document).ready(function($) {
  page.init();
});