/*
	Copyright InventMode, Inc.  Patents Pending
*/
var trip_id = -1;
var rotate = 0;
var current_num_promos;
var current_store_id;
var rotating = false;
var store_showing = false;
var viewShowing = false;
var current_view_id = 0;
var delay = 2000;
var changing = false;
var lastUpdate = "2000-01-01 01:00:00";
var shoppingTrip;
function eraseInitial(e){
	if (e.value == "Type message here"){
		e.value="";
	}
}

function showFunctionality(details_id){
	if (!logged_in){
		showMessage('Please login', '<p>You must be logged in to use this functionality. <a href="https://www.shopnchill.com/login">Click Here</a> to login.</p>');
		return;
	}
	if ($('functionality')){
		Element.hide('chat');
		Element.show('functionality');
		Element.removeClassName('chat-tab', 'here');
		Element.addClassName('func-tab', 'here'); 
		switch(details_id){
			case 3:
				changeToLoading('functionality', 'Loading Style Consultants...', 1);
				//$('functionality_header').innerHTML = 'Style Consultants';
				new Ajax.Updater('functionality', '/consultant_request.php?action_type=show_style',{asynchronous:true, onComplete: 
					function(){	
						updatePicture();
						updateMessages();
					}
				});
				break;
			case 4:
				changeToLoading('functionality', 'Loading My Wardrobe...', 1);				
				//$('functionality_header').innerHTML = 'My Wardrobe';
				new Ajax.Updater('functionality', '/consultant_request.php?action_type=show_wardrobe',{asynchronous:true});
				break;
			case 5:
				changeToLoading('functionality', 'Loading Shared Items...', 1);
				//$('functionality_header').innerHTML = 'Shared Items';
				new Ajax.Updater('functionality', '/shopping_trips_request.php?action_type=get_my_shared_items', {asynchronous: true});
				break;
			case 1:
				//changeToLoading('functionality', 'Loading My Friends...', 1);
				//new Ajax.Updater('functionality', '/shopping_trips_request.php?action_type=get_friends_page', {asynchronous: true});
				//break;
			case 6:
			
				changeToLoading('functionality', 'Loading Friends...', 1);
				new Ajax.Updater('functionality', '/shopping_trips_request.php?action_type=get_friends_views', {asynchronous:true, evalScripts: true});
				break;
		}
	}
}

function reDraw(){
}


function printError(){
	var error = escape($('shopping_message').innerHTML);
	new Ajax.Updater('main_panel_contents', '/shopping_trips_request.php?action_type=get_shopping_split', {asynchronous:true});
}

function showError(){
	new Effect.Appear($('shopping_message'), {duration:.5}); 
	window.setTimeout('Effect.Fade(\'shopping_message\', {duration:.3})',3500);
}

function updatePicture(){
	if ($('select_consultant').value){
		new Ajax.Updater('consultant_rating', '/consultant_request.php?action_type=update_rating&consultant_id=' + $('select_consultant').value, {asynchronous:true});
		new Ajax.Updater('consultant_main_image_container', '/consultant_request.php?action_type=get_consultant_image&consultant_id=' + $('select_consultant').value, {asynchronous:true});
	}
}

function changeTrip(rownum){
	var table = $('shopping_trips');
	var l = table.rows.length;
	for (var i = 0; i < l; i++){
		if (i == rownum){
			table.rows[i].style.background="#fff6c2";
			var switch_id = parseInt(table.rows[i].id.substr(table.rows[i].id.indexOf("_") + 1, 25));
			trip_id = switch_id;
			new Ajax.Updater('trips_friends_list', '/shopping_trips_request.php?action_type=get_trip_friends_list&shopping_id=' + switch_id, {asynchronous:true});
		}else if (i%2 == 0){
			table.rows[i].style.background="#ffffff";
		}else{
			table.rows[i].style.background="#f2f8fb";
		}
	}
}

function noTripSelected(){
	trip_id = -1;
}


function inviteTrip(friend_id){
	if (trip_id != -1){
		showMessage('Shopping Trip', ajaxRequest('/shopping_trips_request.php?action_type=create_shopping_request&customer_invite_id=' + friend_id + '&shopping_id=' + trip_id));
		updateFriendsOnTrip();
	}else{
		alert('Please select a trip to invite this person on');
	}
}

function addConsultant(consultant_id){
	new Ajax.Request('/consultant_request.php?action_type=add_consultant&consultant_id=' + consultant_id, {asynchronous:true, onComplete: updateConsultants});
}

