
var Tools = {
	noSpamEmail: function() {
		var urls = $('a.nospam');

		for (i = 0; i < urls.length; i++) {
			$(urls[i]).attr('href', $(urls[i]).attr('href').replace('[ns]','@'));
		}
	}
}

var MiniGallerySlider = {
	init: function() {
		var miniGalleries = $('ul.gallery-row');
		var maxWidth = 820;
		if (miniGalleries.length > 0) {
			for (g = 0; g < miniGalleries.length; g++) {
				var handleImages = $('#' + $(miniGalleries[g]).attr('id') + ' .category-gallery-slider img');
				var widthBlock = 0;
				var cropSize = 0;
				for (h = 0; h < handleImages.length; h++) {
					if ((cropSize+$(handleImages[h]).width()+1) < maxWidth) {
						cropSize += $(handleImages[h]).width()+1;
					}
					widthBlock += $(handleImages[h]).width()+1;
					$(handleImages[h]).parent().attr('href', $('#'+$(miniGalleries[g]).attr('id')+' .gallery-link').attr('href')+'#'+$(handleImages[h]).attr('id').replace('foto-','foto'));
					$(handleImages[h]).parent().parent().width($(handleImages[h]).width()+1);
					$(handleImages[h]).parent().parent().parent().width($(handleImages[h]).width());
				}
				/*
				if (widthBlock == cropSize) {
					$('#' + $(miniGalleries[g]).attr('id') + ' .ico-x').show();
				}
				if (widthBlock > cropSize) {
					$('#' + $(miniGalleries[g]).attr('id') + ' .ico-next').show();
				}
				*/
				$('#' + $(miniGalleries[g]).attr('id') + ' .category-gallery-slider .carousel').width(cropSize);
				//$('#' + $(miniGalleries[g]).attr('id') + ' .carousel').css('width', cropSize);
				$('#' + $(miniGalleries[g]).attr('id') + ' .jcarousel-container').width(cropSize);
				//$('#' + $(miniGalleries[g]).attr('id') + ' .carousel').css('width', cropSize);
//$('#' + $(miniGalleries[g]).attr('id') + ' .carousel').width(cropSize-30);
				$('#' + $(miniGalleries[g]).attr('id') + ' .jcarousel-next').css('marginLeft', cropSize);
			}
		}
		$('.carousel').jcarousel({
			initCallback: MiniGallerySlider.init_carousel
		});

		//$('ul.gallery-row .category-gallery-slider img').bind("load", function(e){ $(this).fadeIn(1000); });
	},
	init_carousel: function() {

		var miniGalleries = $('ul.gallery-row');
		var maxWidth = 820;
		if (miniGalleries.length > 0) {
			for (g = 0; g < miniGalleries.length; g++) {
				var handleImages = $('#' + $(miniGalleries[g]).attr('id') + ' .category-gallery-slider img');
				var widthBlock = 0;
				var cropSize = 0;
				for (h = 0; h < handleImages.length; h++) {
					if ((cropSize+$(handleImages[h]).width()+1) < maxWidth) {
						cropSize += $(handleImages[h]).width()+1;
					}
					widthBlock += $(handleImages[h]).width()+1;
					$(handleImages[h]).parent().attr('href', $('#'+$(miniGalleries[g]).attr('id')+' .gallery-link').attr('href')+'#'+$(handleImages[h]).attr('id').replace('foto-','foto'));
					$(handleImages[h]).parent().parent().width($(handleImages[h]).width()+1);
					$(handleImages[h]).parent().parent().parent().width($(handleImages[h]).width());
				}
				/*
				if (widthBlock == cropSize) {
					$('#' + $(miniGalleries[g]).attr('id') + ' .ico-x').show();
				}
				if (widthBlock > cropSize) {
					$('#' + $(miniGalleries[g]).attr('id') + ' .ico-next').show();
				}
				*/
				$('#' + $(miniGalleries[g]).attr('id') + ' .category-gallery-slider .carousel').width(widthBlock);
				//$('#' + $(miniGalleries[g]).attr('id') + ' .carousel').css('width', cropSize);
				$('#' + $(miniGalleries[g]).attr('id') + ' .jcarousel-container').width(cropSize+30);
				$('#' + $(miniGalleries[g]).attr('id') + ' .jcarousel-clip').width(cropSize);
				$('#' + $(miniGalleries[g]).attr('id') + ' .jcarousel-next').css('marginLeft', cropSize+20);
				$('#' + $(miniGalleries[g]).attr('id') + ' .jcarousel-prev').css('marginLeft', cropSize+9)

			}
		}


		var miniCarousel = $('ul.carousel');

		if (miniCarousel.length > 0) {
			for(c = 0; c < miniCarousel.length; c++) {

			}
		}

	}
}
var cphoto = 0;
var marginLeft = 0;
var BigGallerySlider = {
	init: function() {
		var maxWidth = 820;

		var handleImages = $('#gallery-view .gallery-in img');
		var widthBlock = 0;
    var is_ie6 = ('ActiveXObject' in window && !('XMLHttpRequest' in window));
		for (h = 0; h < handleImages.length; h++) {
			widthBlock += $(handleImages[h]).width()+3;
      if (is_ie6) {
        widthBlock -= 4;
      }
		}

		$('#gallery-view .gallery-in').width(widthBlock);
		$('#gallery-view .gallery-in li a img').bind("load", function(e){  });
		$('#gallery-view .gallery-in li a').bind("click", function(e){ return false; });
		$('#gallery-details .gallery-min- li a').bind("click", function(e){ BigGallerySlider.show_photo(BigGallerySlider.position_photo($(this).attr('id').replace('link-',''))); return false; });

		BigGallerySlider.init_slide(0);
		BigGallerySlider.parse_link();
		
		
		$('.nv-left img').bind("click", function(e){ BigGallerySlider.pv(); });
    $('.nv-right img').bind("click", function(e){ BigGallerySlider.nx(); });
	},
	pv: function() {
	/*
		marginLeft -= 460;
		var wh = $('#gallery-view .gallery-in').width()-960;
		if (marginLeft > wh) {
			marginLeft = wh;
			$('.nv-right img').hide();
		} else {
      $('.nv-right img').show();
    }
		if (marginLeft <= 0) {
			marginLeft = 0;
			$('.nv-left img').hide();
		} else {
      $('.nv-left img').show();
    }
		$('#gallery-view .gallery-in').animate({ marginLeft: marginLeft*-1 }, 'slow');

		var prc = (marginLeft*100/wh);



		BigGallerySlider.init_slide(prc);
		$('#gallery-view .gallery-in').animate({ marginLeft: marginLeft*-1 }, 'slow');

		var prc = (marginLeft*100/wh);
		*/
		var wh = $('#gallery-view .gallery-in').width()-960;
		var fq = parseInt($('ul.gallery-in').css('marginLeft').replace('px',''));
    fq += 460;

    if (fq > 0) {
      fq = 0;
      $('.nv-left img').hide();
    } else {
      $('.nv-left img').show();
    }
    
    
    $('#gallery-view .gallery-in').animate({ marginLeft: fq+3 }, 'slow');
		var prc = ((fq*-1)*100/wh);

		BigGallerySlider.init_slide(prc);
		
  },
	nx: function() {
	/*
		marginLeft += 460;
		var wh = $('#gallery-view .gallery-in').width()-960;
		if (marginLeft > wh) {
			marginLeft = wh;
			$('.nv-right img').hide();
		} else {
      $('.nv-right img').show();
    }
		if (marginLeft < 0) {
			marginLeft = 0;
			$('.nv-left img').hide();
		} else {
      $('.nv-left img').show();
    }
		$('#gallery-view .gallery-in').animate({ marginLeft: marginLeft*-1 }, 'slow');

		var prc = (marginLeft*100/wh);



		BigGallerySlider.init_slide(prc);
		$('#gallery-view .gallery-in').animate({ marginLeft: marginLeft*-1 }, 'slow');

		var prc = (marginLeft*100/wh);
		*/
		var wh = $('#gallery-view .gallery-in').width()-960;
		var fq = parseInt($('ul.gallery-in').css('marginLeft').replace('px',''));
    fq -= 460;

    if (fq < (wh*-1)) {
      fq = wh*-1;
      $('.nv-right img').hide();
    } else {
      $('.nv-right img').show();
    }
    
    
    $('#gallery-view .gallery-in').animate({ marginLeft: fq+3 }, 'slow');
		var prc = ((fq*-1)*100/wh);

		BigGallerySlider.init_slide(prc);
  },
  set_min: function(prc) {
  
		var handleImages = $('#gallery-details .gallery-min- img');

		var widthBlock = 0;
		for (h = 0; h < handleImages.length; h++) {
			widthBlock += $(handleImages[h]).width()+1;
		
		}
		if ($('#gallery-details .gallery-info').width() == 289) {
			var maxWidth = 657;
		} else {
			var maxWidth = 836;
		}

		if (prc < (widthBlock-maxWidth)) {
      prc = maxWidth-widthBlock;
    //  prc = prc*-1;
    }
  
   // $('#gallery-carousel').css('left', prc);
  },
  changePosition: function(position, ref) {
    //console.log('cw:' + csw);
    
    if ($('#gallery-view')) {
    var ry = $('#gallery-view .gallery-in').width();
    if ($('#gallery-view .gallery-in').css('marginLeft')) { 
      var ty = parseInt($('#gallery-view .gallery-in').css('marginLeft').replace('px',''))*-1;
    } else {
    var ty = 0;
    }
    } else {
    var ry = 0;
    var ty = 0;
    }
    //console.log('==');
    var rz = ry-ty;
    var pc = (rz*100)/ry;
    //console.log(pc+'%');
    
    position = 100-pc;   
    
    
    var ty = $('#gallery-carousel').width();
    var nw = ty*(position/100);
    var gw = $('.jcarousel-clip').width();
  //  caly klip - 1200
    //obszar - 500
    //maxymalny ujemny margines - 700
    //nw < (ty-gw) && 
    if (ref != 1) {
    //nw+= 10;
    //$('#gallery-carousel').css('marginLeft', nw*-1);
    //$('#gallery-carousel').animate({ left: nw*-1 }, 'fast');
    
    
    
		var handleImages = $('#gallery-details .gallery-min- img');

		var widthBlock = 0;
		for (h = 0; h < handleImages.length; h++) {
			widthBlock += $(handleImages[h]).width()+1;
			if (widthBlock > nw) {
			csw.scroll(h);
        break;
      }
		}
    
    
    
    
    
    
    }
    clearTimeout(ee);
    if (ref == 1) {
    
    var ee = setTimeout(BigGallerySlider.changePosition, 200);
    }
   // BottomGalleryComponent.init_carousel();
  },
	init_slide: function(start_pos) {
	
        var ft = $('#gallery-carousel').width()-$('.jcarousel-clip').width();
        
				var psq = ft*(start_pos/100);
				psq -= 3;
				
		if ($('#gallery-details .gallery-info').width() == 289) {
			var maxWidth = 657;
		} else {
			var maxWidth = 836;
		}
				
				
				if ($('#gallery-carousel').width() > maxWidth) {
				  BigGallerySlider.set_min((psq*-1));
				}
				//
				//$('.jcarousel-clip-horizontal').html($('.jcarousel-clip-horizontal').html());
 

        BigGallerySlider.changePosition(start_pos,1);
			//	alert(s);
			//	BottomGalleryComponent.init_carousel();
		$("#gallery-slider").slider('destroy');
		$("#gallery-slider").slider({
			animate: true,
			value: start_pos,
			stop: function(event, ui) {
         BigGallerySlider.changePosition($(this).slider('values', ui),1);
      },
			slide: function(event, ui) {
		var p = $(this).slider('values', ui)/100;

				if (p < 0.2) {
					$('.ui-slider-handle').css('marginLeft', 0);
				} else {
					$('.ui-slider-handle').css('marginLeft', -54);
				}
				
				
      
			},
			slide: function(event, ui) {
				//alert($(this).slider('values', ui));

				var p = $(this).slider('values', ui)/100;
        BigGallerySlider.changePosition(p*100,1);
				if (p < 0.2) {
					$('.ui-slider-handle').css('marginLeft', 0);
				} else {
					$('.ui-slider-handle').css('marginLeft', -54);
				}

				var wc = $('#gallery-view .gallery-in').width()-960;

				var psx = wc*p;
				psx -= 3;
				//$('#gallery-view .gallery-in').animate({ marginLeft: psx*-1 }, 'slow');
				$('#gallery-view .gallery-in').css('marginLeft', psx*-1);
				
        var ft = $('#gallery-carousel').width()-$('.jcarousel-clip').width();
				var psq = ft*p;
				psq -= 3;
			//	$('#gallery-carousel').css('marginLeft', psq*-1);
				
			},
			change: function(event, ui) {
				//alert($(this).slider('values', ui));

				var p = $(this).slider('values', ui)/100;

				if (p < 0.2) {
					$('.ui-slider-handle').css('marginLeft', 0);
				} else {
					$('.ui-slider-handle').css('marginLeft', -54);
				}

				var wc = $('#gallery-view .gallery-in').width()-960;

				var psx = wc*p;
				psx -= 3;
				$('#gallery-view .gallery-in').animate({ marginLeft: psx*-1 }, 'slow');
				
    	  if (p == 0) {
    	   $('.nv-left img').hide();
    	  } else {
          $('.nv-left img').show();
        }
    	  if (p == 1) {
    	   $('.nv-right img').hide();
    	  } else {
          $('.nv-right img').show();
        }
        
        var ft = $('#gallery-carousel').width()-$('.jcarousel-clip').width();
				var psq = ft*p;
				psq -= 3;
	//		$('#gallery-carousel').animate({ marginLeft: psq*-1 }, 'slow');
				//$('#gallery-view .gallery-in').css('marginLeft', psx*-1);
			}
		});

		var p = parseInt($('.ui-slider-handle').css('left'));
		if (p < 50) {
					$('.ui-slider-handle').css('marginLeft', 0);
				} else {
					$('.ui-slider-handle').css('marginLeft', -54);
				}
				
    if (p == 0) {
      $('.nv-left img').hide();
    } else {
      $('.nv-left img').show();
    }
    
    
 

    

	},
	position_photo: function(photoId) {
		var handleImages = $('#gallery-view .gallery-in img');
		var photoMarginLeft = 0;
		for (h = 0; h < handleImages.length; h++) {
			if ($(handleImages[h]).attr('id') == photoId) {
				break;
			}
			photoMarginLeft += $(handleImages[h]).width()+3;

		}

		var wph = $('#'+photoId).width();
		if (wph < 960) {
			var half = (wph/2);
			photoMarginLeft -= ((960-wph)/2);
		}

		return photoMarginLeft;
	},
	parse_link: function() {
		if (document.location.href.match('#')){
			var tmp = document.location.href.split('#');
			if (tmp) {
				BigGallerySlider.show_photo(BigGallerySlider.position_photo(tmp[1].replace('foto','fotografia')));
			}
		}
	},
	show_photo: function(marginLeft) {
		var marginLeft = marginLeft;
		var wh = $('#gallery-view .gallery-in').width()-960;
		if (marginLeft > wh) {
			marginLeft = wh;
			$('.nv-right img').hide();
		} else {
      $('.nv-right img').show();
    }
		if (marginLeft < 0) {
			marginLeft = 0;
			$('.nv-left img').hide();
		} else {
      $('.nv-left img').show();
    }
		$('#gallery-view .gallery-in').animate({ marginLeft: marginLeft*-1 }, 'slow');

		var prc = (marginLeft*100/wh);

    var marginLeft = $('#gallery-view .gallery-in').css('marginLeft');

		BigGallerySlider.init_slide(prc);
	}
}

