/**
 * @author Michal Lipek
 */


$(document).ready(function() {
	$('.zakladki').gsTabs();

	$("#search.delete")[0].defaultValue = $("#search.delete").val();
	$("#search.delete").blur(function() {
		if (this.value=='') this.value = this.defaultValue;
	});
	$("#search.delete").focus(function() {
		if (this.value == this.defaultValue) this.value = '';
	});
	
	tabControll($('#people_tab_controll'), $('#people_tab'));
	
	tabControll($('#profile_tab_controll'), $('#profile_tab'));
	tabControll($('#portfolio_tab_controll'), $('#portfolio_tab'));
	
	tabControll($('.simpleTabsControll'), $('.simpleTabs'));
	tabControll($('.simpleTabsControll2'), $('.simpleTabs2'));
	tabControll($('.simpleTabsControll3'), $('.simpleTabs3'));
	//tabControll($('.simpleTabsControll2'), $('.simpleTabs2', $('.simpleTabsControll2').parent()));
	tabControll($('.simpleTabsControll3'), $('.simpleTabs3', $('.simpleTabsControll3').parent()));
	
	tabControll($('.mainTabsCtrl'), $('.mainTabs', $('.mainTabsCtrl').parent()));
	tabControll($('.mainTabsCtrl2'), $('.mainTabs2', $('.mainTabsCtrl2').parent()));
	tabControll($('.mainTabsCtrl3'), $('.mainTabs3', $('.mainTabsCtrl3').parent()));
	tabControll($('.mainTabsCtrl4'), $('.mainTabs4', $('.mainTabsCtrl4').parent()));
	tabControll($('.mainTabsCtrl5'), $('.mainTabs5', $('.mainTabsCtrl5').parent()));
	tabControll($('.mainTabsCtrl6'), $('.mainTabs6', $('.mainTabsCtrl6').parent()));
	
	tabControll($('#forum_header_zakladki'), $('#forum_tabs'));	
	
	hash = document.location.hash;
	if (hash.indexOf('/')) {
		splited = hash.split('/');
		id = splited[0];
		num = parseInt(splited[1]);
		$(' > span  > a', $(id));
		$(' > span  > a:eq('+(num-1)+')', $('#forum_header_zakladki')).addClass('set_active');
	}
	
	$('.set_active').click();
	
	
	$('.rate.active a').hover(function() {
		var a = $(this);
		var b = a;
		do {
			a.addClass('on');
			a.removeClass('off');
			a = a.prev();
		}
		while (a.length > 0);
		
		b = b.next();
		while (b.length > 0) {
			b.removeClass('on');
			b.addClass('off');
			b = b.next();
		}
	}, function() {
		$('> a', $(this).parent()).removeClass('on');
		$('> a', $(this).parent()).removeClass('off');
	});
	
	
	submitizeAnchors();
	
	$('#company').autocomplete(APPLICATION_URL + "ajax/company/", {
		minChars: 3,
		width: 260,
		selectFirst: false
	});
	
	
	$('#bbcodeHelp').click(function(e) {
		var posx = 0;
		var posy = 0;
		if (e.pageX || e.pageY) {
			posx = e.pageX;
			posy = e.pageY;
		}
		else if (e.clientX || e.clientY) {
			posx = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
			posy = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
		}
		
		url = APPLICATION_URL + 'files/html/bbcode.html';
		$('body').append($('<div id="bbcode>').addClass("loader").css('top', (posy-300)+'px').css('left', posx+'px').css('height', '300px').load(url, {}, function() {
			$('#closeBBCode').click(function() {
				$('#bbcode').remove();
			});
			$('#bbcode').removeClass('loader');
		}));
	});

	
	$('a.lightbox').lightbox({fitToScreen: true, fileLoadingImage: APPLICATION_URL + 'files/img/loader2.gif'});

	$('.rozwin').click(function() {
		$(this).hide();
		$('.rozwinZwinWrap .zwin').show();
		$('.news_content').slideDown('fast');
	});
	$('.zwin').click(function() {
		$(this).hide();
		$('.rozwinZwinWrap .rozwin').show();
		$('.news_content').slideUp('fast');
	});
	
	// target do stron zewnetrznych
	$('a[@href^=http]').not('a[@href^=http://kruszwica.dev.goldensubmarine.com/]').attr('target', '_blank');
	
	$('.portfolioFileNew').click(addPortfolioFile);
	$('.portfolioFileDelete').click(removePortfolioFile);
	
	$('.portfolioDeleteHidden').click(createPortfolioDeleteHidden);
	
	$('.deleteButton').click(function() {
		return confirm('Czy na pewno chcesz usunąć ten wpis?');
	});
});


