var carousel_y_position = 0;
var carousel_mouse_over = false;

function stop_popup() {
	params = '';
	var xmlHttp = getXmlHttp();	
	xmlHttp.open("POST","/bluebook/_inc/stop_popup.php",true);
	//Send the proper header information along with the request
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(params);
}

function switch_search(current_button) {
	if (current_button == 'basic') {
		document.getElementById('search_basic').style.display='block';
		document.getElementById('search_advanced').style.display='none';
		document.getElementById('search_basic_button').className="current";
		document.getElementById('search_advanced_button').className="uncurrent";
	} else if (current_button == 'advanced') {
		document.getElementById('search_basic').style.display='none';
		document.getElementById('search_advanced').style.display='block';
		document.getElementById('search_advanced_button').className="current";
		document.getElementById('search_basic_button').className="uncurrent";
	}
}

function printPage() {
window.print();  
}

function getPostitOrder() {
	var childNodes = document.getElementById('noticeboard_content').childNodes;
	var csv = "";
	for (i=0; i<childNodes.length;i++) {
		if (childNodes[i].id != null) {
			//Create the string
			csv += childNodes[i].id + ",";
		}
	}
	csv = csv.substr(0,csv.length - 1);
	var xmlHttp = getXmlHttp();	
	var params = 'csv=' + csv
	xmlHttp.open("POST","/bluebook/_inc/order.php",true);
	//Send the proper header information along with the request
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(params);
}

function remove_postit(id) {
	var postit_elem = document.getElementById('postit_' + id);
	postit_elem.parentNode.removeChild(postit_elem);
	
	//get the number of post its
	var notes = document.getElementById('noticeboard_content').childNodes;
		
	var xmlHttp = getXmlHttp();
	var xmlHttp2 = getXmlHttp();
		
	xmlHttp.onreadystatechange=function() {
		if(xmlHttp.readyState==4) {
			rss_article_link = xmlHttp.responseText;
		}
	}
		
	var params = 'id=' + id;
		
	xmlHttp.open("POST","/bluebook/_inc/delete-postit.php",false);
	//Send the proper header information along with the request
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(params);
	
	//only replace post its with pr notes is there are less than 8 notes
	if (notes.length < 8) {
		
		//get the number of press release post its
		var pr_count = 0;
		for (i=0; i < notes.length; i++) {
			if (notes[i].id.indexOf("pr") > -1) {
				pr_count++;
			}
		}
	
		//increment to next pr position
		pr_count++;
		
		var rss_article_link = "";
		
		xmlHttp2.onreadystatechange=function() {
			if(xmlHttp2.readyState==4) {
				rss_article_link = xmlHttp2.responseText;
			}
		}
		
		var params = 'article_position=' + pr_count;
		
		xmlHttp2.open("POST","/bluebook/_inc/get_rss_item.php",false);
		//Send the proper header information along with the request
		xmlHttp2.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlHttp2.setRequestHeader("Content-length", params.length);
		xmlHttp2.setRequestHeader("Connection", "close");
		xmlHttp2.send(params);
		
		var new_pr_id = 0;
		
		xmlHttp.onreadystatechange=function() {
			if(xmlHttp.readyState==4) {
				new_pr_id = xmlHttp.responseText;
			}
		}
		
		var params = '';
	
		xmlHttp.open("POST","/bluebook/_inc/add_pr_note.php",false);
		//Send the proper header information along with the request
		xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlHttp.setRequestHeader("Content-length", params.length);
		xmlHttp.setRequestHeader("Connection", "close");
		xmlHttp.send(params);
		
		//Create the HTML
		var postit_top = document.createElement('div');
		postit_top.id = 'pr_top_'+ new_pr_id;
		postit_top.className = 'pr_top';
		
		var postit_content = document.createElement('div');
		postit_content.id = 'pr_mid_'+ new_pr_id;
		postit_content.className = 'pr_mid';
		postit_content.innerHTML = '<p>' + rss_article_link + '</p>';
		
		var postit_bottom = document.createElement('div');
		postit_bottom.id = 'pr_bottom_'+ new_pr_id;
		postit_bottom.className = 'pr_bottom';
		postit_bottom.innerHTML = '<a target="_blank" href="http://www.unbiased.co.uk/rss.asp"><img alt="RSS Icon" src="_images/rss.gif"/></a>';
		
		var postit = document.createElement('div');
		postit.id = 'pr_'+ new_pr_id;
		postit.className = 'pr_noticeboard';
		
		postit.appendChild(postit_top);
		postit.appendChild(postit_content);
		postit.appendChild(postit_bottom);
		
		document.getElementById('noticeboard_content').appendChild(postit);
		
		Sortable.create("noticeboard_content",{tag:'div',overlap:'horizontal',constraint: false,onUpdate: function() {getPostitOrder();}})
	}
}

