/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
function rssLoadFromRss(){

ajax_load = '<div style="text-align:center;padding-top:30px"><img src="artifacts/images/ajax-loader.gif" style="border:none"/></div>';

$('#blog')
.html(ajax_load)
.load('ajax.php',{act:'rssGetLatestPosts'});

$('#movie1')
.html(ajax_load)
.load('ajax.php',{act:'rssGetDreams'});

$('#movie2')
.html(ajax_load)
.load('ajax.php',{act:'rssGetDreams',son:true});

$('#gadgetFlikr')
.html(ajax_load)
.load('ajax.php',{act:'flikr'});

$('#gadgetYoutube')
.html(ajax_load)
.load('ajax.php',{act:'youtube'});





                            
$.fn.getImgSrc = function() {
            id = $(this).html();
            console.log('Cat of requested id:'+ id);
            $(this).load('ajax.php',{act:'catImg',cat:id});
 }


/*
$.ajax({
    type:"POST",
    url:"ajax.php",
    data:({act:'catList'}),
    dataType:"xml",
    success:function(xml){
        //console.log($(xml).find('item'));
        $(xml).find('item').each(function(){
           // console.log($(this).find('title').text());
            $('#carouselList').find("ul").append('<li>'+$(this).find('id').text()+'</li>')
           

        });
        $('#carouselList').find('li').each(function() {
            id = $(this).html();
            console.log('Cat of requested id:'+ id);
            
            $(this).load('ajax.php',{act:'catImg',cat:id});
            });

        
        $('.infiniteCarousel').infiniteCarousel();

       
       

    }
});
*/

 

/*Infinite Carousel*/
$.fn.infiniteCarousel = function () {

    function repeat(str, num) {
        return new Array( num + 1 ).join( str );
    }

    return this.each(function () {
        var $wrapper = $('> div', this).css('overflow', 'hidden'),
            $slider = $wrapper.find('> ul'),
            $items = $slider.find('> li'),
            $single = $items.filter(':first'),

            singleWidth = $single.outerWidth(),
            visible = 4,
		//Math.ceil($wrapper.innerWidth() / singleWidth), 
		// note: doesn't include padding or border
            currentPage = 1,
            pages = Math.ceil($items.length / visible);


        // 1. Pad so that 'visible' number will always be seen, otherwise create empty items
        if (($items.length % visible) != 0) {
            $slider.append(repeat('<li class="empty" />', visible - ($items.length % visible)));
            $items = $slider.find('> li');
        }

        // 2. Top and tail the list with 'visible' number of items, top has the last section, and tail has the first
        $items.filter(':first').before($items.slice(- visible).clone().addClass('cloned'));
        $items.filter(':last').after($items.slice(0, visible).clone().addClass('cloned'));
        $items = $slider.find('> li'); // reselect

        // 3. Set the left position to the first 'real' item
        $wrapper.scrollLeft(singleWidth * visible);

        // 4. paging function
        function gotoPage(page) {
            var dir = page < currentPage ? -1 : 1,
                n = Math.abs(currentPage - page),
                left = singleWidth * dir * visible * n;

            $wrapper.filter(':not(:animated)').animate({
                scrollLeft : '+=' + left
            }, 500, function () {
                if (page == 0) {
                    $wrapper.scrollLeft(singleWidth * visible * pages);
                    page = pages;
                } else if (page > pages) {
                    $wrapper.scrollLeft(singleWidth * visible);
                    // reset back to start position
                    page = 1;
                }

                currentPage = page;
            });

            return false;
        }

        $wrapper.after('');
        //<a class="arrow back">&lt;</a><a class="arrow forward">&gt;</a>
        // 5. Bind to the forward and back buttons
        $('a.back', this).click(function () {
            return gotoPage(currentPage - 1);
        });
        $('#back').click(function () {
            return gotoPage(currentPage - 1);
        });


        $('#forward').click(function () {
            return gotoPage(currentPage + 1);
        });

        $('a.forward', this).click(function () {
            return gotoPage(currentPage + 1);
        });

        // create a public interface to move to a specific page
        $(this).bind('goto', function (event, page) {
            gotoPage(page);
        });
    });
};


/*
 *
 * Category Getter and Carousel Populator
 */

$('#about-content')
.html(ajax_load)
.load('ajax.php',{act:'about'});

$('.infiniteCarousel').infiniteCarousel();
$('#carouselList')
.html(ajax_load)
.load('unit.php',function(){$('.infiniteCarousel').infiniteCarousel()});


/* SWF Player Play!! */

function synchVideo(url,parametre){
     if(window.XMLHttpRequest)
        xhr_object=new XMLHttpRequest();
    else if(window.ActiveXObject)
        xhr_object=new ActiveXObject("Microsoft.XMLHTTP");
    else
        return(false);
    xhr_object.open("POST",url,false);
    xhr_object.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
    xhr_object.send(parametre);
    if(xhr_object.readyState==4){
           var myDocument = xhr_object.responseXML;
             var response = new Array();
             if((myDocument.getElementsByTagName('description')[0].firstChild.nodeValue).length > 480){
             var descriptionTxt = (myDocument.getElementsByTagName('description')[0].firstChild.nodeValue).substring(0,460)+"...";
                 } else { var descriptionTxt = (myDocument.getElementsByTagName('description')[0].firstChild.nodeValue);}
              var embed = '<script type="text/javascript" src="http://www.nomadplayer.tv/embed/'+myDocument.getElementsByTagName('botrcode')[0].firstChild.nodeValue+'.js"></script>';
             
             response[0] = myDocument.getElementsByTagName('botrcode')[0].firstChild.nodeValue;
             response[1] = "<h1>"+ myDocument.getElementsByTagName('title')[0].firstChild.nodeValue +"</h1><p>"+ descriptionTxt+"</p>";
        return(response);

}else
        return(false);
}
 /*resp = synchVideo("ajax.php","act=video");
    if(resp!="NOK"){
    var mybotrcode = resp[0];
    var videodescription = resp[1];
    var obj = {
		"file" : 'http://content.bitsontherun.com/videos/'+ resp[0] +'-34281.m4v',
		type :'video'
	};

$('#myfeaturedinfo')
.html(resp[1])
.show('slow');
*/
$('#myfeaturedinfo-content')
.hide()
.html(ajax_load);

response = new Array();
//autoload
$('#myfeaturedinfo-content').load('ajax.php',{act:'video'}).find("botrcode").text();


$.ajax({
    type:"POST",
    url: 'ajax.php',
    data:({act:'video'}),
    success: function(msg){
       
       botrcode = $(msg).find("botrcode").text();
       description = $(msg).find("description").text();
       title = $(msg).find("title").text();
       $('#myfeaturedinfo-content').html('<a href="http://www.nomadplayer.tv/"><h2>'+ title +'</h2></a><p>'+description+'...</p>').show('slow');

       var obj = {
		"file" : 'http://content.bitsontherun.com/videos/'+ botrcode +'-34281.m4v',
		type :'video'
        };

        
       player = $("#mpl").get(0);
       //console.log(player);
       player.botrCode = botrcode;
        if (player == null || typeof(player.sendEvent) == "undefined" )
        {
           // alert('player was null');
           
           //autoload
           setTimeout(playVideo, 500);
           return;
        }


      
        player.sendEvent("LOAD", {"file" : 'http://content.bitsontherun.com/videos/'+ botrcode +'-34281.m4v',type :'video'});
        //autoload
        player.sendEvent("PLAY", "true");
    }
});


    /*  low tech stuff for testing
       var obj = {
		"file" : 'http://content.bitsontherun.com/videos/'+ botrcode +'-34281.m4v',
		type :'video'
        };
        player = $("#mpl").get(0);
        player.botrCode = botrcode;
        if (player == null || typeof(player.sendEvent) == "undefined" )
        {
            player = $("#mpl").get(0);
            setTimeout(playVideo, 500);
            return;
        }

        player.sendEvent("LOAD", obj);
        player.sendEvent("PLAY", "true");
if(isArray(response)){
    player.botrCode = response[0];
   // $('#myfeaturedinfo').html(response[1]);
} else player.botrCode = "play local file";
*/
   

/* end of player =8( */
}



/*
$('#movie1').html(rssRead('act=rssGetDreams'));


function rssRead(parameter){
    if(window.XMLHttpRequest)
        xmlHttp=new XMLHttpRequest();
    else if(window.ActiveXObject)
        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    else
        return(false);
    xmlHttp.open("POST","ajax.php",false);
    xmlHttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
    //xmlHttp.onreadystatechange = rssRequest;
   xmlHttp.send(parameter);

   if (xmlHttp.readyState == 4) {
      if (xmlHttp.status == 200) {
          return xmlHttp.responseText;
      } else {
          alert('There was a problem with the request.');
      }
   }
    
}

function rssRequest(){
   alert("fire!");
   
}


function synch(url,parametre)
{
    if(window.XMLHttpRequest)
        xhr_object=new XMLHttpRequest();
    else if(window.ActiveXObject)
        xhr_object=new ActiveXObject("Microsoft.XMLHTTP");
    else
        return(false);
    xhr_object.open("POST",url,false);
    xhr_object.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
    xhr_object.send(parametre);
    if(xhr_object.readyState==4)
        return(xhr_object.responseText);
    else
        return(false);
}

*/
