///////////////////////////////////////////////////   
// Adding namespace support

//if (typeof(ASPOT) == "undefined") ASPOT = {}
ASPOT = YAHOO.namespace("ASPOT");

///////////////////////////////////////////////////   
String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.startsWith = function(str) {
	return (this.match("^"+str)==str)
}
String.prototype.endsWith = function(str) {
	return (this.match(str+"$")==str)
}

function $(el) {
	return document.getElementById(el);
}

///////////////////////////////////////////////////   
///////////////////////////////////////////////////   
function getXMLHttpReq () {
     if (window.XMLHttpRequest) {
         return new XMLHttpRequest();
     } else if (window.ActiveXObject) {
         return new ActiveXObject("Microsoft.XMLHTTP");
     } else {
       return null;
     }
}

///////////////////////////////////////////////////   
function searchclick(el) {
	el.value="";
	el.style.fontStyle='normal';
	el.style.color="#000000";
}

function chk_search(el) {
	if (el.value == 'search AppSpotlight') {
		el.value = '';
		return false;
	} else {
		return true;
	}
}
//////////////////////////////////////////////////////////////////
function limit_text(box, limit) {
	if (box.value.length > limit) {
		box.value = box.value.substr(0, limit);
		alert("Sorry, you have reached the size limit for this field which is " + limit + " characters")
	}
}

function chk_cancel() {
    return confirm(
      "Are you sure you want to cancel and discard this information?\nIf you want to cancel, click 'Yes' or 'OK'");
}
//////////////////////////////////////////////////////////
//get another set of featured apps to display

var feat_req;
var curr_feat_offset = 0
var feat_start = 0;

function chg_feat(base_cgi, ajx_url, is_more, num_apps, base) {
 // is_more if 'More>> request', otherwise '<<More'
	
 feat_base = base;
 feat_req = getXMLHttpReq(); 
 if (feat_req == null) return; // no can do

 if (is_more) {
	 old_num = curr_feat_offset 
	 curr_feat_offset += 4;
	 if (curr_feat_offset > num_apps) {
		 curr_feat_offset = old_num;
	 }
	 if (curr_feat_offset+2 >= num_apps) {
		 $('feat_more').style.visibility='hidden';
	 } else {
		 $('feat_more').style.visibility='visible';
	 }
	 if (curr_feat_offset > 0) {
		 $('feat_less').style.visibility='visible';
	 }
	 
 } else {
	 curr_feat_offset -= 4;
	 if (curr_feat_offset <= 0) {
		 curr_feat_offset = 0;
		 $('feat_less').style.visibility='hidden';
	 } else {
		 $('feat_less').style.visibility='visible';
	 }
	 if ( (curr_feat_offset+2) < num_apps) {
		 $('feat_more').style.visibility='visible';
	 }
 }

 var domain = location.hostname;
 var sep = '&'
 if (base_cgi == '') sep = '?';
 var url = ajx_url + base_cgi + sep + 'fb=' + encodeURIComponent(base) + '&fo=' + encodeURIComponent(curr_feat_offset);
 feat_req.open("GET", url, true);
 feat_req.onreadystatechange = process_feat_response;
 feat_req.send("");
}

function process_feat_response() {
    if (feat_req.readyState == 4) {
        if (feat_req.status == 200) {
         	next_apps_html = feat_req.responseText;
        	$('feat_apps').innerHTML = next_apps_html;
        	// alert('chg_feat response: ' + next_apps_html);
        } else {
        	$('feat_apps').innerHTML = 
        		"There was a problem retrieving featured apps:<br /><em>" + feat_req.statusText + "</em>";
        }
    }
}

//////////////////////////////////////////////////////////
//go to a different page of app results; send featured app info so that it can be redisplayed 
function chg_page(base_cgi, pg, feat_base) {
	cgi = base_cgi.trim();
	if (cgi == '') {
		location.href =  '?pg=' + pg + '&fb=' + feat_base + '&fo=' + curr_feat_offset;
	} else {
		location.href =  '?' + cgi + '&pg=' + pg + '&fb=' + feat_base + '&fo=' + curr_feat_offset;
	}
}

//////////////////////////////////////////////////////////
// get new list of apps to display via ajax
var app_req;
var curr_page = 1;