function updateConsultants(){
	new Ajax.Updater('functionality', '/consultant_request.php?action_type=show_style&added=true',{asynchronous:true, onComplete: function(){	updatePicture();updateMessages();}});
}

function removeConsultant(consultant_id){
	if (consultant_id){
		new Ajax.Request('/consultant_request.php?action_type=remove_consultant&consultant_id=' + consultant_id, {asynchronous:true, onComplete: updateConsultants });
	}
}

function eraseInitial(e){
	if (e.value.indexOf("Type message here") != -1){
		e.value="";
	}
}

function sendConsultingMessage(message, consultant_id){
	message = message.replace(/&/,"%26");
	new Ajax.Request('/consultant_request.php?action_type=send_message&message=' + message + '&consultant_id=' + consultant_id, {asynchronous:true, onComplete: updateMessages });
	$('consulting_message').value = 'Type message here';
}

function checkPress(e, message, consultant_id){
	if (e.keyCode == 10 || e.keyCode == 13){
		sendConsultingMessage(message, consultant_id);
		$('consulting_message').blur();
	}
}

function checkFPress(e, message, friend_id){
	if (e.keyCode == 10 || e.keyCode == 13 && friend_id != 0){
		sendFriendMessage(message, friend_id);
		$('friend_message').blur();
	}
}

function sendFriendMessage(message, friend_id){
	message = message.replace(/&/,"%26");

	if (friend_id.substr(0, 4) == 'pers'){
		changeToLoading('chat_body', 'Loading Messages...', 1);
		var switch_id = parseInt(friend_id.substr(friend_id.indexOf("_") + 1, 25));
		new Ajax.Request('/chatdata.php?action_type=send_message&message=' + message + '&friend_id=' + switch_id, {asynchronous:true, onComplete: setChatFocus });
		
	}else if (friend_id.substr(0, 4) == 'trip'){
		$('chat_body').innerHTML = '<div style="width:100%; text-align: center;">Sending message...<br><img src="/images/main/loading_chat.gif"/></div>';
		var switch_id = parseInt(friend_id.substr(friend_id.indexOf("_") + 1, 25));
		new Ajax.Request('/chatdata.php?action_type=send_message&message=' + message + '&shopping_id=' + switch_id, {asynchronous:true, onComplete: setChatFocus });
	}
	$('friend_message').value = '';
}

function setChatFocus(){
	chatUpdate();
	$('friend_message').focus();
}
function updateChatMessages(){
	var check_update = ajaxRequest("/chatdata.php?action_type=check_update");
	if (check_update == 'true'){
		$('chat-tab').innerHTML = 'Chat - New Message!';
		chatUpdate();
	}else{
		setTimeout('updateChatMessages()', 5000);
	}
}

function chatUpdate(){
	scroll1 = $('chat_body').scrollHeight;
	if ($('select_friend').value == 'none'){
		return;
	}
	if ($('select_friend').value.substr(0, 4) == 'pers'){
		var switch_id = parseInt($('select_friend').value.substr($('select_friend').value.indexOf("_") + 1, 25));
		var expireDate = new Date();
		var expstring=expireDate.setDate(expireDate.getDate()+1);
		document.cookie = "chatck="+switch_id+"; expires="+expireDate.toGMTString()+"; path=/";
		new Ajax.Updater('chat_body', '/chatdata.php?action_type=get_chatdata&friend_id=' + switch_id, {asynchronous:true, onComplete: moveScrollChat}); 
		setTimeout('updateChatMessages()', 5000);
	}else if ($('select_friend').value.substr(0, 4) == 'trip'){
		var switch_id = parseInt($('select_friend').value.substr($('select_friend').value.indexOf("_") + 1, 25));
		new Ajax.Updater('chat_body', '/chatdata.php?action_type=get_chatdata&shopping_id=' + switch_id, {asynchronous:true, onComplete: moveScrollChat}); 
		setTimeout('updateChatMessages()', 5000);
	}
	
}

function moveScrollChat(){
	var element_div = $('chat_body');
	if (element_div.scrollHeight > scroll1){
		element_div.scrollTop = element_div.scrollHeight;
	}
}

function updateMessages(){
	if ($('select_consultant')){
		scroll2 = $('consultant_chat').scrollHeight;
		if ($('select_consultant').value){	
			new Ajax.Updater('consultant_chat', '/consultant_request.php?action_type=get_messages&consultant_id=' + $('select_consultant').value, {asynchronous:true, onComplete: moveScroll });
		}
	}
}

