/*
 * file: cubbler.js
 * @author: Zombie! (roguevoo@gmail.com)
 */
var imagePreloader = function()
{
    var img = jQuery('#imagePreloader');
    var width = 0;
    var height = 0;
    var res = "";
    if( !img.length ){
	img = jQuery('<img id="imagePreloader" />').appendTo(document.body).hide();
    }
	
    return jQuery(img).extend({
	preload: function( src, callback ){
	    img.one('load', function(){
		width = jQuery(this).width();
		height = jQuery(this).height();
		if( typeof(callback) === 'function' ) callback.call( jQuery(this) );
	    });
	    img.attr('src',src);
	},
	width: function(){
	    return width;
	},
	height: function(){
	    return height;
	},
	src: function(){
	    return res;
	},
	element: img
    });
}

var poselem = 0;
var zZoomerShow = false;
function showZoomer(options)
{
    var element = jQuery(this);
    var iPreloader = imagePreloader();
    var nt = this.top();
    var nl = this.left();
    var nw = this.width();
    var nh = this.height();
	
    var opt = {
	width:200,
	height:200,
	bgcolor:'#555555',
	border:'1px solid #cccccc',
	loader:'img/ajax-loader.gif',
	hideEvent: 'click',
	speed:200,
	desc:'',
	order:false
    };
	
    if(options && typeof(options) == 'object') opt = jQuery.extend(opt,options);
	
    var hZoomer	= jQuery('#zZoomer');
    var hImage	= jQuery('#zZoomerImage');
    var hDesc	= jQuery('#zZoomerDesc');
    var hOrder	= jQuery('#zZoomerOrder');
	
    function fadeOut(){
	hZoomer.animate({
	    top: element.top() + 'px',
	    left: element.left() + 'px'
	}, {
	    complete: function(){
		hZoomer.fadeOut(opt.speed);
	    },
	    duration: opt.speed,
	    queue:false
	});
		
	hImage.animate({
	    width: element.width() + 'px',
	    height: element.height() + 'px'
	},{
	    duration: opt.speed,
	    queue: false
	});
	hDesc.hide();
    }
	
    function fadeIn(src)
    {
	var elemcenter = {
	    x: element.left() + (element.width() / 2),
	    y: element.top() + (element.height() / 2)
	};
	var nt = elemcenter.y	- (iPreloader.height()	/ 2);
	var nl = elemcenter.x	- (iPreloader.width()	/ 2);
	var nw = iPreloader.width();
	var nh = iPreloader.height();
		
	hZoomer.animate({
	    top: nt + 'px',
	    left: nl + 'px'
	},{
	    duration: opt.speed,
	    queue: false
	});
	hImage.css({
	    'background-image':'url(' + src +')'
	    }).animate({
	    width: nw + 'px',
	    height: nh + 'px'
	},{
	    complete: function(){
		if( opt.desc.length ) hDesc.html(opt.desc).css({
		    width:(nw-10)+'px'
		    }).slideDown(opt.speed * 2);
	    },
	    duration: opt.speed,
	    queue: false
	});
    }
	
    if( !hZoomer.length )
    {
	hZoomer = jQuery(
	    '<div id="zZoomer">'+
	    '<div id="zZoomerImage"></div>'+
	    '<div id="zZoomerDesc"></div>'+
	    '<div id="zZoomerOrder">'+
	    'Узнать по телефону <span class="contact_phone"></span>'+
	    '</div>'+
	    '</div>'
	    );


	hZoomer.css({
	    position:'absolute',
	    'z-index':'999',
	    border:opt.border,
	    backgroundColor:'#555555'
	});

	hZoomer.appendTo( document.body );
		
	hImage	= jQuery('#zZoomerImage');
	hImage.css({
	    width: opt.width + 'px',
	    height: opt.height + 'px',
	    'background-position':'50% 50%',
	    'background-repeat':'no-repeat',
	    'background-color':'#555555'
	});
		
	hDesc = jQuery('#zZoomerDesc');
		
	hDesc.css({
	    height: 'auto',
	    paddingTop: '5px',
	    paddingBottom: '5px',
	    paddingLeft: '5px',
	    paddingRight: '5px',
	    'background-color':'#333333'
	});

	hOrder	= jQuery('#zZoomerOrder').hide();
		
	hZoomer.hide();
    }
	
    hImage.css({
	'background-image':'url(' + opt.loader + ')',
	width:	nw	+ 'px',
	height:	nh	+ 'px'
    });
	
    hDesc.css({
	width:	nw	+ 'px'
    }).slideUp(1);

    hZoomer.css({
	top:	nt	+ 'px',
	left:	nl	+ 'px'
    }).show();

    hOrder.css({
	display: (opt.order) ? 'block' : 'none'
    }).click(OrderClick);
	
    hZoomer.one( opt.hideEvent, fadeOut );

    var zimage = this.attr('res');
    if( zimage ) iPreloader.preload(zimage,fadeIn);
}

