$(document).ready(function(){
    
    $('#facebook-likebox').append('<fb:like-box href="http://www.facebook.com/pages/" width="220" show_faces="false" stream="false" header="true"></fb:like-box>');
    
    $('a.fancybox').fancybox();
    $('a[rel=fancybox]').fancybox();
    
    $('input.default, textarea').hideDefaultValue();
    
    $('#subscribe').click(function(){
        $.fancybox({
            type: 'iframe',
            content: $(this).attr('href'),
            titlePosition: 'over',
            width: 510,
            height: 294,
            padding: 10
        });
        return false;
    })
    
    $('#images').galleria({
         height: 580
    });
    
    $('a[rel=noSpam]').each(function(m){
        var array = $(this).attr("title");
        array = array.split(" ");
        var email = array[2] + "@" + array[1] + "." + array[0];
        $(this).html(email);
        $(this).attr("href", 'mailto:' + email);
        $(this).attr("title", '');
    });
    
    if ($('#nav2 a.active').length > 0) start = parseInt($('#nav2 a.active').attr('data'));
    else start = 0;
    $('#navblock').cycle({
        fx:     'scrollUp',
        speed:   200,
        timeout:  600,
        delay: 600,
        sync: true,
        slideExpr: 'img'
    }).cycle('pause').cycle(start);

    $('#nav2 a').bind('mouseover',function(){
        $(this).animate({  height: '84px'  }, { queue: false, duration: 200 }, 'linear');
        
        var index = parseInt($(this).attr('data'));
        $('#navblock').cycle(index);
        //console.log();
        
        if ($('#navblock.small').length == 0) {
            $("#nav2 span.text").hide();
            $(this).parent().find('span.text').show(0, function(){
                position = $(this).parent().find('span.text').offset().left - $('#navblock').offset().left;
                if (position < 68) 
                    $(this).parent().find('span.text').css('margin-left', -250 - position + 68 + 'px');
                if (position > 379) 
                    $(this).parent().find('span.text').css('margin-left', -250 - position + 379 + 'px');
            });
        }
        
        $('#nav2 a').not($(this)).stop().animate({  height: '29px'  }, { queue: false, duration: 200 }, 'linear');
    });
    $('#main,body').mouseover(function(){
        $('#nav2 a').animate({  height: '29px'  }, { queue: false, duration: 200 }, 'linear');
        $("#nav2 span.text").hide();
    });
    $('#navblock').mouseover(function(event){
        event.stopPropagation();
    })
    

});

(function($){
    //
    // Sometimes you want to display INPUT elements labels inside them as their value
    // This plugin hides these labels on focus and writes them back on blur if input is empty
    // Extra feature: it can convert type=text inputs to type=password and vice-versa
    //
    // Usage: 
    // <input type="text" name="username" value="Username" class="default" /> 
    // <input type="text" name="password" value="Password" class="default password" /> 
    //
    // $('input.default').hideDefaultValue();
    //
    //
    // Maris Kiselovs
    //
    
    var methods = {
        changeType: function(type){
            // IE won't change type attribute, so we need to replace whole input
            var newField = $('<input type="' + type + '" />')
              .addClass(this.options.passwdClass)
              .attr('id', $(this).attr('id'))
              .attr('name', $(this).attr('name'))
              .insertAfter($(this))
              .data('defaultText',$(this).data('defaultText'))
              .hideDefaultValue(this.options);
            $(this).remove();
            return newField;
        }
    }
            
    $.fn.extend({ 
        hideDefaultValue: function(options) {
            
            // Settings
            var defaults = {
                'class': 'default', // Class name for labeled inputs. 
                'passwdClass': 'password' // Class name for password fields
            };
            var options = $.extend(defaults, options);
            
            return this.each(function() {
               var o = options;
               this.options = options;
               
               if ($(this).data('defaultText') === null || $(this).data('defaultText') === undefined) // do not overwrite data
                   $(this).data('defaultText',$(this).val());

               $(this).focus(function() {
                   if ($(this).data('defaultText') == $(this).val() && $(this).hasClass(o['class'])) {
                       $(this).removeClass(o['class']);
                       $(this).val('');
                       if ($(this).hasClass(o.passwdClass)) {
                           var newField = methods.changeType.apply( this, ['password'] );
                           newField.focus();
                       }
                   }
               });
               
               $(this).blur(function() {
                   if ($(this).val().length == 0) {
                       $(this).addClass(o['class']);
                       if ($(this).hasClass(o.passwdClass)) {
                           var newField = methods.changeType.apply( this, ['text'] );
                           newField.val(newField.data('defaultText'));
                           newField.addClass(o['class']);
                       }
                       $(this).val($(this).data('defaultText'));
                   }
               });
            });
        }
    }); 
})(jQuery);
