var clientIE6 = (navigator.userAgent.indexOf("MSIE 6") > -1);
var clientIE8 = (navigator.userAgent.indexOf("MSIE 8.0") > -1);

function showAdvCbo(cbo)
{
	$('div.skinCboList').each(function() {
		if ($(this).attr('id') != cbo.id+'List')
			$(this).hide("fast");
	});

	$('#'+cbo.id+'List').toggle("fast");
	$('#'+cbo.id+'List').css('left', $('#'+cbo.id).position().left+'px');
	$('#'+cbo.id+'List').css('top', ($('#'+cbo.id).position().top+$('#'+cbo.id).outerHeight(true))+'px');
}

function setAdvCbo(id, obj, baseId)
{
	var txt = document.getElementById(baseId);
	var hid = document.getElementById(baseId+'Hid');
	if (!txt || !hid)
		return;

	txt.value = obj.innerHTML;
	hid.value = id;
	$('#'+baseId+'List').toggle("fast");
}

function radClick(id, value, index)
{
	var hid = document.getElementById(id);
	if (!hid)
		return;
	hid.value = value;

	// Tunning for showing keworkd of user type 2
	if (id == 'iduserType') {
		if (value == 1) {
			if (clientIE8) {
				$('#idCategoryTR').show();
				$('#idKeywordsTR').show();
			}
			else {
				$('#idCategoryTR').fadeIn("fast");
				$('#idKeywordsTR').fadeIn("fast");
			}
		}
		else {
			if (clientIE8) {
				$('#idCategoryTR').hide();
				$('#idKeywordsTR').hide();
			}
			else {
				$('#idCategoryTR').fadeOut("fast");
				$('#idKeywordsTR').fadeOut("fast");
			}
		}
	}

	// Cycle other radio elements
	var obj = null;
	for(var i=1; i<100; i++) {
		var obj = document.getElementById(id+'Rad'+i);
		if (!obj)
			break;
		obj.className = (i == index ? obj.className.replace('-off','-on') : obj.className.replace('-on','-off'));
	}
}

function chkClick(id, el)
{
	if (typeof(el) == 'string')
		el = document.getElementById(el);

	var fld = document.getElementById(id);
	fld.value = (parseInt(fld.value)==0 ? 1 : 0);
	el.className =(fld.value==0 ? el.className.replace('on','off') : el.className.replace('off','on') );

	if (id.indexOf('idchkFav') === 0)
		document.location += '&fav=' + id.replace('idchkFav','');
	if (el.id.indexOf('idjobType') === 0)
	{
		var ind = id.substr(id.length-1);
		document.location = filterJobTypeURL[ind] + (filterJobTypeURL[ind].indexOf('?')>-1 ? '&' :'?') + id.substr(2)+'='+fld.value;
	}
}

function textareaMaxLength(obj, maxLen)
{
	return (obj.value.length <= maxLen);
}

function textareaMaxLengthChange(obj, maxLen)
{
	obj.value = obj.value.substring(0, maxLen)
}

function regResult(res, txt)
{
	// Animate and show the message
	$('#idMsgDiv').fadeOut("fast",
		function() {
			document.getElementById('idMsgDiv').className = (res < 1 ? "error" : "success");
			$('#idMsgDiv').html(txt);
			$('#idMsgDiv').fadeIn("fast");
		}
	);

	// If success animate
	if (res == 2)
		$('.section-register table').fadeOut("fast");

	if (res == 1)
		setTimeout(function() {$('#idMsgDiv').fadeOut("fast"); }, 5000);
}

function offerResult(res, txt, oid)
{
	// Animate and show the message
	$('#idMsgDiv').fadeOut("fast",
		function() {
			document.getElementById('idMsgDiv').className = (res < 1 ? "error" : "success");
			$('#idMsgDiv').html(txt);
			$('#idMsgDiv').fadeIn("fast");
		}
	);

	// If success animate
	if (res > 0)
		$('.pageframe-offers').fadeOut("fast", function() {
			goToPage('myoffers&id='+oid);
	});
}

function goToPage(page)
{
	document.location = '?page='+page;
}

function formSubmit()
{
	document.getElementById('idForm').submit();
}

function searchFocus(elem)
{
	if (elem.value == 'ключова дума')
		elem.value = '';
}

