
// Signup
// Updates the link to tell the user where their profile is going to be
function updateprofileurl() {
	
	var user = $("#username").val();
	
	if (!user) {
		user = "your-username";
	}
	
	$("#usernamelink").text(user);
}

// Check whether the username is available
function checkavail() {

	var d = new Date();
	var now = d.getTime();

	new $.get('/signup/main.plm', { username: $('#username').val(), action: "checkavail", cache: now }, function(resp) { $('#availresult').text(resp) });

}

var popup;
function popupwindow(file,key,widt,heit) {

	if (popup == null || popup.closed) {

		popup = window.open(file,key,'status=yes,scrollbars=yes,resizable=yes,width=' + widt + ',height=' + heit);
		popup.focus();

	} else {

		popup.focus();

	}

}

function overlaywindow(thediv) {

	jQuery.facebox({ div: "#" + thediv });

}

function overlayimage(theurl) {

	jQuery.facebox({ image: theurl });

}

function singlephoto(photoid) {

	window.parent.document.getElementById("selectedphoto").src = '/viewphoto.plm?size=small&id=' + photoid;
	window.parent.document.getElementById("selectedphotoid").value = photoid;

	window.parent.document.getElementById("photopreviewarea").style.display = 'block';

	parent.jQuery(window.parent.document).trigger('close.facebox');

}

var selectedphotos = [];

// On multiple selection, ticking one of the images calls this
function multiplephoto(photo) {

	if (photo.checked) {
		selectedphotos.push($(photo).val());
	} else {
		var newarray = [];
		$.each(selectedphotos, function(index, value) {
			if (value != $(photo).val()) {
				newarray.push(value);
			}
		});

		selectedphotos = newarray;
	}

	updateselectedphotoids();

	window.parent.document.getElementById("photopreviewarea").style.display = 'block';

}

// On a multiple popup, this is expected to retick the boxes based on what is in the parent
function reselectphotos() {

	var selphotoelements = parent.jQuery("input[name='selectedphotoid']", window.parent.document);

	selectedphotos = [];
	$.each(selphotoelements, function(index, value) {
		selectedphotos.push($(value).val());
	});

	$("input[name='photo']").val(selectedphotos);

}

function updateselectedphotoids() {

	$('#selectedphotos', window.parent.document).empty();
	$('#selectedphotoids', window.parent.document).empty();

	$.each(selectedphotos, function(index, value) {

		$('#selectedphotoids', window.parent.document).append('<input type="hidden" name="selectedphotoid" value="' + value + '">');
		$('#selectedphotos', window.parent.document).append('<img src="/viewphoto.plm?id=' + value + '&amp;size=uniform" width="80" height="60" style="padding:3px">');

	});

}

function changeforumsort(drop) {
	
	var value = drop.value;
	
	document.location.href = '/forum/?action=changesort&sortdir=' + value;
	
}

function selectallrequests(type) {
	
	if (type == 'approve') {
		$('input[value=Y]').each(function(index, value) {
			$(value).val([ "Y" ]);
		});
		
	} else {
		$('input[value=N]').each(function(index, value) {
			$(value).val([ "N" ]);
		});	
		
	}
	
}

function updatefriendstatus(id, status) {
	
	$.get("/yourstuff/friends.plm", { action: "friendstatus", id: id, status: status } );
	
}

function gridviewselect(item) {
	
	var ischecked = 0;
	
	$('input[name=photositem]').each(function(index, value) {
		if (value.checked) {
			ischecked = 1;
		}
	});
	
	if (ischecked) {
		$('#delete').removeAttr("disabled");
	} else {
		$('#delete').attr("disabled", true);	
	}
	
}

function showhidenewgallery(field) {
	
	if ($(field).val() == '') {
		$('#galleryname').removeAttr("disabled");
	} else {
		$('#galleryname').attr("disabled", true);
	}
	
}

function postcomment(itemtype, itemid, display) {

	if (!$('#commentbox_' + itemtype + itemid).val()) {
		alert("Enter your comment in the box provided.");
		return false;
	}

	$('#postcomment_' + itemtype + itemid).attr("disabled", true);
	$('#progress_' + itemtype + itemid).show();
		
	$.post("/postcomment.plm", { comment: $('#commentbox_' + itemtype + itemid).val(), itemtype: itemtype, itemid: itemid, display: display }, function(data) {
		$('#commentlist_' + itemtype + itemid).append(data);
		$('#postcomment_' + itemtype + itemid).removeAttr("disabled");
		$('#commentbox_' + itemtype + itemid).val("");
		$('#progress_' + itemtype + itemid).hide();
		
		if ($('#newcomment_' + itemtype + itemid)) {
			$('#commenthere_' + itemtype + itemid).hide();
			$('#newcomment_' + itemtype + itemid).show();
		}
	}, "html");
	
	
}

