$(document).ready(function() {
	
	$('input.innerLabel, textarea.innerLabel').each(function() {
		$(this).attr('rel', $(this).val());
	}).focus(function() {
		if ($(this).val() == $(this).attr('rel'))
			$(this).val('');
	}).blur(function() {
		if ($(this).val() == '')
			$(this).val($(this).attr('rel'));
	});
	
	$('#mainnav div.panes > div:first').show();
	
	$('#breadcrumbs a').append('<span class="helper helper1"></span><span class="helper helper2"></span>');
	$('#breadcrumbs a').last().addClass('active');
	
	$('div.ratingChart').sparkline('html', {
		type: 'pie',
		width: '67px',
		height: '67px',
		sliceColors: ['#5ba333', '#c91915']
	});
	
	var _cookie = 'ezyczenia.fontSize';
	$('div.boxSort div.fontSize a').click(function() {
		$(this).parent().find('a').removeClass('active');
		$('div.boxWishes .text').removeClass('small medium big').addClass($(this).attr('class'));
		$.cookie(_cookie, $(this).attr('class'), { expires: 7, path: '/' });
		$(this).addClass('active');
		return false;
	});
	if($.cookie(_cookie) !== null) {
		$('div.boxWishes .text').removeClass('small medium big').addClass($.cookie(_cookie));
		$('div.boxSort div.fontSize a').removeClass('active');
		$('div.boxSort div.fontSize a.' + $.cookie(_cookie)).addClass('active');
	}
//	console.log('cookie: '+$.cookie(_cookie));
	
	$('.categories_more').next().click(function() {
		$(this).prev().show();
		$(this).remove();
	});
});

function updateSelectedOptions() {
	opt = '';
	$('div.categoriesTree div.label.selected').each(function() {
		opt = opt + $(this).attr('rel') + ',';
	});
	opt = opt.substr(0, (opt.length-1));
	$('#selectedOptions').val(opt);
}

Cufon.replace('h1, h2, div.boxOurSites a, div.boxCode p, p.copyright, .tocufon');
/*
 * WINDOWS
 */
function show_login_window() {
	$.get('/base/load_window/login', function(data) {
		$('body').append(data);
		var height = $(document).height();
		var width = $(document).width();
		var window_height = $('#login-window').height();
		var window_width = $('#login-window').width();
		$('#window-background').css('height', height+'px').css('width', width+'px').fadeTo(400, 0.8, function(){
			}).click(function(){
				close_window('login-window');
			});
		$('#login-window').css('margin-left', -(window_width/2) + "px").fadeIn();
		$('#username').focus();
		$('#login-window').draggable({ handle: ".window-header" });
	});
}

function show_add_window() {
	$.get('/base/load_window/add', function(data) {
		$('body').append(data);
		var height = $(document).height();
		var width = $(document).width();
		var window_height = $('#add-window').height();
		var window_width = $('#add-window').width();
		$('#window-add-categories').html('');
		$('#window-add-categories').prev().find('span').html('&nbsp;');
		$('#window-background').css('height', height+'px').css('width', width+'px').fadeTo(400, 0.8, function(){
			}).click(function(){
				close_window('add-window');
			});
		$('#add-window').css('margin-left', -(window_width/2) + "px").fadeIn();
		//getcategories();
		$('#add-window').draggable({ handle: ".window-header" });
	});
}

function show_qr_window(id) {
	var lang = $('#lang_' + id).val();
	$.get('/base/load_window/qr/'+id, function(data) {
		$('body').append(data);
		var height = $(document).height();
		var width = $(document).width();
		var window_height = $('#qr-window').height();
		var window_width = $('#qr-window').width();
		var scroll_top = $(window).scrollTop();
		$('#qr-window').css('top', 50 + scroll_top + 'px');
		var img_src = $('#qr-window .window-content .big-qr').attr('src');
		img_src = img_src.replace(/\_[a-z]{2}_big.png$/,'_' + lang + '_big.png');
		$('#qr-window .window-content .big-qr').attr('src',img_src);
		$('#window-background').css('height', height+'px').css('width', width+'px').fadeTo(400, 0.8, function(){
		}).click(function(){
			close_window('qr-window');
		});
		$('#qr-window').css('margin-left', -(window_width/2) + "px").fadeIn();
		$('#qr-window').draggable({ handle: ".window-header" });
	});
}

