/**
 * carousel.js
 *
 * @author Michal nez Sekula (michal@esee.pl)
 */
 
$(function(){
var channelURL = 'http://www.playmax.tv/programtv/';
var carouselImages = [];

var getRandomNumber = function(lower_limit,upper_limit) {
	return Math.floor((upper_limit-lower_limit+1)*Math.random()+lower_limit);
};

var prepareData = function(xml) {
    var channels = $(xml).find("channel");
    var i = 0;
    
    $(channels).each(function(ndx, val){
        var title = $(val).find("title").text() || "";
        var linktitle = $(val).find("linktitle").text() || "";
        var description = $(val).find("description").text() || "";
        var url = $(val).find("url").text() | "";
        var image_gray = $(val).find("image_gray").text() || "";
        var image_white = $(val).find("image_white").text() || "";
        var epgid = $(val).find("epgid").text() || "";
        epgid = parseInt(epgid);
        if( epgid != "NaN" && epgid > 0 ) {
            var ob = { title: title, description: description, gray: image_gray, color: image_white, link: channelURL + epgid + '/' + linktitle + '/' };
            carouselImages[i] = ob;
            i++;
        }        
    });
    
    startCarousel();
    
    /*
    $("img#im_0").attr('src', carouselImages[0].color);
    
    if( carouselImages.length > 0 ){
    	loadContent(0);
    }*/
    
}
$.ajax({
    type: "GET",
    url: "xml/channels.xml",
    dataType: "xml",
    success: function(xml){
        prepareData(xml);
    }
});

var loadContent = function(imid) {
	$("#nc_content").hide();
	
	$("#nc_title").html( carouselImages[imid].title );
	
	var desc = carouselImages[imid].description;
	var sh = 160;
	if( desc.length > sh ) {
	    desc = desc.substring(0, sh) + '...';
	}
	
	$("#nc_text").html( desc );
	$("#nc_link").attr( 'href', carouselImages[imid].link );
	
	$("#nc_content").fadeIn("slow");
	
	
	//window.location.href=carouselImages[imid].link;
};
var startCarousel = function() {
	var sliderWidth = 0;
	var imageWidth = 60;
	var padding = {left: 15, right: 15};
	
	
	if( carouselImages.length > 0 ){
		$(carouselImages).each(function(ndx, val){
			var im = '<img id="im_'+ndx+'" src="'+val.gray+'" />';
			$("div#nc_image_container").append(im);
		});
		$("div#nc_image_container img").hover(
			function(){
				/*var imid = $(this).attr('id').replace('im_','');
				$("img#im_"+imid).attr('src', carouselImages[imid].color).css('cursor','pointer');*/
				/*var imid = $(this).attr('id').replace('im_','');
				$("img#im_"+imid).css('cursor','pointer');*/
			},
			function(){
				/*var imid = $(this).attr('id').replace('im_','');
				$("img#im_"+imid).attr('src', carouselImages[imid].gray).css('cursor','default');*/
				/*var imid = $(this).attr('id').replace('im_','');
				$("img#im_"+imid).css('cursor','default');*/
			}
		).click(
			function(){
				/*var imid = $(this).attr('id').replace('im_','');
				loadContent( imid );*/
			}
		);
		
		
		sliderWidth = carouselImages.length * imageWidth + carouselImages.length * (padding.left + padding.right);
		// added
        sliderWidth += 45;
        $("div#nc_image_container").css({width: sliderWidth+'px', left: '45px'});
        // -----
        
        $("div#nc_image_container").css({width: sliderWidth+'px'});
        
    	var startx = 0;
    	var starty = 0;
    	var newx = 45;
    	var newy = 0;
    	var step = 90;
    	var scrolltime = 900;
    	var move = 0;
    	var windowWidth = 180;
    	var upd = 0;
    	var actualCentered = 0;
    	var canClick = true;
    	

    	// RANDOM START ------------------------------------
    	var cl = carouselImages.length - 1;
    	actualCentered = getRandomNumber(0, cl);
    	newx = (-90 * actualCentered) + 45;
    	startx = 0;
    	$("div#nc_image_container").css({width: sliderWidth+'px', left: newx+'px'});
    	$("img#im_"+actualCentered).attr('src', carouselImages[actualCentered].color);
    	loadContent(actualCentered);
		//--------------------------------------------------
    	
    	var updateCentered = function() {
            actualCentered += upd;
            if( actualCentered < 0 ) actualCentered = 0;
            else if( actualCentered > carouselImages.length - 1 ) actualCentered = carouselImages.length-1;
            $("img#im_"+actualCentered).attr('src', carouselImages[actualCentered].color);
            loadContent(actualCentered);
            upd = 0;
            canClick = true;
        }
    	
    	if( carouselImages.length > 2 ) {
    	$("div.scrollbtn").css('cursor','pointer').hover(function(){$(this).addClass('hover');},function(){$(this).removeClass('hover');});

    	$("div#nc_container div.right").click(function(){
    	
    	    if( canClick === true ) {
    	        canClick = false;
    	        $("img#im_"+actualCentered).attr('src', carouselImages[actualCentered].gray);
        		var temp = newx-step;
        		if( temp <= -sliderWidth + windowWidth - 45) {
        			move = -sliderWidth + windowWidth;
        			newx = move;
        		} else {
        			move = temp;
        			newx -= step;
        		}
        		upd = 1;
        		$("div#nc_image_container").animate({ 
                	left: (move)+"px"
        	    }, {"duration": scrolltime, "complete": updateCentered } );
    	    }
    	});
    	$("div#nc_container div.left").click(function(){
    	    if( canClick === true ) {
    	        canClick = false;
    	        $("img#im_"+actualCentered).attr('src', carouselImages[actualCentered].gray);
        		var temp = newx+step;
        		if( temp >= startx + 45 ) {
        			move = startx + 45;
        			newx = move;
        		} else {
        			move = temp;
        			newx += step;
        		}
        		upd = -1;
        		$("div#nc_image_container").animate({ 
               		left: (move)+"px"
              	}, {"duration": scrolltime, "complete": updateCentered} );
          	}
       		
    	});
    	}
	};
	};
});
