var cities;

$(document).ready(function(){
	$("#navigation dl").mouseover(function(){
		$(this).css('zIndex', 6);
		$(this).find("dt").css('zIndex', 51);
	});
	$("#navigation dl").mouseout(function(){
		$(this).css('zIndex', 5);
	
	});

	if ($('#city').length) {
		$.getJSON('/ajax/get-cities', function(cities) {
			$("#city").autocomplete(cities);
		});
	}
	if ($('#addAttribute').length) {
		toogleAttributes(document.getElementById('type'));
		
	}



	if ($('#file').length) {
		$(function(){
			var btnUpload=$('#file');
			var status=$('#status');
			var dir_name=Math.floor(Math.random()*110);
			new AjaxUpload(btnUpload, {
				action: '/ajax/upload-file',
				//Name of the file input box
				name: 'uploadfile',
				data: {
				    dir_name : dir_name
				},
				onSubmit: function(file, ext){
					
					if (! (ext && /^(jpg|png|jpeg|gif)$/.test(ext))){
		                  // check for valid file extension
						status.text('Only JPG, PNG or GIF files are allowed');
						return false;
					}
					status.text('Wczytywanie...');
				},
				onComplete: function(file, response){
					//On completion clear the status
					status.text('');
					//Add uploaded file to list
					if(response==="success"){
						$('<li class="hide"></li>').appendTo('#files')
									  .html('<img src="/public/images/upload/tmp/'+dir_name+'/'+file+'" alt="" /><br />'+file+
											 '<input type="hidden" name="files[]" value="'+file+'" />'+
											 '<input type="hidden" name="dir_name" value="'+dir_name+'" />')
									  .addClass('success')
									  .show(1000);
						$('#files_box').show();
					} else{
						$('<li class="hide"></li>').appendTo('#files').text(response).addClass('error').show(1000);
					}
					
				}
			});
		});
	}
})



function changeDisplay(id)
{	
	if(document.getElementById(id).style.display == 'block')
	{
		document.getElementById(id).style.display = 'none';
		showed = 0;
	}
	else
	{
		document.getElementById(id).style.display = 'block';
		showed = 1;
	}		
	
	
}

function toogleLoginPanelAndEmail(input)
{
	
	if (input.value == '1')
	{

		$('#announcementLoginPanel').hide(1000);
		$('#announcementEmail').show(1000);
	}
	else
	{

		$('#announcementLoginPanel').show(1000);
		$('#announcementEmail').hide(1000);
	}
}
function addAttributeValue()
{

	if ($('#attributesBtn').attr( 'class' )=='activeBtn')
	{
		$('#attributes').append('<input class="inpTxt mediumInp" name="values[]" />');
		$('#attributes').show(1000);
	}
	
}
function toogleAttributes(option)
{
	
	if (option.value == '3')
	{
		$('#attributes').html('');
		$('#attributes').hide(1000);
		$('#attributesBtn').attr( 'class' , 'disableBtn')
	}
	else
	{
		$('#attributesBtn').attr( 'class' , 'activeBtn');
		$('#attributes').show(1000);
	}

}
function toogleNip(option)
{
	
	if (option.value == 'companies')
	{
		
		$('#registrationFrm ul.hide').show(1000);
	}
	else
	{
		
		$('#registrationFrm ul.hide').hide(1000);
	}
}
function open_win(url)
{
	var win = window.open (url, "Window", "location=1,status=1,scrollbars=1,width=500,height=500");
	win.moveTo(400,200);
}


function setFavouriteAnnouncement(id)
{
	$('#FavouriteAnnouncementMgs').load('/announcement/add-favourite-announcement/'+id);
}

function unsetFavouriteAnnouncement(id)
{
	$('#FavouriteAnnouncementMgs').load('/announcement/del-favourite-announcement/'+id);
}
function getAllCategories(select)
{
	$('#parent_id_li').hide(1000);
	var id = select.options[select.selectedIndex].value;
	$.get('/ajax/get-all-categories/country_id/'+id, function(data) {
		$('#parent_id').html(data);
		$('#parent_id_li').show(1000);
	});
	//$('#parent_id').load('/ajax/get-all-categories/country_id/'+id);
}
/*function getAttributes()
{
	$('#attributes').load('/ajax/get-attributes/');
}*/
function getCities()
{
	var province_id = $('#province_id:options:selected').attr('value');
	$.getJSON('/ajax/get-cities'+province_id, function(cities) {
		$("#city").unautocomplete();;
		$("#city").autocomplete(cities);
	});
	


}

