var offset = 50;
var loadImageActive = 0;

jQuery(document).ready(function($) {
	  $("a[rel='cb_photo']").colorbox({
	  current:'{current} / {total}',
	  previous:'',
	  next:'',
	  close:'',
	  maxWidth:"95%",
	  maxHeight:"95%",
	  scalePhotos:true 
	});
});

var	imgOnLoad = function() {
		if(loadImageActive == 1) {
			return;
		}
		loadImageActive = 1;
		var scrollPosOffset = $(window).height() + $(window).scrollTop() + offset;
		$('.load_image').each(function(){	
			var $a = $(this);
			if($a.offset().top < scrollPosOffset){
				$a.removeClass('load_image');
				$a.addClass('loading');
				var image = new Image();
				image.src = $a.attr('rel');
				image.onload = function(){
					$a.removeClass('loading');
					$a.append(image);
				}
			}
		});	
		loadImageActive = 0;	
	};


	function sortAsc(a,b)
	{
		return a-b;
	}

	function closeMe(id) {
		$('#'+id).hide();
		$('#overlay').animate({'opacity':'0'},500, function(){
			$('#overlay').remove();
		});
	}
	
		function getPageSize() {
			var xScroll, yScroll;
			if (window.innerHeight && window.scrollMaxY) {	
				xScroll = window.innerWidth + window.scrollMaxX;
				yScroll = window.innerHeight + window.scrollMaxY;
			} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
				xScroll = document.body.scrollWidth;
				yScroll = document.body.scrollHeight;
			} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
				xScroll = document.body.offsetWidth;
				yScroll = document.body.offsetHeight;
			}
			var windowWidth, windowHeight;
			if (self.innerHeight) {	// all except Explorer
				if(document.documentElement.clientWidth){
					windowWidth = document.documentElement.clientWidth; 
				} else {
					windowWidth = self.innerWidth;
				}
				windowHeight = self.innerHeight;
			} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
				windowWidth = document.documentElement.clientWidth;
				windowHeight = document.documentElement.clientHeight;
			} else if (document.body) { // other Explorers
				windowWidth = document.body.clientWidth;
				windowHeight = document.body.clientHeight;
			}	
			// for small pages with total height less then height of the viewport
			if(yScroll < windowHeight){
				pageHeight = windowHeight;
			} else { 
				pageHeight = yScroll;
			}
			// for small pages with total width less then width of the viewport
			if(xScroll < windowWidth){	
				pageWidth = xScroll;		
			} else {
				pageWidth = windowWidth;
			}
			arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
			return arrayPageSize;
		};
		function getPageScroll() {
			var xScroll, yScroll;
			if (self.pageYOffset) {
				yScroll = self.pageYOffset;
				xScroll = self.pageXOffset;
			} else if (document.documentElement && document.documentElement.scrollTop) {	 // Explorer 6 Strict
				yScroll = document.documentElement.scrollTop;
				xScroll = document.documentElement.scrollLeft;
			} else if (document.body) {// all other Explorers
				yScroll = document.body.scrollTop;
				xScroll = document.body.scrollLeft;	
			}
			arrayPageScroll = new Array(xScroll,yScroll);
			return arrayPageScroll;
		};
		
		
/**
 * imageBox
 * 
 * class for handling image box scroll
 * 
 * @param box_id, id element which is a container
 * @return object;
 */
