$(document).ready(function(){
    $("a").bind('focus',function(){
        $(this).blur();
    });
    $('#newsFeed .desc').show();
    $('#newsFeed .readmore').show();
    $('#newsFeed .text').hide();
    
    $('#newsFeed .readmore').bind('click',function(){
        $($(this).parents()[0]).find('.text').show();
        $($(this).parents()[0]).find('.readmore').hide();
        $($(this).parents()[0]).find('.hide').show();
        $($(this).parents()[0]).find('.desc').hide();
        $($(this).parents()[0]).css('marginBottom','1em');
    });
    
    $('#newsFeed .hide').bind('click',function(){
        $($(this).parents()[0]).find('.text').hide();
        $($(this).parents()[0]).find('.readmore').show();
        $($(this).parents()[0]).find('.hide').hide();
        $($(this).parents()[0]).find('.desc').show();
        $($(this).parents()[0]).css('marginBottom','0em');
    });
    
    $jScroller.add("#scroller_container","#scroller","left",1);

  // Start Autoscroller
    $jScroller.start();

    boundCMSEditable();
    bindCMSHover();
    
    
});

$(document).ready(function(){
				  $(".vid").hide();
				  
				  $(".showvideo").click(function(){
										$(".vid").slideToggle(250);
										});
				  });

$(document).ready(function(){
				  $(".video").hide();
				  
				  $(".s").click(function(){
									$(".video").slideToggle(250);
									});
				  });


// remove/add class current for each img element
function cycleImages() {
	var $active = $('#cycleImages .current');
	var $next = ($('#cycleImages .current').next().length > 0) ? $('#cycleImages .current').next() : $('#cycleImages img:first');
	
	if($.browser.msie && $.browser.version == "6.0") {
		fadeTime = 50;
	} else {
		fadeTime = 1500;
	}
	
	$active.fadeOut(fadeTime, function() {
					$active.removeClass('current');
					$next.fadeIn(fadeTime).addClass('current');
					});
}

// call the function every...
$(document).ready(function() {
				  if($.browser.msie && $.browser.version == "6.0") {
					setInterval('cycleImages()', 10000);
				  } else {
					setInterval('cycleImages()', 8000);
				  }
				  });

(function($) {
 var userAgent = navigator.userAgent.toLowerCase();
 
 $.browser = {
 version: (userAgent.match( /.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/ ) || [0,'0'])[1],
 safari: /webkit/.test( userAgent ),
 opera: /opera/.test( userAgent ),
 msie: /msie/.test( userAgent ) && !/opera/.test( userAgent ),
 mozilla: /mozilla/.test( userAgent ) && !/(compatible|webkit)/.test( userAgent )
 };
 
 })(jQuery);

// simple script to do email links, and hide (somewhat) the addr.
function epost(sMailbox,sDomain,sTop) {
	sHref = 'mai';
	sHref = sHref + 'lto' + ':' + sMailbox;
	sHref = sHref + '@' ;
	sHref = sHref + sDomain + '.' + sTop;
	location.href = sHref;
}


function boundCMSEditable(){
    $('.cms-content').bind('dblclick', function(){
        if(!$(this).hasClass('cms-editable-bound')){
            CMSMakeEditable(this);
            $(this).addClass('cms-editable-bound');
        }
    });
}
function bindCMSHover(){
    /*
    $('.cms-content').bind('mouseover', function(){
        if(!$(this).hasClass('cms-content-hover')){
            $(this).before($("<div></div>").html($(this).attr('rel')).bind('click',
                                                                           function(){
                                                                            $(this).remove()
                                                                            }
                                                                        )
                                );
            
            $(this).addClass('cms-content-hover');
            }
        });
    */
    
    $('.cms-content').bind('click', function(e){
    
    
        if(e.shiftKey == true){

            if(!$(this).hasClass('cms-rightclick-bound')){
                identif = $(this).attr('rel');
                
                thisident = $(this).attr('id').split("___")[0];
                
                if(identif == ""){
                    alert('id:\n'+thisident+"\nInte länkad...");
                }else{
                    alert('id:\n'+thisident+'\nlänkad med: \n'+identif);
                }
                
                
                $(this).addClass('cms-rightclick-bound');
            }
        }

    });
}