// onload
function set_curr_page(pg, ajx_url, cgi_args, total_pages) {
	hash = window.location.hash
	if (hash.substr(0,1)=='#') { hash = hash.substr(1); }
	if (hash == '' || hash == 'NaN' || hash < 1 || hash > total_pages) { hash = '1'; }
//alert('set_curr_page: pg = ' + pg + ', hash = ' + hash, ', total_pages = ' + total_pages);

	curr_page = pg;
	if ($('pgnum2')) $('pgnum2').innerHTML = curr_page;

	if (curr_page != hash) {
		curr_page = hash - 1;
		load_app_list(ajx_url, cgi_args, total_pages, true)
	}
}

function load_app_list(ajx_url, cgi_args, total_pages, is_more) {
	if (is_more) {
		curr_page++;
		if (curr_page > total_pages) {
			curr_page = total_pages;
			return;  // hit end
		}
	} else {
		curr_page--;
		if (curr_page < 1) { 
			curr_page = 1;
			return; // unchanged
		}
	}

	location.hash = curr_page;

	if (curr_page > 1) {
		if ($('item_less_1')) $('item_less_1').style.visibility='visible';
		if ($('item_less_2')) $('item_less_2').style.color='#000000';
	} else {
		if ($('item_less_1')) $('item_less_1').style.visibility='hidden';
		if ($('item_less_2')) $('item_less_2').style.color='#888888';
	}
	if (curr_page < total_pages) {
		if ($('item_more_1')) $('item_more_1').style.visibility='visible';
		if ($('item_more_2')) $('item_more_2').style.color='#000000';
	} else {
		if ($('item_more_1')) $('item_more_1').style.visibility='hidden';
		if ($('item_more_2')) $('item_more_2').style.color='#888888';
	}
	
	app_req = getXMLHttpReq(); 
    if (app_req == null) return; // no can do

 	var domain = location.hostname;
 	var url;
	cgi = cgi_args.trim();
	if (cgi == '') {
		url = ajx_url + '?pg=' + curr_page;
	} else {
		url = ajx_url + cgi + '&pg=' + curr_page;
	}
	
//alert('app ajax url: ' + url);
 	
 	app_req.open("GET", url, true);
 	app_req.onreadystatechange = process_app_response;
 	app_req.send("");
 	
 	document.body.style.cursor = 'wait';
}

function process_app_response() {
    if (app_req.readyState == 4) {
        if (app_req.status == 200) {
        	document.body.style.cursor = 'default';
         	resp = app_req.responseText;
         	slpos = resp.indexOf('//');
         	count_info = '';
         	if (slpos >= 0) {
         		count_info = resp.substr(0, slpos);
         		resp = resp.substr(slpos+2);
         	}
         	if ($('app_item_count')) $('app_item_count').innerHTML = count_info;
         	if ($('app_item_count2')) $('app_item_count2').innerHTML = count_info + ' apps';
        	if ($('app_list')) $('app_list').innerHTML = resp;
        	if ($('pgnum2')) $('pgnum2').innerHTML = curr_page;
        } else {
        	if ($('app_list')) $('app_list').innerHTML = 
        		"There was a problem retrieving apps:<br /><em>" + app_req.statusText + "</em>";
        }
        
//        location.href='#applisttop';        
    }
}

//////////////////////////////////////////////////////////
// email selected and this is an fb user that has not given an email address yet
function get_fb_email(checkbox) {
	email = prompt("email address needed")
	if (!email) {
		checkbox.checked=false;
		return;
	}
	$('alert_email_msg').innerHTML = 'A confirmation email has been sent.<br />' +
	    'Email will be enabled when you acknowledge the confirmation'
}



email_checkbox = null
function get_fb_email_perm(checkbox) {
	email_checkbox = checkbox
   if (email_checkbox.checked) {
	   FB.ensureInit(function() { 
		   FB.Connect.showPermissionDialog("email", fb_email_setup_complete); 
	   });
   }
}

function fb_email_setup_complete(success) {
    if (!success) {
        alert("AppSpotlight will not send you email");
        email_checkbox.checked = false;
    }
}

//////////////////////////////////////////////////////////
