var $jQ = jQuery.noConflict();
var fe = new FormElements();
var showPremiumInfo = true;

function loginUserFB(_pic_big) {

	$jQ.ajax({
		   type: "POST",
		   url: "./act/check_login.act.php",
		   data: "act=login_fb&id_login=fb&id_password=fb&pic_big="+_pic_big+"",
		   success: function(msg){
				
				if (msg == '') {
					//alert('Brak odpowiedzi!'); 
					window.location.reload();
				} else if (msg != 'ok') { 
					alert('Błąd'+msg); 
				} else { 
					window.location.reload();
				} 
				     
		}
	 });
}

function FBundo() {
	$jQ.ajax({
		   type: "POST",
		   url: "./act/check_login.act.php",
		   data: "act=fb_undo",
		   success: function(msg){
				
				if (msg == '') {
					//alert('Brak odpowiedzi!'); 
					window.location.reload();
				} else if (msg != 'ok') { 
					alert('Błąd'+msg); 
				} else { 
					window.location.reload();
				} 
				     
		}
	 });
}

function popup(_owner, _text) {
	if (!document.createElement || document.getElementById('popup')) 
		return;
	
	var pop = document.createElement('div');
	pop.id = 'popup';
	pop.className = 'info-popup';
	pop.style.zIndex = 100;
	pop.style.position = 'absolute';
	pop.style.padding = '1px 5px 1px 5px';
	pop.style.fontSize = '12px';
	pop.style.color = '#4a4a4a';
	pop.style.border = '1px solid #bebebe';
	pop.style.backgroundColor = '#fefefe';
	pop.style.borderTransparent = '0';
	pop.style.fontFamily = 'Georgia';
	pop.style.fontStyle = 'italic';
	pop.style.MozOpacity = '0.92';
	pop.style.align = 'left';

	pop.style.left = getX(_owner)+"px";
	pop.style.top = parseInt(getY(_owner)-21)+"px";
	
	pop.innerHTML = _text;
	document.body.appendChild(pop);
	
	var arrow = document.createElement('div');
	arrow.id = 'arrow';
	arrow.className = 'info-arrow';
	arrow.style.zIndex = 100;
	arrow.style.position = 'absolute';
	arrow.style.padding = '0';
	arrow.style.align = 'left';
	arrow.style.height = '5px'
	arrow.style.top = parseInt(getY(_owner)-3)+'px';
	arrow.style.left = getX(_owner)+'px';
	arrow.innerHTML += "<img src='images/triangle.gif' style='position: absolute; left: 10px; top: 0px'>";
	document.body.appendChild(arrow);
		
	pop.hide = function(p) {
		if (p = document.getElementById('popup')) 
			document.body.removeChild(p);
		if (a = document.getElementById('arrow')) 
			document.body.removeChild(a);
	}
	
	_owner.onmousedown = pop.hide;
	_owner.onmouseout = pop.hide;
}

function settingsShowHide() {
	if ($jQ('#settings-hidden').is(':visible')) {
		$jQ('#settings-hidden').fadeOut('fast', function () {
			$jQ('#settings-visible').slideDown('fast', function () {
				if (showPremiumInfo) {
					premiumInfoShow();
				}
			});			
		});
	} else {
		$jQ('html, body').animate({scrollTop:0}, 'fast', function () {
			if (document.getElementById('premium-info')) {
				premiumInfoHide(function () {
					$jQ('#settings-visible').slideUp('fast', function () {
						$jQ('#settings-hidden').fadeIn('fast');
					});						
				});
			} else {
				$jQ('#settings-visible').slideUp('fast', function () {
					$jQ('#settings-hidden').fadeIn('fast');
				});		
			}
		});
	}
}

function pakietShow(_id) {
	$jQ('#show-' + _id).hide();		
	$jQ('#hide-' + _id).show();
	$jQ('#pakiet-' + _id).slideDown('fast');
}

function pakietHide(_id) {
	$jQ('#show-' + _id).show();		
	$jQ('#hide-' + _id).hide();
	$jQ('#pakiet-' + _id).slideUp('fast');
}

function directionChange() {
	if (currentDirection != 'pl-en') {
		currentDirection = 'pl-en';
		$jQ('#flags').fadeOut(75, function () {
			$jQ('#flags').html('<span class="flag-pl"></span><span class="arrow-translation"></span><span class="flag-en"></span>');
			AjaxRequest('./act/change_direction.act.php?dir='+currentDirection, function () {}, function () {});
			$jQ('#flags').fadeIn(75);
		});
	} else {
		currentDirection = 'en-pl';		
		$jQ('#flags').fadeOut(75, function () {
			$jQ('#flags').html('<span class="flag-en"></span><span class="arrow-translation"></span><span class="flag-pl"></span>');
			AjaxRequest('./act/change_direction.act.php?dir='+currentDirection, function () {}, function () {});
			$jQ('#flags').fadeIn(75);
		});
	}
}

