/*
 * jQuery hashchange event - v1.3 - 7/21/2010
 * http://benalman.com/projects/jquery-hashchange-plugin/
 * 
 * Copyright (c) 2010 "Cowboy" Ben Alman
 * Dual licensed under the MIT and GPL licenses.
 * http://benalman.com/about/license/
 */
(function($,e,b){var c="hashchange",h=document,f,g=$.event.special,i=h.documentMode,d="on"+c in e&&(i===b||i>7);function a(j){j=j||location.href;return"#"+j.replace(/^[^#]*#?(.*)$/,"$1")}$.fn[c]=function(j){return j?this.bind(c,j):this.trigger(c)};$.fn[c].delay=50;g[c]=$.extend(g[c],{setup:function(){if(d){return false}$(f.start)},teardown:function(){if(d){return false}$(f.stop)}});f=(function(){var j={},p,m=a(),k=function(q){return q},l=k,o=k;j.start=function(){p||n()};j.stop=function(){p&&clearTimeout(p);p=b};function n(){var r=a(),q=o(m);if(r!==m){l(m=r,q);$(e).trigger(c)}else{if(q!==m){location.href=location.href.replace(/#.*/,"")+q}}p=setTimeout(n,$.fn[c].delay)}$.browser.msie&&!d&&(function(){var q,r;j.start=function(){if(!q){r=$.fn[c].src;r=r&&r+a();q=$('<iframe tabindex="-1" title="empty"/>').hide().one("load",function(){r||l(a());n()}).attr("src",r||"javascript:0").insertAfter("body")[0].contentWindow;h.onpropertychange=function(){try{if(event.propertyName==="title"){q.document.title=h.title}}catch(s){}}}};j.stop=k;o=function(){return a(q.location.href)};l=function(v,s){var u=q.document,t=$.fn[c].domain;if(v!==s){u.title=h.title;u.open();t&&u.write('<script>document.domain="'+t+'"<\/script>');u.close();q.location.hash=v}}})();return j})()})(jQuery,this);


/*
 * veverka.eu
 */

var skipHashChange = false;

$().ready(function(){

	// nice headers
	/*
	$.getScript('/js/cufon-yui-1.09i.js', function(){
		$.getScript('/js/cufon-gentium.js', function(){
			Cufon.replace('h1,h2,h3,h4');
		});
	});
	*/

	// welcome screen
	if ($('#welcomeScreenContainer').length > 0)
		$('#welcomeScreenContainer').welcomeScreen();

	if ($('a.deletePage').length > 0)
	{
		$('a.deletePage').each(function(){
			$(this).click(function(){
				if (confirm('Potvrďte prosím odstranění stránky'))
				{
					href = $(this).attr('href');
					window.location.href = href + '&isConfirmed';
					return false;
				}
				else
				{
					return false;
				}
			});
		});
	}
	
	if ($('.fancybox-image-container').length > 0)
	{
    $("head").append("<link>");
    css = $("head").children(":last");
    css.attr({
      rel:  "stylesheet",
      type: "text/css",
      href: "/theme/fancybox/jquery.fancybox-1.3.1.css"
    });

		$.getScript('/js/jquery.easing-1.3.pack.js', function(){
			$.getScript('/js/jquery.mousewheel-3.0.2.pack.js', function(){
				$.getScript('/js/jquery.fancybox-1.3.1.pack.mvfix.js', function(){
					$('.fancybox-image-container a').fancybox({
							'hideOnContentClick': true,
							'overlayShow': true,
							'padding': 1,
							'titleShow': false
						});
				});
			});
		});
	}

	if ($('#photoList a').length > 0)
	{
		$('#photoList.editMode a').mousedown(function(e){
			if (e.which == 2)
			{
				$(this).attr('href', '/mv-' + $('img', this).attr('alt'));
			}
		}).mouseout(function(){
			$(this).attr('href', $(this).find('img').attr('src').replace('-little', ''));
		});

		$('#photoList a').fancybox(
			{
				'hideOnContentClick': true,
				'overlayShow': true,
				'padding': 1,
				'titleShow': false,
				onStart: function(items, index, opts){
					var photoId = parseInt($('img', items[index]).attr('alt'));
					if (photoId > 0)
					{
						skipHashChange = true;
						window.location.hash = 'mv-' + photoId;
						incrementViewCount(photoId);
					}
				},
				onComplete: function(){
					skipHashChange = false;
				},
				onCleanup: function(){
					skipHashChange = false;
				},
				onCancel: function(){
					skipHashChange = false;
					window.location.hash = '';
				},
				onClosed: function(){
					skipHashChange = false;
					window.location.hash = '';
				}
			});
		
		$('#photoList.editMode a').each(function(){
			$(this).click(function(e){
				if (e.ctrlKey)
				{
					$.fancybox.cancel();
					photoId = $('img', this).attr('alt');
					editLocation = window.location.protocol + '//' + window.location.hostname + '/mv-' + photoId + '?action=edit&selfClose';// + encodeURIComponent(window.location);
					window.open(editLocation, "_blank", "width=860, height=800, scrollbars=1, status=0");
					e.preventDefault();
				}
				else
				if (e.shiftKey)
				{
					$.fancybox.cancel();
					photoId = $('img', this).attr('alt');
					window.location = window.location.protocol + '//' + window.location.hostname + '/mv-' + photoId;
					//editLocation = window.open(editLocation, "_blank", "width=860, height=800, scrollbars=1, status=0");
					e.preventDefault();
				}
			});
		});

		$(window).hashchange(function(e){
			e.preventDefault();
			fancyHashChange(location.hash);
		});
		$(window).hashchange();
	}

	if ($('#mvphoto').length > 0)
	{
		_size = parseInt($('input[name="cropSize"]').val());
		_x1 = parseInt($('input[name="cropX"]').val());
		_y1 = parseInt($('input[name="cropY"]').val());
		_x2 = _x1+_size;
		_y2 = _y1+_size;
		$('#mvphoto').imgAreaSelect({
			x1: _x1,
			y1: _y1,
			x2: _x2,
			y2: _y2,
			minWidth: 200,
			minHeight: 200,
			aspectRatio: '1:1',
			handles: true,
			onSelectChange: updateCropData
		});
		$('body').dblclick(function(){
			$('form').submit();
		});
	}
	
	if ($('#retouchVisible').length > 0)
	{
		$('#retouchVisible').click(function(e){
			$.ajax({
				type: 'GET',
				url: window.location.pathname + '?action=set&retouchVisible=' + ($('#retouchVisible:checked').length > 0 ? 1 : 0),
				dataType: 'json',
				cache: false,
				error: function()
				{
					e.preventDefault();
				}
			});
		});
	}

	function updateCropData(img, selection)
	{
		if (!isNaN(selection.x1) && !isNaN(selection.y1) && !isNaN(selection.width))
		{
			$('input[name="cropX"]').val(selection.x1);
			$('input[name="cropY"]').val(selection.y1);
			$('input[name="cropSize"]').val(selection.width);
		}
	}

	if ($('.PhotoRetouchSingle').length > 0)
	{
		$('.PhotoRetouchSingle').each(function(){
			fader = $('.PhotoRetouchFader', $(this));
			width = 0;
			height = 0;
			currentPhoto = 0;
			countPhotos = 0;
			$('.PhotoRetouchFile', $(this)).each(function(i){
				if (i == 0)
				{
					width = $(this).width();
					height = $(this).height();
				}
				else
				{
					$(this).hide();
				}
				countPhotos++;
			});
			$(this).click(function(){
				changePhoto(this);
			});
			$(this).css('width', width + 'px');
			$(this).css('height', height + 'px');
			updatePhotoInfo(this);
		});
	}
});

function incrementViewCount(photoId)
{
	$.getJSON("/mv-" + photoId + "?view");
}

function updatePhotoInfo(l)
{
	$('.PhotoInfo', l).text(currentPhoto+1);
}

function changePhoto(l)
{
	currentPhoto++;
	if (countPhotos <= currentPhoto)
	{
		currentPhoto = 0;
	}
	$('.PhotoRetouchFile', l).each(function(i){
		if (i == currentPhoto)
		{
			$(this).show();
		}
		else
		{
			$(this).hide();
		}
	});
	updatePhotoInfo(l);
}

function fancyHashChange(hash)
{
	if (skipHashChange)
		return;

	var hasFancyboxStarted = $('#fancybox-wrap:visible').length > 0;
	
	if (hash.length > 4)
	{
		var photoId = parseInt(hash.substring(4));
		if (photoId > 0 && $('#mv-' + photoId + '-thumb').length > 0)
		{
			$('#mv-' + photoId + '-thumb').trigger('click');
		}
		else
		if (hasFancyboxStarted)
			$.fancybox.close();
	}
	else
	if (hasFancyboxStarted)
		$.fancybox.close();
}

