/*
*	image swap for view images from product details page
*/

function viewImage(image, size){
	
	if(typeof(size) == 'undefined'){
		image_size = 'full';
	}else{
		image_size = size;
	}
	
	dojo.byId('view_image').src = image.src.replace('small', image_size);
}

function changeName(){
	dojo.byId('name_link').style.display='none';
	dojo.byId('change_name_form').style.display='inline';
	dojo.byId('change_name_link').style.display='none';
	dojo.byId('new_name').focus();
}

function cancelChangeName(){
	dojo.byId('name_link').style.display='inline';
	dojo.byId('change_name_form').style.display='none';
	dojo.byId('change_name_link').style.display='inline';	
}

function saveNewName(){
	
	var new_name = dojo.byId('new_name').value.trim();
	var user_name_regex = /^([a-zA-Z0-9_-]+)$/;
	
	dojo.byId('submit_name_change').disabled=true;
	
	if(user_name_regex.test(new_name)==false){
		alert('Please use alpha numeric only');
		dojo.byId('submit_name_change').disabled=false;
		return false;	
	}
	
	if(new_name.length > 25){
		alert('Maximum character limit is 25');
		dojo.byId('submit_name_change').disabled=false;
		return false;
	}
	
	if(new_name == dojo.byId('name_link').innerHTML){
		cancelChangeName();
		return false;
	}
	
	var post_data = {"new_name":new_name}
	
	dojo.xhrPost ({
		url: '/account/change-username',
		handleAs: "json",
		content: post_data,
		load: function (data) {
			
			if(data.result > 0){
				
				window.location=WEB_PATH + '/' + data.message;
				
			}else{
				
				alert(data.message);
				dojo.byId('submit_name_change').disabled=false;
				return false;
			}
		},
		error: function (error) {
			console.error ('Error: ', error);
		}
	});
}

function expandSection(a, section){
	a.className = (a.className=='on') ? '' : 'on';
	toggleDiv(section);
}

function getNextProduct(index){

	var next_index = current_index + 1;
		current_index = next_index;
		
	if(next_products[next_index] > 0){
		product_id = next_products[next_index];
	}else{
		//reset back to zero
		product_id = next_products[0]
		current_index = 0;
	}
	
	
	var next_product_div = dojo.byId('next_product');
		next_product_div.innerHTML= '<img src="http://images.shopit.com/img/ajax-loader.gif" />';
	
		dojo.xhrGet({
		
		url: '/products/get-product/pid/'+product_id+'/s/medium',
		handleAs: 'json',
		load: function(product){
				next_product_div.innerHTML = TrimPath.processDOMTemplate("next_product_template", product);
			}
		});
}

function addFriend(user_id){
	
	dojo.xhrPost ({
		url: '/friends/add/user/' + user_id,
		handleAs: 'json',
		load: function (data) {
			
			if(data.result > 0){
				dojo.byId('add_to_friends_link').innerHTML='Friend Request Sent!';
				setTimeout(function(){
					dojo.byId('add_to_friends_link').style.display='none';
				},
				3000);
			}else{
				alert(data.message);
				return false;
			}
		
		},
		error: function (error) {
			console.error ('Error: ', error);
		}
    });
}

function openMessageBox(user_id) {
	
	return false;
}


