/* do Template Player
 * used only for do template creation and testing
 * just for one do template
 * take do data from content/data/content.js file
 * 
 * Requires:
 * jquery.js
 * doData.js
 * usability.js
 */

/* Avoid `console` errors in browsers that lack a console. for IE especialy	
// or at the top of your script use this:
if ( ! window.console ) console = { log: function(){} };
*/

console.log('> player_do_template.js is iniciated;');

// turn off/on console: true:off; false:on;
var playerConsoleOff = false;
(function() {
    var method;
    var noop = function () {};
    var methods = [
        'assert', 'clear', 'count', 'debug', 'dir', 'dirxml', 'error',
        'exception', 'group', 'groupCollapsed', 'groupEnd', 'info', 'log',
        'markTimeline', 'profile', 'profileEnd', 'table', 'time', 'timeEnd',
        'timeStamp', 'trace', 'warn'
    ];
    var length = methods.length;
    var console = (window.console = window.console || {});
    while (length--) {
        method = methods[length];
        // Only stub undefined methods.
        if (!console[method] ) {
            console[method] = noop;
        }
		// if console turned off
        if (playerConsoleOff) {
            console[method] = noop;
        }
    }
}());


// Remove scrollbar in JS
$('#slidesContainer').css({'overflow': 'hidden'});

// change all global variables from this "inSection" to this "_inSection" for better undertanding
var inSection = 0;
var	inSlide = 0;
console.log('inSection='+inSection +'; inSlide='+inSlide);

var sectionName = 'section0';
var slideName = 'slide0';
var ssId = '#section0slide0'; // section and slide name
var ss = 'section0slide0'; // section and slide name

var slideWidth = 1160;
var slideHeight = 718;
    
// global set width and height used at this moment
var setWidth = 1160;
var setHeight = 718;

function seName(){
	var s = 'section'+ (inSection+1);
	return s;
}
function slName(){
	var s = 'slide'+ (inSlide+1);
	return s;
}

// slide and navigation windom animation move dellay/speed
var animatedellay =  10; 


// creates sections and slides
createSS(doObj); 
//drag_and_check_xy();
// create sections and slides
function createSS(doContent) {
	console.log(">>>>>>>>>> >>>>>>>>>> >>>>>>>>>>   createSS Starts   >>>>>>>>>> >>>>>>>>>> >>>>>>>>>>");

    var sectionName,     // temp object for section
        slideName;       // temp object for slides
    var sections = 0
        , slides = 0;
		
    numberOfSlides = [];
    totalSlides = 0;

	// create section
    for (sectionName in doContent) {
        console.log("> Section = " + sectionName);
        $('#slidesContainer')
			.append('<div class="section" id="' + sectionName + '"></div>');
        
		// create slides
		for (slideName in doContent[sectionName]) {
			$('#' + sectionName)
				.append('<div class="slide" id="' + sectionName + slideName + '"></div>');
			$('#' + sectionName + slideName)
				.css({
					'position': "relative",
					"width":"1160px",
					"z-index": "1"
				});
            slides ++; // count slides
        }
		
        $('#' + sectionName)
			.css('height', setHeight*slides);
        numberOfSlides.push(slides); // create slides count per section arrray
		totalSlides += slides; // count all slides
        slides = 0;
        sections ++;
    }
    
    numberOfSections = numberOfSlides.length;
	
	console.log("> Created: totalSlides = " + totalSlides +
				"; numberOfSlides = " + numberOfSlides +
				"; numberOfSections = " + numberOfSections);
	wrapSection();    
	console.log("<<<<<<<<<< <<<<<<<<<< <<<<<<<<<<   createSS Ends   <<<<<<<<<< <<<<<<<<<< <<<<<<<<<<");
}

// Wrap all .slides within #sectionInner div
function wrapSection() {
    $('.section')
		.wrapAll('<div id="sectionInner"></div>')
		.css({
			'float' : 'left',           // Float left to display horizontally, readjust .slides width
			'width' : setWidth
		});

	// Set #sectionInner width equal to total width of all slides
    $('#sectionInner')
		.css('width', setWidth * numberOfSections);
    
    console.log("> wrap setWidth=" + setWidth);
    console.log("> All sections wrapped! Sections = " + numberOfSections + "; setWidth= " + setWidth + "; Total width= " + (setWidth * numberOfSections));
}




runSlideScript_template(doObj,0,0);
// on slide open, looks for slide script and load it if its exists
function runSlideScript_template(doObj,section,slide) {
	console.log('JS f: runSlideScript(){}');
	section = 'section'+section;
	slide = 'slide'+slide;
	var slideObj = doObj[section][slide];
	var template = slideObj.template.name;		// get template name
	
	var scriptObj = doObj[section][slide]['script'];

	var scriptName = scriptObj['name'];
	
	console.log('Template ' + template + ' script ='+scriptName);
	
	if (scriptName){	// check is there a script available
		if (scriptObj['iniciated']) {
			console.log('Template script is already iniciated: ' + scriptName);
		}
		else{
			console.log('Template script is not iniciated: ' + scriptName);
			console.log('Iniciating template script: ' + scriptName);
			//alert(scriptName);                       			
                        window[scriptName]();                       
			scriptObj['iniciated'] = true;
		}
	}
}