function show_info_window(title, contents) {
	$.get('/base/load_window/info', function(data) {
		$('body').append(data);
		var height = $(document).height();
		var width = $(document).width();
		var window_height = $('#info-window').height();
		var window_width = $('#info-window').width();
		$('#info-window .window-title h2').html(title);
		$('#info-window .content').html(contents);
		
		$('#window-background').css('height', height+'px').css('width', width+'px').fadeTo(400, 0.8, function(){
			}).click(function(){
				close_window('info-window');
			});
		$('#info-window').css('margin-left', -(window_width/2) + "px").fadeIn();
		$('#info-window').draggable({ handle: ".window-header" });
	});
}

function close_window(id) {
	$('#'+id).fadeOut(function(){
		$('#window-background').hide();
		$('#'+id).remove();
	});
	
}


function card_add_vote(vote, card_id) {
	$.getJSON('/ajax/card/add_vote/'+card_id+'/'+vote, function(data) {
		$('#card_'+card_id).find('.ratingChart').next().next().html(data.info);
		$('#card_'+card_id+' .ratingChart').html('').html(data.points+','+data.negative_points).sparkline('html', {
			type: 'pie',
			width: '67px',
			height: '67px',
			sliceColors: ['#5ba333', '#c91915']
		});
		$('#card_'+card_id+' .ratingChart').prev().html('+' + data.points);
		$('#card_'+card_id+' .ratingChart').next().html('-' + data.negative_points);
	});
}


// zmiana wersji językowej życzeń
function change_language(wish_id, lang) {
	$('#lang_' + wish_id).val(lang);
	$.get("/ajax/wish/get_wish/" + wish_id + "/" + lang, function(data) {
		$('#wish_' + wish_id + " div.text > p > span").html(data);
		$('#wish_' + wish_id + "> .header > div.languages ul li").removeClass('active');
		$('#wish_' + wish_id + "> .header > div.languages ul li." + lang).addClass('active');
		var img_src = $('#wish_' + wish_id + ' div.qrCode > img').attr('src');
		console.log(img_src);
		img_src = img_src.replace(/\_[a-z]{2}.png$/,'_' + lang + '.png');
		console.log(img_src);
		$('#wish_' + wish_id + ' div.qrCode > img').attr('src', img_src);
		});
}

/*
 * COPYRIGHT
 */
function addLink() {
	var body_element = document.getElementsByTagName('body')[0];
	var selection, href_attr = [];
	selection = window.getSelection();
	if(/([0-9]+\.html){1}$/.test(document.location.href)) {
		href_attr = /^(.*[,a-z-]+)\/([0-9]+\.html)?$/.exec(document.location.href);
	} else {
		href_attr = /^(.*[,a-z-]+)\/?$/.exec(document.location.href);
	}

	var category_url = $('#category_url').val();
	var a_text = "Życzenia świąteczne";
	if(category_url !== '') {
		var category = /^\/?([A-Za-z/-]+)/.exec(category_url)[1];
		var el;
		if ($('#categories-list li[rel="' + category + '"] > a').length == 0) {
			el = $('#categories-list li[rel="' + category + '"] > div');
			a_text = el.attr('title');
		} else {
			el = $('#categories-list li[rel="' + category + '"] > a');
			a_text = el.attr('title');
		}
	}
	
	var pagelink = "[nl][nl]Źródło: "+href_attr[1]+"/[nl]Najlepsze &lt;a href=\""+href_attr[1]+"/\" title=\"e-Zyczenia.pl\"&gt;"+a_text.toLowerCase()+"&lt;\/a&gt;"; // change this if you want
	selection = selection.toString().replace(/\n/g,"[nl]");

	var _el = document.createElement('textarea');
	_el.style.position='absolute';
	_el.style.left='-99999px';
	console.log("before_append");
	body_element.appendChild(_el);
	console.log("after_append");
	var copytext = selection +'[nl]'+ pagelink;
	copytext = copytext.replace(/\[nl\]/g,"\n");
	_el.innerHTML = copytext; 
	//_el.innerHTML += pagelink;
//	var _div = document.getElementById('test');
	selection = window.getSelection();
	selection.selectAllChildren(_el);
	window.setTimeout(function() {
		body_element.removeChild(_el);
	},100);
}

document.oncopy = addLink;