function searchBlur(elem)
{
	if (elem.value == '')
		elem.value = 'ключова дума';
}


// Profile

function profileSubmit(actId)
{
	if (actId == 0 || actId == 1)
		$('#uploadImage').uploadifyUpload();
	if (actId == 0 || actId == 2)
		$('#uploadCV').uploadifyUpload();
	return true;
}

// Image / logo

function profileLogoDelete()
{
	if (confirm('Желаете ли да изтриете изображението?')) {
		$.ajax({
			url: 'ajax.php?t=i&dellogo=1',
			success: function(data) {
				eval(data);
				profileLogoClear(null, null, null, {fileCount:0});
			}
		});
	}
}

function profileUploadComplete(event, data)
{
	$.ajax({
		url: 'ajax.php?t=i',
		success: function(data) {
			eval(data);
			profileLogoClear(null, null, null, {fileCount:0});
		}
	});
}

function profileLogoSelect(e, queueID, fileObj)
{
	document.getElementById('uploadImageUploader').style.height = '1px';
	document.getElementById('uploadImageUploader').style.width = '1px';
	$('#idUserLogoUpload').fadeIn("fast");
	return true;
}

function profileLogoClear(e, queueID, fileObj, data)
{
	if (data.fileCount < 1)
	{
		$('#idUserLogoUpload').fadeOut("fast", function() {
			document.getElementById('uploadImageUploader').style.height = '24px';
			document.getElementById('uploadImageUploader').style.width = '176px';
		});
	}
	return true;
}


// CV

function profileCVDelete()
{
	if (confirm('Желаете ли да изтриете биографията си?')) {
		$.ajax({
			url: 'ajax.php?t=c&delcv=1',
			success: function(data) {
				eval(data);
				profileCVClear(null, null, null, {fileCount:0});
			}
		});
	}
}

function profileCVUploadComplete(event, data)
{
	$.ajax({
		url: 'ajax.php?t=c',
		success: function(data) {
			eval(data);
			profileCVClear(null, null, null, {fileCount:0});
		}
	});
}

function profileCVSelect(e, queueID, fileObj)
{
	document.getElementById('uploadCVUploader').style.height = '1px';
	document.getElementById('uploadCVUploader').style.width = '1px';
	$('#idUserCVUpload').fadeIn("fast");
	return true;
}

function profileCVClear(e, queueID, fileObj, data)
{
	if (data.fileCount < 1)
	{
		$('#idUserCVUpload').fadeOut("fast", function() {
			document.getElementById('uploadCVUploader').style.height = '24px';
			document.getElementById('uploadCVUploader').style.width = '176px';
		});
	}
	return true;
}


// Gallery

function initGallery()
{
	// Hover effect
	$('.gallery-list a').mouseleave(function(e){
		$('div', this).fadeOut("fast");
	});
	$('.gallery-list a').mouseenter(function(e){
		$('div', this).fadeIn("fast");
	});

	// Delete function
	$('.gallery-list a div').click(function(){
		if (confirm('Желаете ли да изтриете изображението?'))
			$(this).load('ajax.php?t=d&id='+this.id.replace('idGalImg',''));
		return false;
	});

	// Make Lightbox
	$('.gallery-list a').lightBox({
		imageLoading:	'img/lightbox-ico-loading.gif',
		imageBtnPrev:	'img/lightbox-btn-prev.gif',
		imageBtnNext:	'img/lightbox-btn-next.gif',
		imageBtnClose:	'img/lightbox-btn-close.gif',
		imageBlank:		'img/s.png',
		txtImage:		'Изображение',
		txtOf:			'от'
	});
}

function updateGallery(item)
{
	if (typeof(item) == 'object')
	{
		var newInd = galCurrentInd;
		if (item.href.indexOf('#') > -1)
			newInd = item.href.substr(item.href.indexOf('#')+1, 10);

		if (newInd == 'prev') {
			if (galCurrentInd > 0)
				galCurrentInd --;
		}
		else
		if (newInd == 'next') {
			if (galCurrentInd < (galPages-1))
				galCurrentInd ++;
		}
		else
			galCurrentInd = parseInt(newInd);
	}

	var from = galCurrentInd*galItemsPerPage;
	var to = (galCurrentInd+1)*galItemsPerPage - 1;

	$('.gallery-list a').each(function(index){
		if (index >= from && index <= to)
			$(this).delay(200).fadeIn(200);
		else
			$(this).fadeOut(200);
	});
}