function moveScroll(){
	var element_div = $('consultant_chat');
	if (element_div.scrollHeight > scroll2){
		element_div.scrollTop = element_div.scrollHeight;
	}
}

function changeChat(user_id){
	var i;
	var element = $('select_friend');
	var expireDate = new Date();
	var expstring=expireDate.setDate(expireDate.getDate()+1);
	document.cookie = "chatck="+user_id+"; expires="+expireDate.toGMTString()+"; path=/";
	
	for (i = 0; i < element.length; i++){
		if (element.options[i].value == 'person_' + user_id){
			element.selectedIndex = i;
			chatUpdate();
			Element.show('chat');
			Element.addClassName('chat-tab', 'here');
			Element.removeClassName('func-tab', 'here');
			Element.hide('functionality');
		}
	}
}

function changeWardrobe(product_id){
	if (product_id){
		new Ajax.Updater('wardrobe_details', '/consultant_request.php?action_type=details&product_id=' + product_id, {asynchronous:true, evalScripts: true, onComplete: reShowWardrobe});
	}else{
		$('hanger_details').style.display = 'block';
		$('hanger_details').innerHTML = "<div id='wardrobe_product_image' style='position: relative; margin: 0px auto; width: 150px; padding-top: 90px'>You have not added any clothing to your wardrobe yet. <a href='/demo_site/catalog/'>Click Here</a> to start shopping!</div>";
	}
	wardrobe_details_showing = false;
}

function reShowWardrobe(){
	new Effect.SlideDown($('wardrobe_product_details'), {duration:.5});
}

function setXY(e, divname){
	new Effect.Move ($(divname),{ x: 0, y: 0, mode: 'absolute', duration: 0});
	new Effect.Move ($(divname),{ x: e.clientX - 60, y: e.clientY - 40, mode: 'fixed', duration: 0});
}

function showTripOptions(){
	$('trip_options').style.display='block';
	new Effect.Appear($('trip_options'), {duration:.5});						
}

function updateFriendsOnTrip(){
	new Ajax.Updater('trips_friends_list', '/shopping_trips_request.php?action_type=get_trip_friends_list&shopping_id=' + trip_id, {asynchronous:true});

}

function searchPress(e, term){
	if (e.keyCode == 10 || e.keyCode == 13){
		my_terms = term.replace(/^\s+|\s+$/g,"");
		my_terms=my_terms.replace(/ /, "~");
		new Ajax.Updater('friend_search_results', '/shopping_trips_request.php?action_type=search_person&search_terms='+my_terms, {asynchronous: true});
	}
}

function searchPress2(term){
	my_terms = term.replace(/^\s+|\s+$/g,"");
	my_terms=my_terms.replace(/ /, "~");
	new Ajax.Updater('friend_search_results', '/shopping_trips_request.php?action_type=search_person&search_terms='+my_terms, {asynchronous: true});
}

function changeDetails(product_id){
	new Ajax.Updater('product_new_main', '/new_products_request.php?action_type=details&product_id=' + product_id, {asynchronous:true, evalScripts: true});
	details_showing = false;
}

function dropDownDetails(){
	if (!details_showing){
		new Effect.BlindDown($('product_details'), {duration:.3});
		details_showing = true;
	}
}

function toggle_visibility(id) {
	var e = $(id);
	if (e){
		if(e.style.display == 'none')
			e.style.display = 'block';
		else
			e.style.display = 'none';
	}
}

function hide_element(id){
	var e = $(id);
	e.style.display = 'none';
}
function show_element(id){
	var e = $(id);
	e.style.display = 'block';
}
function setXY2(divname, button_id){
	var div = $(divname);
	var button = $(button_id);
	new Effect.Move (div,{ x: button.offsetLeft + 16, y: button.offsetTop + 32, mode: 'absolute', duration: 0});
}

function showFittingRoom(){
	$('functionality_header').innerHTML = 'Fitting Room';
	new Ajax.Updater('functionality', '/fitting_room_request.php?action_type=get_products', {asynchronous:true});

}

function showShoppingTrips(){
	new Effect.Appear('friend_chooser');
}


function dropDownWardrobeDetails(){
	if (!wardrobe_details_showing){
		new Effect.BlindDown($('wardrobe_product_details'), {duration:.3});
		wardrobe_details_showing = true;
	}
}

function loadFriend(friend_id){	
	changeToLoading('friend_details','Please wait while we load your friend\'s details...', 1);
	new Ajax.Updater('friend_details', '/facebook_request.php?action_type=get_friends_module&facebook_id=' + friend_id, {asynchronous:true});
}