function CMSMakeEditable(element){
    
    div = $('<div />').addClass('yui-skin-sam');
    textArea = $('<textarea />').attr('name','cms-editable').attr('id','cms-editable');
    
    id = $(element).attr('id');
    
    $(textArea).attr({'id':id});
    
    $(textArea).val($(element).html());
    
    width = $(element).css("width");
    height = $(element).css("height");
    
    if(parseInt(height,10) < 10){
        height = 200;
    }
    
    textAreaCss = $(element).offset();
    
    textAreaCss.position = 'absolut';
    
  //  console.log(textAreaCss);
    
    $(textArea).css(textAreaCss);
    
    //$(textArea).css({'width': width, 'height' : height});
    
    /*
    $(textArea).bind('dblclick', function(){
        CMSMakeNoneEditable(this);
    });
    */
    
    $(div).append($('<div />').html(id.split("___")[0]))
    $(div).append(textArea);
    
    addrichlink = $('<a />').html('[Add Rich Text Editor]');
    
    saveLink = $('<a />').html('[Save]').bind('click',function(){
        CMSMakeNoneEditable($($(this).parents()[1]).find('textarea'));
    });
    
    /*
    $(addrichlink).bind('click', function(){
        
        CMSaddRichTextEditor($($(this).parents()[1]).find('textarea'));
        
    })
    
    $(div).append($('<div />').append(addrichlink));
    */
    
    $(div).append($('<div />').append(saveLink));
    
    CMSaddRichTextEditor($(textArea));
    
    
    tinyMCE.execCommand('mceRemoveControl', false, $(textArea));
    tinyMCE.execCommand('mceAddControl', false, $(textArea));
    
    $(element).replaceWith(div);
    
}




function CMSaddRichTextEditor(elm){
        $(elm).tinymce({
            // Location of TinyMCE script
            script_url : sBaseUrl+'/public/js/tiny_mce/tiny_mce.js',
            
            content_css : sBaseUrl+'/public/css/recruto-screen.css,'+sBaseUrl+'/public/css/recruto-tinymceoverride.css,'+sBaseUrl+'/public/css/blueprint-grid.css',
            
            // General options
            theme : "advanced",
            plugins : "safari,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template",
            // Theme options
            
            // Save button stuff
            theme_advanced_buttons1_add_before : "save",
            
            theme_advanced_buttons1 : "save,|,bold,italic,underline,strikethrough,|,bullist,numlist,|,justifyleft,justifycenter,justifyright,justifyfull,styleselect,formatselect,fontselect,fontsizeselect",
          //  theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,|,insertdate,inserttime,preview,|,forecolor,backcolor",
          //  theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr,|,print,|,ltr,rtl,|,fullscreen",
           // theme_advanced_buttons4 : "insertlayer,moveforward,movebackward,absolute,|,styleprops,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,template,pagebreak",
            theme_advanced_toolbar_location : "top",
            theme_advanced_toolbar_align : "left",
            theme_advanced_statusbar_location : "bottom",
            theme_advanced_resizing : true,
            
            // save plugin stufsfs
            cleanup_on_startup : true,
            cleanup: true,
            debug : false

            
    });

}


function joelbitarAjaxSave(editorId){
    html = $('#'+editorId).val();
    
    identifier = editorId.split("___")[0];
    part = editorId.split("___")[1];
    
    data = {'identifier':identifier,'part':part,'value':html};
    
    $.post(sBaseUrl+"/admin/",data);
    
    tinyMCE.execCommand('mceRemoveControl', false, editorId);
    
}

function CMSMakeNoneEditable(element){
    div = $('<div />').addClass('cms-content');
    
    id = $(element).attr('id');
    
    $(div).attr({'id':id});
    
    value = $(element).val();
    
    $(div).html(value);
    
    $($(element).parents()[0]).replaceWith(div);
    
    tinyMCE.execCommand('mceRemoveControl', false, id);
    
    boundCMSEditable();
    
    identifier = id.split("___")[0];
    part = id.split("___")[1];
    
    data = {'identifier':identifier,'part':part,'value':value};

    $.post(sBaseUrl+"/admin/",data);
    
    console.log('and do post...');
}