// JavaScript Document

playlistChange = 0;
playlistCurrent = 0;

function setCurrentPlaylistItem(itemIndex) {
	playlistChange++;
	playlistCurrent = itemIndex;
	jQuery("#video_gallery > li.current").removeClass("current");
	jQuery("#video_gallery li:nth-child("+(itemIndex+1)+")").addClass("current");
}

function initPlaylist() {
	if (jQuery("#video").attr("nodeName") == "OBJECT") jwplayer().load('video/home/home.xml');
	else setTimeout(function(){setCurrentPlaylistItem(0);}, 100);
}

if (/android|iphone|ipod|ipad|series60|symbian|windows ce|blackberry/i.test(navigator.userAgent)) {
	jQuery(document).ready(function(){
		jQuery("#content_area").css("margin-top", "64px");
		///jQuery("#video_gallery > li").each(function(index) {addSupportForMobilePhoneEvents(jQuery(this));});
		setTimeout(function(){hideAddressBar();}, 100);
	});
}

function selectPlaylistItem(index) {
	if (jQuery("#video").attr("nodeName") == "OBJECT") {
		//alert(playlistItems.trackList.track[index].location);
		jwplayer().playlistItem(index);
	}
	else {
		jwplayer().load({file: playlistItems.trackList.track[index].location});
		jwplayer().play();
		setTimeout(function(){setCurrentPlaylistItem(index);}, 100);
	}
}


/***********************************************
* Fonctions specifiques pour mobile
***********************************************/

function hideAddressBar() { window.scrollTo(0, 1); }


/***********************************************
* gestion evenements specifiques pour "mobiles"
***********************************************/

function touchHandler(event)
{
    var touches = event.changedTouches;
    var first = touches[0];
    var type = "";
    
    switch(event.type)
    {
        case "touchstart":
            type = "mousedown";
            break;
            
        case "touchmove":
            type="mousemove";        
            break;        
            
        case "touchend":
            type="mouseup";
            break;
            
        default:
            return;
    }
   
    var simulatedEvent = document.createEvent("MouseEvent");
    
    simulatedEvent.initMouseEvent(type, true, false, window, 1, first.screenX, first.screenY, first.clientX, first.clientY,
                                  false, false, false, false, 0/*left*/, null);
                                                                            
    first.target.dispatchEvent(simulatedEvent);    
}

function touchAndStopHandler(event)
{
	touchHandler(event);
	
    event.preventDefault();
    event.returnValue = false;	
}

function addSupportForMobilePhoneEvents(domElement)
{
	//pas supporte par IE
	try {
		domElement.addEventListener("touchstart", touchAndStopHandler, false);
		domElement.addEventListener("touchmove", touchAndStopHandler, false);
		domElement.addEventListener("touchend", touchAndStopHandler, false);
		domElement.addEventListener("touchcancel", touchAndStopHandler, false);
	}
	catch (e){}
}