function loadMyspaceFriend(friend_id){
	changeToLoading('friend_details2','Please wait while we load your friend\'s details...', 1);
	new Ajax.Updater('friend_details2', '/myspace.php?action_type=get_friends_details&myspace_id=' + friend_id, {asynchronous:true});
}

function loadFriendsterFriend(friend_id){
	changeToLoading('friend_details3','Please wait while we load your friend\'s details...', 1);
	new Ajax.Updater('friend_details3', '/friendster.php?action_type=get_friends_details&friendster_id=' + friend_id, {asynchronous:true});	
}

function showShoppingTrips(){
	new Effect.Appear('trip_options');
}

function updateView(){
	if (viewShowing){
		new Ajax.Updater('center_widget', '/shopping_trips_request.php?action_type=get_last_viewed&customers_id='+ current_view_id, {asynchronous:true, evalScripts: true, onComplete: function(){
			if ($('product_info_image_link')){
				FancyZoomBox.directory = '/images/main/zoom';
				new FancyZoom('product_info_image_link');
			}
		}});
	}
}

function checkUpdate(){
	var results = ajaxRequest('/shopping_trips_request.php?action_type=check_view&customers_id='+ current_view_id+"&lastupdate=" + escape(lastUpdate));
	if (results.indexOf('false') == -1){	
			updateView();
			delay = 3000;
	}else{
		if (results.indexOf('rotateonly') != -1){
			var info = results.split('~');
			if (info.length == 4){
				var current_frame = parseInt(info[1]);
				var total_frames = parseInt(info[2]);

				$('pimage').writeAttribute('current', current_frame);
				
				$('pimage').src=$('pimage').readAttribute('path')+'img'+formatNumber(current_frame)+'.jpg&h=300&w=220';
				
				
				lastUpdate = info[3];
				
			}
		}
		//delay += 500;
	}
	
	/*if (delay > 10000){
		$('view_sleep').innerHTML = '<a href="javascript: void(0);" onClick="delay=2000; checkUpdate(); $(\'view_sleep\').innerHTML = \'\';"><img src="/images/common/clock.png" height="16px" width="16px" align="top">&nbsp;Wake Up</a>';
	}else{
		window.setTimeout('checkUpdate()',delay);
	}*/
	window.setTimeout('checkUpdate()',delay);
}

