/** 
 * Blogodak common js lib  
 * Contains methods needed to deal with js stuff
 */ 

function switchToActive(categoryId, totalCat) 
{
    var divid;
    var class_name;
    
	//set all catcontainer's to non-active first
    for(i=1; i <= totalCat; i++)
    {
    	divid = document.getElementById('catcontainer' + i);    	
    	divid.className = 'cat' + i;
    	//alert('Set class of '+ divid + 'to ' + divid.className);
    }
    
    //then set proper cat to active
	divid = document.getElementById('catcontainer' + categoryId);
	divid.className = divid.className + ' active';
}

function submitForm(blogsForm, post_destination)
{
	var checkedBlogs = new Array();
	var uncheckedBlogs = new Array();

	var checkedBlogsString = '';
	var uncheckedBlogsString = '';

	var chkCounter = 0;
	var unchkCounter = 0;
	
	//display ajax image	
	if(navigator.userAgent.indexOf("Opera") == -1)
	{
		document.getElementById('ajaxImgContainer').style.display = "block";
	}	

	for(i=0; i < blogsForm.elements.length; i++)
	{
		if(blogsForm.elements[i].type == 'checkbox')
		{
			if(blogsForm.elements[i].checked)
			{
				checkedBlogs[chkCounter] = blogsForm.elements[i].value;
				chkCounter++;
			}
			else
			{
				uncheckedBlogs[unchkCounter] = blogsForm.elements[i].value;
				unchkCounter++;
			}
		}
	}

	checkedBlogsString = checkedBlogs.join(",");
	uncheckedBlogsString = uncheckedBlogs.join(",");

	var poststr = "ajax=true&checked_blogs=" + encodeURI(checkedBlogsString) + "&unchecked_blogs=" + encodeURI(uncheckedBlogsString);

	//alert(poststr);
	sendPostRequest(post_destination, poststr, alertContents)	
}

function fixImages()
{
	//fix images for ie only
	if(navigator.appName == "Microsoft Internet Explorer")
	{
		for(i=0; i<document.images.length; i++)
		{
			//if image is bigger than 560
			if(document.images[i].width > 560)
			{
				imgRatio = document.images[i].width/document.images[i].height;
				document.images[i].width = 560;
				document.images[i].height = 560 / imgRatio;
				
				divid = document.getElementById('mainContent');
				content = divid.innerHTML;
				divid.innerHTML = '';
				divid.innerHTML = content;
			}
		}
	}
}

function selectAll(form, catId, flag)
{
	for(i=0; i < form.elements.length; i++)
	{
		if(form.elements[i].type == "checkbox")
		{
			//form.elements[i].checked = true;
			checkboxId = form.elements[i].id;
			
			if(checkboxId.indexOf(catId + "_") != -1)
			{
				form.elements[i].checked = flag;
			}
		}
	}
}

function toggleYear(year)
{
	id = document.getElementById(year);
	if(id.style.display == "block")
	{
		id.style.display = "none";
	}
	else
	{
		id.style.display = "block";
	}	
}

var postBlogId;

function dropBlog(blogId)
{
	confirmed = window.confirm("Jeste li sigurni da zelite da izbacite ovaj Blog sa vase liste?");
	if(!confirmed)
	{
		return false;
	}
	
	//proceed with removing blog
	postBlogId = blogId;
	//display ajax animated image
	document.getElementById('ajaxPostContainer'+blogId).style.display = "block";	
	
	//send ajax request to proper page
	sendGetRequest(APP_URL + 'misc/manage_blogs.php?action=drop&blog_id='+blogId, dropBlogAction);
}

function addBlog(blogId)
{
	//proceed with removing blog
	postBlogId = blogId;
	//display ajax animated image
	document.getElementById('ajaxPostContainer'+blogId).style.display = "block";	
	
	//send ajax request to proper page
	sendGetRequest(APP_URL + 'misc/manage_blogs.php?action=add&blog_id='+blogId, addBlogAction);
}