var searchListVisible = false;

function searchListShow() {
	var list = '';
	if (searchHistory.length > 0) {
		for (var i = 0; i < searchHistory.length; i++) {
			list += '<a href="szukaj.html?search='+searchHistory[i]+'" class="search-list-link">'+searchHistory[i]+'</a><br/>';
		}
	} else {
		list += 'Wpisz słowo lub wyrażenie, którego chcesz szukać w słowniku...';		
	}
	
	$jQ('body').append(
		$jQ('<div />')
			.attr({'id': 'search-list'})
			.addClass('search-list')
			.offset({
				top: $jQ('#search-input').offset().top+30, 
				left: $jQ('#search-input').offset().left
			})
	);

	$jQ('#search-list').html(list);
	
	$jQ('#search-list').slideDown(100, function () {
		searchListVisible = true;
	});
}

function premiumInfoShow() {
	$jQ('#settings-visible').append(
		$jQ('<div />')
			.attr({'id': 'premium-info'})
			.addClass('premium-info')
			.offset({
				top: $jQ('#settings').offset().top+115, 
				left: $jQ('#settings').offset().left+80
			})
	);

	$jQ('#premium-info').append(
		$jQ('<div />')
			.attr({'id': 'premium-info-close'})
			.addClass('premium-info-close')
			.offset({
				top: 3, 
				left: 373
			})
	);
	
	$jQ('#premium-info').append(
			$jQ('<div />')
				.attr({'id': 'buy-premium-button'})
				.addClass('buy-premium-button')
				.offset({
					top: 120, 
					left: 115
				})
		);

	$jQ('#premium-info-close').click(function () {
		AjaxRequest('./act/cancelPremium.act.php', function () {}, function () {});
		premiumInfoHide(function () { });
		showPremiumInfo = false;
	});

	$jQ('#buy-premium-button').click(function () {
		window.location = 'wykup-dostep-pro.html';
	});
	
	$jQ('#premium-info').fadeIn('fast');	
}

function premiumInfoHide(_callback) {
	$jQ('#premium-info').fadeOut('fast', function () {
		$jQ('#premium-info').remove();
		_callback();
	});
}

$jQ(document).ready(function() {
	if (translate) {
		executeSearch();
	}
	
	fe.ReplaceCheckbox('replace');
		
	$jQ("a.ajax").fancybox({
		'titleShow'     : false,
		'transitionIn'	: 'fade',
		'transitionOut'	: 'fade',
		'overlayColor'	: '#000',
		'overlayOpacity': 	0.7,
		'speedIn'		:	350, 
		'speedOut'		:	250,
		'scrolling'		: 'no'
	});

	$jQ("form.ajax").submit(function() {
		$jQ.ajax({
			type: 'POST',
			cache: false,
			url: $jQ(this).attr('action'),
			data: $jQ(this).serializeArray(),
			success: function(data) {
				$jQ.fancybox(data, {
					'titleShow'     : false,
					'transitionIn'	: 'fade',
					'transitionOut'	: 'fade',
					'overlayColor'	: '#000',
					'overlayOpacity': 	0.7,
					'speedIn'		:	350, 
					'speedOut'		:	250,
					'scrolling'		: 'no'
				});
			}
		})
		return false
    });	
});

var clicked = false;
var deleteSearchText = true;

function clearSearchText() {
	if (deleteSearchText) {
		deleteSearchText = false;
		$jQ('#search-text').val('');
	}
}

$jQ(document).keydown(function (_event) {
	if (!clicked) {
		if (_event.keyCode != 13) {
			$jQ('#search-text').focus();
			clicked = true;
		}
	} else {
		if (_event.keyCode == 13) {
			executeSearch();
		}
	}
});

$jQ(document).click(function() {
	clicked = true;
	if (searchListVisible) {
		$jQ('#search-list').slideUp(100, function () {
			$jQ('#search-list').remove();
			searchListVisible = false;
		});		
	}
});

function executeSearch() {
	if ((!deleteSearchText || translate) && $jQ('#search-text').val()) {
		var actual_url = window.location.href; 
		var search_page = new RegExp('szukaj');
		var search = $jQ('#search-text').val();
		if (actual_url.search(search_page) < 0) {
			window.location = 'szukaj.html?search='+search;
		} else {
			execute(search);
		}
	}
}


