/*
 * Primary Scripts
 * I &heart; HTML5 Boilerplate
 * Author       : Ciobanu N. Cristian <ciobanu.n.cristian@gmail.com>
 * Created on   : Dec 22, 2010, 4:00:00 PM
 */

$(document).ready(function() {
    // Default AJAX call settings
    $.ajaxSetup({
        cache: false,
        timeout: 5000,
        error:function(x,e){
            if('parsererror'==e) {
                notification('Sorry, we ran into a technical problem (parse error). Please try again...', 'red');
            } else if('timeout'==e) {
                notification('Request timed out. Please try again...', 'red');
            } else if ( "status" in x ) {
                if(0 == x.status) {
                    notification('You are offline! Please check your network.', 'red');
                } else if (404 == x.status) {
                    notification('Sorry, we ran into a technical problem (404). Please try again...', 'red');
                } else if (500 == x.status) {
                    notification('Sorry, we ran into a technical problem (500). Please try again...', 'red');
                }
            } else {
                notification('Sorry, we ran into a technical problem (unknown error). Please try again...', 'red');
            }
        }
    });
    // Notification
    $('.notification').live('click', function() {
        $(this).slideUp(1000);
    });
    // Infinite replication
    if ($('#row-template-add').length > 0) {
        $('#row-template-add').click(function() {
            $('#row-template').clone(true).removeAttr('id').val('').append('<a class="row-template-rmv">remove</a>').appendTo($('#row-template-wrapper'));
            return false;
        });
        $('.row-template-rmv').live('click', function() {
            $(this).parent().remove();
            return false;
        });
    }
    if ($('#slide').length > 0) {
        $('#slide').flashembed({src: "assets/swf/slide.swf", wmode:'transparent'});
    }
    if ($('#slideshow').length > 0) {
        setInterval("fadeInOut('slideshow')", 5000);
    }
    if ($('#slideshow-home').length > 0) {
        setInterval("fadeInOut('slideshow-home')", 5000);
    }
    // Validate forms
    $("form.validate").validator();
    //
    if ($('.scrollable').length > 0) {
        $('.scrollable').scrollable({
            disabledClass: 'invisible',
            circular: true,
            keyboard: true,
            mousewheel: true
        });
        $(".scrollable").data("scrollable").onSeek(function() {
            var i = 0;
            var newHeight = 0;
            var scrollIndex = this.getIndex();
            this.getItems().each(function() {
                if(i == scrollIndex) {
                    newHeight = $(this).height();
                    return false;
                }
                i++;
            });
            this.getRoot().add(this.getItemWrap()).animate({
                height : newHeight
            });
        });

        var selector = '';
        if ($('.trigger-overlay').length > 0) {
            selector = '.trigger-overlay .item img';
        }
        if ($('.gallery').length > 0) {
            selector = '.gallery a';
        }

        $.each($(selector), function(i, e) {
            $elem = $(this);
            $(this).overlay({
                target: '.overlay',
                fixed: true,
                top: 5,
                mask: {
                    color: '#000',
                    loadSpeed: 200,
                    opacity: 0.5
                },
                onLoad: function() {
                    $siblings = $elem.parent().children().length;
                    if ($siblings == 1) {
                        $(".overlay .scrollable").data("scrollable").seekTo(i, 0);
                    } else {
                        $(".overlay .scrollable").data("scrollable").seekTo(i - $siblings, 0);
                    }
                }
            });
        });
    }
    $.each($('a.player'), function() {
        var media = $(this).attr("href");
        $f('.player', {
            src: "/clients/diamondback/assets/swf/flowplayer-3.2.5.swf",
            version: [9, 115]
        }, {
            clip: media,
            autoPlay: false,
            autoBuffering: true
        });
    });
});

function notification(text, status) {
    $('body .notification').remove();
    $('body').prepend('<div class="notification ' + status + '">' + text + '</div>');
}

function fadeInOut(selector) {
    var $active = $('#' + selector + ' img.active');
    if ( $active.length == 0 ) $active = $('#' + selector + ' img:last');
    var $next =  $active.next().length ? $active.next() : $('#' + selector + ' img:first');
    $active.addClass('last-active');
    $next.css({
        opacity: 0.0
    })
    .addClass('active')
    .animate({
        opacity: 1.0
    }, 1000, function() {
        $active.removeClass('active last-active');
    });
}

function pagination(form, container, page) {
    if (page > 0) {
        $(container).val(page);
    }
    $(form).data('validator').destroy();
    $(form).submit();
}
