/*
 * Licensed under a Creative Commons Attribution 3.0 License.
 * http://creativecommons.org/licenses/by/3.0/
 * ryan@masugadesign.com
 *
 * Ghost Anime Radio Indx tblx script
 * ReWrited by Sandel
 * sandel[gav]ukr[tchk]net
 *
*/

var xmlhttp
	/*@cc_on @*/
	/*@if (@_jscript_version >= 5)
	  try {
	  xmlhttp=new ActiveXObject("Msxml2.XMLHTTP")
	 } catch (e) {
	  try {
	    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
	  } catch (E) {
	   xmlhttp=false
	  }
	 }
	@else
	 xmlhttp=false
	@end @*/
	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
	 try {
	  xmlhttp = new XMLHttpRequest();
	 } catch (e) {
	  xmlhttp=false
	 }
	}
	function myXMLHttpRequest() {
	  var xmlhttplocal;
	  try {
	    xmlhttplocal= new ActiveXObject("Msxml2.XMLHTTP")
	 } catch (e) {
	  try {
	    xmlhttplocal= new ActiveXObject("Microsoft.XMLHTTP")
	  } catch (E) {
	    xmlhttplocal=false;
	  }
	 }

	if (!xmlhttplocal && typeof XMLHttpRequest!='undefined') {
	 try {
	  var xmlhttplocal = new XMLHttpRequest();
	 } catch (e) {
	  var xmlhttplocal=false;
	  alert('couldn\'t create xmlhttp object');
	 }
	}
	return(xmlhttplocal);
}

function sndReq(vote,id_num,ip_num,units) {
    
	var theUL = document.getElementById('unit_ul'+id_num); // the UL
	
	// switch UL with a loading div
	theUL.innerHTML = '<div class="loading"></div>';
	
    xmlhttp.open('get', 'ajax.php?a=index_rating_rpc&j='+vote+'&q='+id_num+'&t='+ip_num+'&c='+units, true);
    xmlhttp.onreadystatechange = handleResponse;
    xmlhttp.send(null);	
}

function handleResponse() {
    if(xmlhttp.readyState == 4){
        if (xmlhttp.status == 200){

            var response = xmlhttp.responseText;
            var update = new Array();

            if(response.indexOf('|') != -1) {
                update = response.split('|');
                changeText(update[0], update[1]);
            }
        }
    }
}

function changeText( div2show, text ) {
    // Detect Browser
    var IE = (document.all) ? 1 : 0;
    var DOM = 0; 
    if (parseInt(navigator.appVersion) >=5) {DOM=1};

    // Grab the content from the requested "div" and show it in the "container"
    if (DOM) {
        var viewer = document.getElementById(div2show);
        viewer.innerHTML = text;
    }  else if(IE) {
        document.all[div2show].innerHTML = text;
    }
}

/* =============================================================== */

//var doVote = function(elem, theVote,theratingID,theuserIP,theunits) {sndReq(theVote,theratingID,theuserIP,theunits); window.stop();};

//var ratingAction = {
//		'a.rater' : function(element){
//                    //alert('hi');
//			element.onclick = function(){
//
//			var parameterString = this.href.replace(/.*\?(.*)/, "$1"); // onclick="sndReq('j=1&q=2&t=127.0.0.1&c=5');
//			var parameterTokens = parameterString.split("&"); // onclick="sndReq('j=1,q=2,t=127.0.0.1,c=5');
//			var parameterList = new Array();
//
//			for (j = 0; j < parameterTokens.length; j++) {
//				var parameterName = parameterTokens[j].replace(/(.*)=.*/, "$1"); // j
//				var parameterValue = parameterTokens[j].replace(/.*=(.*)/, "$1"); // 1
//				parameterList[parameterName] = parameterValue;
//			}
//			var theratingID = parameterList['q'];
//			var theVote = parameterList['j'];
//			var theuserIP = parameterList['t'];
//			var theunits = parameterList['c'];
//
//
//			//alert('sndReq('+theVote+','+theratingID+','+theuserIP+','+theunits+')'); return false;
//			sndReq(theVote,theratingID,theuserIP,theunits); return false;
//			}
//		}
//
//	};
//
//Behaviour.register(ratingAction);