function remove_pr(id) {
	var pr_elem = document.getElementById('pr_' + id);
	pr_elem.parentNode.removeChild(pr_elem);
}

function remove_profile_postit(id) {
	var postit_elem = document.getElementById('postit_' + id);
	postit_elem.parentNode.removeChild(postit_elem);
		
	var xmlHttp = getXmlHttp();		
	var params = 'id=' + id;
		
	xmlHttp.open("POST","/bluebook/_inc/delete-profile-postit.php",false);
	//Send the proper header information along with the request
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(params);
}

function create_postit(text,elem_id,postit_location) {
	//Add postit to db
//	text = text.replace(/\<(.*?)\>/ig,'');

	//get the number of press release post its
	var notes = document.getElementById('noticeboard_content').childNodes;
	var postit_count = 0;
	for (i=0; i < notes.length; i++) {
		if (notes[i].id.indexOf("postit") > -1) {
			postit_count++;
		}
	}
	
	var pr_count = notes.length - postit_count;
	
	if (notes.length >= 12 && pr_count > 0) {
		var xmlHttp = getXmlHttp();
		
		var pr_id = 0;
		
		xmlHttp.onreadystatechange=function() {
			if(xmlHttp.readyState==4) {
				pr_id = xmlHttp.responseText;
				remove_pr(pr_id);
			}
		}
		
		var params = '';
	
		xmlHttp.open("POST","/bluebook/_inc/delete-pr.php",false);
		//Send the proper header information along with the request
		xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlHttp.setRequestHeader("Content-length", params.length);
		xmlHttp.setRequestHeader("Connection", "close");
		xmlHttp.send(params);
	}
	
	var next_postit_id = 0;
	
	var xmlHttp = getXmlHttp();
	
	xmlHttp.onreadystatechange=function() {
		if(xmlHttp.readyState==4) {
			next_postit_id = xmlHttp.responseText;
			
			//Create the HTML
			var postit_top = document.createElement('div');
			postit_top.id = 'postit_top_'+ next_postit_id;
			//alternate bewteen red and blue pins
			if (postit_count%2 != 1) {
				postit_top.className = 'postit_top postit_top_blue';
			} else {
				postit_top.className = 'postit_top';
			}
			postit_top.innerHTML = '<a href="/bluebook/_inc/edit_postit.php?action=view&amp;id='+ next_postit_id +'" onclick="return GB_showCenter(\'View\', this.href, 200, 400)">View</a> | <a href="/bluebook/_inc/edit_postit.php?action=edit&amp;id='+ next_postit_id +'" onclick="return GB_showCenter(\'Edit\', this.href, 400, 400)">Edit</a> | <a href="/bluebook/_inc/edit_postit.php?action=delete&amp;id='+ next_postit_id +'" onclick="return GB_showCenter(\'Delete\', this.href, 100, 200)">Delete</a>';
			
			var postit_content = document.createElement('div');
			postit_content.id = 'postit_mid_'+ next_postit_id;
			postit_content.className = 'postit_mid';
			postit_content.innerHTML = '<p>'+ text +'</p>';
			
			var postit_bottom = document.createElement('div');
			postit_bottom.id = 'postit_bottom_'+ next_postit_id;
			postit_bottom.className = 'postit_bottom';
			
			var postit = document.createElement('div');
			postit.id = 'postit_'+ next_postit_id;
			postit.className = 'postit_' + postit_location;
			
			postit.appendChild(postit_top);
			postit.appendChild(postit_content);
			postit.appendChild(postit_bottom);
			
			document.getElementById(elem_id).insertBefore(postit, document.getElementById(elem_id).firstChild);
			//document.getElementById(elem_id).appendChild(postit);
			var elem_name = 'postit_'+ next_postit_id;
			
			
			Sortable.create("noticeboard_content",{tag:'div',overlap:'horizontal',constraint: false,onUpdate: function() {getPostitOrder();}})
			
			parent.parent.GB_hide();
		} 
	}
	
	var params = 'text=' + encodeURIComponent(text);
	xmlHttp.open("POST","/bluebook/_inc/add_postit.php",true);
	//Send the proper header information along with the request
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(params);
}

