String.prototype.endsWith = function (suffix) {
  return (this.substr(this.length - suffix.length) === suffix);
}

String.prototype.startsWith = function(prefix) {
  return (this.substr(0, prefix.length) === prefix);
}

$(document).ready(function() {
    /*$('.gallery-content ul').jcarousel({
    auto: 2,
    wrap: 'last',
    initCallback: function(sender){}
    });*/

    $('a.video-player').click(function() {
        var title = $(this).attr('title');
        var videoUrl = this.href.replace(new RegExp("watch\\?v=", "i"), 'v/');
        var typeToUse = videoUrl.endsWith('.wmv') ? 'iframe' : 'swf';

        $.fancybox({
            'padding': 10,
            'autoScale': false,
            'transitionIn': 'none',
            'transitionOut': 'none',
            'title': title,
            'width': 600,
            'height': 450,
            'href': videoUrl,
            'type': typeToUse,
            'swf': {
                'wmode': 'transparent',
                'allowfullscreen': 'false'
            }
        });
        return false;
    });

    $('a.image-viewer').click(function() {
        var title = $(this).attr('title');
        var imageUrl = this.href;
        $.fancybox({
            'padding': 10,
            'autoScale': true,
            'transitionIn': 'none',
            'transitionOut': 'none',
            'title': title,
            'href': imageUrl,
            'type': 'image'
        });
        return false;
    });

    $('li.link a').click(function() {
        var href = $(this).attr('href');

        if (href.startsWith('http://') || href.startsWith('https://')) {
            $(this).attr('target', '_blank');
        }
        return true;
    });

    var watermark = false;
    $('#q').autocomplete('/search/autocomplete', {
        minChars: 2,
        width: 208,
        autofill: false
    })
    .result(function(event, item) {
        location.href = item[1];
        return false;
    })
    .focus(function() {
        if (watermark == false) {
            watermark = true;
            $(this).val('').css('color', '#000000');
        }
    })
    .blur(function() {
        var $me = $(this);
        if ($me.val().length == 0 && watermark) {
            watermark = false;
            $me.val('Search');
            $me.css('color', '#c0c0c0');
        }
    });
});