function showcommentbox(itemtype, itemid) {
	
	$('#commenthere_' + itemtype + itemid).show();	
	$('#commentbox_' + itemtype + itemid).focus();
	$('#newcomment_' + itemtype + itemid).hide();
	
}
function hidecommentbox(itemtype, itemid) {
	
	if ($('#commentbox_' + itemtype + itemid).val() == "") {
		$('#commenthere_' + itemtype + itemid).hide();
		$('#newcomment_' + itemtype + itemid).show();
	}
	
}

function showlocationinfo(id) {
	
	$('#locationinfo').html('<img src="/images/smallprogress.gif" alt="Progress">');
	
	var d = new Date();
	var now = d.getTime();
	
	var wasclicked = 0;
	if (id) {
		wasclicked = 1;
	}
	
	// If not passed in, then grab it from the hashtag
	if (!id) {
		id = window.location.hash;
		id = id.substr(1);
		
		if (id) {
			$('#chosenlocation').val(id);
		} else {
			id = $('#chosenlocation').val();	
		}
	}
	
	new $.get('/suppliers/locationinfo', { id: id, cache: now }, function(resp) { $('#locationinfo').html(resp); if (wasclicked) window.location.hash = id; });
	
}

function list_addtext() {
	
	//$('#listaddtext').style.display = 'block';
	
}

function expandoverviewtext() {
	
	$('#overviewexpand').slideDown("normal");	
	$('#overviewexpandtext').hide();
	
}

function starratingupdate(star) {
	
	var itemid = star.id;
	var details = new Array();
	var details = itemid.split(/_/);
	
	starratingdraw(details[1]);
	
}

function starratingdraw(total) {

	for (var n = 1; n <= 10; n++) {
		$('#star_' + n).attr("src", '/images/stargrey.gif');
	}

	if (total > 0) {
		for (var n = 1; n <= total; n++) {
			$('#star_' + n).attr("src", '/images/star.gif');
		}
	}
	
	$('#ratingnumber').html(total + '/10');
	
}

function starratingreset() {

	starratingdraw($('#rating').val());

}

function starratingclick(star) {
	
	var itemid = star.id;
	var details = new Array();
	var details = itemid.split(/_/);
	
	$('#rating').val(details[1]);
	
}

function populatetip(e) {
	
	var element = e.originalTarget || e.srcElement;
	var d = new Date();
	var now = d.getTime();
	var username = $(element).attr("alt");
	
	new $.get('/userpanelfilm.plm', { username: username, cache: now }, function(resp) { $('#tiptext').html(resp); });

}

function thumbupreview(type, id) {
	
	if (type == 'six') {
		new $.post('/filmreviewvote.plm', { type: 'six', id: id }, function(resp) { if (resp != "0") { $('#reviewscore_' + id).html(resp); $('#reviewsixthumb_' + id).effect("highlight", { color: '#96ff93' }, 1000); } else { $('#reviewsixthumb_' + id).effect("highlight", { color: '#ff9393' }, 1000); } });
		
	} else if (type == 'full') {
		new $.post('/filmreviewvote.plm', { type: 'full', id: id }, function(resp) { if (resp != "0") { $('#fullreviewscore_' + id).html(resp); $('#reviewfullthumb_' + id).effect("highlight", { color: '#96ff93' }, 1000); } else { $('#reviewfullthumb_' + id).effect("highlight", { color: '#ff9393' }, 1000); } });
		
	}
	
}

function showmorewhere() {

	var friends = $('#whowith').val();
	var list = friends.split(/\n/);
	
	var showed = 0;
	$('#wherefriendlist').empty();
	for (n = 0; n <= list.length - 1; n++) {
		if (list[n]) {
			$('#wherefriendlist').append( $('<option></option>').val(list[n]).html(list[n]) );
			showed = 1;
		}
	}
	
	var value = $('input[name=where]:checked').val();
	
	if (value == 'cinema') {
		$('#wherecinema').show();
		$('#wherefriend').hide();
		$('#wherefriendtext').hide();
		
	} else if (value == 'friend') {
		$('#wherecinema').hide();
		
		if (showed) {
			$('#wherefriend').show();	
			$('#wherefriendtext').hide();
		} else {
			$('#wherefriend').hide();
			$('#wherefriendtext').show();					
		}
		
	} else {
		$('#wherecinema').hide();
		$('#wherefriend').hide();
		$('#wherefriendtext').hide();
	
	}
		
}

