var limit = 8;
var searchTerm;
window.onload = function(){
    $('search_button').onclick = function(e) {
        if (e) { Event.stop(e); }
        getSearchResults(0, $('search_input').getValue());
	return false;
    }

//    getPopularMovies(0);
    if ($('next_link')) {
        $('next_link').onclick = function(e) {
            if (e) { Event.stop(e); }
            r = null;
            getPopularMovies(limit);
            return false;
        }
    }
};

// Functions below

var getPopularMovies = function(offset) {
    r = null;
    if ($('restrict_yes') && $('restrict_yes').getValue() == 'en') { r = 'en' }
    $('movies').innerHTML = 'Loading...';
    new Ajax.Request('popular_movies_passthru.php', {
        parameters: {offset: offset, limit: limit, restrict: r},
        method: 'get',
        onSuccess: function(t){
			$('popularbar').setStyle({
				background: 'url(images_en/popular.jpg)'
			});
			showMovies(t,offset,'getPopularMovies');}
    });
}

var getSearchResults = function(offset, term) {
    if (term) { searchTerm = term; }
    r = null;
    if ($('restrict_yes') && $('restrict_yes').getValue() == 'en') { r = 'en' }
    $('movies').innerHTML = 'Loading...';
    new Ajax.Request('search_movies_passthru.php', {
    parameters: {offset: offset, limit: limit, q: searchTerm, restrict: r},
        method: 'get',
        onSuccess: function(t){
			$('popularbar').setStyle({
				background: 'url(images_en/searchResults.jpg)'
			});
			showMovies(t,offset,'getSearchResults');}
    });
   

}

var showMovies =  function(t,offset,f) {
    $('movies').innerHTML = '';
    var x = t.responseXML.documentElement;
    var arr = x.getElementsByTagName('info');
    var tot = x.getAttribute("results");
	if (tot) {
		var first = offset+1; if (first>tot) { first=tot; }
    	if (offset+limit>=tot) {
			last=tot; $('next_link').hide();
		} else {
			last=offset+limit; $('next_link').show();
		}
    	$('search_results_count').innerHTML = first + ' - '+last+' of '+tot+" results";
	}
    if (arr.length==0) {
        if (f == 'getPopularMovies') {
            getPopularMovies(0);
        } else {
            $('movies').innerHTML = '<em>No Results</em>';
        }
    }
    var even = false;
    for(i=0;i<arr.length;i++) {
        if (even) { floatdir = 'right'; even = false; } else { floatdir = 'left'; even = true; }

        el = arr[i];
        m = new Hash();
        m['email'] = el.getElementsByTagName('email')[0].firstChild.nodeValue;
        m['thumb'] = el.getElementsByTagName('thumb')[0].firstChild.nodeValue;
        m['title'] = el.getElementsByTagName('title')[0].firstChild.nodeValue;
        m['views'] = el.getElementsByTagName('views')[0].firstChild.nodeValue;
        m['tag'] = el.getElementsByTagName('tag')[0].firstChild.nodeValue;
        m['length'] = el.getElementsByTagName('length')[0].firstChild.nodeValue;
        m['minutes'] = parseInt(m['length'] / 60);
        m['seconds'] = parseInt(m['length'] % 60);
        if (m['seconds'] < 10) { m['seconds'] = '0'+m['seconds']; }
        m['desc'] = el.getElementsByTagName('description')[0].firstChild.nodeValue;
        m['date'] = el.getElementsByTagName('date')[0].firstChild.nodeValue;
        if (m['date'].length == 14) {
            m['date'] = m['date'].substr(4,2) + '/' +
                m['date'].substr(6,2) + '/' + m['date'].substr(2,2);
        }

        var a = '<a href="viewer.php?a='+m['tag']+'" target="_blank">';

        $('movies').innerHTML += '\n<div class="movie" id="movie_'+(offset+i)+'" style="float:' + 
            floatdir + '">\n\t' +
            '<div class="m_thumbnail">\n\t\t'+a+'<img src="http://webdevideo.net/'+m['thumb']+
            '" height="90" width="120" alt="thumbnail" /></a>\n\t</div>\n\t' +
            '<div class="m_info">\n\t\t<div class="m_infotop">\n\t\t\t<span class="m_title">' + a +
            m['title'] + '</a></span>\n\t\t\t' + 
            '<span class="m_length">'+m['length'] + '</span>\n\t\t</div>\n\t\t' +
            '<div class="m_infomiddle">\n\t\t\t<span class="m_description">' + m['desc'] + 
            '</span>\n\t\t\t<span class="m_views">Views: ' + m['views'] + '</span>\n\t\t</div>' +
            '\n\t\t<div class="m_infobottom">\n\t\t\t<span class="m_owner">' + 
            m['email'] + '</span>\n\t\t\t<span class="m_posted">' + m['date'] + 
            '</span>\n\t\t</div>\n\t</div>\n</div>\n';

    }
    if (arr.length >= limit) {
        $('next_link').onclick = function(e) {
            if (e) { Event.stop(e); }
            eval(f+'('+(offset+limit)+');'); 
            return false;
        };
    } else {
        $('next_link').onclick = function(e) {
            if (e) { Event.stop(e); }
            eval(f+'(0);'); 
            return false;
        };
    }
    if (offset==0) {
        $('prev_link').style.display = 'none';
    } else {
        $('prev_link').style.display = 'inline';
        $('prev_link').onclick = function(e) {
            if (e) { Event.stop(e); }
            eval(f+'('+(offset-limit)+');'); 
            return false;
        };
    }
    if ($('restrict_no')) {
        $('restrict_no').onclick = function(e) {
            eval(f+'('+offset+');');
        };
    }
    if ($('restrict_yes')) {
        $('restrict_yes').onclick = function(e) {
            eval(f+'('+offset+');');
        };
    }
}
