// JavaScript Document
function vote(choice_id, outfit){
	displayModalBox('/mainsite/social/vote.php?choice_id='+choice_id+'&vote_for='+outfit,400,100,'');
}

function getGossip(){
	displayModalBox('/mainsite/social/gossip_get_message.php?user_id='+page_id, 400, 200, '');
}

function sendGossip(){
	var escaped = escape($('#new_gossip').val());
	escaped = escaped.replace(/%0A/g,'');
	escaped = escaped.replace(/%0D/g,'');
	if($('#new_gossip').val() == "" || escaped == ""){
		$('#gossip_message').html("You must enter a message then select post.");
		return false;
	}
	loadPage('/mainsite/social/addComment.php?user_id='+page_id+'&message='+escape($('#new_gossip').val()),'gossip_message','gossip_message','','Sending...');	
}

function addFriend(user_id){
	if(user_id==null)
		user_id = '?user_id='+page_id;
	else
		user_id = '?user_id='+user_id;
	displayModalBox('/mainsite/social/addFriendMessage.php'+user_id,400, 190, 'Add friend');
}

function sendAddFriend(user_id){
	if(user_id == null)
		user_id = page_id;
	loadPage('/mainsite/social/addFriend.php?user_id='+user_id+'&message='+escape($('#new_friend').val()),'friend_message','friend_message','','Sending...');	
	$('#new_friend_submit').attr('disabled','true');
}

function maxChars(textfield, message, maxChars){
	charsLeft = maxChars - $('#'+textfield).val().length;
	$("#"+message).html(charsLeft+" characters left");
}

function getWardrobe(type){
	if(type==null)
		type = "";
	loadPage('/mainsite/social/get_wardrobe.php?user_id='+page_id+'&type='+type,'getwardrobe_div');	
}

function browseOutfits(user_id){
	displayModalBox('/mainsite/social/show_outfits.php?user_id='+user_id, 520, 520, 'Browse outfits');
}

function browseWardrobe(user_id){
	displayModalBox('/mainsite/social/show_wardrobe.php?type=&user_id='+user_id, 520, 520, 'Browse wardrobe');
}

function displayPage(type){
	offset = (_page-1)*perpage;
	html = "";
	switch(type){
		case 'hot':
			link_start = "<a href=\"javascript:setHotPiece('itemcode');\">";
			link_end = "</a>";
			break;
		default:
			link_start = "";
			link_end = "";
			break;
	}
	
	for(var i=0; i<perpage; i++){
		if(i+offset < wardrobelist.length)
			html += "<div class='wardrobe_item' onclick='showZoom(\""+wardrobelist[i+offset]+"\")'>"+link_start.replace(/itemcode/,wardrobelist[i+offset])+"<img src='"+getImage(wardrobelist[i+offset])+"' height='100%' border='0' />"+link_end+"</div>";
	}
	$('#wardrobe_page').html(html);
	doPageLinks('wardrobe', type);
}


function displayOutfitPage(){
	offset = (_page-1)*perpage;
	html = "";
	for(var i=0; i<perpage; i++){
		if(i+offset < outfitslist.length)
			html += "<div class='outfit_item'><img src='"+outfitslist[i+offset]+"' height='100%' /></div>";
	}
	$('#wardrobe_page').html(html);
	doPageLinks('outfits');
}

function displayOutfitChoicesPage(){
	offset = (_page-1)*perpage;
	html = "";
	for(var i=0; i<perpage; i++){
		if(i+offset < outfitslist.length)
			html += "<div class='outfit_item'><a href='javascript:addOutfitChoice("+(i+offset)+");'><img src='"+outfitslist[i+offset]+"' height='100%' border='0' /></a></div>";
	}
	$('#wardrobe_page').html(html);
	doPageLinks('outfit_choices');
}

function displayAdviserPage(){
	offset = (_page-1)*perpage;
	html = "";
	for(var i=0; i<perpage; i++){
		if(i+offset < adviserlist.length)
			html += "<div class='stylist_item'><a href='/stylists/"+advisernames[i+offset]+"' class='option' style='text-transform:none;'><img src='/mainsite/social/"+adviserlist[i+offset]+"' height='90' border='0' /><br>"+advisernames[i+offset]+"</a></div>";
	}
	$('#wardrobe_page').html(html);
	doPageLinks('advisers');
}

