/**
 * @preserve Galleria Classic Theme 2011-08-01
 * http://galleria.aino.se
 *
 * Copyright (c) 2011, Aino
 * Licensed under the MIT license.
 */

/*global jQuery, Galleria 

*/
$(document).ready(function(){
	initGalleria();
});

 
function initGalleria(){
	function isInt(input){
		return ((input)!== undefined && parseInt(input)==input);
	}
	
	$(".shop88_galleria").each(function(){
		var _obj = $(this), obj;
		
		if(_obj.attr('init') === 'true'){
			return;
		}
		
		if (this.nodeName == "IMG") {
			obj = $('<div/>');
			
			for (var i = 0; i < this.attributes.length; i++) {
				if(this.attributes[i].value && this.attributes[i].value !== 'null')
				{
					obj.attr(this.attributes[i].name, this.attributes[i].value);					
				}
			}
			
			_obj.replaceWith(obj);
		} else {
			obj = _obj;
			obj.html("");
		}
				
		if(obj.attr('datasrc') === undefined) {
			return;
		}
		
		var dataSrc = [];
		
		try{
			var tempData = eval(obj.attr('datasrc'));
			
			for(var i=0; i<tempData.length; i++){
				if(tempData[i] !== undefined){
					dataSrc.push(tempData[i]);
				}
			}			
		}
		catch(e) {
			var urlVal = $.deparam.querystring();
			if(urlVal['hideAlert'] == undefined || urlVal['hideAlert'] !== '1'){
				alert('"datasrc" contained unaccepted value. Slider cannot be displayed.');
			}
		}
		
		if(currentShopMode === 'E')
		{
			if(obj.attr('popuplinks')!=='true')
			{
				$.each(dataSrc, function(key, value) { 
					if(value.link){
						value.link = "javascript:void(0)";
					}
				});
			}
		}
		
		// version 1: no information will be shown 
		obj.attr('showinfo','false');
		if(obj.attr('showinfo')!=='true')
		{
			$.each(dataSrc, function(key, value) { 
				
				value.title = "";
				value.discription = "";
				});
		}
		
		var preLoad = isInt(obj.attr('preload'))? parseInt(obj.attr('preload')) : "all";

		obj.galleria({
		    autoplay: (isInt(obj.attr('autospeed')))?parseInt(obj.attr('autospeed')):3000,
		    transition: (obj.attr('transition') !== undefined)?obj.attr('transition'):'fade',
		    width: (isInt(obj.attr('width')))?parseInt(obj.attr('width')):600,
		    height: (isInt(obj.attr('height')))?parseInt(obj.attr('height')):400,
		    dataSource: dataSrc,
		    popupLinks: (obj.attr('popuplinks') !== undefined && obj.attr('popuplinks') ==='true'),
		    preload: (obj.attr('preload') !== undefined)?preLoad:'all',
		    lightbox: (obj.attr('lightbox') === undefined || obj.attr('lightbox') ==='true')
		});
		
		obj.attr('init','true');
		
		
		if((obj.attr('showinfo') === undefined || obj.attr('showinfo') !== 'true'))
		{			
			$('.galleria-info', obj).remove();
		}
		
		if((obj.attr('showthumb') === undefined || obj.attr('showthumb') !== 'true'))
		{
			$('.galleria-stage', obj).css('bottom', '10px');
			$('.galleria-thumbnails-list', obj).css('display', 'none');
		}
		
		if((obj.attr('showbg') !== undefined && obj.attr('showbg') !== 'true'))
		{
			$('.galleria-container', obj).css('background', '#FFF');		
			
			$('.galleria-stage', obj)
				.css('left', '0px')
				.css('right', '0px')
				.css('top', '0px')
				.css('bottom', '0px');
				
			if($.browser.msie && $.browser.version < 7){
				$('.galleria-stage', obj).css('height', '100%');
			}
		}

		if((obj.attr('showcontrol') === undefined || obj.attr('showcontrol') !== 'true'))
		{
			$('.galleria-image-nav', obj).remove();
		}
		
		if((obj.attr('showcounter') === undefined || obj.attr('showcounter') !== 'true'))
		{
			$('.galleria-counter', obj).remove();
		}
	});
}



Galleria.requires(1.25, 'This version of Classic theme requires Galleria 1.2.5 or later');

(function($) {

Galleria.addTheme({
    name: 'classic',
    author: 'Galleria',
    css: 'css/jquery.shop88Slider.css',
    defaults: {
        transition: 'slide',
        thumbCrop:  'height',

        // set this to false if you want to show the caption all the time:
        _toggleInfo: true
    },
    init: function(options) {

        // add some elements
        this.addElement('info-link','info-close');
        this.append({
            'info' : ['info-link','info-close']
        });

        // cache some stuff
        var info = this.$('info-link,info-close,info-text'),
            touch = Galleria.TOUCH,
            click = touch ? 'touchstart' : 'click';

        // show loader & counter with opacity
        this.$('loader,counter').show().css('opacity', 0.4);

        // some stuff for non-touch browsers
        if (! touch ) {
            this.addIdleState( this.get('image-nav-left'), { left:-50 });
            this.addIdleState( this.get('image-nav-right'), { right:-50 });
            this.addIdleState( this.get('counter'), { opacity:0 });
        }

        // toggle info
        if ( options._toggleInfo === true ) {
            info.bind( click, function() {
                info.toggle();
            });
        } else {
            info.show();
            this.$('info-link, info-close').hide();
        }

        // bind some stuff
        this.bind('thumbnail', function(e) {

            if (! touch ) {
                // fade thumbnails
                $(e.thumbTarget).css('opacity', 0.6).parent().hover(function() {
                    $(this).not('.active').children().stop().fadeTo(100, 1);
                }, function() {
                    $(this).not('.active').children().stop().fadeTo(400, 0.6);
                });

                if ( e.index === this.getIndex() ) {
                    $(e.thumbTarget).css('opacity',1);
                }
            } else {
                $(e.thumbTarget).css('opacity', this.getIndex() ? 1 : 0.6);
            }
        });

        this.bind('loadstart', function(e) {
            if (!e.cached) {
                this.$('loader').show().fadeTo(200, 0.4);
            }

            this.$('info').toggle( this.hasInfo() );

            $(e.thumbTarget).css('opacity',1).parent().siblings().children().css('opacity', 0.6);
        });

        this.bind('loadfinish', function(e) {
            this.$('loader').fadeOut(200);
        });
    }
});

}(jQuery));

