(function($) {

    // Fady version 0.1
    // Simple Cross Fade Images
    // By Kuzin

    $.fn.fady = function(options) {

        if (!this.length) { return this; }

        var opts = $.extend(true, {}, $.fn.fady.defaults, options);
        
        this.each(function() {
            var $this       = $(this),
                $image      = $this.find('img'),
                $background = $image.attr('src');

            var height = (opts.vertical == true) ? $image.height() / 2 : $image.height();
            var width  = (opts.vertical != true ) ? $image.width() / 2 : $image.width();

            $this.css({
                'background' : 'url(' + $background + ')',
                'background-position' : 'bottom left',
                'background-repeat' : 'no-repeat',
                'overflow' : 'hidden'
            }).height(height).width(width);

            $this.hover(function() {
                $image.stop().animate({opacity:0}, opts.speed);
            }, function() {
                $image.stop().animate({opacity:1}, opts.speed);
            });

        });
        return this;
    };

    // Options
    $.fn.fady.defaults = {
    'speed'     : 500, // int
    'vertical'  : true // null if horizontal
    };

})(jQuery);



// run placeholder plugin
var placeholder = function () {
    $('input[placeholder], textarea[placeholder]').placeholder();
};



// createSlideshow('string', 'string', int, bol, bol);
var createSlideshow = function (elem, fx, timeout, pause, pager) {
    var $slideshow = $(elem).cycle({
        fx: fx == null ? 'fade' : fx,
        pause: pause == true ? true : false,
        pager: pager == true ? '#pager' : null,
        timeout: timeout == null ? 6000 : timeout,
    });
};


// Hot Tickets
var sliderTimeout = null;

var sliderAutoplay = function () {
    $('#trending-container').trigger('nextSlide');
    sliderTimeout = setTimeout(sliderAutoplay, 3000);
}

var hotTickets = function () {
    window.onload = function() {
           $('#trending-container').lemmonSlider({ 'fast' : 1000 });
           sliderAutoplay();
    }
    $('#trending-container').live('mouseover', function(){
        clearTimeout(sliderTimeout);
    });
    $('#trending-container').live('mouseout', function(){
        sliderTimeout = setTimeout(sliderAutoplay, 1000);
    });
}

var area, type, text, url, which;

var check = function(a) {
    var o = {};
    for (var i=0; i<a.length;i++) {
        o[a[i]]='';
    } return o;
}

// Check XML Joblist
var jobList = function(){
    $.ajax({
        type: 'GET',
        url: '/static/resumator.xml',
        dataType: 'xml',
        success: function(xml) {
            $(xml).find('job').each(function(){
                if ($(this).find('status').text() == 'Open') {

                    var location = $(this).find('location').text();

                    text = $(this).find('title').text();
                    url = $(this).find('url').text();
                    area = text.split(' ');

                    if ('Engineer' in check(area)) {
                        type = 'Software Engineering';
                    } else if ('Intern' in check(area)) {
                        type = 'Internships';
                    } else if ('Internship' in check(area)) {
                        type = 'Internships';
                    } else if ('Customer' in check(area)) {
                        type = 'Customer Care';
                    } else if ('Designer' in check(area)) {
                        type = 'Creative';
                    } else if ('Creative' in check(area)) {
                        type = 'Creative';
                    } else if ('Sales' in check(area)) {
                        type = 'Sales';
                    } else if ('Account' in check(area)) {
                        type = 'Customer & Client Support';
                    } else if ('Marketing' in check(area)) {
                        type = 'Marketing';
                    } else if ('Assistant' in check(area)) {
                        type = 'Administration';
                    } else {
                        type = 'other'; }

                    which = type.replace(/\s+/g, '-').toLowerCase();

                    if ($('ul#jobs').find('li.' + which).attr('class') == which) {
                        $('ul#jobs').find('li.' + which)
                            .append('<br/><a href="' + url +
                            '">' + text +' <span>»</span></a>');
                    } else {
                        $('ul#jobs')
                            .append('<li class="' +
                            which +'"><strong>' + type +
                            '</strong><br/><a href="' + url +
                            '">' + text + ' <span>»</span></a></li>');
                    }
                };
            });
        }
    });
};