// omg, ale nazwa
function submitizeAnchors() {
	$('input + a.button').click(function() {
		$(this).prev().click();
	});
}


function tabControll(controll, tabs) {
	$('a', controll).click(function() {
		var anchors = $('a', controll);
		var index = -1;
		
		for (var i = 0; i < anchors.length; i++) {
			if (anchors[i] == this) {
				index = i;
			}
		}
		if (index >= 0) {
			$('> div.tab', tabs).hide();
			$('> div.tab:eq('+(index)+')', tabs).show();
		}
	});
	
	$('> div.tab', tabs).hide();
	$('> div.tab:first-child', tabs).show();
}


function setActionAndSubmit(form, action) {
	form.action = action;
	form.submit();
}



function selectCheckboxes(element) {
	$('input[@type=checkbox]', element).each(function() {
		this.checked = !this.checked;
	});
}



function daysInMonth(year, month) {
	var dd = new Date(year, month, 0);
	return dd.getDate();
} 



function insertTopFlash(link, version) {
	var flashvars = {
		'link': link
	};
	swfobject.embedSWF(APPLICATION_URL + "files/swf/top"+version+".swf", "top_banner", "347", "130", "9.0.0", "", flashvars);
}


function insertMapFlash(link) {
	var flashvars = {
		'xmlLink': link
	};
	// /wm_pokemon/files/cache/map.xml
	swfobject.embedSWF(APPLICATION_URL + "files/swf/map_right.swf", "mapa", "200", "200", "9.0.0", "", flashvars);
}


function onABCommComplete() {
	var emails = $('#plaxo_recipients').val().match(/\<([^\s])+\>/g);
	var result = '';
	for (i in emails) {
		email = emails[i].replace('<', '').replace('>', '');
		addMail(email);
	}
}


function addMail(value) {
	value = typeof(value) == 'undefined' ? '' : value;
	firstInput = $('#maillist input:first');
	if (firstInput.val().length || !value) {
		usun = $('<a>').addClass('button').html('Usu&#324;');
		usun.click(function(){
			$(this).parent().remove();
		});
		
		$('#maillist').append(
			$('<p>').append(
				$('<input>').attr('name', 'friend_email[]').css('width', '460px').val(value)
			).append('<span>&nbsp;</span>').append(usun)
		);
	}
	else {
		firstInput.val(value);
	}
}


function removePortfolioFile() {
	$(this).parent().remove();
}


function addPortfolioFile() {
	if (typeof(document.fileId) == "undefined") {
		document.fileId = 0;
	}
	document.fileId++;
	input = $('<input type="file" name="files'+document.fileId+'">');
	anchor = $('<a>').addClass('portfolioFileDelete').addClass('button').html('Usuń');
	span = $('<span>');
	$('#portfolioFileList').append(span.append(input).append(anchor).append($('<br>')));
	span.find('a').click(removePortfolioFile);
}



function createPortfolioDeleteHidden() {
	$form = $('#portfolio_edit');
	$this = $(this);
	$li = $this.parent();
	$li.hide(300);
	id = $this.attr('id').split('_')[1];
	$form.prepend('<input type="hidden" name="removeFile[]" value="'+id+'">');
}