// Index AJAX Table
loading_index_table = '<img src="./images/ajax/ajax-loader-index.gif" alt="Loading.." />';

// ajax-loader-online.gif
loading_dj_contacts_img = '<img src="./images/ajax/ajax-loader-online.gif" alt="Loading.."  />';

$(document).ready(function(){

    // News

    $("#close-news").click(function(){

        $("#bye-bye-section").slideUp('slow', function() {
        // Animation complete.
        });

        return false;
    });

    $("#other-news").click(function(){

        $("#bye-bye-section").slideUp('slow', function() {
        // Animation complete.
        });

        return false;
    });


    // DJ Contacts

    $('#dj-contacts').html(loading_dj_contacts_img);

    $.ajax({
            type: "GET",
            url: "ajax.php?a=dj_contacts",
            cache: false,
            success: function(msg){
                $('#dj-contacts').html(msg);
            },

            error: function(){
                $('#dj-contacts').html('&nbsp;');
            }

    });

    setInterval(function() {

        $.ajax({
            type: "GET",
            url: "ajax.php?a=dj_contacts",
            cache: false,
            success: function(msg){
                $('#dj-contacts').html(msg);
            },

            error: function(){
                $('#dj-contacts').html('&nbsp;');
            }

        });

    }, 240000);
    // 4 min = 240sec

    


    // Index Main Table

        $('#index-table').html(loading_index_table);

        $.ajax({
                type: "GET",
                url: "ajax.php?a=index_table",
                cache: false,

                success: function(msg){

                    $('#index-table').html(msg);

                    $('a.rater').click(function() {
                        var get_rating_data = $(this).prev('input').attr('value');
                        //alert(get_rating_data);
                        var get_rating_data_array = get_rating_data.split("&");
                        //alert(get_rating_data_array[1]);
                        var theVote = get_rating_data_array[0];
                        var theratingID = get_rating_data_array[1];
			var theuserIP = get_rating_data_array[2];
			var theunits = get_rating_data_array[3];

                        //alert('sndReq('+theVote+','+theratingID+','+theuserIP+','+theunits+')');

                        sndReq(theVote,theratingID,theuserIP,theunits);

                        return false;
                        
                    });

                },

                error: function(){

                        $('#index-table').html(msg).fadeIn('fast');

                        $('#index-table').html('An error occurred while processing request...').fadeIn('fast');
                }

        });

        setInterval(function() {

           $('#index-table').fadeOut('slow', function() {

               $.ajax({
                    type: "GET",
                    url: "ajax.php?a=index_table",
                    cache: false,

                    success: function(msg){

                        $('#index-table').html(msg).fadeIn('fast');

                        $('a.rater').click(function() {
                            var get_rating_data = $(this).prev('input').attr('value');
                            //alert(get_rating_data);
                            var get_rating_data_array = get_rating_data.split("&");
                            //alert(get_rating_data_array[1]);
                            var theVote = get_rating_data_array[0];
                            var theratingID = get_rating_data_array[1];
                            var theuserIP = get_rating_data_array[2];
                            var theunits = get_rating_data_array[3];

                            //alert('sndReq('+theVote+','+theratingID+','+theuserIP+','+theunits+')');

                            sndReq(theVote,theratingID,theuserIP,theunits);

                            return false;

                        });

                    },

                    error: function(){

                        $('#index-table').html(msg).fadeIn('fast');

                        $('#index-table').html('An error occurred while processing request...').fadeIn('fast');
                        
                    }

                });

           });

           //$('#index-table').fadeIn('fast');

        }, 60000);

        

});