function getCitiesInSelect(select)
{
	
	var province_id = select.options[select.selectedIndex].value;
	
	$('#city_id').load('/ajax/get-cities-html/province_id/'+province_id)
	
}

function getAttributes(select)
{
	var category_id = $('#subcategory_id option:selected').attr('value');
	$('#attributes').load('/ajax/get-attributes-html/category_id/'+category_id, function(){
		$('#attributes').show(1000)
	});
	
	
}

function getSubcategories(select)
{
	var category_id = select.options[select.selectedIndex].value;
	$.get('/ajax/get-subcategories/category_id/'+category_id, function(data){
		$('#subcategory_id').hide(1000);
		$('#subsubcategory_id').hide(1000);
		$('#subcategory_id').text('');
		$('#subsubcategory_id').text('');
		
		if (data != '') {
			$('#subcategory_id').html(data);
			$('#attributes').load('/ajax/get-attributes-html/category_id/'+$('#subcategory_id option:selected').attr('value'))
			$('#subcategory_id').show(1000);
			//$('#subcategory_id').getAttributes();
			getAttributes(document.getElementById('#subcategory_id'));
		}
	});


	
}
function getSubsubcategories(select)
{
	var category_id = select.options[select.selectedIndex].value;
	$.get('/ajax/get-subcategories/category_id/'+category_id, function(data){
		$('#subsubcategory_id').hide(1000);
		$('#subsubcategory_id').text('');
		if (data != '') {
			$('#subsubcategory_id').html(data);
			$('#attributes').load('/ajax/get-attributes-html/category_id/'+$('#subsubcategory_id option:selected').attr('value'))
			$('#subsubcategory_id').show(1000);
		}

	});
	/*$('#subsubcategory_id').load(
			'/ajax/get-subcategories/category_id/'+category_id, function() {
			$('#subsubcategory_id').show();
			$('#attributes').load('/ajax/get-attributes-html/category_id/'+$('#subsubcategory_id option:selected').attr('value'))

	})*/
	
}

function report_announcement(announcement_id)
{
	var win = window.open ('/announcement/report/announcement_id/'+announcement_id, 'report announcement', 'location=1, status=1, scrollbars=1, width=500, height=300');
	win.moveTo(200,200);
	
}
function show_foto(img, file)
{
	var first_row = img.parentNode.parentNode.parentNode.firstChild;
	first_row.getElementsByTagName('a')[0].href = file;
	first_row.getElementsByTagName('img')[0].src = file;
}
/**
 * Sets a Cookie with the given name and value.
 *
 * name       Name of the cookie
 * value      Value of the cookie
 * [expires]  Expiration date of the cookie (default: end of current session)
 * [path]     Path where the cookie is valid (default: path of calling document)
 * [domain]   Domain where the cookie is valid
 *              (default: domain of calling document)
 * [secure]   Boolean value indicating if the cookie transmission requires a
 *              secure transmission
 */
function setCookie(name, value, expires, path, domain, secure) {
    document.cookie= name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires.toGMTString() : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}

/**
 * Gets the value of the specified cookie.
 *
 * name  Name of the desired cookie.
 *
 * Returns a string containing value of specified cookie,
 *   or null if cookie does not exist.
 */
function getCookie(name) {
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1)
    {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    }
    else
    {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1)
    {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}

/**
 * Deletes the specified cookie.
 *
 * name      name of the cookie
 * [path]    path of the cookie (must be same as path used to create cookie)
 * [domain]  domain of the cookie (must be same as domain used to create cookie)
 */
function deleteCookie(name, path, domain)
{
    if (getCookie(name))
    {
        document.cookie = name + "=" +
            ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") +
            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}
function setFontSize(size)
{
	styleSheet = document.styleSheets[0];
    if (styleSheet)
    {
    	if (styleSheet.cssRules)
        	cssRule = styleSheet.cssRules[0]; // Firefox
        else if (styleSheet.rules)
        	cssRule = styleSheet.rules[0];        // IE
        if (cssRule)
        {
        	cssRule.style.fontSize = size;
        	
        }
    }
}