function create_profile_postit(text,elem_id,postit_location,profile_id) {
	//Add postit to db
//	text = text.replace(/\<(.*?)\>/ig,'');
	
	var xmlHttp = getXmlHttp();
	
	var next_postit_id = 0;
	
	xmlHttp.onreadystatechange=function() {
		if(xmlHttp.readyState==4) {
			next_postit_id = xmlHttp.responseText;
			
			//Create the HTML
			var postit_top = document.createElement('div');
			postit_top.id = 'postit_top_'+ next_postit_id;
			postit_top.className = 'postit_top';
			postit_top.innerHTML = '<a href="/bluebook/_inc/edit_profile_postit.php?action=view&amp;id='+ next_postit_id +'" onclick="return GB_showCenter(\'View\', this.href, 200, 400)">View</a> | <a href="/bluebook/_inc/edit_profile_postit.php?action=edit&amp;id='+ next_postit_id +'" onclick="return GB_showCenter(\'Edit\', this.href, 400, 400)">Edit</a> | <a href="/bluebook/_inc/edit_profile_postit.php?action=delete&amp;id='+ next_postit_id +'" onclick="return GB_showCenter(\'Delete\', this.href, 100, 200)">Delete</a>';
			
			var postit_content = document.createElement('div');
			postit_content.id = 'postit_mid_'+ next_postit_id;
			postit_content.className = 'postit_mid';
			postit_content.innerHTML = '<p>'+ text +'</p>';
			
			var postit_bottom = document.createElement('div');
			postit_bottom.id = 'postit_bottom_'+ next_postit_id;
			postit_bottom.className = 'postit_bottom';
			
			var postit = document.createElement('div');
			postit.id = 'postit_'+ next_postit_id;
			postit.className = 'postit_' + postit_location;
			
			postit.appendChild(postit_top);
			postit.appendChild(postit_content);
			postit.appendChild(postit_bottom);
			
			var new_note = document.getElementById(elem_id).appendChild(postit);
			var x_position = Math.floor(Math.random()*541);
			var y_position = Math.floor(Math.random()*280);
			
			y_position += 40;
			new_note.style.top = '' + y_position + 'px';
			new_note.style.left = '' + x_position + 'px';
			storePostitPosition(x_position, y_position, next_postit_id);
			
			var elem_name = 'postit_'+ next_postit_id;
			
			new Draggable('postit_'+ next_postit_id,{handle:'pin_top_'+ next_postit_id, onEnd : function(){ storePostitPosition(document.getElementById('postit_' + next_postit_id).style.left, document.getElementById('postit_' + next_postit_id).style.top, next_postit_id);}, snap: function(x,y,draggable) {return position(x,y,draggable)}, revert:false });
		
			
			parent.parent.GB_hide();
		}
	}
	
	var params = 'text=' + encodeURIComponent(text) + '&profile_id=' + profile_id;
	
	xmlHttp.open("POST","/bluebook/_inc/add_profile_postit.php",true);
	//Send the proper header information along with the request
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(params);
	
	
}

function edit_postit(text,id) {
	var xmlHttp = getXmlHttp();
	
	var params = 'text=' + encodeURIComponent(text) + '&id=' + id;
	
	xmlHttp.open("POST","/bluebook/_inc/update_postit.php",true);
	//Send the proper header information along with the request
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(params);
	
	document.getElementById('postit_mid_' + id).innerHTML = '<p>'+ text +'</p>';
	
	parent.parent.GB_hide();
}

