/**
 * @author Bartek G
 */
 
X.checkBaseOverride = function(funcName,variant) {
    if (variant) { //czy ladowac dodatkowy styl, czy nie? i czy juz zaladowany?
        if ((X.baseProject && (X.additionalCss.indexOf(funcName) > -1 || X.additionalCss === 'all')) //jesli na liscie dodatkowych css jest funcName
            || (X[funcName] && X[funcName].cssPath)) { //jesli jest sciezka do css dla tej funcName
            return true;
        } else {
            return false;
        }
    } else { //czy ladowac defaultowy styl, czy nie? i czy juz zaladowany?
        if (!X.baseOverride || (X.baseOverride && X.baseOverride !== 'all' && X.baseOverride.indexOf(funcName) < 0)) {
            return true;
        } else {
            return false;
        }
    }
};
/*
 * ImageZoom/FancyBox
 */
X.imageZoom = function() {
    X.ImageZoom.createLinks();
    if (X.checkBaseOverride('imageZoom')) { //czy ladowac defaultowy styl, czy nie? i czy juz zaladowany?
        X.addCss(baseUrl + '/static/js/client/lib/jquery/plugins/fancybox/jquery.fancybox-1.3.1.css');
    }
    if (X.checkBaseOverride('imageZoom',true)) { //czy ladowac dodatkowy styl, czy nie? i czy juz zaladowany?
        X.addCss(X.imageZoom.cssPath ? X.imageZoom.cssPath : baseUrl + '/static/js/' + X.baseProject + '/lib/jquery/plugins/fancybox/jquery.fancybox-1.3.1.css');
    }
    require(
        [
            baseUrl + '/static/js/client/lib/jquery/plugins/jquery.easing-1.3.pack.js',
            baseUrl + '/static/js/client/lib/jquery/plugins/fancybox/jquery.fancybox-1.3.1.pack.js'
        ],
        function(){
            X.ImageZoom.fancybox();
        }
    );
};

/*
 * DatePicker
 */
X.datePicker = function(selector,options,doNotLoadUI) {
	var load = [
		baseUrl + '/index/lang',
		baseUrl + '/static/js/client/lib/jquery-ui/jquery-ui-1.8.8.custom.min.js'
	];
	if (doNotLoadUI || (jQuery.ui && jQuery.ui.datepicker)) {
		var load = [baseUrl + '/index/lang'];
	}
    if (X.checkBaseOverride('datePicker')) {
        X.addCss(baseUrl + '/static/js/client/lib/jquery-ui/jquery-ui-1.8.8.custom.css');
    }
    if (X.checkBaseOverride('datePicker',true)) {
        X.addCss(X.datePicker.cssPath ? X.datePicker.cssPath : baseUrl + '/static/js/' + X.baseProject + '/lib/jquery-ui/jquery-ui-1.8.8.custom.css');
    }
    require(
        load,
        function(){
            var l = X.Lang.calendar;
            $(selector).datepicker($.extend({},{
                dayNames: l.days,
                dayNamesMin: l.daysMin,
                dayNamesShort: l.daysShort,
                monthNames: l.months,
                monthNamesShort: l.monthsShort,
                currentText: l.today,
                prevText: l.prev,
                nextText: l.next,
                closeText: l.done,
                weekHeader: l.wkHeader,
                firstDay: 1,
                altField: null,
                altFormat: '@',
                dateFormat: 'dd-mm-yy',
                showOn: 'both',
                buttonImage: baseUrl + '/static/img/' + (X.baseProject ? X.baseProject : 'client') + '/calendar.png',
                buttonImageOnly: false
            },options));
            $(selector).releaseEvent();
        }
    );
};
/*
 * FontSize
 */
X.fontSize = function() {
    if (X.checkBaseOverride('fontSize')) {
        X.addCss(baseUrl + '/static/js/client/lib/mp/fontsize/fontsize.css');
    }
    if (X.checkBaseOverride('fontSize',true)) {
        X.addCss(X.fontSize.cssPath ? X.fontSize.cssPath : baseUrl + '/static/js/' + X.baseProject + '/lib/mp/fontsize/fontsize.css');
    }
    require(
        [
            baseUrl + '/index/lang',
            baseUrl + '/static/js/client/lib/jquery/plugins/jquery.cookies.2.2.0.min.js',
            baseUrl + '/static/js/client/lib/mp/fontsize/fontsize.js'
        ],
        function(){
            new X.FontSize({
                lang: X.Lang.fontsize
            }).initialize();
        }
    );
};
/*
 * Bookmarks
 */
X.bookmarks = function() {
    if (X.checkBaseOverride('bookmarks')) {
        X.addCss(baseUrl + '/static/js/client/lib/mp/bookmarks/bookmarks.css');
    }
    if (X.checkBaseOverride('bookmarks',true)) {
        X.addCss(X.bookmarks.cssPath ? X.bookmarks.cssPath : baseUrl + '/static/js/' + X.baseProject + '/lib/mp/bookmarks/bookmarks.css');
    }
    require(
        [
            baseUrl + '/index/lang',
            baseUrl + '/static/js/client/lib/jquery/plugins/jquery.hoverIntent.min.js',
            baseUrl + '/static/js/client/lib/mp/bookmarks/bookmarks.js'
              ],
        function(){
            new X.Bookmarks({
                amount: 3,
                more: -1,
                visited: 1,
                lang: X.Lang.bookmarks
            }).initialize();
        }
    );
};
/*
 * Recommend/Tell A Friend
 */
