/**
 * Executed when document is finished loading elements into DOM
 */
var pagingInited = false;
var currentUrl = false;
var statusOverlay = false;
var deleteOverlay = false;
var bcDropDown = false;
var fx = 0;

$(document).ready(function()
{
	categoryChange($('#subcategory select'));
	
	alignFirstpage();
	
	$('#bc-dropdown').click(function(){
		if(bcDropDown)
		{
			$('#footer-clearfix').height(fx);
			$(this).find('li ul').hide();
			bcDropDown = false;
		}
		else
		{
			$(document).unbind('click');
			var el = $(this).find('li ul');
			if($(el).width() < $(this).width())
			{
				$(el).width($(this).width());
			}
			
			$(el).show();
			
			bcDropDown = true;
			
			fx = $('#footer-clearfix').height();
			if($('#footer-clearfix').height() + $('#bc-dropdown').next('div').next('div').height() < $(el).height())
			{
				$('#footer-clearfix').height($(el).height() - $('#bc-dropdown').next('div').next('div').height() + fx);
			}
			
			$(this).hover(function(){
				
			},function(){
				$(document).click(function(){
					bcDropDownClose();
				});
			});
		}
	});
	
	$('#bc-dropdown').find('li ul li a').click(function(){
		$('#bc-dropdown > li > a').html($(this).html());
	});
	
});

function bcDropDownClose()
{
	if(bcDropDown)
	{
		$('#footer-clearfix').height(fx);
		$('#bc-dropdown').find('li ul').hide();
		bcDropDown = false;
	}
}

function categoryChange(el)
{
	if($(el).val() > 0)
	{
		if(subcats[$(el).val()] != undefined)
		{
			var h = '';
			$.each(subcats[$(el).val()], function(i, item){
				h += '<option value="' + item.id + '">' + item.title + '</option>'
			});
			
			$('#subcategory select')
				.find('option')
				.remove()
				.end()
				.append(h)
				.attr('disabled', false);
				
			$('#subcategory').show();
			$('#subcategory2').hide();
			$('#type').hide();
		}
	}
	else
	{
		$('#subcategory').hide();
		$('#subcategory2').hide();
		$('#type').hide();
	}

	$('#type select')
		.find('option')
		.remove()
		.end()
		.val('')
		.attr('disabled', true);
	typeChange($('#type select'));
}

function subCategoryChange(el)
{
	if($(el).val() > 0)
	{
		if(subcats[$(el).val()] != undefined)
		{
			var h = '';
			$.each(subcats[$(el).val()], function(i, item){
				h += '<option value="' + item.id + '">' + item.title + '</option>'
			});
			
			$('#subcategory2 select')
				.find('option')
				.remove()
				.end()
				.append(h)
				.attr('disabled', false);
				
			$('#subcategory2').show();
			
			$('#type select')
				.find('option')
				.remove()
				.end()
				.append(h)
				.attr('disabled', false);
			$('#type').hide();
			
			typeChange($('#type select'));
		}
		else
		{
			if(types[$(el).val()] != undefined)
			{
				var h = '';
				$.each(types[$(el).val()], function(i, item){
					h += '<option value="' + item.id + '">' + item.title + '</option>'
				});
				
				$('#type select')
					.find('option')
					.remove()
					.end()
					.append(h)
					.attr('disabled', false);
				
				$('#type').show();
				
				if($(el).parent('div').parent('div').attr('id') != 'subcategory2')
				{
					$('#subcategory2').hide();
				}
				
				typeChange($('#type select'));
			}
			else
			{
				typeChange($(el));
			}
		}
	}
	else
	{
		$('#type select')
			.find('option')
			.remove()
			.end()
			.val('')
			.attr('disabled', true);
		
		$('#type').hide();
		
		if($(el).parent('div').parent('div').attr('id') != 'subcategory2')
		{
			$('#subcategory2 select')
				.find('option')
				.remove()
				.end()
				.attr('disabled', true);
			$('#subcategory2').hide();
		}
		
		typeChange($('#type select'));
	}
}

function typeChange(el)
{
	if($(el).length)
	{
		if($(el).val() > 0)
		{
			$('#AdvertAddForm .submit input').attr('disabled', false).removeClass('disabled');
		}
		else
		{
			$('#AdvertAddForm .submit input').attr('disabled', true).addClass('disabled');
		}
	}
}

function alignFirstpage()
{
	var width = ($(window).width() - 850) / 2;
	$('#fp-banner .banner-container a.browse').width(width);
	
	var navi_pos = width + 850 - ($('#fp-banner .items .item').length * 20) - 20;
	$('#fp-banner .navi').css('left', navi_pos);
	
	$(window).resize(function() {
		var width = ($(window).width() - 850) / 2;
		$('#fp-banner .banner-container a.browse').width(width);
		
		var navi_pos = width + 850 - ($('#fp-banner .navi a').length * 20) - 20;
		$('#fp-banner .navi').css('left', navi_pos);
	});
	
	if($('#fp-adverts, #new-projects').length)
	{
		$('.#fp-adverts .row, #new-projects .row').each(function(){
			var mh = 0;
			var ph = 0;
			
			$(this).find('.info').each(function(){
				var h = $(this).height();
				if(h > mh){ mh = h; }
			});
			$(this).find('.info').height(mh);
			
			$(this).find('.price, .title').each(function(){
				var h = $(this).height();
				if(h > ph){ ph = h; }
			});
			$(this).find('.price, .title').height(ph);
		});
	}
}

/**
 * Print email link (protection from spambots)
 * If javascript is disabled, nothing is visible
 *
 * @param string p1 Part 1 - this is username
 * @param string p2 Part 2 - this is domain and TLD
 */
function writemail(p1, p2)
{
	document.write("<a href='mailto:"+p1+"@"+p2+"'>"+p1+"@"+p2+"</a>");
}

function writephone(p)
{
	document.write(p);
}

(function($){
	$.doAfter = function(time,f) {
		$('head').animate({ opacity: 1 }, time, f);
	};
})(jQuery);

function browserReload() { 
	try { 
		var headElement = document.getElementsByTagName("head")[0]; 
		if (headElement && headElement.innerHTML) 
			headElement.innerHTML += "<meta http-equiv=\"refresh\" content=\"1\">"; 
	} 
	catch (e) {} 
}

function initAjaxPaging(el, href) {
	if(href != false && pagingInited == false)
	{
		pagingInited = true;
		$overlay = $('.loading-overlay');
		$overlay.css('width', $(el).width())
				.css('height', $(el).find('table').height())
				.css('margin-top', 34)
				.css('left', $(el).find('table').offset().left);
		$overlay.children('.note').css('margin-top', (($overlay.height() / 2) - ($overlay.children('.note').height() * 2) - 34));
		
		$overlay.show();

		var tsTimeStamp = new Date().getTime();
		$.get(href, {action: "get", time: tsTimeStamp}, function(data){
			$(el).html(data);
			pagingInited = false;
			currentUrl = href;
		});
		return false;
	}
	else
	{
		$(el).find('#paginator a').click(function(){
			return initAjaxPaging($(el), $(this).attr('href'));
		});
	}
	return false;
}


/**
 * Cookie plugin
 *
 * Copyright (c) 2006 Klaus Hartl (stilbuero.de)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 */
jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};