// MAIN

function HideNavBar() {
	setTimeout(scrollTo, 0, 0, 1);
}


// INDEX functions

function addtotags(){
	tag = document.getElementById('tag').value;
	if(tag != ""){
		tags.push(tag);
		buildlist();
		document.getElementById('tag').value = "";
	}
}

function removefromtags(start){
	tags.splice(start, 1);
	buildlist();
}

function buildlist(){
	list = ""
	for (i=0;i<tags.length;i++) {
	 	list += "<li>" + tags[i] + " <img src='image/delete_btn.png' onclick='removefromtags(" + i + ")' /></li>"
	}
	
	document.getElementById('tagcontainer').innerHTML = list;
}

function filterTag(value){
	value = value.replace(/#/, "%23");
	value = value.replace(/@/, "%40");
	value = value.replace(/ /, "+");
	return value;
}

function generate(){
	link = "tweetwall.php";
	title = "?t=" + filterTag(document.getElementById('title').value);
	query = "&q=";
		
	for (i=0;i<tags.length;i++) {
		query += filterTag(tags[i]);
		if((i+1) != tags.length){
			query += "+OR+";
		}
	}
	
	link = link + title + query;
	
	if(tags.length > 0){
		window.location = link;
	} else {
		alert('Please fill in some tags');
		document.getElementById('boxcontainer').className = 'stepTwo';
	}
}


// TWEETWALL FUNCTIONS

function createREQ() { 
        try { 
            req = new XMLHttpRequest(); // firefox, safari, ó 
        } 
        catch (err1) { try { 
            req = new ActiveXObject("Msxml2.XMLHTTP"); // sommige IE 
        } 
        catch (err2) { try { 
            req = new ActiveXObject("Microsoft.XMLHTTP"); // meeste IE 
        } 
        catch (err3) { 
            req = false; 
            alert("Sorry maar je browser ondersteunt geen Ajax dus zal een groot deel van mijn site niet werken"); 
        }}} 
        return req; 
}

function getList(query){
	document.getElementById("message").innerHTML = "Loading...";
	document.getElementById("message").style.display = "block";
	var url= "tweets.php?query=" + query;
	var rand = parseInt(Math.random()*9999999);
	url = url + "&rand=" + rand;
   	http = createREQ(); 
    http.open("GET", url); 
    http.send(null); 
    http.onreadystatechange = function() { 
      	if (http.readyState == 4) { 
			respons = http.responseText;
			error = respons.search("simplexml_load_file()");
			if(error <= 0){
				document.getElementById("container").innerHTML = respons;
				document.getElementById("message").innerHTML = "";
				document.getElementById("message").style.display = "none";
			} else {
				document.getElementById("message").innerHTML = "Failed to connect with Twitter, please try again";
			}
		}  
	}
	HideNavBar(); 
}


if((navigator.userAgent.match(/iPhone/i))||(navigator.userAgent.match(/iPod/i))) {
	addEventListener("load", function() {
        setTimeout(updateLayout, 0);
    }, false);

    var currentWidth = 0;
    
    function updateLayout(){
        if (window.innerWidth != currentWidth){
        
            currentWidth = window.innerWidth;

            var orient = currentWidth == 320 ? "profile" : "landscape";
            document.body.setAttribute("orient", orient);
            setTimeout(function(){
				setTimeout(scrollTo, 0, 0, 1);
            }, 100);            
        }
    }
    
	setInterval(updateLayout, 200);
}


function Desktop() {
	if (window.innerWidth >= 1000) {
 		currentWidth = window.innerWidth;
    	var orient = "true";
    	document.body.setAttribute("desktop", orient);  
   	} else {
    	var orient = "false";
    	document.body.setAttribute("desktop", orient); 
   	}
}
