/*!
 * Projekktor II Plugin: Share
 * VERSION: 1.0
 * DESC: Adds social share icons to the video display: facebook, twitter and embedd where possible
 * Copyright 2010, Sascha Kluger, Spinning Airwhale Media, http://www.spinningairwhale.com
 *
 * under GNU General Public License
 * http://www.projekktor.com/license/
 */
var projekktorShare = function(){};
jQuery(function($) {
projekktorShare.prototype = {
    
    _controlHideTimer: null,
    _isFading: false,
    _wasPlaying: false,
    embPopup: null,
    
    socialLink: false,
    socialSidebar: null,
    socialPopup: null,
    socialButtons: [],
    
    config: {
	
	/* domId names for embed elements */
	socialbarDomId:		'socialbar',
	socialButtonDomId:	'socialbbutton',
	socialPopupDomId:	'socialpopup',
	
	embed: {
	    domId:		'embed',	   
	    code:		'<iframe id="{embedid}" src="{playerurl}" width="640" height="385" frameborder="0"></iframe>',
	    enable:		false,
	    nameText:		'embed',
	    headlineText:	'Copy this:',
	    closeText:		'Close Window',
	    descText:		'This is the embed code for the current video which supports iPad, iPhone, Flash and native players.'
	},
	
	twitter: {
	    nameText:		'twitter',
	    domId:		'twitter',
	    enable:		true,
	    text:		'I found a cool HTML5 video player. Check this out.',
	    code: 		'http://twitter.com/share?url={pageurl}&text={text}&via=projekktor'	    
	},
	
	facebook: {
	    nameText:		'facebook',
	    domId:		'facebook',
	    enable:		true,
	    text:		'I found a cool HTML5 video player. Check this out.',	    
	    code: 		'http://www.facebook.com/sharer.php?u={pageurl}&t={text}'	    
	}	

    },
    
    initialize: function() {	

	
	this.drawSidebar();
	this.drawPopup();

	if (this.getPlayerConfig('sandBox')!==false || this.getItemConfig('embed').enable==true) this.addTool('embed');
	if (this.getItemConfig('twitter').enable!==false)this.addTool('twitter');
	if (this.getItemConfig('facebook').enable!==false)this.addTool('facebook');
	
	this.pluginReady = true;
    },

    itemHandler: function() {
	try {
	    this.socialLink = (!this.getItemConfig('link')) ? this.player.getSandboxWindow().attr('location') : this.getItemConfig('link');
	} catch(e) {
	    this.socialLink = this.getItemConfig('link');
	}
	
	this.toggleTool('facebook', this.socialLink===false);
	this.toggleTool('twitter', this.socialLink===false);
    },
    
    
    /*******************************
        DOM Monipulations
    *******************************/
    /* the sidebar */
    drawSidebar: function() {
	this.socialSidebar =  $(document.createElement('div'))
	    .appendTo(this.playerDom)
	    .attr({'id':this.player.getId()+"_"+this.config.socialbarDomId,'class':this.player.getCssClass()+this.config.socialbarDomId})
	    .hide();
    },
    
    /* general popup */
    drawPopup: function() {
        this.socialPopup = $(document.createElement('div'))	
	    .appendTo(this.playerDom)
	    .hide()
	    .attr({'id':this.player.getId()+"_"+this.config.socialPopupDomId,'class':this.player.getCssClass()+this.config.socialPopupDomId});	   	
    },
    
    openWindow: function(name) {
	this._isFading=true;
	this._wasPlaying  = (this.player.getState()==='PLAYING');
	if (this._wasPlaying===true) {
	    this.player.setPause();
	}
	this[name+"FillWindow"](this.socialPopup);	
	this.socialSidebar.hide();
	this.socialPopup.show();
    },   
 
    closeWindow: function() {
	this._isFading=false;
	this.socialSidebar.show();
	this.socialPopup.hide().html('');
	if (this._wasPlaying===true) {
	    this.player.setPlay();
	}

    },
    
    openURL: function(url) {
	var dest = window;
	dest.open(url);
	return false;
    },

   /*******************************
        stack button
    *******************************/   
    addTool: function(name) {	
	var ref = this;
	var button = $(document.createElement('div'))
	    .appendTo(this.socialSidebar)
	    .attr({'id':this.player.getId()+"_"+this.config[name].domId})
	    .html(this.getPlayerConfig(name).nameText)
	    .addClass(this.player.getCssClass()+'socialbutton')
	    .show();
	
	// icon
	$(document.createElement('div'))
	     .addClass(this.player.getCssClass()+'socialicon_'+this.config[name].domId)
	    .prependTo(button)
	
        button.click(function(evt){evt.stopPropagation();ref[name+"Click"]();});
	this.socialButtons.push()
    },
    
    toggleTool: function(name, block) {
	var ref = this;
	$.each($('.'+this.player.getCssClass()+'socialbutton'), function() {
	    if ($(this).attr('id')===ref.player.getId()+"_"+ref.config[name].domId) {
		if (block===true) {		    
		    $(this).hide();
		} else {
		     $(this).show();
		}
	    }
	});
    },
    
   /*******************************
        fade in / fade out
    *******************************/        
    hideSidebar: function() {	
        clearTimeout(this._controlHideTimer);
        if (!this.socialSidebar.is(':visible')) {return;}     
        this.socialSidebar.stop(true, true);
	this.socialSidebar.fadeOut('slow');
    },
    
    showSidebar: function() {
        var ref=this;
	if (this._isFading==true) return;
	if (this.player.getState()==='IDLE') return;
	
        clearTimeout(this._controlHideTimer);              
        if (this.socialSidebar.is(':visible')) {       	  
            ref._controlHideTimer=setTimeout(function(){ref.hideSidebar();},1500);
            return;
        };
        this._isFading = true;              
        this.socialSidebar.stop(true, true);        
        this.socialSidebar.fadeIn('fast', function(){
            ref._isFading=false;
        });
    },
    
    

   /*******************************
        Player-Event Handlers
    *******************************/    
    mousemoveHandler: function() {
        this.showSidebar();
    },  	


    
    /*******************************
        EMBED TOOL
    *******************************/
    embedClick: function() {
	this.openWindow('embed');
    },
    
    embedFillWindow: function(dest) {
	var ref = this;
	$(document.createElement('p'))
	    .appendTo(dest)
	    .html(this.getPlayerConfig('embed').descText);

	$(document.createElement('p'))
	    .appendTo(dest)
	    .html(this.getPlayerConfig('embed').headlineText);

	$(document.createElement('textarea'))
	    .appendTo(dest)
	    .attr('readonly', 'readonly')
	    .val(this.getEmbedCode())
	    .click(function(){this.select();})
	    .focus(function(){this.select();})

	$(document.createElement('a'))
	    .appendTo(dest)
	    .html(this.getPlayerConfig('embed').closeText)
	    .click(function(){ref.closeWindow();})	   
    },
            
    getEmbedCode: function() {
	// replace standard items
	var result = this.config.embed.code;
	var data = {};
		
	data.embedid = this.player.randomId(8);
	data.playerurl = window.location.href+window.location.hash;
	data.ID = this.player.getItemConfig('ID');

	for(var i in data) {
	    result = result.replace('{'+i+'}', data[i]);
	}
	return result;
    },
    
    
    
    /*******************************
        TWITTER TOOL
    *******************************/
    twitterClick: function() {
	// replace standard items
	var result = this.config.twitter.code;
	var data = {};
    
	data.text = escape(this.getPlayerConfig('twitter').text);
	data.pageurl = escape(this.socialLink);	

	for(var i in data) {
	    result = result.replace('{'+i+'}', data[i]);
	}
	this.openURL(result);
    },
    
    /*******************************
        FACEBOOK TOOL
    *******************************/
    facebookClick: function() {
	// replace standard items
	var result = this.config.facebook.code;
	var data = {};
    
	data.text = escape(this.getPlayerConfig('facebook').text);
	data.pageurl = escape(this.socialLink);	

	for(var i in data) {
	    result = result.replace('{'+i+'}', data[i]);
	}
	this.openURL(result);
    }	    
}
});