function cinemaselect(id, name) {
	
	window.parent.document.getElementById("cinemaid").value = id;
	window.parent.document.getElementById("cinemabutton").innerHTML = 'Change Cinema: ' + name;
	
	parent.jQuery(window.parent.document).trigger('close.facebox');
	
}

function showquizoptions() {
	
	if ($('#quizshowlink').html() == 'Show Options') {
		document.getElementById("quizquestion").style.height = '40px';	
		$('#quizshowlink').html("Hide Options");
	} else {
		document.getElementById("quizquestion").style.height = '14px';	
		$('#quizshowlink').html("Show Options");

	}
}

function showmorelocation() {
		
	if ($('#country').val()) {
		$('#locationpickarea').show();
		$('#locationpickareatext').hide();
		
	} else {
		$('#locationpickarea').hide();
		$('#locationpickareatext').show();
		
	}

}

function locationselect(id, name) {
	
	window.parent.document.getElementById("locationid").value = id;
	window.parent.document.getElementById("locationname").innerHTML = name;
	
	parent.jQuery(window.parent.document).trigger('close.facebox');
	
}

function answerquestion() {
	
	var questionid = $('input[name=questionid]').val();
	var quizanswer = $('input[name=quizanswer]:checked').val()
	
	if (!questionid || !quizanswer) {
		alert("You must choose one of the available options to answer a question.");
		return false;
	}

	new $.post('/questionmaster.plm', { action: 'answer', questionid: questionid, quizanswer: quizanswer }, function(resp) { 
		document.getElementById("quizquestion").style.height = '14px';	
		$('#quizshowlink').html("Show Options");		
		$('#quizquestion').html(resp);		
	});
	
	return;	
	
}

function changequestion() {
	
	$('#quizquestion').html('<img src="/images/smallprogress.gif" alt="Progress">');
	
	new $.post('/questionmaster.plm', { }, function(resp) { 
		document.getElementById("quizquestion").style.height = '14px';	
		$('#quizshowlink').html("Show Options");			
		$('#quizquestion').html(resp);
		updatepointsvalue();
	});
	
}

function updatepointsvalue() {
	
	new $.get('/questionmaster.plm', { action: 'currentpoints' }, function(resp) { 
		if (resp) {
			$('span[name=userpointsvalue]').each(function(index, ele) { $(ele).html(resp) });
		}
	});	
	
	return;
	
}

function selecttwitfriend(id) {

	var userlist = $('#whowithlist').val();	
	
	var users;
	if (userlist != undefined) {
		users = userlist.split(/,/);
	} else {
		userlist = "";
		users = new Array();	
	}
	
	if ($.inArray(id, users) > -1) {
		return;
	} else {
		if (userlist.length > 0) {
			userlist += "," + id;	
		} else {
			userlist = id;
		}
		$('#whowithlist').val(userlist);
	}
	
	$('#twitterselected').html(userlist);
	$('#twitterfriend_' + id).hide();
	
}

function enabledisabletweetbox() {
	
	if ($('#check4').attr('checked') == false) {
		$('#tweet').attr('disabled', 'disabled');
	} else {
		$('#tweet').removeAttr('disabled');
	}
	
}

function countchars(box, output) {
	
	var size = box.value.length;

	var color;
	if (size <= 120) {
		color = '#008200';	
	} else {
		color = '#d20000';
	}
	$('#' + output).html('<span style="font-weight:bold;color:' + color + '">' + size + ' characters.</span>');

}

function viewdateupdate() {
	
	var year = $('#seenyeardrop').val();
	var month = $('#seenmonthdrop').val();
	var day = $('#seendaydrop').val();

	document.getElementById('seenmonth').style.display = 'none';
	document.getElementById('seenday').style.display = 'none';		
	
	if (year) {
		document.getElementById('seenmonth').style.display = 'block';	
	}
	if (month) {
		document.getElementById('seenday').style.display = 'block';		
	}

}

function setviewdate(date) {
	
	var dateparts = date.split(/-/);
	
	$('#seenyeardrop').val(dateparts[0]);	
	$('#seenmonthdrop').val(parseInt(dateparts[1], 10));
	$('#seendaydrop').val(parseInt(dateparts[2], 10));
	
	viewdateupdate();
	
}


