jQuery.fn.jumpTo = function() {

    var removeBG = function() {
        $(this).css("color", "");
    }

    var moveAndChangeBG = function() {
        $(this).css("color", "#65ab39");
        
        var targetOffset = $(this).offset().top-200;
        $('html,body').animate({scrollTop: targetOffset}, 600, "swing");
    }

    $(this).parent().find("> div").find("> p").each(removeBG);
    $(this).find("> p").each(moveAndChangeBG);
    
}

jQuery.fn.setupAktiv = function() {

    var imageCount = 1;
    var headlineCount = 1;
    var flashplayer = "9.0.0";

	var swfparams = {
		menu: "false",
		wmode: "transparent",
		scale: "noscale",
		quality: 'high',
		align: 'middle',
		play: 'true',
		loop: 'true',
		devicefont: 'false',
		allowFullScreen: 'false',
		allowScriptAccess: 'sameDomain',
		movie: 'image',
		salign: 'lt'
	};


    var replaceSpot = function(){
        var src = $(this).attr("title");
        
        if (src != "") {
            var cls = $(this).attr("class");   
            swfparams['width'] = $(this).width();
            swfparams['height'] = $(this).height();
            
            var st = {
                'class' : cls
            };

            $.swfobject.embedSWF(src, $(this)[0].id, $(this).width(), $(this).height(), flashplayer, "", {}, swfparams, st);
        }
    }



    var replaceImage = function(){
        $(this)[0].id = "flashimage"+imageCount;
        
        swfparams['width'] = $(this).width();
        swfparams['height'] = $(this).height();

    	//margin & padding
        var margins = {
            style:"margin-bottom:" + $(this).css("padding-bottom") + "; margin-top:" + $(this).css("padding-top") + ";"
        };

        // flashvars
    	var flashvars = {
    		src: $(this)[0].src
    	};
        $.swfobject.embedSWF("/public/swf/image.swf", $(this)[0].id, $(this).width(), $(this).height(), flashplayer, "", flashvars, swfparams, margins);
        imageCount++;      
    } 
    
    
    var replaceImageCustom = function(){
        $(this)[0].id = "flashimage"+imageCount;    
        swfparams['width'] = $(this).width();
        swfparams['height'] = $(this).height();

    	//margin & padding
        var margins = {
            style:"margin-bottom:" + $(this).css("padding-bottom") + "; margin-top:" + $(this).css("padding-top") + ";"
        };
        
        var src = $(this).attr('title');
        
        $.swfobject.embedSWF(src, $(this)[0].id, $(this).width(), $(this).height(), flashplayer, "", {}, swfparams, margins);
        imageCount++;
    } 

    
    
    var replaceHeadline = function(){
        
        $(this)[0].id = "headline"+headlineCount;
        
    	var fontsize = $(this).css("font-size");
    	fontsize = parseInt(fontsize);
    	    	
    	var color = $(this).css("color");
    	if (String(color).substr(0,1) != "#") {
            color = color.replace(/rgb/, "");
            color = color.replace(/\)/, "");
            color = color.replace(/\(/, "");
            color = color.split(/,/);
            color = rgb2hex(color);
        }
        else {
            color = color.replace(/#/gi, "0x");
        }

        
        swfparams['width'] = $(this).width();
        swfparams['height'] = fontsize+4;
        
        // flashvars
    	var flashvars = {
    	    text:escape($(this)[0].innerHTML.replace(/&amp;/, '&')),
    	    size:fontsize,
    	    color:color
    	};
    	
    	//margin & padding
        var margins = {
            style:"margin-left:" + $(this).css("margin-left") + "; margin-bottom:" + $(this).css("padding-bottom") + "; margin-top:" + $(this).css("padding-top") + ";"
        };

        $.swfobject.embedSWF("/public/swf/header.swf", $(this)[0].id, $(this).width(), fontsize+4, flashplayer, "", flashvars, swfparams, margins);
        headlineCount++;
        
        //Function to get hex format a rgb colour
        function rgb2hex(rgb) {
            //generates the hex-digits for a colour.
            function hex(x) {
                hexDigits = new Array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F");
                return isNaN(x) ? "00" : hexDigits[(x - x % 16) / 16] + hexDigits[x% 16];
            }

            return "0x" + hex(rgb[0]) + hex(rgb[1]) + hex(rgb[2]);
        }
    }


    var replaceMenu = function() {

        swfparams['width'] = $(this).width();
        swfparams['height'] = $(this).height();

        var menuStr = "<?xml version=1.0 encoding=UTF-8?><ul>";
        var addOption = function() {
            menuStr += "<li>";

            // add link
            var a = $(this).find("> a")[0];

            var cls = "";
            if ($(a).attr("class") == "selected") { cls += " class='selected'"; }

            menuStr += "<a href='" + $(a).attr("href") + "' " + cls + " >" + $(a).html() + "</a>";
            $(this).find("> ul").each(function() {
                menuStr += "<ul>";
                $(this).find("> li").each(addOption);
                menuStr += "</ul>";
            });


            // submenus 
            menuStr += "</li>";
        }

        $("#menu").find("> li").each(addOption);


        menuStr += "</ul>";

        var flashvars = {
            'html': menuStr
        };
        $.swfobject.embedSWF("/public/swf/menu.swf", $(this)[0].id, $(this).width(), $(this).height(), flashplayer, "", flashvars, swfparams);
    }

    var replaceMenuHome = function() {

        swfparams['width'] = $(this).width();
        swfparams['height'] = $(this).height();

        var menuStr = "<?xml version=1.0 encoding=UTF-8?><ul>";
        var addOption = function() {
            menuStr += "<li>";

            // add link
            var a = $(this).find("> a")[0];

            var cls = "";
            if ($(a).attr("class") == "selected") { cls += " class='selected'"; }

            menuStr += "<a href='" + $(a).attr("href") + "' " + cls + " >" + $(a).html() + "</a>";
            $(this).find("> ul").each(function() {
                menuStr += "<ul>";
                $(this).find("> li").each(addOption);
                menuStr += "</ul>";
            });


            // submenus 
            menuStr += "</li>";
        }

        $("#menuhome").find("> li").each(addOption);


        menuStr += "</ul>";

        var flashvars = {
            'html': menuStr
        };
        $.swfobject.embedSWF("/public/swf/menu.swf", $(this)[0].id, $(this).width(), $(this).height(), flashplayer, "", flashvars, swfparams);
    }        
    
    
    
    
	var replaceVideo = function(){
        swfparams['width'] = $(this).width();
        swfparams['height'] = $(this).height();
		swfparams['allowFullScreen'] = 'true';
		
		// flashvars
    	var flashvars = {
			file: $(this).attr("title"), //$(this)[0].innerHTML,
			backcolor: "000000",
    		skin: "/public/swf/stylish.swf",
    		autostart: "true"
    	};
        $.swfobject.embedSWF("/public/swf/player.swf", $(this)[0].id, $(this).width(), $(this).height(), flashplayer, "/public/swf/expressInstall.swf", flashvars, swfparams);

	}

    
	var showHideForm = function(e){
		
		var formDiv = $(document).find("div#"+"doctor_"+$(this).attr("id").substr(6));
		if (formDiv.css("display") == "none")
			formDiv.fadeIn("slow");
		else
			formDiv.fadeOut("fast");
	}
	

	// Uppdatera "1000 tecken kvar." i "fråga doktorn"-fomuläret
	var updateChars = function(e){
		
		var charsLeft = 1000 - parseInt($(this).attr("value").length);
		
		if (charsLeft < 0) {
			$(this).attr("value", $(this).attr("value").substr(0, 1000) );
		}
		
		var cLeft = 1000 - parseInt($(this).attr("value").length);
		$(this).parent().find(".chars").children("strong").text(cLeft);
		
	}
	
	
	

    // // Replace spots
    $(this).find('.module').each(replaceSpot);   
    
    // // add click event to doctor buttons
    $(this).find("div#buttonsearch").bind("click", showHideForm);
    $(this).find("div#buttonask").bind("click", showHideForm);
    
    // // add keyup event to doctor
    $(this).find('textarea#question').bind("keyup", updateChars);

    // replace player div to swf
    $(this).find('#player').each(replaceVideo);

    // replace menu with swf
    $(this).find('#menu').each(replaceMenu);

    // replace menu with swf
    $(this).find('#menuhome').each(replaceMenuHome);    
    
    // replace headlines
    $(this).find('h1').each(replaceHeadline);
    $(this).find('h2').each(replaceHeadline);
    $(this).find('h3').each(replaceHeadline);
    $(this).find('h4').each(replaceHeadline);
    
    
    // replace images with standard Blur Zoom flash
    $(this).find('.flashImageBZ').each(replaceImage); 
    
    // replace images with custom flash (src set in title)
    $(this).find('.subpageSwf').each(replaceImageCustom);   
    
    
    // png fix IE 6
    $(document).pngFix();    
    
	
    // Replace Main Flash or activate Slide
	if ($.swfobject.hasFlashPlayerVersion(flashplayer)) {

	    // Insert Flash Carousel
	    var replaceCarousel = function() {

	        var cls = $(this).attr("class");
	        swfparams['width'] = $(this).width();
	        swfparams['height'] = $(this).outerHeight();


	        // flashvars
	        var flashvars = {
	            xmlsrc: $(this).attr("title")
	        };

	        var st = {
	            'class': cls,
	            'style': 'z-index:10;'
	        };

	        swfparams['wmode'] = "opaque";
	        $.swfobject.embedSWF('/public/swf/startflash.swf', $(this)[0].id, $(this).width(), $(this).outerHeight(), flashplayer, '', flashvars, swfparams, st);
	        swfparams['wmode'] = "transparent";
	    }

	    var replaceMainFlash = function() {

	        var cls = $(this).attr("class");
	        swfparams['width'] = $(this).width();
	        swfparams['height'] = $(this).outerHeight();


	        // flashvars
	        var flashvars = {
	            xmlsrc: $(this).attr("title")
	        };

	        var st = {
	            'class': cls,
	            'style': 'z-index:10;'
	        };

	        swfparams['wmode'] = "opaque";
	        $.swfobject.embedSWF($(this)[0].title, $(this)[0].id, $(this).width(), $(this).outerHeight(), flashplayer, '', flashvars, swfparams, st);

	    }	    

    	$('.carousel').each(replaceCarousel);
    	$('.mainflash').each(replaceMainFlash);
    	
		
	} else {	
		$('#slides').cycle({
			fx: 'scrollLeft',
    		timeout: 0,
            speed: 500,
            next: '#arrowright',
            slideExpr: 'table'
        });

		$('#arrowright').mouseover(function(){
			$(this).children("img").attr("src", "/public/images/arrow_right_over.gif");
		})

		$('#arrowright').mouseout(function(){
			$(this).children("img").attr("src", "/public/images/arrow_right.gif");
		})
		
	}

}



$(document).ready(
    function() {  
        // setup page    
        $('#main').setupAktiv();
    }
);

