/**
 * Gallery
 *
 * @package WordPress
 * @subpackage Supreme
 */

(function($) {

    $.hash = {

        get: function() {
            return window.location.hash.substring(1);
        },

        set: function(hash) {
            window.location.hash = hash;
        }

    };

    $.fn.notClickable = function() {

        var _init = function() {
            return false;
        };

        return this.unbind('click').click(_init);

    };

    $.fn.gallery = function(settings) {

        var settings = $.extend({
            clickable: '',
	    time: 500,
            open: null,
            templateDir: ''
	}, settings),

        gallery = {},

        _init = function(obj) {

            gallery = {
                container: $(obj),
                input: $(obj).children('input[name="galleryPointer"]'),
                elements: $(obj).find(settings.clickable),
                view: $(obj).children('.galleryView'),
                close: $(obj).children('.galleryView').find('.close'),
                buttons: $(obj).children('.galleryView').children('.btn')
            };

            $(gallery.elements).unbind('click').click(_init);

            $(gallery.close).click(function() {
                _close(false);
            });

            $(gallery.buttons).click(function() {

                if ($(this).hasClass('btnPrevious')) {
                    _open('previous', true);
                } else if ($(this).hasClass('btnNext')) {
                    _open('next', true);
                }

            });

            $(gallery.elements).each(function() {

                $(this).notClickable().click(function() {

                    var openElement = Number($(this).attr('class').replace('element_', ''));

                    _open(openElement, false);

                    return false;

                });
                
            });

            (settings.open) ? _open(settings.open, true) : null;
            
        },

        _open = function(openElement, strict) {

            var pointer = Number($(gallery.input).val());

            var elements = $(gallery.elements).length;

            var elementId;

            switch (openElement) {

                case 'next':
                    elementId = (pointer == elements) ? 1 : pointer+1;
                break;

                case 'previous':
                    elementId = (pointer == 1) ? elements : pointer-1;
                break;

                default:
                    elementId = openElement;
                break;

            }

            var src = $(gallery.container).find('.element_'+elementId).attr('href');

            if (elementId != pointer && src != undefined) {

                _close(true);

                var slide = Math.ceil(elementId/20);

                if (_isImage(src) != -1) {

                    $('#imageSlider').slider({
                        time: [0, 1000],
                        slideTo: slide
                    });

                    $.preload([src], {
                        init: function() {

                            var hash = '?image='+elementId;

                            $.hash.set(hash);
                        
                            var href = location.href.split('?');

                            var uri = encodeURIComponent(href[0].replace('#', '')+hash);
                            
                            _fb.share(uri);
                            _fb.like(uri);

                            if (strict) {
                                $(gallery.view).show();
                            } else {
                                $(gallery.view).fadeIn(settings.time);
                            }
                            
                        },
                        loaded: function(img, loaded, total) {

                            $(gallery.view).children('.ct').append(img);

                            $(gallery.view).children('.ct').children('img').css({
                                'marginTop': (-1)*(img.height/2)+'px'
                            }).hide().fadeIn(500);

                            $(gallery.input).val(elementId);

                        }
                    });

                } else {

                    $('#videoSlider').slider({
                        time: [0, 1000],
                        slideTo: slide
                    });

                    var hash = '?video='+elementId;

                    $.hash.set(hash);

                    var href = location.href.split('?');
                    
                    var uri = encodeURIComponent(href[0].replace('#', '')+hash);
                    
                    _fb.share(uri);
                    _fb.like(uri);

                    if (strict) {
                        $(gallery.view).show();
                    } else {
                        $(gallery.view).fadeIn(settings.time);
                    }

                    $(gallery.view).children('.ct').append('<div id="player" />');

                    if ($(gallery.container).find('.element_'+elementId).parent('li').hasClass('youtube')) {

                        var video = $(gallery.container).find('.element_'+elementId).attr('id').replace('v_', '');

                        $('#player').append('<object width="500" height="471"><param name="movie" value="http://www.youtube.com/v/'+video+'?fs=1&amp;hl=de_DE"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/'+video+'?fs=1&amp;hl=de_DE" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="500" height="471"></embed></object>');

                    } else {

                        var flashvars = {
                            'file': src
                        };

                        var params = {
                            'wmode': 'transparent'
                        };

                        swfobject.embedSWF(settings.templateDir+'/player/player.swf', 'player', '100%', '100%', '9.0.0', settings.templateDir+'/player/expressInstall.swf', flashvars, params);

                    }

                    $(gallery.view).children('.ct #player').hide().fadeIn(500);

                    $(gallery.input).val(elementId);

                }

            }

        },

        _isImage = function(src) {

            var extensions = ['jpg', 'jpeg', 'png', 'gif'];

            var path = src.split('.');

            return ($.inArray(path[path.length-1].toLowerCase(), extensions));

        },

        _close = function(strict) {

            $.hash.set('');

            $(gallery.input).val(0);
            
            _fb.share('');
            _fb.like('');

            if (strict) {

                $(gallery.view).hide();
                $(gallery.view).children('.ct').empty();

            } else {

                $(gallery.view).fadeOut(settings.time, function() {
                    $(gallery.view).children('.ct').empty();
                });

            }

        },
        
        _fb = {
        	
            share: function(uri) {
                $('.fbShareBtn').attr('href', 'http://www.facebook.com/sharer.php?u='+uri);
            },

            like: function(uri) {
                $('.fbLikeBtn').attr('src', 'http://www.facebook.com/plugins/like.php?href='+uri+'&layout=button_count&show_faces=false&action=like&locale=en_US');
            }
        
        };

        _init(this);

    };

})(jQuery);