function ajaxRequest(request_string){
	if (window.XMLHttpRequest) {
		xmlhttp = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.open('GET', request_string, false);
	xmlhttp.send('');
	if (xmlhttp.status == 200){
		return xmlhttp.responseText;
	}
}
function logoutFB(){
	window.location.reload();
	
}

function logoutFacebook(){
		document.facebook_logout.submit();
		window.location.href="/social.php?action_type=logout_fb";
}

function startRotator(num_promos, store_id){
	current_num_promos = num_promos;
	current_store_id = store_id;
	if (!rotating){
		rotating = true;
		rotate_ads();
	}
}

function rotate_ads(){
	
	rotate += 1;
	if (rotate > current_num_promos){
		rotate = 1;
	}
	if (rotate%2 == 0 && $('store_promo')){
		new Effect.Fade('store_promo', 500);
		$('store_promo1').innerHTML = '<img src="/phpthumb/phpThumb.php?src=/images/stores/promos/' + current_store_id + '/p' + rotate + '.jpg&h=273&w=250"/>';
		new Effect.Appear('store_promo1',500);
	}else if ($('store_promo')){
		new Effect.Fade('store_promo1', 500);
		$('store_promo').innerHTML = '<img src="/phpthumb/phpThumb.php?src=/images/stores/promos/' + current_store_id + '/p' + rotate + '.jpg&h=273&w=250"/>';
		new Effect.Appear('store_promo',500);
	}	
	
	if (current_num_promos > 1){
		window.setTimeout('rotate_ads()', 3000);
	}else{
		rotating = false;
	}
}


function openStoreFeed(){
	viewShowing = false;
	new Effect.Fade('store_promo_area', 25);
	new Effect.Fade('stores_policies', 25);
	new Effect.Fade('stores_info', 25);
	if ($('stores_category_area')){
		new Effect.Fade('stores_category_area', 25);
		
	}
	new Effect.Appear('stores_feed',100);
}

function showVStore(){
	viewShowing = false;
	new Effect.Fade('store_promo_area', 25);
	new Effect.Fade('stores_policies', 25);
	new Effect.Fade('stores_feed', 25);
	new Effect.Fade('stores_info', 25);
	if ($('stores_category_area')){
		new Effect.Fade('stores_category_area', 25);
		
	}
	new Effect.Appear('virtual_store',100); 
}
function openStoresPolicies(){
	viewShowing = false;
	new Effect.Fade('store_promo_area', 25);
	new Effect.Fade('stores_feed', 25);
	new Effect.Fade('stores_info', 25);
	if ($('stores_category_area')){
		new Effect.Fade('stores_category_area', 25);
	}
	
	new Effect.Appear('stores_policies',100); 
}

function openStoresInfo(){
	viewShowing = false;
	new Effect.Fade('store_promo_area', 25);
	try{
		new Effect.Fade('stores_feed_area', 25);
	}catch(e){
	}
	new Effect.Fade('stores_policies', 25);
	if ($('stores_category_area')){
		new Effect.Fade('stores_category_area', 25);
	}
	new Effect.Appear('stores_info',100);
}

function openStorePromo(){
	viewShowing = false;
	new Effect.Fade('stores_feed', 25);
	new Effect.Fade('stores_policies', 25);
	new Effect.Fade('stores_info', 25);
	if ($('stores_category_area')){
			new Effect.Fade('stores_category_area', 25);
	}
	new Effect.Appear('store_promo_area',100);
}

function placeHolder(message){
	if (!message){
		$('panel').innerHTML='<div style="text-align: center; width: 100%; top: 200px; position: absolute;">Loading Store...<br><img src="/images/main/process.gif"/></div>';
	}else{
		$('panel').innerHTML='<div style="text-align: center; width: 100%; top: 200px; position: absolute;">'+message+'<br><img src="/images/main/process.gif"/></div>';
	}
}


function placeHolder2(){
	$('panel').innerHTML='<div style="text-align: center; width: 100%; top: 200px; position: absolute;">Loading Category...<br><img src="/images/main/process.gif"/></div>';
}

function openStore(store_id){
	viewShowing = false;
	placeHolder();
	new Ajax.Updater('panel', '/store_request.php?store_id='+store_id, {asynchronous:true, evalScripts:true});
}

function showCategory(store_id, category_id){
	viewShowing = false;
	placeHolder2();
	store_showing = false;
	new Ajax.Updater('panel', '/load_category.php?store_id='+store_id+'&category_id='+category_id, {asynchronous:true, evalScripts:true});
}

function checkMatch(regex, subject){
	var re = new RegExp(regex);
	if (!subject){
		return false;
	}
	if (subject.match(re)) {
		return true;
	} else {
		return false;
	}
}

function checkRMA(orders_products_id, theForm){
		if (theForm.rma_reason.value.length == 0){
			$('rma_error').innerHTML = '<br>Please enter a reason for your RMA request<br><br>';
			return;
		}
		var request = ajaxRequest('/shopping_trips_request.php?action_type=check_rma_request&orders_products_id='+orders_products_id);
		if (request.indexOf('false') == -1){
			new Ajax.Updater('rma_requests', '/shopping_trips_request.php?action_type=create_rma_request', {asynchronous: true, parameters: Form.serialize(theForm)});
				
		}else{
			$('rma_error').innerHTML = '<br>There is already an RMA request outstanding for this product<br><br>';
		}
} 

function changeToLoading(element, message, image_colour){
	switch(image_colour){
		case 0:
			$(element).innerHTML = '<div style="width: 100%; text-align: center; margin-top: 150px"><img src="/images/loading/loading.gif"><br><p>'+message+'...</p></div>';
			break;
		case 1:
			$(element).innerHTML = '<div style="width: 100%; text-align: center; margin-top: 10px"><img src="/images/loading/l2.gif"><br><p>'+message+'...</p></div>';
			break;
		case 2:
			$(element).innerHTML = '<div style="width: 100%; text-align: center; margin-top: 10px"><img src="/images/loading/l1.gif"><br><p>'+message+'...</p></div>';
			break;
	}
}

function changeMeasPanel(this_panel_id, new_panel_id){
			
	var this_panel = $(this_panel_id);
	var new_panel = $(new_panel_id);
	this_panel.style.display = "none";
	new Effect.Appear(new_panel, {duration: .5});
}

function showPanel(panelName, element, len){
	for (var i = 0; i < len; i++){
		$('button[' + i + ']').className = 'measure_type';
	}
	
	for (var i = 0; i < len; i++){
		if ($('app['+i+']')){
			$('app['+i+']').style.display="none";
		}
	}
	element.className = 'measure_type_sel';
	new Effect.Appear(panelName, 500);
}

function showProfile(customer_id, customer_name){
	//placeHolder('Loading '+customer_name+'\'s profile...');
	new Ajax.Updater('center_widget', '/profile.php?customer_id='+customer_id,{asynchronous: true, evalScripts:true});
}

function showMessage(message_title, message){
	
	var popupmessage = document.createElement('div');
	
	popupmessage.setAttribute('class','popup_message');
	popupmessage.setAttribute('id','popup_message');
	popupmessage.setAttribute('onClick','');
	var innerHTML = '<table cellspacing="0px" cellpadding="0px"><tr><td class="pop_topleft"></td><td class="pop_border"></td><td class="pop_topright"></td></tr><tr><td class="pop_border"></td><td>';
	if (message_title.length != 0){
		innerHTML += '<div class="message_title">'+message_title+'</div>';
	}
	innerHTML += '<div class="message_body"><p>'+message+'</p><br><div style="text-align:center; margin-top: 5px;"><a href="javascript: void(0);" onClick="$(\'popup_message\').style.display=\'none\'; var olddiv = $(\'popup_message\'); $(\'message_area\').removeChild(olddiv);">OK</a></div></td><td class="pop_border"></td></tr><tr><td class="pop_bottomleft"></td><td class="pop_border"></td><td class="pop_bottomright"></td></tr></table>';
	popupmessage.innerHTML = innerHTML;
	var ni = $('message_area');
	ni.appendChild(popupmessage);
	var screen_height = window.innerHeight;
	if (!screen_height){
			screen_height = document.body.offsetHeight;
	}
	var screen_width = window.innerWidth;
	if (!screen_width){
		screen_width = document.body.offsetWidth;
	}
	$('message_area').style.top=(screen_height/2)-100 + "px";
	$('message_area').style.left=(screen_width/2)-235 + "px";
}

function showURLMessage(message_title, url){
	var data_string = ajaxRequest(url);
	showMessage(message_title, data_string);
}

function createVote(theForm){
	changeToLoading('vote_area', 'Create new vote...', 2);
	new Ajax.Request('/votes.php?action_type=create_vote', {asynchronous: true, parameters: Form.serialize(theForm), 
		onComplete: function(){
									new Ajax.Updater('vote_area', '/votes.php?action_type=myvotes', {asynchronous: true});
									toggle_visibility('create_vote_area');
									document.create_vote.reset();
								}});
}

function deleteVote(vote_id){
	if (confirm('Are you sure you want to delete this vote?')){
		new Ajax.Request('/votes.php?action_type=delete_vote&votes_id='+vote_id, {asynchronous: true, 
			onComplete: function(){
				new Ajax.Updater('vote_area', '/votes.php?action_type=myvotes', {asynchronous: true}); 
			}
		});
	}
}

function checkNumber(evt){
	var charCode = (evt.which) ? evt.which : event.keyCode
  if (charCode > 31 && (charCode < 48 || charCode > 57)){
  	return false;
	}
  return true;
}

function waitCurs(){
	document.body.style.cursor = 'wait';
}

function normCurs(){
	document.body.style.cursor = 'default';
}

function removeFriend(friend_id){
	if (confirm('Are you sure you want to remove this friend?')){
		new Ajax.Request('/shopping_trips_request.php?action_type=remove_friend&customer_remove_id=' + friend_id, {asynchronous: true, onComplete:
		//new Ajax.Updater('rightfriends', '/shopping_trips_request.php?action_type=remove_friend&customer_remove_id=' + friend_id, {asynchronous: true, onComplete: 
			function() { 
				showMessage('Success', '<p>Successfully Removed Friend</p>');
				new Ajax.Updater('functionality', '/shopping_trips_request.php?action_type=get_friends_views', {asynchronous:true});
			}
		});
	}
}

function addShoppingTrip(e, theForm){
	if (e != null){
		if (e.keyCode == 10 || e.keyCode == 13){
			new Ajax.Updater('s-trips', '/shopping_trips_request.php?action_type=add_shopping_trip', {asynchronous:true, evalScripts: true, 
				parameters:Form.serialize(theForm), onComplete: function(){ theForm.trip_name.value=""; }});
		}
	}else{
		new Ajax.Updater('s-trips', '/shopping_trips_request.php?action_type=add_shopping_trip', {asynchronous:true, evalScripts: true, 
			onComplete: function(){ theForm.trip_name.value=""; }, parameters:Form.serialize(theForm)});
	}
		
}

function showView(customers_id){
	var result = ajaxRequest('/shopping_trips_request.php?action_type=check_viewable&customers_id='+customers_id);
	if (result == '1'){
		current_view_id = customers_id;
		if (!viewShowing){ 
			changeToLoading('center_widget', 'Loading view...', 1);
			viewShowing = true;
			lastUpdate = '2000-01-01 01:00:00'; 
			delay = 2000; 
			checkUpdate(); 
		}else{ 
			delay = 5000; 
			viewShowing = true; 
		}
	}else{
		showMessage('Customer Not Sharing View', '<p>This person is not sharing their view</p>');
	}
}

function setStores(stores_id){
	$$('.country_button').invoke('removeClassName','selected');
	$$('.country_button[rel='+stores_id+']').invoke('addClassName','selected');
	changeToLoading('catsearch', 'Loading stores...', 1);
	
	new Ajax.Updater('catsearch', '/get_category.php?country_id='+stores_id, {asynchronous: true, evalScripts: true});
	return false;
}

function getCategory(category_id){
	changeToLoading('catsearch', 'Loading stores...', 1);
	new Ajax.Updater('catsearch', '/get_category.php?category_id='+category_id, {asynchronous: true, evalScripts: true});
}

function loadBlog(url){
	changeToLoading('center_widget', 'Loading Article...', 1);
	new Ajax.Updater('center_widget', url, {asynchronous: true});
}

sfHover = function() {
	var sfEls = document.getElementById("topnav").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}

function lookup(theForm) {

	if(theForm.quickSearch.value.length == 0) {
  	// Hide the suggestion box.
  	 $('suggestions').style.display="none";
  } else { 
    new Ajax.Updater('autoSuggestionsList', '/shopping_trips_request.php?action_type=auto_tags', {asynchronous: true, 
    	parameters: Form.serialize(theForm), 
    	onComplete: function(response){  
    			if (response.responseText.length > 0){ 
    				$('suggestions').style.display="block"; 
    			}else{ 
    				$('suggestions').style.display="none";
    			}
    		}});
	}
} // lookup

function fill(thisValue) {
   $('quickSearch').value = thisValue;
   $('suggestions').style.display="none";
}

function searchEnter(evt){
}

function showVideo(videoCode, description){
	showMessage('Video', '<object width="380" height="234"><param name="movie" value="http://www.youtube.com/v/'+videoCode+'&hl=en&fs=1"></param><param name="allowFullScreen" value="false"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/'+videoCode+'&hl=en&fs=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="false" width="380" height="234"></embed></object><br><p>'+description+'</p>');
}

function imgLoader(num, store_id, product_id, current){
	var path = '/phpthumb/phpThumb.php?src=/images/rotation/'+store_id+'/'+product_id+'/';
	changeToLoading('rotation_area', 'Loading 3D Image...', 1);
	var images=new Array();
	
	for (i = 1; i <= num; i++){
		var image_name = i + '.jpg';
		if (image_name.length == 5){
			image_name = '00'+image_name;
		}else if (image_name.length == 6){
			image_name = '0'+image_name;
		}
		images[i] = new Image();
		images[i].src = path+'img'+image_name+'&h=300&w=220';
	}
	new Ajax.Updater('rotation_area', '/rotator.php',{asynchronous:true, evalScripts: true,parameters: { product_id: product_id, current: current }});
}

function loadImg(path){
	if ($('pimage')){
		var total = parseInt($('pimage').readAttribute('total'));
		var next = parseInt($('pimage').readAttribute('next'));
		var image_name = next + '.jpg';
		if (next == total){
			next = 0;
		}

		if ($('pimage')){
			$('pimage').setAttribute('next', next + 1);
			if (image_name.length == 5){
				image_name = '00'+image_name;
			}else if (image_name.length == 6){
				image_name = '0'+image_name;
			}
			$('pimage').src = path + 'img'+image_name+'&h=300&w=220';
			window.setTimeout("loadImg('"+path+"', 0);",300);
		}
	}
}

function formatNumber(number, zeros){
	number = number + '';
	if (number.length == 1){
		number = '00' + number;
	}else if (number.length == 2){
		number = '0' + number;
	}
	return number;
}

function setDirectoryStore(stores_id, category){
	$$('.directory_button').invoke('removeClassName','selected');
	$$('.directory_button[rel='+stores_id+']').invoke('addClassName','selected');
	changeToLoading('directory_stores', 'Loading stores...', 1);
	if (category){
		new Ajax.Updater('directory_stores', '/directory_stores.php?stores_id='+stores_id+'&category_id='+category, {asynchronous: true, evalScripts: true});
	}else{
		new Ajax.Updater('directory_stores', '/directory_stores.php?stores_id='+stores_id, {asynchronous: true, evalScripts: true});
	}
	return false;
}

if (window.attachEvent) window.attachEvent("onload", sfHover);



function ShoppingTrip(trip_id){
	var tripDetails = ajaxRequest('/tripreplay.php?trip_id='+trip_id);
	this.tripArray = tripDetails.evalJSON();

}

ShoppingTrip.prototype.start = function(){
	$('progress-bar').setStyle({ width: '0px'});
	$('chat_body').innerHTML = '';
	this.play();
	this.timeCounter = 0;
	this.elemCounter = 0;
}

ShoppingTrip.prototype.stop = function(){
	if (this.pe){
		this.pe.stop();
	}
	$('progress-bar').setStyle({ width: '0px'});
	$('replay-button-play').removeClassName('pause');
	$('replay-button-play').addClassName('play');
}

ShoppingTrip.prototype.pause = function(){
	if (this.pe){
		this.pe.stop();
	}
	$('replay-button-play').removeClassName('pause');
	$('replay-button-play').addClassName('play');
	var sTrip = this;
	$('replay-button-play').onclick = function(){
		sTrip.play();
	};
}

ShoppingTrip.prototype.play = function(){
	$('replay-button-play').removeClassName('play');
	$('replay-button-play').addClassName('pause');
	
	var sTrip = this;
	$('replay-button-play').onclick = function(){
		sTrip.pause();
	};

	this.pe = new PeriodicalExecuter(function(pe) {
 		var portion = (sTrip.timeCounter/sTrip.tripArray['maxTime'])*220;
 		$('progress-bar').setStyle({ width: portion+'px'});
 		if (sTrip.tripArray[sTrip.timeCounter] != undefined){
 			if (sTrip.tripArray[sTrip.timeCounter][0]['type'] == 0){
 				$('chat_body').innerHTML = $('chat_body').innerHTML + sTrip.tripArray[sTrip.timeCounter][0]['customers_firstname']+' says<br><div style="padding-left: 4px"><font style="color: #5aaeda">'+sTrip.tripArray[sTrip.timeCounter][0]['message']+'</font></div>';
 				var objDiv = document.getElementById("chat_body");
				objDiv.scrollTop = objDiv.scrollHeight;
 			}else if (sTrip.tripArray[sTrip.timeCounter][0]['type'] == 2){
 				new Ajax.Updater('vidarea', '/loadustream.php?ustream='+sTrip.tripArray[sTrip.timeCounter][0]['url'], {asynchronous: false});
 			}else if (sTrip.tripArray[sTrip.timeCounter][0]['type'] == 3){
 				new Ajax.Updater('vidarea', '/loadyoutube.php?youtube_id='+sTrip.tripArray[sTrip.timeCounter][0]['url'], {asynchronous: false});
 			}else{
				new Ajax.Updater('center_widget', '/shopping_trips_request.php?action_type=get_last_viewed&shopping_viewing_id='+ sTrip.tripArray[sTrip.timeCounter][0]['shopping_viewing_id'], {asynchronous:true, evalScripts: true, onComplete: function(){
					if ($('product_info_image_link')){
						FancyZoomBox.directory = '/images/main/zoom';
						new FancyZoom('product_info_image_link');
					}
				}});
 			}
 			sTrip.elemCounter++;
 		}
		
 		if (sTrip.timeCounter == sTrip.tripArray['maxTime']){
			// you can stop trip now
			$('replay-button-play').removeClassName('pause');
			$('replay-button-play').addClassName('play');
 			sTrip.pe.stop();
			$('replay-button-play').onclick = function(){
				sTrip.start();
			};
 		}
 		sTrip.timeCounter++;
 		
	}, 1);
}

function prepareTrip(){
	new Ajax.Updater('right_panel', '/preparetrip.php', {asychronous: false});
}

function replayTrip(trip_id){
	new Ajax.Updater('replay_area', '/replayer.php?shopping_trip_id='+trip_id, {asynchronous: true, onComplete: function(){
		shoppingTrip = new ShoppingTrip(trip_id); 
		shoppingTrip.start();
	}});
}