function edit_profile_postit(text,id) {
	var xmlHttp = getXmlHttp();
	
	var params = 'text=' + encodeURIComponent(text) + '&id=' + id;
	
	xmlHttp.open("POST","/bluebook/_inc/update_profile_postit.php",true);
	//Send the proper header information along with the request
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(params);
	
	document.getElementById('postit_mid_' + id).innerHTML = '<p>'+ text +'</p>';
	
	parent.parent.GB_hide();
}
	
function moveToPrevious() {
  var leftPosition = parseInt(document.getElementById('imageBoxInside').style.left);
  clearInterval(myInterval);
  if (leftPosition < 0) {
		// if the end of the images is within a scroll of 50px only scroll to the end of the images
		if (leftPosition > -50) { 
			new Effect.Move('imageBoxInside', { x: (0 - leftPosition), y: 0, transition: Effect.Transitions.linear 
		});
	} else {
  		new Effect.Move('imageBoxInside', { x: 50, y: 0, transition: Effect.Transitions.linear });
		myInterval = setInterval("moveToPrevious()", 500);
	}
  }
}

function startPreviousScroll() {
	clearInterval(myInterval);
	moveToPrevious();
}

function stopPreviousScroll() {
	clearInterval(myInterval);
}
var myInterval = null;
function moveToNext() {
  var numberOfImages = document.getElementById('imageBoxInside').childNodes.length;
  var leftPosition = document.getElementById('imageBoxInside').style.left;
  if (leftPosition == "") {
	  leftPosition = 0
  } else {
	  leftPosition = parseInt(document.getElementById('imageBoxInside').style.left)
  }
  clearInterval(myInterval);
  var imageWidthWithPadding = 55;
  var defaultScrollWidth = 7 * imageWidthWithPadding;
  var imagesWidth = numberOfImages * imageWidthWithPadding;
  if ((leftPosition - defaultScrollWidth) > -imagesWidth) {
	// if the end of the images is within a scroll of 50px only scroll to the end of the images
	if ((imagesWidth + (leftPosition - defaultScrollWidth)) < 50) {
		new Effect.Move('imageBoxInside', { x: - ((leftPosition - defaultScrollWidth) + imagesWidth), y: 0, transition: Effect.Transitions.linear });
	} else {
  		new Effect.Move('imageBoxInside', { x: -50, y: 0, transition: Effect.Transitions.linear });
		myInterval = setInterval("moveToNext()", 500);
	}
  }
}

function startNextScroll() {
	clearInterval(myInterval);
	moveToNext();
}

function stopNextScroll() {
	clearInterval(myInterval);
}

function scroller_details(id) {
	var xmlHttp = getXmlHttp();
	
	xmlHttp.onreadystatechange=function() {
		if(xmlHttp.readyState==4) {
			document.getElementById("scroller_details").innerHTML = xmlHttp.responseText;
		}
		else {
			document.getElementById("scroller_details").innerHTML = "<img src=\'/bluebook/_images/ajax-loader.gif\' alt=\'Working...\' />";
		}
	}
	
	var params = 'id=' + id
	
	xmlHttp.open("POST","/bluebook/_inc/scroller_details.php",true);
	//Send the proper header information along with the request
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(params);
}

function carousel_img_switcher(id, state) {
	if (state == "off") {
		document.getElementById("carouselimg" + id).src = "http://unbiased2.mwdev.co.uk/msd_images/unbiased/thumbnails/small/greyscale/" + id + ".jpg";
	} else {
		document.getElementById("carouselimg" + id).src = "http://unbiased2.mwdev.co.uk/msd_images/unbiased/thumbnails/small/colour/" + id + ".jpg";
	}
}