var itemShown = 0;

jQuery.fn.outerHTML = function(s) {
	return ((s) ? this.before(s).remove() : jQuery("<p>").append(this.eq(0).clone()).html());
};

$(document).ready(function() {
	// Swicth to normal top bar
	$('.btn-close').click(function(){
		$('#idHeadMsg').hide();
		$(".forgottohide").fadeIn();

		if (itemShown == 1)
			$('#idLogin').hide('fast');
		if (itemShown == 2)
			$('#idForgot').hide('fast');
		$('#idHeadBox').switchClass('hb-big', 'hb-normal', 'fast');
		$('#idMenu').show('fast');
		itemShown = 0;
		return false;
	});

	// Switch to login form
	$('#idEntr').click(function(){
		$('#idHeadMsg').hide();
		itemShown = 1;
		$('#idMenu').hide('fast');
		$('#idHeadBox').switchClass('hb-normal', 'hb-big', 'fast');
		$('#idLogin').show('fast');
		return false;
	});

	// Swicth to forgotten password from login
	$('#idForgotBtn').click(function(){
		$('#idHeadMsg').hide();
		itemShown = 2;
		$('#idForgot .skinEdit').attr('value', '');
		$('#idLogin').hide('fast');
		$('#idForgot').show('fast');
		$('#idForgot td img').attr('src', 'captcha.php?time='+Date());
		$('#idForgot td img').click(function(){
			this.src = 'captcha.php?time='+Date();
			return false;
		});
		return false;
	});

	// Poll "Vote" button
	$('#idPollVote').click(function(){
		$.post("ajax.php?t=p", { pollanswer: $('#idpoll').val() },
			function(data){
				$('#idPollContent').fadeOut("fast",
					function() {
						$('#idPollContent').html(data);
						$('#idPollContent').fadeIn("fast");
					}
				);
			}
		);
		return false;
	});

	// Login
	$('#idLogin .btn-111').click(function(){
		$.post("ajax.php?t=l", { email: $('#idLEmail').val(), pass: $('#idLPass').val(), rememberMe: $('#idrememberMe').val() },
			function(data){
				if (data == '')
					return;
				else
				if (data == 'OK')
					$('#idLogin').fadeOut("fast", function(){ goToPage('home') } ); /*profile*/
				else {
					$('#idHeadMsg').fadeOut("fast",
						function() {
							$('#idHeadMsg').html(data);
							$('#idHeadMsg').fadeIn("fast");
						}
					);
				}
			}
		);
		return false;
	});

	// Forgot
	$('#idForgot .btn-176').click(function(){
		$.post("ajax.php?t=f", { email: $('#idFEmail').val(), code: $('#idFCode').val() },
			function(data){
				if (data == '')
					return;
				else {
					$('#idHeadMsg').fadeOut("fast",
						function() {
							$('#idHeadMsg').html(data);
							$('#idHeadMsg').fadeIn("fast");
						}
					);
				}
			}
		);
		return false;
	});

	// Check code
	$('.vip-div .btnRed').click(function(){
		$('#idVipDivResult').load('ajax.php?t=s', { check: $('#idCheck').val(), code: $('#idCode').val() } );
	});

	// Fix shitty gray selection
	$('a.boxlink-chosen').each(function(index) {
		if ($(this).height() >= 32 && $(this).height() <= 40) {
			// 2 rows text
			$(this).css('background-position', 'right -24px');
		}
		if ($(this).height() >= 45 ) {
			// 3 rows text
			$(this).css('background-position', 'right -66px');
		}
		if (clientIE6)
		{
			$(this).css('position', 'absolute');
			$(this).css('left', '0px');
			$(this).outerHTML($(this).outerHTML() + '<a href="" class="boxlink" style="height:'+$(this).height()+'px">&nbsp;</a>');
		}
	});

	// Fix shitty box2 background
	$('div.box2, div.box3').each(function(index) {
		if ($(this).height() < 150)
			$(this).css('height', '150px');
	});

	// Aplly
	$('.btn-apply').click(function() {
		$(this).fadeOut("fast", function() {
			document.getElementById('idApplyForm').submit();
		});
	});
});

