﻿function videos_init(){
	
	swfUrls = new Array();
	descriptions = new Array();
	collections = new Array();
	// attach video events to thumb images
	var elThumbLinks = document.getElementById('datalist-content').getElementsByTagName('img');
	for(var i=0; i < elThumbLinks.length; i++){
	    if(i%2 == 0){
	        addSwfItem(elThumbLinks[i]);
	        countVideos++;
	    }
		addEvent(elThumbLinks[i], 'click', displayVideo);
	}
	elThumbLinks = null;// IE memory leak protection
	
//	elThumbLinks = document.getElementById('divRelatedVideos').getElementsByTagName('img');
//	for(var i=0; i < elThumbLinks.length; i++){
//	    addEvent(elThumbLinks[i], 'click', displayVideo);
//	}
//	elThumbLinks = null;// IE memory leak protection
	
	currentVideo = 0;
	var elFloZoom = document.getElementById("video_zoom");
	var params = { allowScriptAccess: "always" };
    var atts = { id: "myytplayer" };
    var so = new SWFObject(swfUrls[currentVideo] + "&fs=1&rel=0", 
                       "ytapiplayer", "425", "344", "8", null, null, params, atts);
    //var so = new SWFObject("http://www.youtube.com/v/VIDEO_ID&enablejsapi=1&playerapiid=ytplayer", "mymovie", "425", "356", "7", "#FFFFFF");
    so.addParam("wmode", "opaque");
    so.addParam("quality", "high");
        
    so.addParam("allowFullScreen", "true");
    //so.addParam("allowScriptAccess", "always");

    so.write("flashcontent_all");
    
    document.getElementById("videoDescription").innerHTML = descriptions[currentVideo];
    
    // Update the Related Videos list
//    var nodes = document.getElementById("ctl00_MainContent_dlRelatedVideos").getElementsByTagName('tr');
//    var currentCollections = collections[currentVideo].split("-");
//    for(i = 0; i < nodes.length; i++) {
//        //TODO: Remove videos not related and show related
//        for(j = 0; j < currentCollections.length; j++) {
//            if(collections[i].indexOf(currentCollections[j]) >= 0) {
//                nodes[i].style.display = '';
//            } else {
//                nodes[i].style.display = 'none';
//            }
//        }        
//        
//    }
//    nodes[currentVideo].style.display = 'none';
    
    
    //document.getElementById("flashcontent_all").innerHTML = QT_GenerateOBJECTText(swfUrls[currentVideo] , '400', '320', '', 'SCALE', 'aspect', 'obj#ID', 'movieOBJ', 'emb#ID', 'movieEMBED');
    
//    document.getElementById("related-videos").style.display = '';
    
    addEvent(document.getElementById('videoBack'), 'click', displayPreviousVideo);
    addEvent(document.getElementById('videoNext'), 'click', displayNextVideo);
}

function addSwfItem(elem){
    
    var imgID = elem.getAttribute('id').replace("BIS", "");
	
	//var imgUrl = target.getAttribute('src');	
	var imgUrl = document.getElementById(imgID).getAttribute('src');
	var description = document.getElementById(imgID + "HID").value;
	
	//alert(imgUrl);
	// strip image params off url
	if(imgUrl.indexOf('&') > -1){
		imgUrl = imgUrl.substring(0, imgUrl.indexOf('&'));
	}

	if(imgUrl.indexOf('src=') > -1){
		imgUrl = imgUrl.substring(imgUrl.indexOf('?src=')+5);
	}

    //var swfUrl = "http://images.simplefloors.com/erez4/fsi4/fsi.swf?FPXBase=http://images.simplefloors.com/erez4%2Ferez%3Fsrc%3D&amp;CropToViewport=0&amp;Effects=%26sharpen%3d50&amp;FPXSrc=" + imgUrl;

    var videoURL = document.getElementById(imgID + "VIDURL").value;
    //swfUrl = "/videos/videoPlayer.swf?VideoURL=../../../videos/videos/" + videoURL + "&ImageURL=../../../videos/imagenes/1.jpg";
    var swfUrl = videoURL;
    
    swfUrls[countVideos] = swfUrl;
    descriptions[countVideos] = description;
    collections[countVideos] = document.getElementById(imgID + "COLLECTIONS").value;
    
	//var so = new SWFObject(swfUrl, "mymovie", "400", "320", "7", "#FFFFFF");
	//var so = new SWFObject(swfUrl, "mymovie", "500", "312", "7", "#FFFFFF");
	
	var params = { allowScriptAccess: "always" };
    var atts = { id: "myytplayer" };
    var so = new SWFObject(swfUrl + "&fs=1&rel=0", 
                       "ytapiplayer", "425", "344", "8", null, null, params, atts);
	
	so.addParam("wmode", "opaque");
	so.addParam("quality", "high");
	so.addParam("allowFullScreen", "true");
	//so.addParam("allowScriptAccess", "always");
	
	so.write("flashcontent_all");
	//document.getElementById("flashcontent_all").innerHTML = QT_GenerateOBJECTText(swfUrl , '400', '320', '', 'SCALE', 'aspect', 'obj#ID', 'movieOBJ', 'emb#ID', 'movieEMBED');
}

