/**
 * zaplata.bg common functions and constants
 *  
 * @package zaplata
 * @category script
 * @version 20110804
 */

var BG_ID = 28;
function timeFilter(select) {
	document.location = document.getElementById('filterForm').action + '&time=' + select.value; 
}
function setCookie(name, value, days, path, domain, secure){
	var today = new Date();
	today.setTime(today.getTime());
	var expires_date = new Date(today.getTime() + (days*24*60*60*1000));
	document.cookie = name + "=" + escape(value) +
	((days) ? ";expires=" + expires_date.toGMTString() : "") +
	((path) ? ";path=" + path : "") +
	((domain) ? ";domain=" + domain : "") +
	((secure) ? ";secure" : "");
}
function closead(adname) {
	document.getElementById(adname).style.display = 'none';
	setCookie('hide' + adname, 1, 2, '/');
	return false;
}
function closeFixedBanner() {
	document.getElementById("fixedBanner").style.display = "none";
	setCookie('fbanner', 1, 0.25);
}

$(function() {
	$('#subscribeForm').submit(function () {
		$('#subscribeFormErr').hide();
		$('#subscribeFormInfo').hide();
		$.ajax({
			url: 'subscribe.php',
			type: $(this).attr('method'),
			dataType: 'json',
			data: $(this).serialize(),
			success: function (json) {
				if (json.error) {
					$('#subscribeFormErr').text(json.error).show();
				}
				else if (json.done) {
					$('#subscribeFormInfo').text(json.done).show();
					$('#subscribeForm').hide();
				}
			}
		});
		return false;
	});

	$('a.poplight').live('click', function() {
		var width, popMargTop, popMargLeft, popID = $(this).attr('rel'), href = $(this).attr('href'), redirect_uri;
		popID = popID.split('?');

		width = (popID[1] == undefined) ? 460 : popID[1];
		popID = popID[0];
		
		if (href) {
			redirect_uri = href.split('redirect_uri=');
			if (redirect_uri[1] != undefined) {
				href = redirect_uri[0] + 'redirect_uri=http://' + redirect_uri[1].split('%2F')[2] + '/reload.php';
			}
			$('#' + popID).html('<iframe width="' + width + '" frameborder="0" scrolling="no" height="260" src="' + href + '"></iframe>');
		}

		$('#' + popID).fadeIn().css({'width': width}).prepend('<a href="#" class="close"><img src="/img/close_pop.png" class="btn_close" title="Затвори" alt="Затвори" /></a>');

		popMargTop = ($('#' + popID).height() + 80) / 2;
		popMargLeft = ($('#' + popID).width() + 80) / 2;

		$('#' + popID).css({'margin-top' : -popMargTop, 'margin-left' : -popMargLeft});

		$('body').append('<div id="fade"></div>');
		$('#fade').css({'filter' : 'alpha(opacity=70)'}).fadeIn();

		if ($('#' + popID + ' textarea').length){$('#' + popID + ' textarea').focus();}

		return false;
	});

	$('a.close, #fade, .lightbox_cancel').live('click', function() {
		$('.lightbox_div').fadeOut();

		$('#fade , .popup_block').fadeOut(function() {
			$('#fade, a.close').remove();
		});

		return false;
	});
});

