function setCookie(c_name,value,expiredays) {
	var expiration_date=new Date();
	expiration_date.setDate(expiration_date.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+ ((expiredays==null) ? "" : ";expires="+expiration_date.toGMTString()+"; path=/");
}

function getCookie(c_name) {
	if (document.cookie.length>0) {
  		c_start=document.cookie.indexOf(c_name + "=");
	  	if (c_start!=-1) { 
	    	c_start=c_start + c_name.length+1; 
	    	c_end=document.cookie.indexOf(";",c_start);
	    	if (c_end==-1) {
				c_end=document.cookie.length;
			}
	    	return unescape(document.cookie.substring(c_start,c_end));
	    }
  	}
	return "";
}

function sortNetworks() {
	var url = window.location.toString();
	var baseURL = url.substring(0,url.lastIndexOf("?"));
	if (baseURL=="") baseURL = url.substring(0,url.lastIndexOf("/"));
	
	if(getCookie("sort")!=null && getCookie("sort")!="") {
		var sort = getCookie("sort");
	} else {
		var sort = "n";
		setCookie('sort',sort,30);
	}
	if(getCookie("dir")!=null && getCookie("dir")!="") {
		var dir = getCookie("dir");
	} else {
		var dir = "a";
		setCookie('dir',dir,30);
	}
	if(getCookie("nss")!=null && getCookie("nss")!="") {
		var nss = getCookie("nss");
	} else {
		var nss = "";
		setCookie('nss',nss,30);
	}
	//alert("getCookie(\"sort\") = "+getCookie("sort")+"\ngetCookie(\"dir\") = " + getCookie("dir"));
	top.location = "networks/?sort="+sort+"&dir="+dir+"&nss="+nss;
}

function setSortView() {
	if(getCookie("sort")!=null && getCookie("sort")!="") {
		var sort = getCookie("sort");
	} else {
		var sort = "n";
	}
	if(getCookie("dir")!=null && getCookie("dir")!="") {
		var dir = getCookie("dir");
	} else {
		var dir = "a";
	}
	if(sort=="n") {
		if(document.getElementById("sort-by-network")) document.getElementById("sort-by-network").className="current";
		if(document.getElementById("sort-by-vertical")) document.getElementById("sort-by-vertical").className="";
	} else if(sort=="v") {
		if(document.getElementById("sort-by-network")) document.getElementById("sort-by-network").className="";
		if(document.getElementById("sort-by-vertical")) document.getElementById("sort-by-vertical").className="current";
	}
	if(dir=="a") {
		if(document.getElementById("sort-asc")) document.getElementById("sort-asc").className="current";
		if(document.getElementById("sort-desc")) document.getElementById("sort-desc").className="";
	} else if(dir=="d") {
		if(document.getElementById("sort-asc")) document.getElementById("sort-asc").className="";
		if(document.getElementById("sort-desc")) document.getElementById("sort-desc").className="current";
	}
}

function formatImageSinglePost() {

	var imgSrc     = jQuery("#blog-post-container img:first").attr("src");
	var origWidth  = jQuery("#blog-post-container img:first").width();
	var origHeight = jQuery("#blog-post-container img:first").height();
	var newHeight;
	var newWidth;
	var newPosition;
		
	jQuery("#blog-post-container img:first").remove();
	
	jQuery("#blog-post-container").prepend('<div id="blog-post-i"></div>');
	jQuery("#blog-post-i").prepend('<div id="blog-post-j"></div>');
	jQuery("#blog-post-j").prepend('<div id="blog-post-k"></div>');
	jQuery("#blog-post-j").append('<span></span>');
	jQuery("#blog-post-k").prepend('<img src="'+imgSrc+'"></div>');
	
	jQuery("#blog-post-i").css({"float":"right","margin":"5px 0px 15px 15px"});
	jQuery("#blog-post-j").css({"position":"relative"});
	jQuery("#blog-post-j span").css({"position":"absolute" ,"top":"0px" ,"left":"0px" ,"display":"block"});

	var isVertical = (origHeight > origWidth) ? true : false;
	var isVert	   = (isVertical) ? "_vert" : "";

	if(isVertical) {
		newWidth   = 150;
		newHeight  = Math.round((newWidth*origHeight)/origWidth);
		//newPosition = ((newHeight/2)-90)*-1;
		jQuery("#blog-post-j span").css({"background":"transparent url(themes/horngroup/images/blog-img-big-vert.png) 0px 0px no-repeat" ,"width":"152px" ,"height":"197px"});
		jQuery("#blog-post-k").css({"width":"137px","height":"181px","overflow":"hidden","position":"relative","top":"5px","left":"5px"});
		jQuery("#blog-post-k img").css({"width":newWidth,"height":newHeight});

		} else {
			
		newHeight  = 139;
		newWidth   = Math.round((newHeight*origWidth)/origHeight);
		//newPosition = ((newWidth/2)-90)*-1;
		jQuery("#blog-post-j span").css({"background":"transparent url(themes/horngroup/images/blog-img-big-hor.png) 0px 0px no-repeat","width":"197px","height":"152px"});
		jQuery("#blog-post-k").css({"width":"181px","height":"137px","overflow":"hidden","position":"relative","top":"5px","left":"5px"});
		jQuery("#blog-post-k img").css({"width":newWidth,"height":newHeight,"position":"relative"});
	}
}

function formatImageAllPosts() {

	jQuery('.content img').each(function(i){
		var origWidth  = jQuery(this).width();
		var origHeight = jQuery(this).height();
		
		var newHeight;
		var newWidth;
	
		jQuery(this).wrap('<div id="blog-post-i" style="float:left; margin:5px 15px 25px 0px;"></div>');
		jQuery(this).wrap('<div id="blog-post-j" style="position:relative;"></div>');
		jQuery(this).wrap('<div id="blog-post-k"></div>');
		jQuery(this).parent().parent().append('<span style="position:absolute; top:0px; left:0px; display:block;"></span>');
		
		var isVertical = (origHeight > origWidth) ? true : false;
		var isVert	   = (isVertical) ? "_vert" : "";
	
		if(isVertical) {
			newWidth   = 87;
			newHeight  = Math.round((newWidth*origHeight)/origWidth);
			jQuery(this).parent().parent().children('span').css({"background":"transparent url(themes/horngroup/images/blog-img-sm-vert.png) 0px 0px no-repeat" ,"width":"97px" ,"height":"124px"});
			jQuery(this).parent().css({"width":"88px","height":"118px","overflow":"hidden","position":"relative","top":"1px","left":"2px"});
			jQuery(this).css({"width":newWidth,"height":newHeight,"margin":"2px 0px 0px 1px"});

			} else {
			
			newHeight = 87;
			newWidth = Math.round((newHeight*origWidth)/origHeight);
			jQuery(this).css({"width":newWidth,"height":newHeight});
			jQuery(this).parent().parent().children('span').css({"background":"transparent url(themes/horngroup/images/blog-img-sm-hor.png) 0px 0px no-repeat","width":"124px","height":"97px"});
			jQuery(this).parent().css({"width":"116px","height":"87px","overflow":"hidden","position":"relative","top":"3px","left":"3px"});
			jQuery(this).css({"width":newWidth,"height":newHeight,"margin":"0px 0px 0px 0px"});
		}
		
	});
	
	var isPostsByAuthor = jQuery('.content h1').text();
	if(isPostsByAuthor == "Posts by Author" && authorHasBio){
		jQuery('.content p:first').css({'margin':'0px 0px 70px 0px'});
		jQuery('#blog-post-i:first').css({'margin':'5px 15px 5px 0px'});
	}
	
	//alert("newWidth = "+newWidth+"\nnewHeight = "+newHeight);
}

function ajaxCall(url) {
		var currentDomain = document.domain;
		
		jQuery.ajax({
			url: 'http://'+currentDomain+'/mysite/ajax-proxy.php?proxy_url='+url,
			type: 'get',
			dataType: 'html',
			timeout: 10000,
			success:
				function(){
					$('#ajaxTarget').load('http://'+currentDomain+'/mysite/ajax-proxy.php?proxy_url='+url+' .innerPanel > div > div > div');
				},
			error:
				function(){
					$('#ajaxTarget').text('Sorry. We could not complete your request.');
				}
	});
	return false;
}