var ContactForm = {
	init: function() {
		$('#contact-form-page input.intxt').bind("focus", function(e) { if ($(this).val() == $(this).attr('rel')) {$(this).val('');} });
		$('#contact-form-page input.intxt').bind("blur", function(e) { if($(this).val() == '') { $(this).val($(this).attr('rel')); } });
		$('#contact-form-page textarea').bind("focus", function(e) { if ($(this).val() == $(this).attr('rel')) {$(this).val('');} });
		$('#contact-form-page textarea').bind("blur", function(e) { if($(this).val() == '') { $(this).val($(this).attr('rel')); } });

		$('#contact-form-page .copy-me').bind("click", function(e) {
			if ($(this).hasClass('copy-me-0')) {
				$(this).removeClass('copy-me-0');
				$(this).addClass('copy-me-1');
				$('#ecopy_me').val('1');
			} else {
				$(this).addClass('copy-me-0');
				$(this).removeClass('copy-me-1');
				$('#ecopy_me').val('0');
			}
		});

		$('#contact-form-page form').bind("submit",  function(e) {
			$('#please-wait').show();
			$('#no-correct').hide();
			$.post('/contact/send',{
				name: $('#ename_surname').val(),
				email: $('#eemail').val(),
				content: $('#econtent').val(),
				copy_me: $('#ecopy_me').val()
			},
			function(data) {
				$('#please-wait').hide();
				if (data.code == "-1") {
					$('#no-correct').show();
				}
				if (data.code == "1") {
					$('#no-correct').hide();
					$('#please-wait').hide();
					$('#contact-form-page form').hide();
					$('#thank-you').show();
				}
			}, "json");

			return false;
		});
	}
}
var cc = null;
var csw = null;
var BottomGalleryComponent = {

	init_carousel: function(carousel) {
		//var carousel = carousel;
    //console.log(carousel);
    csw = carousel;

    

		var handleImages = $('#gallery-details .gallery-min- img');

		var widthBlock = 0;
		for (h = 0; h < handleImages.length; h++) {
			widthBlock += $(handleImages[h]).width()+1;
			$(handleImages[h]).parent().parent().width($(handleImages[h]).width()+1);
		}
		$('#gallery-details .gallery-min').width(widthBlock);


		if ($('#gallery-details .gallery-info').width() == 289) {
			var maxWidth = 657;
		} else {
			var maxWidth = 836;
		}
		var cropSize=maxWidth-20;
		$('.gallery-min- ul').width(widthBlock);
		//$('#' + $(miniGalleries[g]).attr('id') + ' .carousel').css('width', cropSize);
		$('.gallery-min- .jcarousel-container').width(cropSize+30);
		$('.gallery-min- .jcarousel-clip').width(cropSize);
		$('.gallery-min- .jcarousel-next').css('marginLeft', cropSize+20);
		$('.gallery-min- .jcarousel-prev').css('marginLeft', cropSize+9)
		//carousel.reload();
    

	},
	reload_carousel: function() {

	},
	init: function() {
		$('#gallery-details .gallery-info .gallery-link-more').bind("click", function(e) { BottomGalleryComponent.show_desc(); return false;});
		$('#gallery-details .gallery-info .gallery-desc').bind("click", function(e) { BottomGalleryComponent.show_desc(); return false;});


		var handleImages = $('#gallery-details .gallery-min- img');

		var widthBlock = 0;
		for (h = 0; h < handleImages.length; h++) {
			widthBlock += $(handleImages[h]).width()+1;
			$(handleImages[h]).parent().parent().width($(handleImages[h]).width()+1);
		}
		//$('#gallery-details .gallery-min-').width(widthBlock);
		//BottomGalleryComponent.reload_carousel();
		$('#gallery-carousel').jcarousel({
			initCallback: BottomGalleryComponent.init_carousel
		});
	},
	scroll_next: function() {
		var twi = $('#gallery-details .gallery-min ul').width();
		var csz = $('#gallery-details .gallery-min').width();
		var cpos = parseInt($('#gallery-details .gallery-min ul').css('marginLeft').replace('px',''));

		if ($('#gallery-details .gallery-info').width() == 289) {
			var maxWidth = 639;
		} else {
			var maxWidth = 818;
		}


		if ((cpos-csz) >= (twi*-1)) {
			$('#gallery-details .gallery-min ul').animate({ marginLeft: (twi*-1) }, 'slow', function() { BottomGalleryComponent.refresh_icons(); });
		} else {
			$('#gallery-details .gallery-min ul').animate({ marginLeft: (cpos-csz) }, 'slow', function() { BottomGalleryComponent.refresh_icons(); });
		}


	},
	scroll_prev: function() {
		var twi = $('#gallery-details .gallery-min ul').width();
		var csz = $('#gallery-details .gallery-min').width();
		var cpos = parseInt($('#gallery-details .gallery-min ul').css('marginLeft').replace('px',''));

		if ((cpos+csz) < 1) {
			$('#gallery-details .gallery-min ul').animate({ marginLeft: (cpos+csz) }, 'slow', function() { BottomGalleryComponent.refresh_icons(); });
		}


	},
	refresh_icons: function() {
		if ($('#gallery-details').attr('id')) {
			var twi = $('#gallery-details .gallery-min ul').width();
			var csz = $('#gallery-details .gallery-min').width();
			var cpos = parseInt($('#gallery-details .gallery-min ul').css('marginLeft').replace('px',''));

			if ($('#gallery-details .gallery-info').width() == 289) {
				var maxWidth = 639;
			} else {
				var maxWidth = 818;
			}

			if (cpos < 0) {
				$('#gallery-details .ico-prev').show();
			} else {
				$('#gallery-details .ico-prev').hide();
			}

			if ((cpos-csz) < (twi*-1)) {
				$('#gallery-details .ico-x').show();
			} else {
				$('#gallery-details .ico-x').hide();
			}

			if ((cpos-csz) < (twi*-1)) {
				$('#gallery-details .ico-next').hide();
			} else {
				if (maxWidth <= twi) {
				$('#gallery-details .ico-next').show();
				}
			}



		}
	},
	show_desc: function() {
		if ($('#gallery-details .gallery-info').width() == 289) {
			$('#gallery-details .gallery-info h2.gallery-description').hide();
			$('#gallery-details .gallery-info .gallery-desc').show();
			$('#gallery-details .gallery-info').css('width',110);
			$('.jcarousel-container').removeClass('jcarousel-container-f');
		}
		else {
			$('#gallery-details .gallery-info h2.gallery-description').show();
			$('#gallery-details .gallery-info .gallery-desc').show();
			$('#gallery-details .gallery-info').css('width',289);
			$('.jcarousel-container').addClass('jcarousel-container-f');
		}
		//$('#gallery-details .gallery-min ul').animate({ marginLeft: 0 }, 'slow', function() { BottomGalleryComponent.refresh_icons(); });

		$('#gallery-carousel').jcarousel({
			initCallback: BottomGalleryComponent.init_carousel
		});
	}
}

// Document Ready event
$(document).ready(function() {
	Tools.noSpamEmail();
	MiniGallerySlider.init();
	BigGallerySlider.init();

	BottomGalleryComponent.init();
	ContactForm.init();

  $("a[href*='http']").each( function(i) {
			if (this.href.match('wadach')) {
      
      } else {
        this.target = "_blank";
      }
		});


});