function marqueeSource(type) {
	var xmlHttp = getXmlHttp();
	
	xmlHttp.onreadystatechange=function() {
		if(xmlHttp.readyState==4) {
			document.getElementById("scroller_details").innerHTML = xmlHttp.responseText;
		}
		else {
			document.getElementById("scroller_details").innerHTML = "<img src=\'/bluebook/_images/ajax-loader.gif\' alt=\'Working...\' />";
		}
	}
	
	var params = 'id=' + id
	
	xmlHttp.open("POST","/bluebook/_inc/scroller_details.php",true);
	//Send the proper header information along with the request
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(params);
}

function addToFavourites(ifa_id, user_id) {
	var xmlHttp = getXmlHttp();
	xmlHttp.open("GET","/bluebook/_inc/favourites.php?ifaid=" + ifa_id + "&userid=" + user_id,true);
	xmlHttp.send(null);
	document.getElementById('favouritelink').innerHTML = 'Remove from favourites';
	document.getElementById('favouritelink').onclick = function(){removeFromFavourites(ifa_id, user_id)};
}

function removeFromFavourites(ifa_id, user_id) {
	var xmlHttp = getXmlHttp();
	xmlHttp.open("GET","/bluebook/_inc/favourites.php?ifaid=" + ifa_id + "&userid=" + user_id + "&delete=true",true);
	xmlHttp.send(null);
	document.getElementById('favouritelink').innerHTML = 'Add to favourites';
	document.getElementById('favouritelink').onclick = function(){addToFavourites(ifa_id, user_id)};
}

function changeCarouselSource(list_type) {
	var xmlHttp = getXmlHttp();
	xmlHttp.onreadystatechange=function() {
		if(xmlHttp.readyState==4) {
			document.getElementById("imageBoxInside").innerHTML = xmlHttp.responseText;
		}
		else {
			//document.getElementById("scroller_details").innerHTML = "<img src=\'/bluebook/_images/ajax-loader.gif\' alt=\'Working...\' />";
		}
	}
	if (navigator.userAgent.indexOf("Firefox")!=-1) {
		xmlHttp.open("Get","/bluebook/_inc/carousel_source.php?list_type=" + list_type,true);
	} else {
		xmlHttp.open("Get","/bluebook/_inc/carousel_source.php?list_type=" + list_type,false);
	}
	xmlHttp.send(null);
	document.getElementById('imageBoxInside');
	if (list_type == 'toprated') {
		//dont set link styles if not logged in
		if (document.getElementById('carouselfav') != null && document.getElementById('carouselfav') != null) {
			document.getElementById('imageBoxInside').style.left = 0;
			document.getElementById('carouselfav').style.fontWeight = 'normal';
			document.getElementById('carouseltop').style.fontWeight = 'bold';
			document.getElementById('carouseltop').className="current";
			document.getElementById('carouselfav').className="uncurrent";
		}
	} else if (list_type == 'favourites') {
		//dont set link styles if not logged in
		if (document.getElementById('carouselfav') != null && document.getElementById('carouselfav') != null) {
			document.getElementById('imageBoxInside').style.left = 0;
			document.getElementById('carouseltop').style.fontWeight = 'normal';
			document.getElementById('carouselfav').style.fontWeight = 'bold';
			
			document.getElementById('carouselfav').className="current";
			document.getElementById('carouseltop').className="uncurrent";
		}
	}
}

function storePostitPosition(x, y, id) {
	
	var xmlHttp = getXmlHttp();
	
	var params = 'x=' + parseInt(x) + '&y=' + parseInt(y) + '&id=' + id;
	
	xmlHttp.open("POST","/bluebook/_inc/update_note_position.php",false);
	//Send the proper header information along with the request
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(params);
}

function getXmlHttp() {
	var xmlHttp;
	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	
	catch (e) {
		// Internet Explorer
		try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
			try {
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e) {
				alert("Your browser does not support AJAX!");
				return false;
			}
		}	
	}
	return xmlHttp;
}

function submitNameSearch() {
	if (document.getElementById("profile_id").value == "") {
		document.getElementById("name_search").action = "/bluebook/search_results.php";
	}
}

function copyIfaName() {
	document.getElementById("hiddenname").value = document.getElementById("name").value;
}

function carousel_img_onError (img) {
	document.getElementById('imageBoxInside').removeChild(img);
}