jQuery(document).ready(function(){

    /* Animate Flashmessages */
    animateMessages();

    /* Popup */
    $(".video-popup").fancybox({
            'width': 800,
            'height': 450,
            'transitionIn': 'none',
            'transitionOut': 'none',
            'type' : 'iframe'
    });

    /* overlays for thumbs */
    jQuery('.thumb-300, .home-thumb-185').hover(function(){
            jQuery(".desc-overlay", this).stop().animate({
                bottom:'-0px'
            },{queue:false,duration:300});
            jQuery(".function-overlay", this).stop().animate({
                top:'-0px'
            },{queue:false,duration:300});
    }, function() {
            jQuery(".desc-overlay", this).stop().animate({
                bottom:'-300px'
            },{queue:false,duration:300});
            jQuery(".function-overlay", this).stop().animate({
                top:'-300px'
            },{queue:false,duration:300});
    });

    /* put salt into the pw */
    jQuery("#protectedform").submit(function(){

        var salt =  jQuery("#password_salt").val();
        var pwLengthMinimum = jQuery("#password_length").val();
        var pw1 = jQuery("#password").val();
        var pw2 = jQuery("#password_repeat").val();
        var pw1Length = pw1.length;
        var pw2Length = pw2.length;

        if (pw1Length > 0) {
            if (pw1Length >= pwLengthMinimum) {
                pw1 = jQuery.md5(salt+pw1);
            } else {
                substr(pw1, 0, pw1Length);
            }
        }

        if (pw2Length > 0) {
            if (pw2Length >= pwLengthMinimum) {
                pw2 = jQuery.md5(salt+pw2);
            } else {
                substr(pw2, 0, pw2Length);
            }
        }

        jQuery("#password").val(pw1);
        jQuery("#password_repeat").val(pw2);
        jQuery("#password_salt").val('');
    });

    /* put salt into the pw */
    jQuery("#login-form").submit(function(){

        var salt =  jQuery("#password_salt").val();
        var pwLengthMinimum = jQuery("#password_length").val();
        var pw1 = jQuery("#password").val();
        var pw1Length = pw1.length;

        if (pw1Length > 0) {
            if (pw1Length >= pwLengthMinimum) {
                pw1 = jQuery.md5('12345');
            } else {
                substr(pw1, 0, pw1Length);
            }
        }

        jQuery("#password").val(pw1);
        jQuery("#password_salt").val('');
    });

    /* inline-editing with input */
     jQuery(".editable").addClass('input-noedit').attr({
        'onBlur'  : 'jQuery(this).removeClass("editable").addClass("input-noedit")',
        'onClick' : 'jQuery(this).removeClass().focus().select()'
    }).after('<a onClick="jQuery(this).prev().removeClass().select()"><img class="icon-very-small" src="/themes/default/images/icon-edit.png"></a>');


    /* Toggle Group Tabs */
    jQuery('#group-threads-tab').click(function() {
      jQuery('#group-threads-tab-title').toggle();
      jQuery('#group-threads-tab-title-member').toggle();
      jQuery('#group-threads-list').toggle();
      jQuery('#group-threads-list-member').toggle();
      return false;
    });

    /* Toggle Selling Options */
    if(jQuery('#sellingdetails').val() != '') {
           jQuery('#sellingdetailsrow, #sellingdetailsrow .hint').fadeIn('slow');
    } else if(jQuery('#sellingoption').attr("checked")) {
        jQuery('#sellingdetailsrow, #sellingdetailsrow .hint').fadeIn('slow');        
    }
    else {
        jQuery('#sellingoption').click(function() {

             if(jQuery(this).attr("checked")) {
                //show
               jQuery('#sellingdetailsrow, #sellingdetailsrow .hint').fadeIn('slow');
             } else {
               // hide
               jQuery('textarea#sellingdetails').val(null);
               jQuery('#sellingdetailsrow, #sellingdetailsrow .hint').fadeOut('slow');
             }

        });
    }

    /* Toggle Licence Options */
    if(jQuery('#license').attr("checked")) {
           jQuery('#work-license-table').fadeIn('slow');
    } else {
           jQuery('#work-license-table').hide();
    }

    jQuery('#license').click(function() {
         if(jQuery(this).attr("checked")) {
            //show
           jQuery('#work-license-table').fadeIn('slow');
         } else {
           // hide
           jQuery('#work-license-table').fadeOut('slow');
         }

    });

    /* search */
    var defaultSearchValue = $('#search-input').val();

    // blur
    $('#search-input').bind({
        focus: function() {
            if(this.value == defaultSearchValue)
                    this.value = '';
        },
        blur: function() {
            if(this.value == '')
                this.value = defaultSearchValue;

        }
    });

    $('#new-search-right').mouseover(function() {
        $('#new-search-filter').show();
    });

    $('#new-search-right').mouseout(function() {
        $('#new-search-filter').hide();
    });

    $('#search-form').submit(function() {
        var input = $('#search-input');
        if(input.val() == defaultSearchValue) {
            $('#search-input').focus();
            return false;
        }
        if(input.val() == '') {
            $('#search-input').focus();
            return false;
        }
    });

    /* tooltip help */
    $('a.help').qtip({
        show: {
            when: {
                target: false,
                event: 'click'
            }
        },
        hide: { 
            when: { 
                event: 'unfocus'
            }
        },
        style: {
            name : 'blue',
            tip: true,
            border: {
                width: 1,
                radius: 1,
                color: '#299fcb'
            },
            tip: {
                corner: 'topLeft',
                color: '#94d9f3'
            },
            width: {
                min: '400'
            }
        },
        content: {
            prerender: true,
            text : 'Loading'
        },
        api: {
           beforeShow: function() {
              var url = this.elements.target.attr('href');
              if (url != '') {
                 this.loadContent(url);
              }
           },
           onContentLoad: function() {
              this.elements.target.attr('href', '');
           }
        }
    })

    $('a.help').click(function() {
        return false;
    });

    /* gallery */
    $(".choose-gallery").mouseenter(function() {
        $(this).css('background-color', '#77b82a');
        $(this).css('color', 'white');
    }).mouseleave(function() {
        $(this).css('background-color', '#EAEAEA');
        $(this).css('color','#555555');
    });

    /* biling form */
    if($(".billing-form #countryCode").val() == 'DE') {
        $('#fieldset-group3').show();
        $('#fieldset-group4').hide();
    } else {
        $('#fieldset-group3').hide();
        $('#fieldset-group4').show();
    }

    $(".billing-form #countryCode").click(function() {
        if($(this).val() == 'DE') {
            $('#fieldset-group3').show();
            $('#fieldset-group4').hide();
        } else {
            $('#fieldset-group3').hide();
            $('#fieldset-group4').show();
        }
    });

 
    /* AJAX paging */
	
	
    $('#pagingButton').bind('click', {target: '#page'}, pagingHandler);
    function pagingHandler(event) {
        var button = jQuery(this);
        var url = button.attr('href');
        var target = event.data.target;
        var pageWrapper = jQuery(target);

        button.removeAttr('onClick');
        button.css('backgroundImage', "url('/themes/default/images/stripe-loader.gif')");

        var nextPage = jQuery(document.createElement('div')).hide();
        pageWrapper.append(nextPage);
        nextPage.load(url + ' ' + event.data.target, function(){
            button.fadeOut('fast', function(){
                button.remove();
                nextPage.fadeIn();
                jQuery.getScript('/js/ldk-functions.js');
            });
        });
        return false;
    }


  jQuery("span.timeago").timeago();
  
$('#homeSlider').anythingSlider({





  buildArrows         : false,      // If true, builds the forwards and backwards buttons
  buildNavigation     : true,      // If true, builds a list of anchor links to link to each panel
  buildStartStop      : false,      // If true, builds the start/stop button

  
  toggleArrows        : false,     // If true, side navigation arrows will slide out on hovering & hide @ other times
  toggleControls      : false,     // if true, slide in controls (navigation + play/stop button) on hover and slide change, hide @ other times

 
  // Navigation
  startPanel          : 1,         // This sets the initial panel
  changeBy            : 1,         // Amount to go forward or back when changing panels.
  hashTags            : false,      // Should links change the hashtag in the URL?
  infiniteSlides      : false,      // if false, the slider will not wrap & not clone any panels
  navigationFormatter : null,      // Details at the top of the file on this use (advanced use)
  navigationSize      : 5,     // Set this to the maximum number of visible navigation tabs; false to disable

  // Slideshow options
  autoPlay            : true,     // If true, the slideshow will start running; replaces "startStopped" option
  autoPlayLocked      : true,     // If true, user changing slides will not stop the slideshow
  autoPlayDelayed     : false,     // If true, starting a slideshow will delay advancing slides; if false, the slider will immediately advance to the next slide when slideshow starts
  pauseOnHover        : true,      // If true & the slideshow is active, the slideshow will pause on hover
  stopAtEnd           : false,     // If true & the slideshow is active, the slideshow will stop on the last page. This also stops the rewind effect when infiniteSlides is false.
  playRtl             : false,     // If true, the slideshow will move right-to-left

  // Times
  delay               : 8000,      // How long between slideshow transitions in AutoPlay mode (in milliseconds)
  resumeDelay         : 2000,     // Resume slideshow after user interaction, only if autoplayLocked is true (in milliseconds).
  animationTime       : 600,       // How long the slideshow transition takes (in milliseconds)
  delayBeforeAnimate  : 0
  
 

});
}); // end dom ready

function animateMessages() {
    window.setTimeout(function() {
    jQuery("#flashmessage").fadeIn("fast", function() {
        window.setTimeout(function() {
            jQuery("#flashmessage").fadeOut("slow", function() {
                jQuery(this).hide();
            });
        }, 4000);
    });
    },500);
}

function goToSelectedUrl (select)
{
    var value = select.options[select.options.selectedIndex].value;
    if (value == "") {
        select.form.reset();
        return;
    } else {
        location.href = value;
    }
}

function removeNotification(id, el)
{
    jQuery.getJSON('/notification/delete/',
        {'id':id, 'format':'json'},
        function(response){
           if(response.success)
                el.fadeOut('slow');
        }
    );
}

function disableEnterKey(e)
{
     var key;

     if(window.event)
          key = window.event.keyCode;     //IE
     else
          key = e.which;     //firefox

     if(key == 13)
          return false;
     else
          return true;
}