function displaySearchAdviserPage(){
	offset = (_page-1)*perpage;
	html = "";
	for(var i=0; i<perpage; i++){
		if(i+offset < adviserlist.length){
			if(pendingadviser.indexOf(adviserIDs[i+offset]) == -1){
				html += "<div class='stylist_item_add'><a href='javascript:addFriend("+adviserIDs[i+offset]+");' class='option' style='text-transform:none;'><img src='/mainsite/social/"+adviserlist[i+offset]+"' height='90' border='0' /><br>"+advisernames[i+offset]+"<br><img src='/mainsite/images/skins/"+skin_name+"/add_advisers.png' border='0' align='absmiddle'><span style='text-transform:uppercase; padding-left:5px;'>Add</span></a> | <a href='/stylists/"+advisernames[i+offset]+"' class='option'><span style='text-transform:uppercase;'>View</span></a></div>";
			} else {
				html += "<div class='stylist_item_add'><img src='/mainsite/social/"+adviserlist[i+offset]+"' height='90' border='0' /><br>"+advisernames[i+offset]+"<br><img src='/mainsite/images/skins/"+skin_name+"/pending_advisers.png' border='0' align='absmiddle'><span style='text-transform:uppercase; padding-left:5px; font-size:0.8em;'>Pending</span></a></div>";
			}
		}
	}
	$('#wardrobe_page').html(html);
	doPageLinks('search_advisers');
}

function displayPendingAdviserPage(){
	offset = (_page-1)*perpage;
	html = "";
	for(var i=0; i<perpage; i++){
		if(i+offset < adviserlist.length){
			html += "<div class='stylist_item_pending'><img src='/mainsite/social/"+adviserlist[i+offset]+"' height='90' border='0' /><br>"+advisernames[i+offset]+"<div class='stylist_item_pending_links'>"+
			"<a href='javascript:confirmFriend("+adviserIDs[i+offset]+");' class='option'>Confirm</a> |"+
			"<a href='javascript:denyFriend("+adviserIDs[i+offset]+");' class='option' style='padding-left:5px;'>Deny</a><br>"+
			"<a href='/stylists/"+advisernames[i+offset]+"' class='option'>View</a>"+
			"</div></div>";
		}
	}
	$('#wardrobe_page').html(html);
	doPageLinks('pending_advisers');
}

function displayEditAdviserPage(){
	offset = (_page-1)*perpage;
	html = "";
	for(var i=0; i<perpage; i++){
		if(i+offset < adviserlist.length){
			html += "<div class='stylist_item_pending'><img src='/mainsite/social/"+adviserlist[i+offset]+"' height='90' border='0' /><br>"+advisernames[i+offset]+"<div class='stylist_item_pending_links'>"+
			"<img src='/mainsite/images/skins/"+skin_name+"/clear_advisers.png' border='0' align='absmiddle'><a href='javascript:removeAdviser("+adviserIDs[i+offset]+");' class='option' style='padding-left:5px;'>Remove</a>"+
			"</div></div>";
		}
	}
	$('#wardrobe_page').html(html);
	doPageLinks('edit_advisers');
}

function doPageLinks(type, linktype){
	switch(type){
		case 'wardrobe':
			array = wardrobelist;
			break;
		case 'outfits':
		case 'outfit_choices':
			array = outfitslist;
			break;
		case 'advisers':
		case 'search_advisers':
		case 'pending_advisers':
		case 'edit_advisers':
			array = adviserlist;
	}
	total = array.length;
	pages = Math.ceil(total/perpage);
	html = "";
	if(pages>0){
		if(_page>1)
			html += "<a href=\"javascript:prevPage('"+type+"', '"+linktype+"');\">&lt; previous</a> | ";
		html += "page "+_page+" of "+pages+" ";
		if(_page<pages)
			html += "| <a href=\"javascript:nextPage('"+type+"', '"+linktype+"');\">next &gt;</a>";
	}
	$('#wardrobe_page_links').html(html);
}

function nextPage(type, linktype){
	_page ++;
	switch(type){
		case 'wardrobe':
			displayPage(linktype);
			break;
		case 'outfits':
			displayOutfitPage();
			break;
		case 'outfit_choices':
			displayOutfitChoicesPage();
			break;
		case 'advisers':
			displayAdviserPage();
			break;
		case 'search_advisers':
			displaySearchAdviserPage();
			break;
		case 'pending_advisers':
			displayPendingAdviserPage();
			break;
		case 'edit_advisers':
			displayEditAdviserPage();
			break;
	}
}

function prevPage(type, linktype){
	_page --;
	switch(type){
		case 'wardrobe':
			displayPage(linktype);
			break;
		case 'outfits':
			displayOutfitPage();
			break;
		case 'outfit_choices':
			displayOutfitChoicesPage();
			break;
		case 'advisers':
			displayAdviserPage();
			break;
		case 'search_advisers':
			displaySearchAdviserPage();
			break;
		case 'pending_advisers':
			displayPendingAdviserPage();
			break;
		case 'edit_advisers':
			displayEditAdviserPage();
			break;
	}
}

function getImage(itemcode){
	var rcode = itemcode.substr(2,2);
	return '/nn4m/dbproduct/'+rcode+'/small/'+itemcode+'.jpg';
}

