function randomMinToMax(min, max_v) {
	var range = max_v - min + 1;
	return Math.floor(Math.random()*range + min);
}
// (function($) {
// 		$.fn.clearOnFocus = function() {
// 				function clearOnFocusFocus(event) {
// 					if ($(this).val() == $(this).data('original')) {
// 						$(this).val('');
// 					}
// 				}
// 				function clearOnFocusBlur(event) {
// 					if ($.trim($(this).val()) == '') {
// 						$(this).val($(this).data('original'));
// 					}
// 				}
// 				return this.each(function(){
// 					$(this).data('original', $(this).attr('value'));
// 					$(this).unbind('focus', clearOnFocusFocus).unbind('blur', clearOnFocusBlur).bind('focus', clearOnFocusFocus).bind('blur', clearOnFocusBlur);
// 				});
// 		};
// })(jQuery);

$(document).ready(function(){
	var $body = $('body'); var $header = $('#header'); var $mainMenuLists = $('#main-menu li ul.submenu'); var $mainWrap = $('#main-wrap'); $footer = $('#footer');
	
	function fixLayout() {
		$mainWrap.css({minHeight: $(window).height() - $footer.height() - 23});
// 		var wWidth = $(window).width();
// 		//console.log(wWidth);
// 		$body.removeClass('mq-narrow mq-mobile');
// 		if (wWidth < 1024) {
// 			if (wWidth < 500) {
// 				$body.addClass('mq-mobile');
// 			} else {
// 				$body.addClass('mq-narrow');
// 			}
// 		}
	}
	fixLayout();
	$(window).resize(function(){ fixLayout() });

	// mobile
	if ($.browser.mobile) {
    $('html').addClass('mobile');
	}

	// circles hide
	if ($('#choose-member').length > 0) {
		var chooseMemberList = $('#choose-member li.popup');
		var chooseMemberListLength = chooseMemberList.length - 2;
		var firstIndex = randomMinToMax(1, chooseMemberListLength);
		var secondIndex = randomMinToMax(1, chooseMemberListLength);
		do {
			 secondIndex = randomMinToMax(1, chooseMemberListLength);
		} while (secondIndex == firstIndex);
		chooseMemberList.eq(firstIndex).css({visibility:'hidden'});
		chooseMemberList.eq(secondIndex).css({visibility:'hidden'});
	}

	// history masonry
	if ($('.history-images ul, #attached-photos ul').length > 0) {
		$('.history-images ul, #attached-photos ul').map(function(){
			$(this).masonry({
				itemSelector : 'li',
				columnWidth: 5,
				gutterWidth: 0
			});
		});
	}
	
	// search highlight
	if ($('#search').length > 0) {
    $('#search .result').highlight($('#search_expression').text());
	}

  $('p, h1, h2, h3, h4, h5, strong, li').cstypo();
  
	// em
	$('a.em[data-em]').each(function(){
		e = $(this).data('em').replace('/','@');
		$(this).attr('href', 'mailto:'+e).not('.custom-text').text(e);
	});
	
	// clearOnFocus
	$('#search-box input[type="text"], .newsletter input[type="text"]').focus(function(){
	  var elThis = $(this);
		if ($.trim(elThis.val()) == elThis.data('original')) {
			elThis.val(elThis.data(''));
		}
	}).blur(function(){
	  var elThis = $(this);
		if ($.trim(elThis.val()) == '') {
			elThis.val(elThis.data('original'));
		}
	});
	
	// popups
// 	var $popup;
// 	$('.popup[data-title][data-subtitle][data-description]').hover(function(){
// 		var $this = $(this);
// 		$this.append('<div class="popupWindow"><div class="popupWindowInner"><strong>'+$this.data('title')+'</strong><i>'+$this.data('subtitle')+'</i><p>'+$this.data('description')+'</p></div></div>');
// 		$popup = $this.children('.popupWindow');
// 		$popup.css({top:$this.offset().top - $(window).scrollTop() + ($this.height()-$popup.height())/2, left:$this.offset().left - $(window).scrollLeft() + ($this.width()-$popup.width())/2});
// 		$(window).scroll(function(){
// 			$popup.remove();
// 		});
// 	}, function(){
// 		$popup.remove();
// 	});
	$('.popup[data-title][data-subtitle][data-description]').tooltip({
    delay: 0,
    fade: 400,
    showURL: false,
    track: true,
    bodyHandler: function() {
      var elThis = $(this);
      return '<div class="popupWindow"><div class="popupWindowInner"><strong>'+elThis.data('title')+'</strong><i>'+elThis.data('subtitle')+'</i><p>'+elThis.data('description')+'</p></div></div>';
    }
	});
	
	// lightbox
	$('a.lightbox').not('[data-title]').data('title', ' ');
	$('a.lightbox').lightBox({
		imageLoading: '/files/images/lightbox-ico-loading.gif',
		imageBtnClose: '/files/images/lightbox-btn-close.gif',
		imageBtnPrev: '/files/images/lightbox-btn-prev.gif',
		imageBtnNext: '/files/images/lightbox-btn-next.gif',
		imageBlank: '/files/images/lightbox-blank.gif'
	});
	console.log($('a.lightbox'));
	
	// mobile navigation
	$('#main-menu-mobile select').change(function(){
		window.location = $(this).val();
	});
	
	// main menu
// 	$mainMenuLists.each(function(){
// 		var $this = $(this);
// 		$this.css({display:'block', visibility:'hidden'});
// 		$this.data('height',$this.outerHeight()).css({display:'none', visibility:'visible'});
// 	});
//
// 	$('#main-menu > li').mouseenter(function(){
// 		var menu = $(this).find('ul.submenu');
// 		$mainMenuLists.filter('.in').removeClass('in').stop().slideUp(function(){ $(this).css({height: 'auto', paddingTop: 42}) });
// 		$header.stop().animate({paddingBottom:parseInt(menu.data('height'))}, 400);
// 		menu.addClass('in').stop().slideDown(function(){ $(this).css({height: 'auto', paddingTop: 42}) });
// //
// // 		$mainMenuLists.not(menu).filter('.in').removeClass('in').slideUp(400);
// // 		$header.add(menu).toggleClass('in');
// 	});
//   $('#main-menu').mouseleave(function(){
//     	$header.stop().animate({paddingBottom:0}, 400);
// 			$mainMenuLists.filter('.in').removeClass('in').stop().slideUp(function(){ $(this).css({height: 'auto', paddingTop: 42}) });
// 	});
// 	$('#main-menu > li.single').mouseenter(function(){
//     	$header.stop().animate({paddingBottom:0}, 400);
// 			$mainMenuLists.filter('.in').removeClass('in').stop().slideUp(function(){ $(this).css({height: 'auto', paddingTop: 42}) });
// 	});
	
	// footer newsletter submit
	$('#footer #newsletter-submit').click(function(){
		$(this).parents('form').submit();
	});
	
	// player
	soundManager.url = '/files/soundmanager/swf/'; // path to directory containing SoundManager2 .SWF file
	soundManager.flashVersion = 8;
	soundManager.useFlashBlock = false; // skip for now. See the flashblock demo when you want to start getting fancy.
	var mySoundObject;	var length;	var windowSize;	var offset;	var track;	
	
	soundManager.onload = function() {
		$("a.play").click(function (e) {
			e.preventDefault();
			$(this).toggleClass('stop');
			
			// $("div.duration span").html('');
			if(track==$(this).attr('data-track')){
				mySoundObject.stop();
				track = null;
			}else{
				track = $(this).attr('data-track');
				// duration = $(this).parent().find("div.duration span");
				soundManager.destroySound('mySound');
				mySoundObject = soundManager.createSound({
					id: 'mySound',
					url: track,
					autoLoad: true,
					autoPlay: true//,
					// whileplaying: function() {
					// 	duration.html(elapsedTime(this.position)+" / ");
					// 				    }
				});
			}
	    });
	}
	
	// zmena jazyku
	$('#language a').click(function(){
		var href = window.location.href;
		window.location.href = href.replace(/(\/+)[a-zA-Z]{2}(\/+)/, $(this).attr('href'));
	//	console.log(href)
		return false;
	});
	
	function sinfinCredit(){
		var span = $('#credit span.credit');
		var width = span.outerWidth('true');
		// var min = ($.browser.msie && $.browser.version <= 7) ? 0 : 0;
		var min = 0;
		$('#credit').css('width',min);
		span.css('color','#fff');

		$('#credit').hover(function(){
			span.css('color','#666');
			$('#credit').stop().animate({'width':width-1 + 'px'});
		}, function(){
			$('#credit').stop().animate({'width':min}, function(){
				span.css('color','#fff');
			});
		})
	}
	
	sinfinCredit();
	
}); // document.ready
