$(document).ready(function(){
	
	// tabulky
	$("tr:odd").addClass('odd');
	$("tr:even").addClass('even');
	
	//sortable
	$('.sortable').sortable({ axis: 'y',
														handle: '.handle'
														});
	
	// pridat credit kategoriu
	$("a#add-credits").click(function(){
		categoryID = $('select[name=credits]').val();
		if (categoryID != 0) {
			url = $('#siteurl').text()+'admin/ajax_get_work_credits_row/'+categoryID;
			$.ajax({
			  url: url,
			  success: function(html){
					$("a#add-credits").parents('tr').before(html);
				}
			});
		}
	});
	
	$('#remove-credit').live('click', function() {
		var zmazat = confirm('Naozaj chcete zmazať túto položku?');
		if (zmazat) {
			$(this).parents('tr').remove();
		}
	});
	
	// lightBox
	$(function() {
		$('a[rel=slides]').lightBox();
	});
	
	$('a[href|=#]').click(function() {
		return false;
	});

	$('a[target=_blank]').addClass('external');
	
	$('#top').click(function() {
		$('html,body').animate({scrollTop: 0}, 500);
	});
	
	// Menu
	$('#menu .select, #menu .sub a').click(function() {
		$('#menu .sub div').toggleClass('open');
	});
	$('body').click(function() {
		$('#menu .sub div').removeClass('open');
	});
	
	// Work tabs
	$('a#photo').addClass('active');
	$('.box').hide();
	$('#box-photo').show();
	$('#panel li a').click(function() {
		var target = $(this).attr('id');
		$('#panel li a').removeClass("active");
		$(this).addClass("active");
		$('.box').slideUp(200);
		$('#box-' + target).slideDown(200);
	});
	
	$('#blackbox p:last-child').addClass('last');
	
	// Banner
	$('#banner .slides')
	.cycle({
		fx: 'fade',
		speed: 500, 
    	timeout: 5000,
		pause: 1,
	});
	
	// ImageViewer
	$('.imageviewer .slides')
	.cycle({
		fx: 'fade',
		speed: 500, 
    	timeout: 5000,
		pause: 1,
		prev: '.prev',
		next: '.next',
		after: function(curr,next,opts) {
		var caption = (opts.currSlide + 1) + ' <i>/</i> ' + (opts.slideCount);
		$('.counter').html(caption); }
	});
	
	// getPageSize() by quirksmode.com
	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){
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		} else {
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}
		var windowWidth, windowHeight;
		if (self.innerHeight) {
			if(document.documentElement.clientWidth){
				windowWidth = document.documentElement.clientWidth; 
			} else {
				windowWidth = self.innerWidth;
			}
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) {
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) {
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}	
		if(yScroll < windowHeight){
			pageHeight = windowHeight;
		} else { 
			pageHeight = yScroll;
		}
		if(xScroll < windowWidth){	
			pageWidth = xScroll;		
		} else {
			pageWidth = windowWidth;
		}
		arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
		return arrayPageSize;
	};
	
	/* getPageScroll() by quirksmode.com */
	function ___getPageScroll() {
		var xScroll, yScroll;
		if (self.pageYOffset) {
			yScroll = self.pageYOffset;
			xScroll = self.pageXOffset;
		} else if (document.documentElement && document.documentElement.scrollTop) {
			yScroll = document.documentElement.scrollTop;
			xScroll = document.documentElement.scrollLeft;
		} else if (document.body) {
			yScroll = document.body.scrollTop;
			xScroll = document.body.scrollLeft;	
		}
		arrayPageScroll = new Array(xScroll,yScroll);
		return arrayPageScroll;
	};
		
	$('.playvideo').click(function() {
		$('embed, object, select').css({'visibility':'hidden'});
		var video = $(this).attr('href');
		$('body').append('<div id="video-overlay"></div><div id="video"><object width="640" height="360"><param name="movie" value="http://www.youtube.com/v/' + video + '&hl=en_US&fs=1&rel=0&autoplay=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/' + video + '&hl=en_US&fs=1&rel=0&hd=1&autoplay=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="640" height="360"></embed></object></div>');
		$('#video').hide();
		var arrPageSizes = ___getPageSize();
		$('#video-overlay').css({width:arrPageSizes[0],height:arrPageSizes[1]}).fadeIn();
		var arrPageScroll = ___getPageScroll();
		$('#video').css({top:arrPageScroll[1] + (arrPageSizes[3]/10)}).fadeIn(500);
		$('#video-overlay').click(function() {
			$(this).next('#video').remove();
			$(this).fadeOut(function() { $(this).remove(); });
			$('embed, object, select').css({'visibility':'visible'});
		});
		return false;
	});
	
	$(window).resize(function() {
		var arrPageSizes = ___getPageSize();
		$('#video-overlay').css({width:arrPageSizes[0],height:arrPageSizes[1]});
		var arrPageScroll = ___getPageScroll();
		$('#video').css({top:arrPageScroll[1] + (arrPageSizes[3] / 10),left:arrPageScroll[0]});
	});
});

function writeTo(u, d) {
	var l="\x6D\x61\x69";
	l+="\x6C\x74\x6F\x3A"+u;
	window.location=l+"\x40"+d;
}