function editSites(){
	displayModalBox('/mainsite/social/edit_sites.php?user_id='+page_id, 500, 300, 'Edit your other sites:');
	
	$('#modal_dialogue').dialog('option', 'buttons', { "Update": function() { updateSites(); },
													   "Close" : function() { $(this).dialog("close"); }
													 });
}

function updateSites(){
	url = "/mainsite/social/update_sites.php?user_id="+page_id+"&"+$('#sites_form').serialize();
	loadPage(url,'sites_message');
}

function clearTextField(name, defaultVal){
	if(defaultVal == null)
		defaultVal = "";
	$("#"+name).val(defaultVal);	
}

function removeGossip(id){
	displayModalBox('/mainsite/social/gossip_confirm_remove.php?mess_id='+id, 400, 100, 'Confirm');	
}

function confirmRemoveGossip(id){
	loadPage('/mainsite/social/gossip_remove.php?mess_id='+id, 'gossip_remove_message');	
}

function editHotPiece(){
	displayModalBox('/mainsite/social/show_wardrobe.php?user_id='+page_id+'&type=hot', 520, 520, 'Select the item you want as your favourite piece');	
}

function setHotPiece(itemcode){
	loadPage('/mainsite/social/hot_item_set.php?itemcode='+itemcode, 'wardrobe_message');	
}

function editProfile(){
	displayModalBox('/mainsite/social/update_profile.php?user_id='+page_id, 590, 490, 'Edit Profile');
	
	if($('#wardrobe_div')){
		setTimeout(1000,$('#wardrobe_div').css('visibility', 'visible'));	
	}
}

function showFileProgress(){
	$('#file_upload_message').html("Uploading <img src='/mainsite/images/loader.gif' align='absmiddle' />");
	$('#avatar_form').submit();
}

function friendSearch(){
	searchval = $('#friend_search').val();
	if(searchval.length>1)
		displayModalBox('/mainsite/social/search_advisers.php?search='+searchval, 500, 440, 'Find Friends');	
}

function friendRequests(){
	displayModalBox('/mainsite/social/pending_advisers.php?user_id='+page_id, 500, 360, 'Friend Requests');	
}

function confirmFriend(friend_id){
	loadPage('/mainsite/social/update_advisers.php?action=confirm&friend_id='+friend_id,'wardrobe_message');	
}

function denyFriend(friend_id){
	loadPage('/mainsite/social/update_advisers.php?action=deny&friend_id='+friend_id,'wardrobe_message');	
}

function editChoices(){
	displayModalBox('/mainsite/social/edit_choices.php?user_id='+page_id, 670, 510, 'Select the two outfits that want your friends to vote on.');
		
	$('#modal_dialogue').dialog('option', 'buttons', { "Update": function() { saveOutfitChoice(); },
													   "Close" : function() { $(this).dialog("close"); }
													 });
}

function addOutfitChoice(outfit_index){
	if(outfit_choices.length == 0){
		outfit_choices[0] = outfitIDs[outfit_index];
		$('#outfit1').html("<p align='center'><img src='"+outfitslist[outfit_index]+"' height='150' /></p>");
	} else 	if(outfit_choices.length == 1){
		outfit_choices[1] = outfitIDs[outfit_index];
		$('#outfit2').html("<p align='center'><img src='"+outfitslist[outfit_index]+"' height='150' /></p>");
	} else {
		alert("You can only select two outfits. Please remove one of your selections first.");	
	}
}

function clearOutfitChoice(index){
	outfit_choices.splice(index,1);
	$('#outfit'+(index+1)).html("<p align='center'></p>");
	
	if(index==0){
		$('#outfit1').html($('#outfit2').html());
		$('#outfit2').html("<p align='center'></p>");		
	}
}

function saveOutfitChoice(){
	if(outfit_choices.length<2){
		alert("You must select two outfits to proceed");
	} else {
		loadPage('/mainsite/social/save_outfit_choice.php?user_id='+page_id+'&outfit1='+outfit_choices[0]+'&outfit2='+outfit_choices[1], 'outfit_choice_save');
	}
}

function showAllAdvisers(user_id){
	displayModalBox('/mainsite/social/show_advisers.php?user_id='+user_id, 520, 500, 'All Friends');
}

function editAdvisers(){
	displayModalBox('/mainsite/social/edit_advisers.php?user_id='+page_id, 550, 500, 'Edit Friends');	
}

function removeAdviser(id){
	if(confirm("Are you sure you want to remove this friend?")){
		loadPage('/mainsite/social/remove_adviser.php?user_id='+page_id+'&friend_id='+id, 'remove_friend');	
	}
}

function getSelectionStart(o) {
	if (o.createTextRange) {
		var r = document.selection.createRange().duplicate()
		r.moveEnd('character', o.val().length)
		if (r.text == '') return o.val().length
		return o.val().lastIndexOf(r.text)
	} else return o.selectionStart
}

