$(document).ready(function() {
	csb.init();
	
	// zakaze autovyplnovani policek formulare
	$('input.disable-ac').attr('autocomplete', 'off');
});

var csb = {
	emailsInit : function() {
		// automaticke prirazeni formulare pro odeslani emailu
		$('a[href]').each(function(){
			if ($(this).attr('href').match(/mailto:/)) {
				var mailto = $(this).attr('href').replace('mailto:','');
				$(this).click(function(e){
					e.preventDefault();
					csb.getEmailForm(mailto);
				});
			}
		});
	},
	
	getEmailForm : function(mailto) {
		$.fancybox.showActivity();
		$.ajax({
			type: 'POST',
			url: '/webpages/ajax/sendEmail.php',
			dataType: 'html',
			data: 'mailto=' + URI.encode(mailto),
			success: function(data) {
				$.fancybox.hideActivity();
				
				if (!data) return;
				if (data != '') {
					$.fancybox(data);
					$('.btn-update-captcha').each(function(){
						csb.updateCaptcha($(this));
					});
				}
			}
		});
	},
	
	sendEmailForm : function() {
		$.fancybox.showActivity();
		$.ajax({
			type: 'POST',
			url: '/webpages/ajax/sendEmail.php',
			data: 'firstname=' + URI.encode($('#csb-firstname').val()) + 
			      '&lastname=' + URI.encode($('#csb-lastname').val()) + 
			      '&company=' + URI.encode($('#csb-company').val()) + 
			      '&email=' + URI.encode($('#csb-email').val()) + 
			      '&phone=' + URI.encode($('#csb-phone').val()) + 
			      '&message=' + URI.encode($('#csb-message').val()) + 
			      '&publish=' + ($('#csb-publish').is(':checked') ? 1 : 0) +
				  '&emailCC=' + ($('#csb-emailCC').is(':checked') ? 1 : 0) + 
			      '&mailto=' + URI.encode($('#csb-mailto').val()) + 
			      '&ccode=' + $('#csb-ccode').val() + 
			      '&ckey=' + $('#csb-ckey').val() + 
			      '&token=' + $('#csb-token').val(),
			success: function(data) {
				$.fancybox.hideActivity();
				
				if (!data) return;
				if (data != '') {
					$.fancybox(data);
				}
			}
		});
	},
	
	updateCaptcha : function(obj) {
		var self = $(obj);
		
		$.fancybox.showActivity();
		$.ajax({
			type: 'POST',
			async: false,
			url: '/webpages/ajax/getCaptcha.php',
			dataType: 'text',
			data: 'getNewCkey=',
			success: function(data) {
				$.fancybox.hideActivity();
				
				if (!data) return;
				if (data != '') {
					var id = self.attr('rel');
					$('img.'+id).attr('src','/webpages/ajax/getCaptcha.php?ckey='+data);
					$('input.'+id).val(data);
				}
			}
		});
	},
	
	issuuInit: function() {},
	
	photogalleryInit : function() {
		// editor fotogalerie
		$('a.photogallery').attr('rel','photogallery');
		$('a[rel=photogallery]').fancybox();
		// modul fotogalerie
		$('a[rel=csb-photogallery]').fancybox({
			'transitionIn'   : 'none',
			'transitionOut'  : 'none',
			'titlePosition'  : 'inside',
			'titleFormat'    : function(title, currentArray, currentIndex, currentOpts) {
				return $('#' + currentArray[currentIndex].id + '-description').html();
			}
		});
	},
	
	rollInit : function() {
		$('.roll').mouseover(function(){
			$(this).parent().next('.roll-content').show();
		});
	},
	
	videosInit : function() {
		// editor videa
		$('a.videos').attr('rel','videos');
		$('a[rel=videos]').fancybox();
		// modul videa
		$('a[rel=csb-videos]').fancybox({
			'padding'       : 0,
			'autoScale'     : false,
			'transitionIn'  : 'none',
			'transitionOut' : 'none',
			'title'         : this.title,
			'type'          : 'swf',
			'swf'           : {
				'wmode'             : 'transparent',
				'allowfullscreen'   : 'true'
			}
		});
	},
	
	getPhotos : function(elm) {
		var id = elm.id;
		var path = elm.href.replace(/http:\/\/(.*)\/katalog\//,'');
		var that = this;
		
		// remove action
		$('#'+id).removeAttr('onclick');
		$('#'+id).attr('href','javascript:void(0);');
		
		$.fancybox.showActivity();
		$.ajax({
			type: 'POST',
			url: '/cs/fotogalerie',
			dataType: 'html',
			data: 'categoryPath=' + path,
			success: function(data) {
				$.fancybox.hideActivity();
				
				if (!data) return;
				if (data != '') {
					// create panel
					var panel = document.createElement('div');
					panel.id = 'csb-photos';
					panel.innerHTML = data;
					// add html content
					$('#'+id).parent().after(panel).fadeIn('slow');
					that.photogalleryInit();
					// set toogle action
					$('#'+id).toggle(function() {
						$(panel).hide();
					}, function() {
						$(panel).show();
					});
				}
			}
		});
		return false;
	},
	
	getVideos : function(elm) {
		var id = elm.id;
		var path = elm.href.replace(/http:\/\/([^\/]*)\//,'');
		var that = this;
		
		// remove action
		$('#'+id).removeAttr('onclick');
		$('#'+id).attr('href','javascript:void(0);');
		
		$.fancybox.showActivity();
		$.ajax({
			type: 'POST',
			url: '/cs/videa',
			dataType: 'html',
			data: 'categoryPath=' + path,
			success: function(data) {
				$.fancybox.hideActivity();
				
				if (!data) return;
				if (data != '') {
					// create panel
					var panel = document.createElement('div');
					panel.id = 'csb-videos';
					panel.innerHTML = data;
					// add html content
					$('#'+id).parent().after(panel).fadeIn('slow');
					that.videosInit();
					// set toogle action
					$('#'+id).toggle(function() {
						$(panel).hide();
					}, function() {
						$(panel).show();
					});
				}
			}
		});
		return false;
	},
	
	init : function() {
		this.emailsInit();
		this.issuuInit();
		this.photogalleryInit();
		//this.rollInit();
		this.videosInit();
	}
}

csb.news = {
	interval : 5000,
	timer : null,
	data : new Array(),
	panel : null,
	
	setData : function() {
		var items = $('#csb-news li.slide');
		for (i=0; i < items.length; i++) {
			this.data[i] = $(items[i]).children('p.item').html();
		}
	},
	
	getNavigation : function() {
		var self = this;
		
		var html = '';
		for (i=1; i <= this.data.length; i++) {
			html += '<a ' + (i == 1 ? 'class="active"' : '') + 'href="#" title="Klikněte pro zobrazení více informací">' + i + '</a>';
		}
		$('#news-slider .nav').html(html);
		
		$('#news-slider .nav a').click(function(e){
			e.preventDefault();
			self.slide($(this).index());
		});
	},
	
	slide : function(index) {
		var self = this;
		
		self.panel.stop(true,true);
		clearTimeout(this.timer);
		this.panel.fadeOut('slow',function(){
			if (index >= self.data.length) {
				index = 0;
			}
			
			$('#news-slider .nav a').removeClass('active');
			$('#news-slider .nav a').eq(index).addClass('active');
			
			self.panel.html(self.data[index]);
			self.panel.fadeIn('slow',function(){
				if (self.data.length > 1) {
					self.timer = setTimeout(function(){
						index++;
						self.slide(index);
					}, self.interval);
				}
			});
		});
	},
	
	init : function() {
		if ($('#csb-news li.slide').length) {
			this.setData();
			this.panel = $('#news-slider .preview .in');
			this.panel.html('');
			this.getNavigation();
			this.slide(0);
		}
	}
}

/**
 *  URI encode / decode
 *  http://www.webtoolkit.info/
 */
var URI = {
	// public method for uri encoding
	encode : function (string) {
		return escape(this._utf8_encode(string));
	},
	
	// public method for uri decoding
	decode : function (string) {
		return this._utf8_decode(unescape(string));
	},
	
	// private method for UTF-8 encoding
	_utf8_encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";
		
		for (var n = 0; n < string.length; n++) {
			var c = string.charCodeAt(n);
			
			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}
		}
		return utftext;
	},
	
	// private method for UTF-8 decoding
	_utf8_decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;
		
		while ( i < utftext.length ) {
			c = utftext.charCodeAt(i);
			
			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}
		}
		return string;
	}
};

function getPrintButton(name,title) {
	document.write('<a class="print" href="#" onclick="window.print();" title="'+title+'">'+name+'</a>&nbsp;|&nbsp;');
}

function getHomeButton(name,webname,page_url,title) {
	document.write('&nbsp;|&nbsp;<a class="home" href="#" onclick="external.addfavorite(\''+page_url+'\',\''+webname+'\');return false;" title="'+title+'">'+name+'</a>');
}

