// Sets a cookie
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

// Reads a cookie
function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}


// Called when view drop down is changed
function setview(viewselect) {
	createCookie('view', viewselect.options[viewselect.options.selectedIndex].value, 30);
	location.reload(true);
	return false;
}

// Called when the sort drop down is changed
function setsort(sortselect) {
	createCookie('sort', sortselect.options[sortselect.options.selectedIndex].value, 30);
	location.reload(true);
	return false;
}

// Sets current drop down value
function setcurrentvalue(cookieval, ddlid) {

	var ddl = document.getElementById(ddlid);
	
	// If we have a view cookie and a reference
	if ((cookieval != null) && (ddl != null)) {
	
		// Loop through each option
		for (var i = 0; i < ddl.length; i++) {
		
			// If this is the current value, set it
			if (ddl.options[i].value == cookieval) ddl.selectedIndex = i;
		
		}
	
	}
	
}

// Initialize items per page links
function initipp() {

	// Get reference to container
	var ipp = document.getElementById('ipp');
	
	// If we have it
	if (ipp != null) {
	
		// Get cookie value
		var ippval = readCookie('ipp');
		if (ippval == null) ippval = 50;
	
		// Get array of anchors within container
		var anchors = ipp.getElementsByTagName('a');
		 
		// loop through all of the anchors
		 for (var i = 0; i < anchors.length; i++) {
		  
			// If this link is active
			if (anchors[i].id == ippval) {
				anchors[i].className = 'active';
			}
			
			else {
				// Otherwise add the href target
				anchors[i].href = 'javascript:setipp(' + anchors[i].id + ');';
			}
		   
		 }
		 
		 
	}

}

// Initialize picture size links
function initpicsize() {

	var picsizecontainer = document.getElementById('picsize');
	
	// If we have it
	if (picsizecontainer != null) {
	
		// Get current value
		var picsize = 120;
		if (readCookie('picsize') != null) picsize = readCookie('picsize');
	
		// Get array of anchors within container
		var picanchors = picsizecontainer.getElementsByTagName('a');
		 
		// loop through all of the anchors
		 for (var j = 0; j < picanchors.length; j++) {
		  
			// Add href target
			picanchors[j].href = 'javascript:setpicsize(' + picanchors[j].id + ');';
			
			// Check if it is the active one
			var innerimg = picanchors[j].getElementsByTagName('img')[0];
			if (picanchors[j].id == picsize) innerimg.src = innerimg.src.replace('-inactive', '-active');
		   
		 }
		  
	}

}

// Called when items per page link is clicked
function setipp(ippval) {
	createCookie('ipp', ippval, 30);
	location.reload(true);
}

// Called when pic size icon is clicked
function setpicsize(picsizeval) {
	createCookie('picsize', picsizeval, 30);
	location.reload(true);
}

// Called when document is ready
$(document).ready(function() {

	// Set current view and sort values from cookie
	setcurrentvalue(readCookie('view'), 'view');
	setcurrentvalue(readCookie('sort'), 'sort');

	initipp();
	initpicsize();
	
});