function displayVideo(e){
	var elFloZoom = document.getElementById("video_zoom");
	// get the image url
	var target = getEventTarget(e);
	
	var imgID = target.getAttribute('id').replace("BIS", "");
	
	//var imgUrl = target.getAttribute('src');	
	var imgUrl = document.getElementById(imgID).getAttribute('src');
	
	//alert(imgUrl);
	// strip image params off url
	if(imgUrl.indexOf('&') > -1){
		imgUrl = imgUrl.substring(0, imgUrl.indexOf('&'));
	}

	if(imgUrl.indexOf('src=') > -1){
		imgUrl = imgUrl.substring(imgUrl.indexOf('?src=')+5);
	}
	// set url of flash object
	//ex: http://images.simplefloors.com/erez4/fsi4/fsi.swf?FPXBase=http://images.simplefloors.com/erez4%2Ferez%3Fsrc%3D&amp;FPXSrc=xBar/samples/BrazilianWalnut%255FFlat%255Fsmall.tif
	//var swfUrl = "http://images.simplefloors.com/erez4/fsi4/fsi.swf?FPXBase=http://images.simplefloors.com/erez4%2Ferez%3Fsrc%3D&amp;CropToViewport=0&amp;Effects=%26sharpen%3d50&amp;FPXSrc=" + imgUrl;
    
    var videoURL = document.getElementById(imgID + "VIDURL").value;
    //swfUrl = "/videos/videoPlayer.swf?VideoURL=../../../videos/videos/" + videoURL + "&ImageURL=../../../videos/imagenes/1.jpg";
    var swfUrl = videoURL;
    
	//var so = new SWFObject(swfUrl, "mymovie", "400", "320", "7", "#FFFFFF");
	//var so = new SWFObject(swfUrl, "mymovie", "500", "312", "7", "#FFFFFF");
	
	var params = { allowScriptAccess: "always" };
    var atts = { id: "myytplayer" };
    var so = new SWFObject(swfUrl + "&fs=1&rel=0", 
                       "ytapiplayer", "425", "344", "8", null, null, params, atts);
	
	so.addParam("wmode", "opaque");
	so.addParam("quality", "high");
	so.addParam("allowFullScreen", "true");
	//so.addParam("allowScriptAccess", "always");
	
	so.write("flashcontent_all");
	//document.getElementById("flashcontent_all").innerHTML = QT_GenerateOBJECTText(swfUrl , '400', '320', '', 'SCALE', 'aspect', 'obj#ID', 'movieOBJ', 'emb#ID', 'movieEMBED');
	
	currentVideo = parseInt(document.getElementById(imgID + "ARRAYINDEX").value);
	
	document.getElementById("videoDescription").innerHTML = descriptions[currentVideo];
	
	// Update the Related Videos list
//    var nodes = document.getElementById("ctl00_MainContent_dlRelatedVideos").getElementsByTagName('tr');
//    var currentCollections = collections[currentVideo].split("-");
//    for(i = 0; i < nodes.length; i++) {
//        //TODO: Remove videos not related and show related
//        for(j = 0; j < currentCollections.length; j++) {
//            if(collections[i].indexOf(currentCollections[j]) >= 0) {
//                nodes[i].style.display = '';
//            } else {
//                nodes[i].style.display = 'none';
//            }
//        }        
//        
//    }
//    nodes[currentVideo].style.display = 'none';

}

