/**
 * jQuery Magnifier Plugin
 */
(function($){
	$.extend($.fn,
	{
		magnify:function(options)
		{
			return this.each(function()
			{
				var magnifier = 
				{
					
					init:function(options)
					{
						var self = magnifier;
            self.options = $.extend({lensWidth: 200, lensHeight: 200}, options);
						self.a = this;
						self.$a = $(this);
						self.$a.parent().css('position', 'relative');
						
						
						self.$img = $('img', this);
						self.bounds = {};
						
						
            self.$img.load(function(e){self.bounds.cx = self.$img.width();self.bounds.cy = self.$img.height();});
            self.bounds.cx = self.$img.width();
						self.bounds.cy = self.$img.height();
						
						self.setBigImage();
						self.setLens();
						self.setLoader();
						self.loadImage();
						self.$a.
							mousemove(function(e){ self.handleMouseMove(e);}).
							mouseout(function(e){ self.mouseOut(e);}).
              click(function(e) { e.preventDefault();})
					},
					
					setBigImage:function()
					{
						this.$largeImage = $(new Image());
						this.$largeImage.attr('src', this.a.href).css('display', 'none');
					},
					
					setLens:function()
					{
						this.$lens = $("<div id='o-lens'></div>");
						this.$lens.css({
							width: this.options.lensWidth,
	            height: this.options.lensHeight,
							visibility: 'hidden',
							overflow: 'hidden',
							position: 'absolute',
							left:0,
							top:0
						}).appendTo(this.a);
					},
					
					setLoader:function()
					{
						this.$loader = $("<div id='o-loader'>loading</div>").css({width: this.options.lensWidth, height: this.options.lensHeight});
						this.$lens.append(this.$loader);
					},
					
					loadImage:function()
					{
            this.$largeImage.load(function(e){ magnifier.imgLoadCheck(magnifier.$largeImage[0], magnifier.loadCallback) });
					},
					
					imgLoadCheck:function(img, loadCallback)
					{
						if(img!=null) {
							function imgWatch()
							{
								if(img.complete)
								{
									clearInterval(loadWatch);
									loadCallback();
								}
							}			
							var loadWatch = setInterval(imgWatch, 100);	
						}
					},
					
					loadCallback:function()
					{
						magnifier.$lens.append(magnifier.$largeImage);
						
						function moveWatch()
						{
							if(magnifier.$largeImage.width())
							{
								magnifier.largeWidth = magnifier.$largeImage.width();
								magnifier.largeHeight = magnifier.$largeImage.height();								
							}
							if (magnifier.largeWidth) {
								magnifier.$loader.remove();
								clearInterval(moveID);
							}
						}
						var moveID = setInterval(moveWatch, 100);
					},
					
					handleMouseMove:function(e)
					{
						this.bounds.x = this.$img.offset().left;
						this.bounds.y = this.$img.offset().top;
            
						this.$lens.css({
							left: parseInt(e.pageX - this.bounds.x - (this.options.lensWidth * .5)) + "px",
							top: parseInt(e.pageY - this.bounds.y -(this.options.lensHeight * .5)) + "px"
						});
						
						this.$lens.css('visibility', 'visible');
						if(this.largeWidth){ this.positionLargeImage(e);}
						
						this.$lens.css('display', 'block');
						if(e.pageX < this.bounds.x || e.pageX > this.bounds.x + this.bounds.cx || 
								e.pageY < this.bounds.y || e.pageY > this.bounds.y + this.bounds.cy)
									this.mouseOut(); 
					
					},
					
					positionLargeImage:function(e)
					{				
						var scale = {};
						scale.x = this.largeWidth / this.$img.width();
						scale.y = this.largeHeight / this.$img.height();
					
						var left = -scale.x * Math.abs((e.pageX - this.$img.offset().left)) + this.options.lensWidth / 2 + "px";
						var top = -scale.y * Math.abs((e.pageY - this.$img.offset().top)) + this.options.lensHeight / 2 + "px";
										
						this.$largeImage.css(
						{
							position: 'absolute',
							left: left,
							top: top,
							display:'block'
						});
					},
					
					mouseOut: function(e)
					{
						this.$lens.css({
							visibility: 'hidden',
							display: 'none'
						});
					}				
				};
				
				magnifier.init.call(this,options);			
			});
		}
	});
})(jQuery);

$(document).ready(function()
{
	
	$('a[rel*=magnify]').magnify();
  // $('#promotion img').attr('src','../img/site/praca2.jpg'); //'promotion' + math.floor(math.random()-3+1) + '.jpg');
	
	/*
	 * support for :first-child
	 */
	$( '#content-main-article .content-main-entry p:first-child, #content-main-introduction .content-main-entry p:first-child' ).addClass("first-child");
	
	/*
	 * support for :hover
	 */
	$( '#menu > li' ).hover(function()
	{
		$( this ).addClass( 'hover' );
	}, function ()
	{
		$( this ).removeClass( 'hover' );
	});
	
	/*
	 * support for swfobject
	 */
	//if( $ ( '#promotion' ).length > 0 )
	//{
	//	var so = new SWFObject( "/img/site/banner.swf", "banner", "875", "150", "6", "#ffffff" );
	//	so.addParam( "wmode", "opaque" );
	//	so.write( "promotion" );
	//};
	
	/*
	 * support for external window
	 *
	 * TO-DO: ewrite to support rel attribute 
	 */
	$( '#mapa-dromet' ).click(function()
	{								 
		window.open( 'http://www.dromet.pl/dromet/img/site/mapa_dromet.jpg', '','width=640,height=395,resizable=yes,scrollbars=yes' );
		return false;	
	});	
	
	$( '#maps' ).click(function()
	{						  
		window.open( 'http://www.dromet.pl/dromet/img/site/map.gif', '','width=400,height=400,resizable=yes,scrollbars=yes' );
		return false;
	});		
	
	$( '#mapa-satelita' ).click(function()
	{	
		window.open( 'http://www.dromet.pl/dromet/img/site/mapa_satelita.jpg','','width=640,height=395,resizable=yes,scrollbars=yes' );
		return false;
	});
	

	//funkcja odpowiedzialna za otwarcie okna z formularzem zapytania
	function openWindow()
	{
		window.open( 'http://www.dromet.pl/pl/zapytanie', 'Zapytanie', 'width=530, height=500, resizable=yes' );
	}

	//funckja odpowiedzialna za otwarcie okna z kalkulatorem
	function openCalculateWindow()
	{
		window.open( 'http://dromet.pl/pl/sprawdz-wage/przelicz', 'Sprawdź wagę', 'width=370, height=130, resizable=yes' );
	}
	
	// mapa strony
	$('ul.mainmapsite').attr('id', 'mainmapsite');
	$('.mainmapsite ul').each(function(i)
	{
		this.id = $(this).attr('class');
	});
	// attr('id',$(this).attr('class'));

	
	function load() 
	{
		if (GBrowserIsCompatible()) 
		{
			var map = new GMap2(document.getElementById("map"));
			map.setCenter(new GLatLng(52.089283, 20.550699), 10);
			map.openInfoWindowHtml(map.getCenter(),"DROMET S.W.H. Drązikowscy Spółka Jawna<br />ul. 3 Maja 4, 96-313 Jaktorów, Chylice Kolonia");
			map.addControl(new GMapTypeControl());
			map.addControl(new GSmallMapControl());
		}
	}
});
