/**
 * @author Brightcove, modified by Michael Bosworth to use mootools
 */

// Extend Array class to add an equivalent "push" function as in prototype
Array.extend({
	sortBy: function(iterator) {
	  return this.map(function(value, index) {
	    return {value: value, criteria: iterator(value, index)};
	  }).sort(function(left, right) {
	    var a = left.criteria, b = right.criteria;
	    return a < b ? -1 : a > b ? 1 : 0;
	  }).pluck('value');
	},		
	pluck: function(property) {
	  var results = [];
	  this.each(function(value, index) {
	    results.push(value[property]);
	  });
	  return results;
	},
	uniq: function(sorted) {
	  return this.inject([], function(array, value, index) {
	    if (0 == index || (sorted ? array.getLast() != value : !array.include(value)))
	      array.push(value);
	    return array;
	  });
	},
	inject: function(memo, iterator) {
	  this.each(function(value, index) {
	    memo = iterator(memo, value, index);
	  });
	  return memo;
	},
	clone: function(){
		return this.copy();
	},
	without: function(value){
		return this.remove(value);
	}	
});

var Effect = new Class({ });

Effect.Fade = function(id, options) {
   	var effect;
   	if(options != null){
   		effect = new Fx.Style(id, "opacity", {duration:(options.duration != null)? options.duration * 1000 : 500, onComplete:function(){$(id).style.display="none"}}).start(1,0);
   	}
   	else{
   		effect = new Fx.Style(id, "opacity", {duration:500, onComplete:function(){$(id).style.display="none"}}).start(1,0);    	
   	}
   	return effect;
};

Effect.Appear = function(id, options) {
   	var effect; 
   	if(options != null){
   		effect = new Fx.Style(id, "opacity", {duration:(options.duration != null)? options.duration * 1000 : 500, onStart:function(){Element.show(id);}}).start(0,1);
   	}
   	else{
   		console.log("no duration");
   		effect = new Fx.Style(id, "opacity", {duration:500, onStart:function(){Element.show(id);}}).start(0,1);    	
   	}
   	return effect;
};
   
Effect.toggle = function(id, effect){
	var el = $(id);
	if(el.style.display != "none"){
		el.style.display = "none"; console.log("hide");
	}
	else{
		el.style.display = ""; console.log("show");
	}
};

Element.show = function(id){
	$(id).style.display = "";
}
Element.hide = function(id){
	$(id).style.display = "none";
}
Element.remove = function(id){
	$(id).remove();
}

Element.extend({
	up: function(){
		return this.getParent();
	}
});

function $F(id){
	return $(id).getValue();
}