/** Funkcje zapisania do sesji informacji o aktualnie wybranych słownikach*/
function storeAdvDictState(_str, toggle) { 
	
	AjaxRequest('./act/change_settings.act.php?act=store_dict&str='+_str, function () {}, function () {});
	if (toggle)
		settingsShowHide();
}



function selectAllDicts(_cat, _start) {
	if ($jQ('#'+_cat).is(':checked')) {
		var i = _start;

		while ($jQ('#'+_cat+'-'+i).is('input')) {
			var checkbox = document.getElementById(_cat+'-'+i);
			if (!checkbox.checked) {
				checkbox.previousSibling.onclick();
			}
			var j = 0;
			while ($jQ('#'+_cat+'-'+i+'-'+j).is('input')) {
				checkbox = document.getElementById(_cat+'-'+i+'-'+j);
				if (!checkbox.checked) {
					checkbox.previousSibling.onclick();
				}
				//storeAdvDictState(dicts[_cat][i][j],1);
				j++;
			}
			i++;
		}
	} else {
		var i = _start;
		while ($jQ('#'+_cat+'-'+i).is('input')) {
			var checkbox = document.getElementById(_cat+'-'+i);
			if (checkbox.checked) {
				checkbox.previousSibling.onclick();
			}
			var j = 0;
			while ($jQ('#'+_cat+'-'+i+'-'+j).is('input')) {
				checkbox = document.getElementById(_cat+'-'+i+'-'+j);
				if (checkbox.checked) {
					checkbox.previousSibling.onclick();
				}
				//storeAdvDictState(dicts[_cat][i][j],0);
				j++;
			}
			i++;
		}		
	}
}

function subSelectAllDicts(_cat, _sub) {
	if ($jQ('#'+_cat+'-'+_sub).is(':checked')) {
		var j = 0;
		while ($jQ('#'+_cat+'-'+_sub+'-'+j).is('input')) {
			var checkbox = document.getElementById(_cat+'-'+_sub+'-'+j);
			if (!checkbox.checked) {
				checkbox.previousSibling.onclick();
			}
			j++;
		}
	} else {
		var j = 0;
		while ($jQ('#'+_cat+'-'+_sub+'-'+j).is('input')) {
			var checkbox = document.getElementById(_cat+'-'+_sub+'-'+j);
			if (checkbox.checked) {
				checkbox.previousSibling.onclick();
			}
			j++;
		}
	}
}

function settingsSave(toggle) {
	if (dictList) {
		var inputs = document.getElementsByTagName('input');
		for (i = 0; i < inputs.length; i++) {
			if (inputs[i].type == 'checkbox') {
				for (j = 0; j < dictList.length; j++) {
					if (inputs[i].name == dictList[j][0]) {
						dictList[j][4] = inputs[i].checked;
					}
				}
			}
		}
	}
	if (segmList) {
		var inputs = document.getElementsByTagName('input');
		for (i = 0; i < inputs.length; i++) {
			if (inputs[i].type == 'checkbox') {
				for (j = 0; j < segmList.length; j++) {
					if (inputs[i].name == segmList[j][0]) {
						segmList[j][4] = inputs[i].checked;
					}
				}
			}
		}
	}
	
	var cat=0;	
	var str='';	
	var i=1;
	for (_cat=1; _cat<4; _cat++) {
		while ($jQ('#'+_cat+'-'+i).is('input')) {
			
			var checkbox = document.getElementById(_cat+'-'+i);
			if (checkbox.checked) {
			//	checkbox.previousSibling.onclick();
			} 
			var j = 0;
			
			while ($jQ('#'+_cat+'-'+i+'-'+j).is('input')) {
				
				checkbox = document.getElementById(_cat+'-'+i+'-'+j);
				
				if (checkbox.checked) {
					str=str+dicts[_cat+''+i+''+j]+":1;";
				} else {
					str=str+dicts[_cat+''+i+''+j]+":0;";
				}
				
				
				j++;
			}
			i++;
		}
	}
	storeAdvDictState(str, toggle);
}

function getY(element) {
	var iReturnValue = 0;
	while(element != null) {
		iReturnValue += element.offsetTop;
		element = element.offsetParent;
	}
	return iReturnValue;
}

function getX(element) {
	var iReturnValue = 0;
	while(element != null) {
		iReturnValue += element.offsetLeft;
		element = element.offsetParent;
	}
	return iReturnValue;
}