X.recommend = function() {
    if (X.checkBaseOverride('recommend')) {
        X.addCss(baseUrl + '/static/js/client/lib/mp/recommend/recommend.css');
    }
    if (X.checkBaseOverride('recommend', true)) {
        X.addCss(X.recommend.cssPath ? X.recommend.cssPath : baseUrl + '/static/js/' + X.baseProject + '/lib/mp/recommend/recommend.css');
    }
    require(
        [
            baseUrl + '/index/lang',
            baseUrl + '/static/js/client/lib/mp/recommend/recommend.js'
        ],
        function(){
            new X.Recommend({
                lang: X.Lang.recommend
            }).initialize();
        }
    );
};
/*
 * Uploadify
 */
X.uploadify = function(selector,options) {
    $('#' + selector).parent().addClass('x-uploadify');
    $('#' + selector).css('display','none');
    if (X.checkBaseOverride('uploadify')) {
        X.addCss(baseUrl + '/static/js/client/lib/jquery/plugins/jquery.uploadify-v2.1.4/uploadify.css');
    }
    if (X.checkBaseOverride('uploadify',true)) {
        X.addCss(X.uploadify.cssPath ? X.uploadify.cssPath : baseUrl + '/static/js/' + X.baseProject + '/lib/jquery/plugins/jquery.uploadify-v2.1.4/uploadify.css');
    }
    var defaultOptions = {
        uploader:  baseUrl + '/static/js/client/lib/jquery/plugins/jquery.uploadify-v2.1.4/uploadify.swf',
        script: baseUrl + '/xhr-upload',
        auto: true,
        wmode: 'transparent',
        multi: true,
        buttonText: 'Choose files',
        hideButton: false,
        rollover:true,
        buttonImg: baseUrl + '/static/img/' + (X.baseProject ? X.baseProject : 'client') + '/upload/pl/button.png',
        cancelImg: baseUrl + '/static/img/' + (X.baseProject ? X.baseProject : 'client') + '/stop.png',
        thumbPath: baseUrl + '/static/images/thumb/',
        deletePath: null,
        expressInstall: baseUrl + '/static/js/client/lib/jquery/plugins/jquery.uploadify-v2.1.4/expressInstall.swf',
        removeCompleted: false,
        sizeLimit: null, //bytes
        fileExt: "*.jpg;*.jpeg;*.png;*.gif",
        fileDesc: "Images (*.jpg:*.jpeg:*.png:*.gif)",
        uploadedList: [],
        onComplete: function(event, ID, fileObj, response, data){
            X.c(response);
        }
    };

    var newOptions = $.extend({},defaultOptions,options);

    var uploadedFiles = function() {
        if (newOptions.uploadedList && newOptions.uploadedList.length) {
            var queue = $('#' + selector).parent().find('.uploadifyQueue');
            $.each(options.uploadedList,function(index,element) {
                $(queue).append('<div class="uploadifyQueueItem completed"><div class="thumb"><img src="'
                                + newOptions.thumbPath + element.name.replace(/\./g,"_")
                                + '"/></div>'
                                + '<div class="cancel"><a href="#"><img src="' + newOptions.cancelImg + '" /></a></div>'
                                + '<div class="fileName">' + element.name + ' (' + element.size + ')</a></div>'
                                + '</div>');
                var el = $(queue).find(".uploadifyQueueItem").last();
                $(el.find('a')).bind('click',function(e) {
                    e.preventDefault();
                    if (newOptions.deletePath) {
                        $.ajax({
                            url: newOptions.deletePath + element.name.replace(/\./g,"_"),
                            success: function(res) {
                                X.c('Deleted: ' + element.name);
                            }
                        });
                    }
                    $(el).fadeOut(300,function(){
                        $(el).remove();
                    });
                });
            });
        }
    };

    require(
        [
            baseUrl + "/static/js/client/lib/swfobject/swfobject.js",
            baseUrl + "/static/js/client/lib/jquery/plugins/jquery.uploadify-v2.1.4/jquery.uploadify.v2.1.4.min.js"
        ],
        function() {
            $('#' + selector).uploadify(newOptions);
            uploadedFiles();
        }
    );
};

$(document).bind('ready', function() {
    //fancybox
    if ($('#x-content a[href$=".jpg"], #x-content a[href$=".JPG"], #x-content a[href$=".jpeg"], #x-content a[href$=".JPEG"], #x-content a[href$=".gif"], #x-content a[href$=".png"], #x-content a[href$=".PNG"], #x-content a[href$=".tiff"], #x-content a[href$=".TIFF"]').length 
        || $('#x-content img.x-zoom-thumb').length) {
        X.imageZoom();
    }
    //fontsize
    if ($('.x-tools-fontsize').length) {
        X.fontSize();
    }
    //bookmarks
    if ($('.x-tools-bookmarks').length) {
        X.bookmarks();
    }
    //print
    if ($('.x-tools-print').length) {
        X.addCss(baseUrl + '/static/js/client/lib/mp/print/print.css');
    }
    //tell-a-friend
    if ($('.x-tools-recommend').length) {
        X.recommend();
    }
    //date-picker
    if ($('.x-date-picker').length) {
        $.each($('.x-date-picker'), function(index, element) {
            X.datePicker($(element));
        });
    }
});