function setDescription( desc ){
    var hDesc = jQuery('#zZoomerDesc');
    if( !hDesc.length ) {
	hDesc.html( '' ).slideUp(100)
	return;
    }
	
    if( !isString( desc ) ) return;
    if( zZoomerShow ) hDesc.html( desc ).slideDown(100);
}


var zZoomerWindows = function(options){

    if( !options ) options = {};
	
    /**
     *	Default options
     */
    var opt = {
	width:			options.width	|| 200,
	height:			options.height	|| 200,
	bgcolor:		options.bgcolor	|| '#555555',
	border:			options.border	|| '1px solid #cccccc',
	loader:			options.loader	|| 'img/ajax-loader.gif',
	css:			options.css		|| {},
	hideEvent:		options.hideEvent || 'click',
	speed:			options.speed	|| 200,
	desc:			options.desc	|| '',
	order:			options.order	|| false,
	onShow:			options.onShow	|| function(){},
	onHide:			options.onHide	|| function(){},
	onLoad:			options.onLoad	|| function(){},
	onError:		options.onError || function(){}
    };

    var plugins = {};

	
    /**
     *	Make main element
     */
    var elem = jQuery('<div id="zZoomer"></div>').css({
	'background-color': opt.bgcolor,
	'border': opt.border,
    'z-index':'11'
    });

    var img = jQuery('<div id="zZoomerImage"></div>').css({
	'background-position':'50% 50%',
	'background-repeat':'no-repeat'
    });

    elem.append(img).css({
	position:'absolute'
    }).hide().appendTo(document.body);


    /**
     * Init image preloader
     */
    var preloader = imagePreloader();

    /**
     * Show window function
     */
    function show_func(src){

	var elemcenter = {
	    x: this.left() + (this.width() / 2),
	    y: this.top() + (this.height() / 2)
	};

	var nt = parseInt( elemcenter.y	- (preloader.height()	/ 2), 10);
	var nl = parseInt( elemcenter.x	- (preloader.width()	/ 2), 10);

	var nw = preloader.width();
	var nh = preloader.height();


	img.css({
	    'background-image':'url(' + src +')'
	}).click(function(){

	    }).animate({
	    width:	nw		+ 'px',
	    height:	nh		+ 'px'
	},{
	    duration: opt.speed,
	    queue: false
	});

	elem.animate({
	    top:	nt		+ 'px',
	    left:	nl		+ 'px',
	    width:	nw		+ 'px'
	/*height:	nh		+ 'px'*/
	},{
	    duration: opt.speed,
	    queue: false
	});

	jQuery.each(plugins, function(i,val){
	    if( this && typeof this === 'object' && this.onShow && typeof this.onShow === 'function' ){
		this.onShow.call(this);
	    }
	});
    }

    /**
     * Hide window function
     */
    function hide_func(){
	jQuery.each(plugins, function(i,val){
	    if( this && typeof this === 'object' && this.onHide && typeof this.onHide === 'function' ){
		this.onHide.call(this);
	    }
	});
	elem.hide();
    }

    return {
	show: function(url, parent){

	    var me = this;

	    var x = jQuery(parent).left()	|| 0;
	    var y = jQuery(parent).top()	|| 0;
	    var h = jQuery(parent).height()	|| 0;
	    var w = jQuery(parent).width()	|| 0;

	    elem.show().animate({
		top		: y + 'px',
		left	: x + 'px',
		width	: w + 'px'
	    },{
		duration:	opt.speed,
		queue	:	false
	    });

	    img.css({
		backgroundImage:'url(' + opt.loader +')'
	    }).animate({
		width	: w + 'px',
		height	: h + 'px'
	    },{
		duration:	opt.speed,
		queue:	false,
		complete: function(){
		    preloader.preload(url,function(){

			jQuery.each(plugins, function(i,val){
			    if( this && typeof this === 'object' && this.onLoad && typeof this.onLoad === 'function' ){
				this.onLoad.call(this);
			    }
			});

			if( opt.onLoad && typeof opt.onLoad === 'function' ){
			    opt.onLoad.call(me, url);
			}

			show_func.call(parent||me, url);
		    });
		}
	    });

	    jQuery.each(plugins, function(i,val){
		if( this && typeof this === 'object' && this.onChange && typeof this.onChange === 'function' ){
		    this.onChange.call(this);
		}
	    });
	},
	hide: function(parent, callback, thisp, args){
	    hide_func();
	},
	element: function(){
	    return elem;
	},
	plugin: function(o){
	    if( !o ) return;

	    switch( typeof o ){
		case 'string':{
		    return plugins[o];
		}
		break;

		case 'object':{
		    if( o.name && typeof o.name === 'string' ){
			plugins[o.name] = o;
			if( plugins[o.name].init && typeof plugins[o.name].init === 'function' ) plugins[o.name].init.call(this,parent);
			return true;
		    }
		}
		break;
	    }
	}
    }
}
