$(function() {
    
    //$(document).pngFix();

    var version = $.browser.version.split(".");
    version.pop();
    version = version.join("");

    /*if ($.browser.mozilla == true && version < 193) {
	$("header > nav.slideshow > ul > li > a > img").corner("left 15px");
    }*/
  
        $.getJSON("/<?=strtolower($this->requestpars['lang'])?>/search/keywords?language=<?=strtoupper($this->requestpars['lang'])?>", function(data)
        {
            var tags    = new Array();
            var i       = 0;
            var highest = 0;
            var lowest  = 0;

            $.each(data, function(tag, item)
            {
                tags[i] = tag;
                if (parseInt(item) > parseInt(highest)) highest = item;
                if (parseInt(lowest) == 0) lowest = item;
                else if (parseInt(item) < parseInt(lowest)) lowest = item;
                i++;
            });

            highest = highest - lowest;

            $.each(data, function(tag,item)
            {
                data[tag] = 10 + Math.round(((item - lowest) / highest) * 12);
            });

            var tagdata = "";

            $.each(tags, function(i,item)
            {
                tagdata += "<a href='/<?=strtolower($this->requestpars['lang'])?>/search?query=" + item + "' style='font-size: " + data[item] + "px;'>" + item + "</a>";
            });

            var par = {};
            var flashvars = {};
            par.wmode = "transparent";
            flashvars = {
                mode            : "tags",
                tagcloud        : "<div>" + tagdata + "</div>",
                distr           : true,
                tcolor          : "0x7d7d7d",
                tcolor2         : "0x9d9d9d",
                hicolor         : "0x007836",
                tspeed          : 100
            };
            swfobject.embedSWF("/tagcloud/tagcloud.swf", "cloud", "345", "237", "9.0.0", "/cubes/expressInstall.swf", flashvars, par);

           
        });
  
    var Slideshow = {}, slideshow;
    
    Slideshow = function(slides) {
	
	var pager, interval, fade, interval, setInterval, stopInterval, startInterval, fadeInterval;
	
	fade = function(e) {
	    
	    var speed, target, data, index, href, linktest;
	    
	    speed = (e.type == 'click' || e.type == 'mouseenter') ? 'medium' : 3000;    
	    index = (e.data != undefined) ? e.data.parent().index() : null;
	    target = (e.currentTarget == undefined) ? pager.children('a:eq(' + index + ')') : $(e.currentTarget);
	    target = (e.type == 'mouseenter') ? pager.children('a.active').first() : target;
	    
	    slides.children('a').stop().fadeTo(speed, 0).parent().removeClass('active');
	    $(e.data).stop().fadeTo(speed, 1);
	    target.siblings().children('div.button').stop().fadeTo('medium', .4);
	    target.children('div.button').stop().fadeTo('medium', .8);
	    
	    $(e.data).parent().addClass('active');
	    
	    if (e.currentTarget != undefined && e.currentTarget.href != undefined) {
		href = e.currentTarget.href;
		linktest = href[href.length - 1];
		
		if (linktest == "#") {
		    return false;
		}
	    }	    
	}

	fadeInterval = function() {
	    
	    var next, button, e, index;
	    
	    next = slides.siblings('.active').next();
	    next = (next.length > 0) ? next : slides.first();
            
	    e = {
	        data: next.children('a')
	    }
	    fade(e);
        }
	
	startInterval = function() {
	    interval = window.setInterval(fadeInterval, 6250);
	}
	
	stopInterval = function() {
	    clearInterval(interval);
	}
        
	pager = $('<div class="pager"></div>');
        
	slides.each(function() {
	    
	    var slide, button, target, hit, tooltip, arrow, image;
            
	    slide = $(this).children('a');
	    target = (slide.attr('href').indexOf('http://')) ? '' : 'target="_blank" ';
	    button = $('<a href="' + slide.attr('href') + '" ' + target + '></a>');
	    tooltip = $('<div class="tooltip">' + slide.attr('rel') + '</div>');
	    image = new Image();
	    image.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='/css/img/button.png', sizingMethod='crop')";
	    image.src = "/css/img/button.png";
	    hitit = $('<div class="button"></div>');
	    hitit.append(image);
	    //hitit.pngFix();
	    button.click(slide, fade);
	    hitit.hoverIntent(function() {
		tooltip.slideDown();
	    }, function() {
		tooltip.slideUp();
	    });
	    button.append(hitit);
	    button.append(tooltip);
	    pager.append(button);
	});
	
	slides.parent().parent().hover(stopInterval, startInterval);
	slides.parent().parent().append(pager);
	fadeInterval();
	startInterval();
    }
    
    slideshow = new Slideshow($('nav.slideshow > ul > li'));
	
    var mainintent = {
        sensitivity: 1,
        interval: 50,
        timeout: 500,
        over: function() {
            $(this).children("ul").slideDown();
        }, 
        out: function() {
            $(this).children("ul").slideUp();
        }
    };

    var relatedintent = {
        sensitivity: 1,
        interval: 500,
        timeout: 5000,
        over: function() {},
        out: function() {
            $(this).children("ul").slideUp(500, "swing");
        }
    };
    
    $("nav.docs> ul.bottom > li > ul:eq(0)").animate({height: 50, paddingBottom: 12}, 500, "swing");
    
    $("nav.mainmenu > ul > li").hoverIntent(mainintent);
    
    $("nav.docs > ul > li").hoverIntent(relatedintent);
    
    $("nav.docs > ul > li").click(function() {
	var child = $(this).children("ul");
	var visible = $(this).parent().parent().find("ul > li > ul:visible");
	if(child.css("height") == "50px") {
	    child.slideUp(500, "swing", function() {
		child.css("height", "auto");
		child.slideDown(500, "swing");
	    });
	} else if(child.is(":hidden")) {
	    visible.slideUp(500, "swing");
	    child.slideDown(500, "swing");
	} else {
	    child.slideUp(500, "swing");
	}
    });
    
});
