/**
 * @project talenthouse
 * @package th.post
 * @author  Dimitar Parouchev <dimitar@parouchev.com>
 * @since   31.05.2009
 *
 * clearspring login
 * username: ymonork
 * password: 123456
 */

th.post = {
		  
		postEmbedChangeTo: function(platform){
            $('#post_to_container').removeClass();
            $('#post_to_container').addClass("rounded_all choose_embed post_to_"+platform );

            th.post.showWidget(platform)
            
		},		
		
        showWidget: function(platform){
             
            $("#export_targets").children().hide();
            $("#export_"+platform+"_target").show();
             
            $('#embed_code_height').val(th.post.getEmbedSizes(platform)['height']); 
            $('#embed_code_width').val(th.post.getEmbedSizes(platform)['width']); 
            
            var embedCode = th.post.getEmbedCode({platform: platform});
            $('#embed_code_inputfield').val( embedCode );
            $("#externalEmbedCode").html(embedCode)
            
            //clearspring config object
            conf = new Object();
            conf.userId = "4896df8c0c0abd17";
            conf.servicesInclude = [platform];
            conf.targetElement = "export_"+platform+"_target";
            conf.customCSS = th.params.url_ClearspringCss; 
            conf.widgetName = "export.talenthouse.to."+platform;
            conf.servicesExclude = ["desktop","embed"];
            conf.menuWidth = 363;
            conf.menuHeight = 240;
            conf.unique = true;
            conf.source = "externalEmbedCode";
            conf.wid = th.post.getClearspringWidget(platform);

            if (conf.wid){
                $Launchpad.ShowMenu(conf);
            }
            
        },
        
        getClearspringWidget: function(platform){
            var clearspringWidgets = {
                "blogger": "4a23ac37eecf5b1e",
                "xanga": "4a23c1437fc14ebf",
                "friendster": "4a23c22b8cfdba2e",
                "typepad": "4a23c2d03cfbd7cc"
            }
            return clearspringWidgets[platform] != undefined ? clearspringWidgets[platform] : null;
        },
        
        getEmbedSizes: function(platform){
            var embedSizes = { "standard": [605,365], "mini": [320,193], "customize": [605,365],
            "blogger": [410,247], "facebook": [700,422], "friendster": [605,365], 
            "myspace": [605,365], "typepad": [325,196], "xanga": [320,193] };  
            return {"width": embedSizes[platform][0], "height": embedSizes[platform][1]}
        },
        
        
        getEmbedCode: function(params){
            if(typeof params.platform == "undefined") params.params = "standard";

            //default width & height
            var values = {
               width : (typeof params.width == "undefined") ? th.post.getEmbedSizes(params.platform)['width'] : params.width,
               height : (typeof params.height == "undefined") ? th.post.getEmbedSizes(params.platform)['height'] : params.height
            };
            //parse the template and enter width & height
            //replace &amp; => &
            //clean + strip
            return $.string($.string($.tmpl( $("#externalEmbedTemplate").html(), values)).gsub('&amp;', '&').str).gsub(/\s\s+/m, '').strip().str;
        },
        
        onUpdateWidth: function(){
            elementWith = $('#embed_code_width');
            elementHeight = $('#embed_code_height');
            elementInput = $('#embed_code_inputfield');

            var regexToCheckIfIsANum=/(^\d+$)|(^\d+\.\d+$)/
            width = (regexToCheckIfIsANum.test( $('#embed_code_width').val() )) ? $('#embed_code_width').val() : $('#embed_code_width').attr('rel');
            width = (width < 300) ? 300 : width; 

            //var height = Math.round((width > 500) ? width / (605/300) : width / (320/200));
            var height = Math.round( width / (730/440) );

            $('#embed_code_height').val(height); 
            $('#embed_code_width').val(width); 
            $('#embed_code_inputfield').val( th.post.getEmbedCode({code: $('externalEmbed.template').innerHTML, width: width, height: height}) );
        },
        
        initCopyToClipboard: function(){
            clip = new ZeroClipboard.Client();
            clip.setText( $('#embed_code_inputfield').val() );
            clip.setHandCursor();
            clip.glue( 'clip_button' );
            $('#embed_code_inputfield').select();
            //clip.addEventListener('complete', '_successTextCopied');
            clip.reposition(); 
            
            return clip;
        },
        repositionClip: function(){
            if (clip == undefined) clip = th.post.initCopyToClipboard();
            clip.reposition()
        },
        
        postToMyspace: function(t,c,u,l){

            $("#post_to_container").attr("action", th.params.url_MyspacePostUrl);
            $("#post_to_container").attr("target", "blank");
            $("#post_to_container").attr("method", "post");
            
            $('#externalEmbedCodeMyspace').val( th.post.getEmbedCode({platform: 'myspace'}) );
            
            $("#post_to_container").submit();
        }
        
        
}