//-------------------------------------------------------------------------- OBJECT DECLARATION
var bc_Item = new Class({
	initialize: function()
    {
        this._init.call(arguments);
    },
    
    _init: function(pName, pIdPrefix)
    {
        this.name = pName;
        this.selected = false;
		this.id = pIdPrefix + "_" + String(pName).replace(/[ '"]/g, ""); //replaces all spaces and single quotes with nothing to make the name like bodyzone_Abs
	},
	
	getName: function()
    {
        return this.name;
    },
    
	getSelected: function()
	{
		return this.selected;
	},
	
    isSelected: function()
    {
        return this.selected;
    },
	
	setSelected: function(pSwitch)
	{
		this.selected = pSwitch;
	},
	
	getId: function()
	{
		return this.id;
	}
});

var bc_Checkbox = bc_Item.extend({
    initialize: function(pName, pType)
    {
		this._init(pName, null);
        //this.name = pName;
        this.type = pType;
        //this.checked = false;
		this.enabled = true;
		//this.id = pType + "_" + String(pName).replace(/ /g, ""); //replaces all spaces with nothing to make the name like bodyzone_Abs
    },
	
	isEnabled: function()
	{
		return this.enabled;
	},
	
	setEnabled: function(pEnabled)
	{
		this.enabled = pEnabled;
	}
});

var bc_BodyZoneCheckbox = bc_Checkbox.extend({
    initialize: function(pName)
    {
        this._init(pName, "bodyzone");
        this.bodyZone = pName.toUpperCase();
    },
    
    getBodyZone: function()
    {
        return this.bodyZone;
    }
});

var bc_EquipmentCheckbox = bc_Checkbox.extend({
    initialize: function(pName)
    {
		this._init(pName, "equipment");
        this.equipment = pName.toUpperCase();
    },
    
    getEquipment: function()
    {
        return this.equipment;
    }
});

var bc_TimeButton = bc_Item.extend({
	initialize: function(pTime)
    {
		this._init(pTime+" Minutes", "time");
        this.time = pTime;
		//this.name = pTime + " Minutes";
		//this.id = "time_" + pTime;
		this.selected = false;
    },
	
	getTime: function()
	{
		return this.time;
	}
});

var bc_PlaylistItem = bc_Item.extend({
    initialize: function()
    {	
		var videos = $A(arguments); //make arguments an Array so we can loop through it
		var item = new Object(); //temporary object so that we can assign local variables after the loop
		
		videos.each(function(videoDTO)
		{
			var video = $H(videoDTO); //creates a hash for each video object sent in
			video.keys().each(function(pKey, pIndex)
			{
				if(pKey == "displayName") item.name = video.values()[pIndex];
				if(pKey == "bodyZones") item.bodyZones = video.values()[pIndex]; 
				if(pKey == "thumbnail") item.thumbnail = video.values()[pIndex];
				if(pKey == "equipment") item.equipment = video.values()[pIndex];
				if(pKey == "description") item.description = video.values()[pIndex]; 
				if(pKey == "videoId") item.videoId = video.values()[pIndex];
				if(pKey == "time") item.time = video.values()[pIndex];
				if(pKey == "thumbnail") item.thumbnail = video.values()[pIndex]; 
				if(pKey == "selected") item.selected = video.values()[pIndex];  
			});
		});
		
		this._init(item.name, "title");
		this.hidden = false;
		//this.name = item.name;
		//this.id = "title_" + item.name.replace(/ /g, ""); //replaces all spaces with nothing to make the name like title_MegaPushups
		this.orderNumber = null;
		this.bodyZones = item.bodyZones
		this.equipment = item.equipment;
		this.thumbnail = item.thumbnail;
		this.videoId = item.videoId;
		this.description = item.description;
		this.time = item.time;
		this.thumbnail = item.thumbnail;
		this.selected = item.selected;
    },
	
	getBodyZones: function()
	{
		return this.bodyZones;
	},
	
	getEquipment: function()
	{
		return this.equipment;
	},
	
	getThumbnail: function()
	{
		return this.thumbnail;
	},
	
	getVideoId: function()
	{
		return this.videoId;
	},
	
	getTime: function()
	{
		return this.time;
	},
	
	isHidden: function()
	{
		return this.hidden;
	},
	
	setHidden: function(pValue)
	{
		this.hidden = pValue;
	},
	
	getOrderNumber: function()
	{
		return this.orderNumber;
	},
	
	setOrderNumber: function(pOrderNumber)
	{
		this.orderNumber = pOrderNumber;
	}
});


var bc_LineupItem = bc_Item.extend({
    initialize: function()
    {	
		var videos = $A(arguments); //make arguments an Array so we can loop through it
		var item = new Object(); //temporary object so that we can assign local variables after the loop
		
		videos.each(function(videoDTO)
		{
			var video = $H(videoDTO); //creates a hash for each video object sent in
			video.keys().each(function(pKey, pIndex)
			{
				if(pKey == "displayName") item.name = video.values()[pIndex];
				if(pKey == "bodyZones") item.bodyZones = video.values()[pIndex]; 
				if(pKey == "thumbnail") item.thumbnail = video.values()[pIndex];
				if(pKey == "description") item.description = video.values()[pIndex];
				if(pKey == "equipment") item.equipment = video.values()[pIndex];
				if(pKey == "videoIds") item.videoIds = video.values()[pIndex];
				if(pKey == "time") item.time = video.values()[pIndex];
				if(pKey == "thumbnail") item.thumbnail = video.values()[pIndex];
				if(pKey == "selected") item.selected = video.values()[pIndex];
			});
		});
		
		this._init(item.name, "lineup_");
		this.bodyZones = item.bodyZones
		this.equipment = item.equipment;
		this.thumbnail = item.thumbnail;
		this.description = item.description;
		this.videoIds = item.videoIds;
		this.time = item.time;
		this.thumbnail = item.thumbnail;
		this.selected = item.selected;
    },
	
	getBodyZones: function()
	{
		return this.bodyZones;
	},
	
	getEquipment: function()
	{
		return this.equipment;
	},
	
	getVideoId: function()
	{
		return this.videoId;
	},
	
	getThumbnail: function()
	{
		return this.thumbnail;
	},
	
	getDescription: function()
	{
		return this.description;
	},
	
	getTime: function()
	{
		return this.time;
	}
});
//--------------------------------------------------------------------------

var apiToken = encodeURIComponent("bkkyRXZCC4pq_wrL6cEux_XpNsb2Z6xDVH-OJJ7Qtlw.");

function setCookie()
{
	var cookie = new Object();
	cookie.path = "/";
	cookie.secure = "";
	cookie.domain = "";
	
	var args = $H(arguments[0]);
	args.keys().each(function(pKey, pIndex)
	{
		if(pKey == "name") cookie.name = args.values()[pIndex];
		if(pKey == "data") cookie.data = args.values()[pIndex];
		if(pKey == "path") cookie.path = args.values()[pIndex];
		if(pKey == "domain") cookie.domain = args.values()[pIndex];
		if(pKey == "secure") cookie.secure = args.values()[pIndex];
		if(pKey == "expires") cookie.expires = args.values()[pIndex];
	});
	
	document.cookie = cookie.name+"="+cookie.data+"; expires="+cookie.expires+"; path=/";
}

function deleteCookie()
{
	var cookie = new Object();
	cookie.path = "/";
	cookie.domain = "";
	cookie.secure = "";
	
	var args = $H(arguments[0]);
	args.keys().each(function(pKey, pIndex)
	{
		if(pKey == "name") cookie.name = args.values()[pIndex];
		if(pKey == "path") cookie.path = args.values()[pIndex];
		if(pKey == "domain") cookie.domain = args.values()[pIndex];
		if(pKey == "secure") cookie.secure = args.values()[pIndex];
	});
	
	var expiryDate = new Date();
	expiryDate.setTime(expiryDate.getTime() - (365 * 24 * 60 * 60 * 1000)); //1 year ago
	document.cookie = cookie.name+"=; path="+cookie.path+"; domain="+cookie.domain+"; expires="+expiryDate+"";
}


/*------------------------------------------------------------------------------------
 
 Preloader
 A very simple image preloader object
 Author: Warpspire (http://warpspire.com/tipsresources/interface-scripting/image-preloading-revisited/)
 
 Usage:
 
 Preloader.add(path);
 Preloader.onFinish(func);
 Preloader.load();
 
 path: 		A string or array of strings of image paths to preload
 func:     A function or array of functions to be called after images are loaded
 load():   Start the preloader
 
 ------------------------------------------------------------------------------------*/
var Preloader = 
{
    callbacks: [],
    images: [],
    loadedImages: [],
    imagesLoaded: 0,
    
    add: function(image)
    {
        if (typeof image == 'string') this.images.push(image);
        if (typeof image == 'array' || typeof image == 'object') 
        {
            for (var i = 0; i < image.length; i++) 
            {
                this.images.push(image[i]);
            }
        }
    },
    onFinish: function(func)
    {
        if (typeof func == 'function') this.callbacks.push(func);
        if (typeof func == 'array' || typeof func == 'object') 
        {
            for (var i = 0; i < func.length; i++) 
            {
                this.callbacks.push(func[i]);
            }
        }
    },
    load: function()
    {
        for (var i = 0; i < this.images.length; i++) 
        {
            this.loadedImages[i] = new Image();
            this.loadedImages[i].onload = function()
            {
                Preloader.checkFinished.apply(Preloader)
            }
            this.loadedImages[i].src = this.images[i];
        }
    },
    
    checkFinished: function()
    {
        this.imagesLoaded++;
        if (this.imagesLoaded == this.images.length) this.fireFinish();
    },
    fireFinish: function()
    {
        for (var i = 0; i < this.callbacks.length; i++) 
        {
            this.callbacks[i]();
        }
        this.images = [];
        this.loadedImages = [];
        this.imagesLoaded = 0;
        this.callbacks = [];
    }
}

//Preloader.add(gImages);
//Preloader.load();