// A: Václav VESELÝ (www.deltaori.com)
// V: 0.5.0
// D: 11-09-13

var strHashString = '#!';
var strCssContainer = '#main';
var strAction = '';
var intVerticalScroll = '';
var strLastPage = '';

$(document).ready(
	function(){
		// postprocess hmenu
		$('#hmenu li').each(function () {
			$(this).corner("top 10px");
		});
		// postprocess body
		fncPostprocessContainer('body');
		$(window).bind('hashchange', fncHashChange);
		//document.documentElement.scrollTop = intVerticalScroll;
		$('#underall').css('marginTop',0);
	}
);

function fncPostprocessContainer(strCssContainer) {
	fncDecodeMailLink(strCssContainer);
	fncDecodeQrCode(strCssContainer);
	fncColorBox(strCssContainer);
	fncIsotope(strCssContainer);
	fncSlides(strCssContainer, ' .smallslides', '7000', false);
	fncSlides(strCssContainer, ' .slides', '12000', false);
	$(strCssContainer+' .slideheader').corner("right 10px");
	$(strCssContainer+' .hstripeline').corner("10px");
	$(strCssContainer+' .ajaxmenu').corner("5px");
	$(strCssContainer+' #status').corner("10px");
	$(strCssContainer+" .thumbcontainer").each(function () {
		$(this).corner("all 10px");
	});
	// process titles (very slow processing)
	/*$(strCssContainer+' img').each(function () {
		//$('a[title]').qtip({
		$('img[title]').qtip({
			position: {
				my: 'center',
				at: 'bottom center',
				adjust: {
					y: 5
				}
			},
		  style: {
				classes: 'ui-tooltip-dark'
			},
			hide: {
				event: 'click mouseleave'
			}
		});
	});*/
	// process links
	$(strCssContainer+' a').each(function () {
		if ($(this).attr('rel') == "external"){
			$(this).addClass("external");
		}
		if ($(this).attr('rel') == "externalnoimg"){
			$(this).addClass("externalnoimg");
		}
		if ($(this).hasClass("ajax")) {
			if ($(this).attr('href').indexOf('./index.php?action=') != -1){
				$(this).attr('href','/'+strHashString+$(this).attr('href').replace('./index.php?action=','').replace(location.hostname,'').replace('http://',''));
			} else {
				$(this).attr('href','/'+strHashString+$(this).attr('href').replace('/','').replace(location.hostname,'').replace('http://',''));
			}
		};
	});
	//fncBreadcrumb();
	//fncTipsy();
}

function fncIsotope(strCssContainer) {
	$('#filters a').click(function(){
		var selector = $(this).attr('rel');
		$(strCssContainer+' #isotope').isotope({
				filter: selector,
			  animationEngine:'best-available',
			  animationOptions: {
					duration: 250,
					easing: 'linear',
					queue: false
				}
			});
		return false;
	});
}

function fncHashChange() {
	// get vertical scroll
	intVerticalScroll = fncGetScrollOffset();
	// post hash parse
	strAction = location.hash.replace(strHashString,'');
	//alert(strAction);
	var strActionArray = strAction.split("/");
	// if no action set action is homepage
	if (strAction == '') {
		strAction = cnsDefaultHomepageJs;
		strActionArray[0] = cnsDefaultHomepageJs;
	}
	// load php
	$(strCssContainer).load('./null.php?action='+strAction, function() { fncPostprocessContainer(strCssContainer); });
	// postprocess hmenu
	$("#hmenu li").each(function () {
		// is action || contains base action
		if (($(this).find('a').attr('href') == '/'+strHashString+strAction) || ($(this).find('a').attr('href').indexOf(strActionArray[0]) != -1)){
			$(this).addClass('selected');
		} else {
			$(this).removeClass('selected');
		};
	});
	// colorbox backspace fix
	$('div#cboxClose:visible').click();
}

function fncBreadcrumb() {
	strAction = location.hash.replace(strHashString,'');
	var strActionFirst = strAction.split("/");
	//alert(strActionFirst);
}

/*function fncTipsy(strCssContainer) {
  //$('#example-1').tipsy({gravity: 's', fade: true});
  //$('#qr-1').tipsy({gravity: 's', delayOut: 0});
  //$('#qr-2').tipsy({gravity: 's', delayOut: 0});
  //$('#qr-3').tipsy({gravity: 's', delayOut: 0});
  //$('.blocktable').tipsy({gravity: 'w', delayOut: 0});
  alert("test");
}*/

function fncSlides(strCssContainer, strSubContainer, intPlaySpeed, bolHoverPause) {
	$(strCssContainer+strSubContainer).slides({
		//$(strCssContainer+" .slidescontainer").slides({
		//width: 1000,
		//height: 420,
		//preload: true,
		//preloadImage: './img/slides/loading.gif',
		container: 'slidescontainer',
		//container: 'slidesingle',
		//generateNextPrev: true,
		next: 'next',
		prev: 'prev',
		//pagination: true,
		pagination: false,
		//generatePagination: true,
		generatePagination: false,
		//paginationClass: 'pagination',
		fadeSpeed: 250,
		//slideSpeed: 1000,
		start: 1,
		//effect: 'slide, fade',
		effect: 'fade',
		//crossfade: true,
		randomize: false,
		play: intPlaySpeed,
		pause: 1000,
		hoverPause: bolHoverPause,
		//scrolling: false,
		preloading: true,
		//autoHeight: true,
		//autoHeightSpeed: 350,
		//bigTarget: true,
		animationStart: function(){
		},
		animationComplete: function(){
		}
	});
}

function fncColorBox(strCssContainer) {
	$(strCssContainer+' .colorbox').colorbox({
	transition: 'fade',
	speed: 50,
	//width: '50%',
	//height: '70%',
	maxWidth: '100%',
	maxHeight: '90%',
	photo: true,
	opacity: 0.85,
	//preloading: true,
	slideshow: true,
	slideshowSpeed: 2500,
	slideshowAuto: false,
	slideshowStart: 'přehrát',
	slideshowStop: 'zastavit',
	current: 'obrázek {current} | {total}',
	scrolling: false,	
	previous: 'předchozí',
	next: 'další',
	close: 'zavřít'
	});
	//$(strCssContainer+' .colorbox').colorbox.resize();
}

function fncGetScrollOffset() {
  var scrOfX = 0, scrOfY = 0;
  if(typeof(window.pageYOffset) == 'number' ){
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if(document.body && (document.body.scrollLeft || document.body.scrollTop)) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if(document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return scrOfY;
}

// Generic ROT-n algorithm for keycodes in MAP
function fncRotN(strInput, strMap) {
  var strRotTemp = new String()
  var i, j, c, len = strMap.length
  for(i = 0; i < strInput.length; i++) {
    c = strInput.charAt(i)
    j = strMap.indexOf(c)
    if (j >= 0) {
      c = strMap.charAt((j + len / 2) % len)
    }
    strRotTemp = strRotTemp + c
  }
  return strRotTemp;
}

// ROT47 decode algorithm for ASCII map
function fncRot47(strInput) {
	var strOutput = new String()
  strOutput = fncRotN(strInput,"!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~")
  return strOutput;
}

function fncDecodeMailLink(strCssContainer) {
$(strCssContainer+' .rotemail').each(function () {
	$(this).text(fncRot47($(this).text()));
	$(this).attr('href', fncRot47($(this).attr('href')));
	});
}

function fncDecodeQrCode(strCssContainer) {
$(strCssContainer+' .qrcode').each(function () {
	$(this).text(draw_qrcode($(this).text()));
	});
}