function getSelectionEnd(o) {
	if (o.createTextRange) {
		var r = document.selection.createRange().duplicate()
		r.moveStart('character', -o.val().length)
		return r.text.length
	} else return o.selectionEnd
}

function addSmiley(val, textfield){
	var v = textfield.val();
	var s = getSelectionStart(textfield);
	var e = getSelectionEnd(textfield);
	
	var newText = v.substr(0,s) + val + v.substr(e);
	textfield.val() = newText;
}


function checkDetails(button){
	if($('#pass1').val() != ""&& !validatePassword())
		return false;
  
  //IF THE USER ENTERED A STUDENT CODE AND CLICKED CONFIRM, OR IF THEY DIDN'T
  //ENTER A STUDENT CODE AND CLICKED UPDATE
  if(button == 'confirm' || $('#scode').val() == "")  {
	loadPage('/mainsite/social/save_profile_details.php?user_id='+page_id+'&'+$('#details_form').serialize(), 'details_upload_message');
  } else if(button == 'update'){
  	loadPage('/mainsite/social/confirm_profile_details.php?user_id='+page_id+'&'+$('#details_form').serialize(), 'details_upload_message');
  }
}

function showTab(tabs, selectedTab){
	for(var i=0; i<tabs.length; i++){
		$("#"+tabs[i]+'_tab').removeClass();	
		$("#"+tabs[i]+'_pane').css('display',"none");	
	}
	$("#"+selectedTab+'_tab').addClass("current");
	$("#"+selectedTab+'_pane').css('display', "block");
	
	switch(selectedTab){
		case 'details':
			$('#modal_dialogue').dialog('option', 'buttons', { "Update": function() { checkDetails('update'); },
															   "Close" : function() { $(this).dialog("close"); }
															 });
			break;
		case 'image':
			$('#modal_dialogue').dialog('option', 'buttons', { "Update": function() { showFileProgress(); },
															   "Close" : function() { $(this).dialog("close"); }
															 });
			break;
		case 'skin':
			$('#modal_dialogue').dialog('option', 'buttons', { "Update": function() { changeSkin(); },
															   "Close" : function() { $(this).dialog("close"); }
															 });
			break;
	}
}

var profileTabs = new Array('details', 'image', 'skin', 'layout');

function prevSkin(){
	var idx = $('#selected_skin').val();
	
	$('#sample_box').css('borderColor', borderColours[idx]);
	$('#sample_box').css('backgroundColor', boxColours[idx]);
	
	$('#sample_banner').css('color', headerTextColours[idx]);
	$('#sample_banner').css('backgroundColor', headerColours[idx]);
	
	$('#sample_link').css('color', linkColours[idx]);
}

function changeSkin(){
	var idx = $('#selected_skin').val();
	loadPage('/mainsite/social/save_skin.php?user_id='+page_id+'&skin_id='+idx, 'skin_upload_message');
}

function enlargeFavourite(url, w, h){
	displayModalBox(url, w, h, '');	
}

function storeWindowSize() {
  return;
	
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
 
  loadPage('/mainsite/social/window_size.php?width='+myWidth+'&height='+myHeight, '');
}

function validatePassword(){
	if($('#pass1').val() != $('#pass2').val()){
		alert("The passwords you have entered do not match");
		return false;
	} else if($('#pass1').val().length < 6){
		alert("The password you have entered is not long enough, it must be between 6 and 15 characters long");
		return false;
	}
	return true;
}

function showZoom(img){
	retailer = img.substr(2,2);
	type = img.substr(0,2);
	img_url = '/nn4m/dbproduct/'+retailer+'/large/'+img;
	
	switch(type){
		case 'TP':
		case 'SK':
		case 'DR':
		case 'PA':
		case 'JA':
			img_url+='.gif';
			break;
		default:
			img_url+='.jpg';
			break;
	}
	
	$('#wardrobe_zoom_image').attr('src', img_url);
	$('#wardrobe_zoom').css('display', 'block');
}

function closeZoom(){
	$('#wardrobe_zoom').css('display', 'none');
}

function addToWardrobe(itemcode){
	displayModalBox('/mainsite/social/addToWardrobe.php?itemcode='+itemcode, 300, 160, '');	
}

function removeFromWardrobe(itemcode){
	displayModalBox('/mainsite/social/removeFromWardrobe.php?itemcode='+itemcode, 300, 160, '');	
}


var currHelp = 1;

function showHelp(number){
	for(i = 1; i < 5; i++){
		$('#help_0'+i+'_img').attr('src', 'images/help_0'+i+'.jpg');
		hide('help_0'+i+'_content');
	}
	
	currHelp = number;
	$('#help_0'+number+'_img').attr('src', 'images/help_0'+number+'_over.jpg');
	show('help_0'+number+'_content');
}
