/*
 * Primary Scripts
 * I &heart; HTML5 Boilerplate
 * Author       : Ciobanu N. Cristian <ciobanu.n.cristian@gmail.com>
 * Created on   : Feb 4, 2011, 8:36:00 AM
 */

$(document).ready(function() {
    // Default AJAX call settings
    $.ajaxSetup({
        timeout: 10000,
        error:function(x,e){
            if('parsererror'==e) {
                notification('Sorry, we ran into a technical problem (parse error). Please try again...', 'error');
            } else if('timeout'==e) {
                notification('Request timed out. Please try again...', 'error');
            } else if ( "status" in x ) {
                if(0 == x.status) {
                    notification('You are offline! Please check your network.', 'error');
                } else if (404 == x.status) {
                    notification('Sorry, we ran into a technical problem (404). Please try again...', 'error');
                } else if (500 == x.status) {
                    notification('Sorry, we ran into a technical problem (500). Please try again...', 'error');
                }
            } else {
                notification('Sorry, we ran into a technical problem (unknown error). Please try again...', 'error');
            }
        }
    });
    // Notification
    $('.notification').live('click', function() {
        $(this).slideUp(1000);
    });
    // Go Back :)
    $('.cancel').live('click', function() {
        history.go(-1);
    });
    // Validate forms
    $("form.validate").validator();
    // Date (Range) input
    if ($('.date').length > 0) {
        $('.date').dateinput({format: 'yyyy-mm-dd', trigger: true});
    }
    // Slideshow
    if ($('#home-slideshow').length > 0) {
        $('#home-slideshow').flashembed({src: 'assets/swf/slide.swf', wmode: 'transparent'});
    }

    $('a.open-popup').overlay({
        target: '#open-popup',
        fixed: 'true',
        mask: {
            color: '#000',
            opacity: 0.5,
            loadSpeed: 200
        },
        onBeforeLoad: function(){
            this.getOverlay().find('.wrap').html('<img src="'+this.getTrigger().attr("href")+'" alt="" class="center">');
        }
    });
});

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

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