blockScroll = new Array();
function imageBox(box_id, options)
{
  this.box_id = $(box_id);
  this.list = this.box_id.find('ul');
  this.list_elements = this.list.find('li');
  this.item_width = this.list_elements.first().outerWidth(true);
  this.image_box_inside = this.box_id.find('.imageBoxInside').first();
  this.big_image = this.box_id.find('.imageBig img').first();
  this.options = options || {time: 0};
  this.prev = this.box_id.find('.controlsLeft');
  this.next = this.box_id.find('.controlsRight');
  var self = this;
  /**
   * scroll left image box
   * 
   * @return boolean;
   */
  this.moveToPrevious = function() {
    self.list.css('left', -self.item_width); 
    self.list_elements.first().before(self.list_elements.last());
    self.list_elements = self.list.find('li'); //refresh list elements 
    self.list.not(':animated').animate({'left' : 0}, self.options.time, function(){ 
        
    }); 
    self.refreshBigPhoto();

    return true;
  };

  this.refreshBigPhoto = function() {
    element = self.list_elements;
    element.removeClass('selected');
    p = self.list_elements.first().addClass('selected');
    i = p.find('img');
    //show big photo
    self.showBigImage(i);
    self.refreshImgNumber(i.attr('number'));
  };

  this.refreshImgNumber = function(nr) {
    if (typeof this.img_number_container == 'undefined') {
      this.img_number_container = this.box_id.find('.imgNumber').first();
    }

    if (this.img_number_container) {
      this.img_number_container.html(nr);
    }
  };

  /**
   * 
   * scroll right image box
   * 
   * @return boolean;
   */
  this.moveToNext = function(event, element) {
    slice = element ? element.index() : 1;
    self.list.not(':animated').animate({'left' : -self.item_width}, self.options.time, function(){ 
        self.list_elements.last().after(self.list_elements.slice(0, slice));
        self.list_elements = self.list.find('li'); //refresh list elements 
        self.list.css({'left' : 0});
         
    }); 
    self.refreshBigPhoto();
  };
  
 this.showBigImage = function(mini) {
   if ((typeof self.big_image == 'object') && (typeof mini == 'object')) {
     
     if (self.big_image.attr('src') == mini.attr('src_midi')) {
       return;
     }
     var spinner = $('<div class="spinner"><img src="/images/imageBox/spinner.gif"></div>').prependTo(self.big_image.parent());

     new_img = $('<img />');
     new_img.load(function() {
       spinner.remove();
       self.big_image.fadeOut(150, function(){
         $(this).attr('src', new_img.attr('src'));
         $(this).fadeIn(150);
       });
     });
     new_img.attr('src', mini.attr('src_midi'));
     self.big_image.attr('number', mini.attr('number'));
   } else {
     return false;
   }
 };
  this.startShow = function() {
     $('.imageBoxInside a[number="'+self.big_image.attr('number')+'"]').click();
  };
  this.onLoad = function() {
    this.refreshBigPhoto();
    this.box_id.find('.btnPreview').click(function() {
      self.startShow();
    })
    this.big_image.click(function() {
      self.startShow();
    });
    this.list_elements.bind('click', function(event) {
      self.moveToNext(event, $(this));
    });
    this.prev.bind('click', this.moveToPrevious); 
    this.next.bind('click', this.moveToNext);
  };
}

function myFb() {
  var s = document.createElement('script');
  var root = document.getElementById('fb-root');
 
  window.fbAsyncInit = function() {
    FB.init({status: true, cookie: true, xfbml: true});
    root.removeChild(s);
  };
 
  s.async = true;
  s.src = document.location.protocol +
    '//connect.facebook.net/pl_PL/all.js';
  root.appendChild(s);
}

var validEmail = function(email) {
		return /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i.test(email);
}

var sendNewsletter = function() {
	var data = $('#newsletter_email');
	if(data[0] == null) return;
	var email = data[0].value;

	if(!validEmail(email)) {
//		$('#newsletter_email').css({
//			backgroundColor: '#ff9a9a'
//		});	
		return false;
	}
	$.post(
	  "/newsletter/add",
	  {'email': email},
	  function(data) {
	  	$('#newsletter_email').val('');
	  	$('#newsletter_text').html(data);
	  }
	);
}

var unsubscribeNewsletter = function() {
	var data = $('#unsubscribe_email');
	if(data[0] == null) return;
	var email = data[0].value;
	
	$('#unsubscribe_email').css({
			backgroundColor: '#FFF'
		});	
	if(!validEmail(email)) {
		$('#unsubscribe_email').css({
			backgroundColor: '#ff9a9a'
		});	
		return false;
	}
	
	$.post(
	  "/newsletter/remove",
	  {'email': email},
	  function(data) {
	  	$('#unsubscribe_email').val('');
	  	$('#unsubscribe_msg').html(data);
	  }
	);
}


 var GMap_1 = null;
 var infowindow;

  function GMap_initialize_1(objInput)
  {
    var mapOptions = {
      center: new google.maps.LatLng(52.0979, 19.1368),
      zoom: 6,
      mapTypeId: google.maps.MapTypeId.ROADMAP,
      mapTypeControl: true,
      mapTypeControlOptions: {style: google.maps.MapTypeControlStyle.DROPDOWN_MENU},
    };

    GMap_1 = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
  }
  
var addMarker = function(markerOpt) {
	 var iconPrecision = new google.maps.MarkerImage(markerOpt.icon); 
	 iconPrecision.iconSize = new google.maps.Size(12,20);
	 iconPrecision.iconAnchor = new google.maps.Point(6,20);
	 iconPrecision.infoWindowAnchor = new google.maps.Point(6,3);
	 iconPrecision.shadow = "";
	 iconPrecision.shadowSize = new google.maps.Size(22,20);

	var marker = new google.maps.Marker({
      position:  new google.maps.LatLng(markerOpt.geo_x, markerOpt.geo_y),
      map: GMap_1,
	  icon: iconPrecision
  	});
	
	google.maps.event.addListener(marker, "click", function() {
      if (infowindow) {
              infowindow.close();
            }
            infowindow = new google.maps.InfoWindow({content: markerOpt.content});
            infowindow.open(GMap_1, marker);
    });
}
var empty = function(mixed_var) {
    var key;
    if (mixed_var === "" || mixed_var === 0 || mixed_var === "0" || mixed_var === null || mixed_var === false || typeof mixed_var === 'undefined') {
        return true;
    }
    if (typeof mixed_var == 'object') {
        for (key in mixed_var) {
            return false;
        }
        return true;
    }
    return false;
}
