var xmlHttp;
function ajaxRequest(url, callback)
{
	try
	{
		// Firefox, Opera 8.0+, Webkit
		xmlHttp=new XMLHttpRequest();
	}
	catch (e)
	{
		// Internet Explorer
		try
		{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			try
			{
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e)
			{
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}

	xmlHttp.onreadystatechange = function ()
	{
		// When we are finished, we will deliver the xml text as JSON response. Eval'ing it will make it JSON.
		if (xmlHttp.readyState == 4)
		{
			callback(eval('(' + xmlHttp.responseText + ')'));
		}
                
	}

	// Now
	xmlHttp.open("GET", url, true);
	xmlHttp.send(null);
}

///////////////////////////////////////////
////////////// Geo  Location //////////////
///////////////////////////////////////////
var country = geoip_country_name();
var city    = check_location(cities);


function change_anchors(city){
    parent      = document.getElementById('menu_div');
    anchors     = parent.getElementsByTagName('a');
    for(var i = 0;i < anchors.length;i++){
        if(anchors[i].href.search("#\/([A-z_]+)\/([A-z_]+)") > 0){
            str = anchors[i].href.split("#");
            str = String(str[1]);
            str = str.split("/");
            anchors[i].href = base_url + "consumer/feed/#/"+str[1]+"/"+city+"/1";
        } else {
            anchors[i].href += city + "/1";
        }
    }
}
///////////////////////////////////////////
////////////// Ajax Requests //////////////
///////////////////////////////////////////

function initialize_website(){
    change_anchors(city);
    hash        = split_hash(check_hash());
    type        = hash[1];
    limit       = 50;
    offset      = 0;
    type_title  = type.replace("_","&");
    document.getElementById('page_title').innerHTML = type_title;
    //change_highlight(type);
    startLoadingIcon();
    ajaxRequest(base_url+"consumer/ajax_feed/"+city+"/"+type+"/"+offset+"/"+limit, feed_callback);
}

function change_feed(feed_type){
    startLoadingIcon();
    type_title  = feed_type.replace("_","&");
    document.getElementById('page_title').innerHTML = type_title;
    city = check_location(cities);
    set_hash(feed_type,city,1);
    ajaxRequest(base_url+"consumer/ajax_feed/"+city+"/"+feed_type+"/"+0+"/"+50, feed_callback);
}

function change_location(city){
    startLoadingIcon();
    city = city.replace(" ","_");
    type = check_type();
    window.location.hash = "#/"+type+"/"+city+"/1";
    change_anchors(city);
    ajaxRequest(base_url+"consumer/ajax_feed/"+city+"/"+type+"/"+0+"/"+50, feed_callback);
}

function load_feed(type,page){
    window.location='#main';
    offset  = 0;
    limit   = 50;
    startLoadingIcon(base_url+"consumer/ajax_feed/"+city+"/"+type+"/"+offset+"/"+limit, feed_callback);
    ajaxRequest(base_url+"consumer/ajax_feed/"+city+"/"+type+"/"+offset+"/"+limit, feed_callback);
}

///////////////////////////////////////////
//////////////// Callbacks ////////////////
///////////////////////////////////////////

function feed_callback(data){
    // The data now contains an OBJECT with the array prepared in PHP.
    // Do something with the data.
    var page_numbering          = "";
    var page_numbering_top      = document.getElementById('page_numbering_top');
    var page_numbering_bottom   = document.getElementById('page_numbering_bottom')
    if(data[0].available == true){
        current_page    = split_hash(check_hash())[3];
        offset          = (current_page - 1) * 10;
        limit_onpage    = offset + 10;
        limit           = 50;
        document.getElementById("ajax_feed").innerHTML = "";
        var first = true;
        var a   = "";
        for(var i = 1;i < data.length;i++){
            if(i > offset && i <= limit_onpage){
                var image   = "";
                var time    = "";
                if(data[i].image_available == true){
                    image = "<td rowspan='3' valign='top' width='100px'><img src='"+data[i].image[0]+"' width='102' height='102' alt='"+data[i].name[0]+"'/></td>";
                }
                if(data[0].type == "shopping"){
                    time = "";
                }
                else if(data[0].type == "drink_dance"){
                    time = "<tr><td><strong>"+data[i].time+"</strong></td></tr>";
                }
                else {
                    time = "<tr><td><strong>"+data[i].time+"</strong></td></tr>";
                }

                a += "<table width='100%' border='0' cellspacing='0' cellpadding='4'>";
                a += "<tr>";
                a += image;
                a += "<td><strong><a href='"+base_url+"consumer/event/"+data[i].ID[0]+"'>"+data[i].name[0]+"</a></strong></td>";
                a += "</tr>";
                a += time;
                a += "<tr><td>"+data[i].description[0]+"</td></tr>";
                a += "<tr><td>";
                //facebook and twitter here
                a += "</td></td>";
                a += "</table><hr />";
            }
        }
        document.getElementById("ajax_feed").innerHTML = a;
        offset = 0;
        limit=50;
        
        city            = get_city_from_hash();
        max_pages       = data[0].ammount_of_pages;
        if(max_pages > 1){
            if(current_page <= max_pages && current_page > 1){
                previous_page   = current_page - 1;
                previous_offset = (previous_page - 1) * 10;
                page_numbering += "<a onclick=\"load_feed('"+data[0].type+"','"+city+"')\" href='#/"+data[0].type+"/"+city+"/"+previous_page+"'>&#60;&#60;previous</a> &nbsp;";
            }
            for(var i = 1;i <= max_pages;i++){
                if(i == current_page){
                    page_numbering += i+"&nbsp;";
                } else {
                    page_numbering += "<a onclick=\"load_feed('"+data[0].type+"','"+city+"')\" href='#/"+data[0].type+"/"+city+"/"+i+"'>"+i+"</a>&nbsp;";
                }
            }
            if(current_page < max_pages && current_page > 0){
                next_page   = parseInt(current_page) + 1;
                next_offset = current_page * 10;
                page_numbering += "<a onclick=\"load_feed('"+data[0].type+"','"+city+"')\" href='#/"+data[0].type+"/"+city+"/"+next_page+"'>next>></a>";
            }
        }
    } else {
        document.getElementById('ajax_feed').innerHTML = "No data available";
        page_numbering.innerHTML = "";
    }
    page_numbering_top.innerHTML    = page_numbering;
    page_numbering_bottom.innerHTML = page_numbering;
    stopLoadingIcon();
}

///////////////////////////////////////////
////////////// Loading Icon ///////////////
///////////////////////////////////////////

function startLoadingIcon()
{
document.getElementById('loadingIcon').innerHTML = "<img src='"+base_url+"images/ajax-loader.gif' border=0>";
}

function stopLoadingIcon()
{
document.getElementById('loadingIcon').innerHTML = "&nbsp;";
}

///////////////////////////////////////////
/////////////// Hash check ////////////////
///////////////////////////////////////////

function check_hash(){
    if(window.location.hash){
        return(window.location.hash);
    } else {
        return false;
    }
}

function split_hash(hash){
    hash = hash.split("/");
    if(typeof hash[3] == 'undefined'){
        hash[3] = 1;
    }
    return hash;
}

function get_city_from_hash(){
    if(window.location.hash){
        hash = window.location.hash.split("/");
        if(typeof hash[2] == "undefined"){
            return "amsterdam";
        }
        return hash[2];
    } else {
        return "amsterdam";
    }
}

function check_location(city_array){
    var type = check_type();
    if(window.location.hash){
        var hash1 = type;
        var hash2 = "";
        var hash3 = "";
        hash = window.location.hash.split("/");
        if(typeof hash[2] != "undefined"){
            hash2 = hash[2];
            if(typeof hash[3] != "undefined"){
                hash3 = "/"+hash[3];
            }
            if(hash[2].replace("_"," ") in city_array){
                window.location.hash = "#/"+type+"/"+hash2+hash3;
                return hash[2];
            }
        }
    }
    if(geoip_city().toLowerCase() in city_array){
        window.location.hash = "#/"+type+"/"+geoip_city().toLowerCase()+"/1";

        return geoip_city().toLowerCase();
    }
    else{
        window.location.hash = "#/"+type+"/amsterdam/1";
        return "amsterdam";
    }
}

function check_type(){
    if(window.location.hash){
        hash = window.location.hash.split("/");
        if(typeof hash[1] != "undefined"){
            return hash[1];
        }
    }
    return "shopping";
}

function set_hash(type,location,page){
    window.location.hash = "#/"+type+"/"+location+"/"+page;
}