function displayNextVideo(e){
    if(currentVideo < (countVideos-1)){
        currentVideo++;
        //var so = new SWFObject(swfUrls[currentVideo], "mymovie", "400", "320", "7", "#FFFFFF");
        var params = { allowScriptAccess: "always" };
        var atts = { id: "myytplayer" };
        var so = new SWFObject(swfUrls[currentVideo] + "&fs=1&rel=0", 
                           "ytapiplayer", "425", "344", "8", null, null, params, atts);
        so.addParam("wmode", "opaque");
        so.addParam("quality", "high");
        so.addParam("allowFullScreen", "true");
        //so.addParam("allowScriptAccess", "always");
	
	    so.write("flashcontent_all");
	    
	    document.getElementById("videoDescription").innerHTML = descriptions[currentVideo];	    
	    
	    // Update the Related Videos list
//        var nodes = document.getElementById("ctl00_MainContent_dlRelatedVideos").getElementsByTagName('tr');
//        var currentCollections = collections[currentVideo].split("-");
//        for(i = 0; i < nodes.length; i++) {
//            //TODO: Remove videos not related and show related
//            for(j = 0; j < currentCollections.length; j++) {
//                if(collections[i].indexOf(currentCollections[j]) >= 0) {
//                    nodes[i].style.display = '';
//                } else {
//                    nodes[i].style.display = 'none';
//                }
//            }        
//            
//        }
//        nodes[currentVideo].style.display = 'none';
	    //document.getElementById("flashcontent_all").innerHTML = QT_GenerateOBJECTText(swfUrls[currentVideo] , '400', '320', '', 'SCALE', 'aspect', 'obj#ID', 'movieOBJ', 'emb#ID', 'movieEMBED');
    }

}

function displayPreviousVideo(e){
    if(currentVideo > 0){
        currentVideo--;
        //var so = new SWFObject(swfUrls[currentVideo], "mymovie", "400", "320", "7", "#FFFFFF");
        var params = { allowScriptAccess: "always" };
        var atts = { id: "myytplayer" };
        var so = new SWFObject(swfUrls[currentVideo] + "&fs=1&rel=0", 
                           "ytapiplayer", "425", "344", "8", null, null, params, atts);
        so.addParam("wmode", "opaque");
        so.addParam("quality", "high");
        so.addParam("allowFullScreen", "true");
        //so.addParam("allowScriptAccess", "always");
	
	    so.write("flashcontent_all");	    
	    document.getElementById("videoDescription").innerHTML = descriptions[currentVideo];
	    
	    // Update the Related Videos list
//        var nodes = document.getElementById("ctl00_MainContent_dlRelatedVideos").getElementsByTagName('tr');
//        var currentCollections = collections[currentVideo].split("-");
//        for(i = 0; i < nodes.length; i++) {
//            //TODO: Remove videos not related and show related
//            for(j = 0; j < currentCollections.length; j++) {
//                if(collections[i].indexOf(currentCollections[j]) >= 0) {
//                    nodes[i].style.display = '';
//                } else {
//                    nodes[i].style.display = 'none';
//                }
//            }        
//            
//        }
//        nodes[currentVideo].style.display = 'none';
	    //document.getElementById("flashcontent_all").innerHTML = QT_GenerateOBJECTText(swfUrls[currentVideo] , '400', '320', '', 'SCALE', 'aspect', 'obj#ID', 'movieOBJ', 'emb#ID', 'movieEMBED');
    }

}

var currentVideo = 0;
var countVideos = 0;
var swfUrls;
var descriptions;
var collections;
addEvent(window, 'load', videos_init);
