url = pathname.split('/');

router = new Object;
router.module = url[0];
router.action = url[1];
router.id = url[2];
router.language = $('meta[name=language]').attr('content');

inputs = new Object;


if(router.module=='')
router.module = 'home';

inputs['first_name'] = 'Imię';
inputs['last_name'] = 'Nazwisko';
inputs['company_name'] = 'Nazwa firmy';
inputs['phone'] = 'Telefon';
inputs['handy'] = 'Komórka';
inputs['email'] = 'E-mail';
inputs['body'] = 'Wiadomość';

function overlay()
{
	$height = $('body').height();
	$('html, body').css({'overflow': 'hidden', 'height': $(window).height()});
	jQuery('body').prepend('<div id="body_overlay"></div>');
	jQuery('#body_overlay').css('opacity', '0.7');
	jQuery('#body_overlay, #overlay_close').live('click', function() {
		jQuery('#body_overlay, #overlay_container, #photo_list, #overlay_close').remove();
		$('html, body').css({'overflow-y': 'auto', 'height': $height});
		return false;
	})
	jQuery('body').prepend('<div id="overlay_container"><img src="/media/core/loading.gif" alt="Loading" id="overlay_loader" /></div>');
	jQuery('#overlay_container').center('fixed');
	jQuery('#overlay_loader').center();
			

	return jQuery('#overlay_container');
}

jQuery.fn.center = function () {
	
	return this.each(function (position) {
		var $this = jQuery(this);
		var marginLeft = $this.width()/-2;
		var marginTop = $this.height()/-2;

		pos = position ? position : 'absolute';
		
		$this.css({'position': pos, 'left': '50%', 'top': '50%', 'marginLeft': marginLeft, 'marginTop': marginTop});
	});
	
};

	function onPhoto()
	{
		jQuery('#overlay_active_photo').fadeIn();
		jQuery('#overlay_active_photo p').fadeIn();
		jQuery('#overlay_loader').hide();
		$photo_container.enabled = true;
		

		$('body').prepend('<a href="#" class="btn btn_close" id="overlay_close"></a>');

		
	
		$('.btn_close').css('marginTop', (jQuery('#overlay_active_photo').height()/-2)+10);
		$('.btn_close').animate({'marginLeft': (jQuery('#overlay_active_photo').width()/2)});
				
	}

	jQuery.fn.zoomImg = function() {
		jQuery(this).each(function() {

			$(this).addClass('photo_container_list');
			
			multi_height = 0;
			if(jQuery('img', this).length > 1)
			multi_height = 50;

			$('img', this).click(function() {
				$(document).scrollTop(0);
				$photo_container = $(this).parents('.photo_container_list');
		
				$clicked_img = $(this);
				overlay();

				if(multi_height > 0)
				{
					jQuery('body').prepend('<div id="photo_list"></div>');
					
					jQuery('#photo_list').css('opacity', '0.85');
					jQuery('img', $photo_container).each(function() {
						var $thumb = jQuery('#photo_list').append('<img src="'+$(this).attr('src')+'" />');
						if($clicked_img.attr('src')==$(this).attr('src'))
						jQuery('#photo_list img:last').addClass('active');
					});

					
					//if((jQuery('#photo_list img:first').width()+10) * jQuery('#photo_list img').length > $(window).width())
					if(jQuery('#photo_list img').length > 6)
					{
						//var counter = Math.floor($(window).width()/(jQuery('#photo_list img:first').width()+25));
						var counter = 6;
						jQuery('#photo_list').paginate({size: counter, children: 'img', use_pagination: false})
					}
				}

						
				jQuery('#photo_list img').click(function() {
					
					if($photo_container.enabled == false)
					return false;

					$photo_container.enabled = false;

					jQuery('#photo_list img').removeClass('active');
					jQuery(this).addClass('active');

					$clicked_img = $(this);
							
					$('#overlay_active_photo').fadeOut();
					$('#overlay_loader').fadeIn();

					jQuery('#overlay_loader').fadeOut('normal', function() {loadImage();});

				});

				jQuery('#overlay_loader').fadeOut('normal', function() {loadImage();});
				
			});
			$(this).elements = jQuery('img', this);
		});
	}

function loadImage()
	{
		$('.btn_close').css({'marginLeft': 0, 'marginTop': 0});
		$('#overlay_active_photo').remove();
		link = $clicked_img.attr('src').replace(/_sm/i, '');

		
		var img = new Image(); 
		$(img).load(function() {

			var $img = $(this);
			
			jQuery('#overlay_container').append('<img src="'+link+'" id="overlay_active_photo" />');

			
			$new_width = $('#overlay_active_photo').width();
			$new_height = $('#overlay_active_photo').height();

			$new_width = this.width;
			$new_height = this.height;

			if(jQuery(window).height() - 110 < $new_height)
			{
				calculated_height = jQuery(window).height() - 200;
				calculated_width = Math.round(calculated_height * ($new_width/$new_height));
				$new_height = calculated_height;
				$new_width = calculated_width;
			}
			if(jQuery(window).width() < $new_width)
			{
				calculated_width = jQuery(window).width() - 100;
				calculated_height = Math.round(calculated_width * ($new_height/$new_width));
				$new_height = calculated_height;
				$new_width = calculated_width;
			}


			$new_margin_top = Math.round(($new_height/-2)-multi_height);
			$new_margin_left = Math.round($new_width/-2);
				
			jQuery('#overlay_container').animate({'width': $new_width, 'height': $new_height, 'marginLeft': $new_margin_left, 'marginTop': $new_margin_top}, onPhoto);

			
		}).attr('src', link);
	}




$(document).ready(function() {
	jQuery('#gallery').zoomImg();
	
	jQuery('.gallery_offers').zoomImg();
	jQuery('.gallery_offers').paginate({size: 3, children: '.gal_element', pagination_before: true, use_pagination: false});


	jQuery('.gallery_big').zoomImg();
	jQuery('.gallery_big').paginate({size: 2, children: '.gal_element', pagination_before: true, use_pagination: false});

	$('.gal_element .border').click(function() {
		$(this).parent().find('img:visible').trigger('click');
	});

	$('#content_inner form.validate').validate({container: '#content_inner'})


	$('#menu .menu_'+router.module).addClass('active');

	if(router.module=='prices')
	$('#right .submenu_'+router.id).addClass('active');

	if(router.module=='gallery')
	$('#right .submenu_'+router.action).addClass('active');

	if(router.module=='home' && $('#cobad').length > 0)
	{
		jQuery('#cobad').zoomImg();
		$('#cobad img').trigger('click');
	}

	$('#cycle').cycle({next: '#cycle_next', prev: '#cycle_prev' });

	$('#home h1 span').click(function() {
		$('#home h1 span').removeClass('active');
		$(this).addClass('active');
		
		index = $(this).index();
		$('.news_home').hide();
		$('.news_home').eq(index).show();
	});

	$('.btn_home_right').slice($('#home_right .text').length, 4).remove();

	$('#home_right .text').slice(1,4).hide();
	$('.news_home').slice(1,4).hide();

	/*Home prawa strona*/
	$('.btn_home_right').click(function() {
		$('.btn_home_right').removeClass('active');
		$(this).addClass('active');
		index = $(this).index();

		$('#home_right .text').hide();
		$('#home_right .text').eq(index).show();
	});
	homeRightIndex=1;
	
	HomeRight = function()
	{
		if(homeRightIndex>=$('.btn_home_right').length)
		homeRightIndex=0;
		$('.btn_home_right').eq(homeRightIndex).trigger('click');
		homeRightIndex++;
	}

	setInterval('HomeRight()', 20000);
	/* //Home */
	$('body').mouseover(function() {
		$('#menu li ul').hide();
		return false;
	});

	$('#menu > ul > li').mouseover(function() {
		$('#menu li ul').hide();
		$('ul', this).show();
		return false;
	});



	$('.map').click(function() {
		$container = overlay();
		$container.animate({'marginLeft': -400, 'marginTop': -250, 'width': 800, 'height': 500}, function() {$(this).append('<div id="map"></div>'); $('#map').DeseoMap(50.214442748005176, 18.996927738189697, 14);});
		
		return false;
	});
	 

	$('.btn_info').mouseover(function() {
		$('.price_info', this).show();
	});

	$('.btn_info').mouseout(function() {
		$('.price_info', this).hide();
	});


	if($('#buy_now .counter').length>0)
	{
		var date = {}
		org_date = $('#buy_now .counter .date').text();

		function buy_now_time()
		{
		
			date.second = org_date;
			date.hour = Math.floor(date.second/3600);
			date.second -= date.hour*3600;

			date.minute = Math.floor(date.second/60);
			date.second -= date.minute*60;
			
			$('#buy_now .counter .hour').text(date.hour);
			$('#buy_now .counter .minute').text(date.minute);
			$('#buy_now .counter .second').text(date.second);

			org_date--;

			if(org_date <= 0)
			{
				document.location.reload(true)
				clearInterval(counter_interval);
			}
		}

		counter_interval = setInterval(buy_now_time, 1000);

	}

});
