var ZINDEX_TOPMOST = 1000;
var LIGHTBOX_OPTIONS = {
    imageLoading:			RESOURCES_PATH+'images/lightbox/lightbox-ico-loading.gif',		// (string) Path and the name of the loading icon
    imageBtnPrev:			RESOURCES_PATH+'images/lightbox/lightbox-btn-prev.gif',			// (string) Path and the name of the prev button image
    imageBtnNext:			RESOURCES_PATH+'images/lightbox/lightbox-btn-next.gif',			// (string) Path and the name of the next button image
    imageBtnClose:			RESOURCES_PATH+'images/lightbox/lightbox-btn-close.gif',		// (string) Path and the name of the close btn
    imageBlank:				RESOURCES_PATH+'images/lightbox/lightbox-blank.gif',			// (string) Path and the name of a blank image (one pixel)
    nothing: null
};

    var rounded10 = {
      tl: { radius: 10 },
      tr: { radius: 10 },
      bl: { radius: 10 },
      br: { radius: 10 },
      antiAlias: true
    } 
    var rounded5 = {
      tl: { radius: 5 },
      tr: { radius: 5 },
      bl: { radius: 5 },
      br: { radius: 5 },
      antiAlias: true
    } 
    var rounded5Left = {
      tl: { radius: 5 },
      tr: { radius: 0 },
      bl: { radius: 5 },
      br: { radios: 0 },
      antiAlias: true
    }
    var rounded5Right = {
      tl: { radius: 0 },
      tr: { radius: 5 },
      bl: { radius: 0 },
      br: { radios: 5 },
      antiAlias: true
    }

// Calculates the height of an object, without considering the element's style attribute
$.fn.unabrigedHeight = function () {
    var previousHeight = this.css('height');
    this.css('height', null);
    var height = this.height();
    this.css('height', previousHeight);
    return height;
}

// Ajaxifying forms
$.fn.ajaxify = function () {
    var $form = this;
    var $div = this.closest("div");
    $form.ajaxForm({
    	target: $div,
    	success: function() { 
            $div.fadeIn('slow');
            $("form.ajaxified").ajaxify(); 
            }
    });
}



$(function () {

    /* Lightbox */
    $('a.lightBox').lightBox(LIGHTBOX_OPTIONS);
            
    /* Ajaxified */
    $('.ajaxified').ajaxify();
    
    /* H1 playlists */
    $('.playlist').each(function () {
        var $p = $(this);
        var $i = $p.find('> *').hide().css({ visibility: 'visible' });
        var $cu = $i.eq(':last');
        function playNext() {
        	$cu.animate({ left: -500 }, 334, function () { $(this).hide(); });
        	// $cu.hide();
    		$cu = $cu.next(); if ($cu.size() == 0) $cu = $i.eq(0);
    		$cu.css({ left: 1000 }).show().animate({ left: 30 }, 667);
    		// $cu.show();
    		/*if (!$cu.hasClass('sIFR-replaced')) {
                    sIFR.replace(chevin, {
                      selector: '#homepage h1.white',
                      wmode: 'transparent',
                      css: '.sIFR-root { color: #ffffff; } .sIFR-root strong { font-weight: normal !important; font-family: "Chevin Pro Bold"; }',
                      sharpness: -50,
                      thickness: -50
                    });

                    sIFR.replace(chevin, {
                      selector: '#homepage h1.black',
                      wmode: 'transparent',
                      css: '.sIFR-root { color: #000000; } .sIFR-root strong { font-weight: normal !important; font-family: "Chevin Pro Bold"; }',
                      sharpness: -50,
                      thickness: -50
                    });
		}*/
    		setTimeout(playNext, parseFloat($cu.attr('duration'))*1000 || 2000);
        }
        playNext();
    });

   
    /* Main Navigation */
    $('.overNav').hide();
    $('#mainNav >ul >li').hover(function () {
        $(this).addClass('mouse-over')
        .find('.overNav').css({ height: null, width: $('#mainNav').width() }).stop().animate({ height: "show" }, { queue: false }).end()
            .find('> a > object').each(function () { 
                //sIFR.getReplacementByFlashElement(this).changeCSS(sIFR.hoverStyles[MAINNAV_BUTTON_SELECTOR].over); 
            })
            ;
    }, function () {
        $(this).removeClass('mouse-over')
            .find('.overNav').stop().hide().end()
            .find('> a > object').each(function () { 
                //sIFR.getReplacementByFlashElement(this).changeCSS(sIFR.hoverStyles[MAINNAV_BUTTON_SELECTOR].out); 
            })
            ;
    });
    
    /* Dropdown sections */
    $('.dropdown-sections').each(function () {
        var $div = $(this);
        var $ul = $('<ul></ul>').attr('class', 'dropdown-links full-content');
        $div.prepend($ul);
        
        var liid = (new Date()).getTime();
        $div.find('h6').hide().each(function () {
            var $h6 = $(this);
            var $section = $h6.next('div').eq(0);
            var $li = $('<li></li>');
            $li
                .attr('id', 'li'+liid)
                .append(
                    $('<a></a>')
                        .attr('href', '#'+liid)
                        .text($h6.text())
                        .click(function (e) {
                            e.preventDefault();
                            $div.find('h6 + div').hide();
                            $section.show();
                        })
                )
            ;
            $ul.append($li);
            liid++;
        });
        $div.find('div').hide();
        $div.find('div').eq(0).show();
    });
    
    /* Dropdown links */
    $('ul.dropdown-links').each(function () {
        var $dropdown = $(this).removeClass('dropdown-links');
        var $container = $('<div class="replaced-dropdown-links"></div>');
        var $outerContainer = $container.clone();
        $dropdown.after($container);
        $container.append($dropdown);

        // Add a list item at the top to show by default, before the user clicks
        // The title attribute of the UL is used for this task
        var teaserTitle = $dropdown.attr('title');
        if (!teaserTitle.match(/^\s*$/)) {
            $dropdown.prepend($('<li class="replaced-dropdown-teaser"><span>'+teaserTitle+'</span></li>'));
        } else {
            if ($dropdown.find('li.selected').size() == 0) $dropdown.find('li').eq(0).addClass('selected');
        }
        
        var $li_teaser = $dropdown.find('li.replaced-dropdown-teaser');
        var $li_first = $dropdown.find('li').eq(0);
        function closeTeaser(e) {
                // if ($(e.target).parents('li')[0] == $li_teaser[0]) return;
                
                $dropdown.find('li.selected').removeClass('selected');
                $(e.target).parents('li').eq(0).addClass('selected');
                
                $outerContainer.css({ zIndex: ZINDEX_TOPMOST-1 });
                $container.animate({ height: $dropdown.find('li.selected, li.replaced-dropdown-teaser, li').eq(0).height() });
                $dropdown
                    .animate({ height: $dropdown.find('li.selected, li.replaced-dropdown-teaser, li').eq(0).height() }, { 
                            queue: false, 
                            duration: 200, 
                            complete: function () { 
                                    $container.append($dropdown);
                                    $outerContainer.remove();
                                    $dropdown.bind('click', openTeaser);
                                    $li_first.css({
                                            marginTop: $li_first.offset().top - $dropdown.find('li.selected, li.replaced-dropdown-teaser, li').eq(0).offset().top
                                    });
                            }
                    })
                    .removeClass('expanded').addClass('collapsed')
                ;
                $(document).unbind('click', closeTeaser);
        }
        function openTeaser(e) {
                if ($container.size() == 0) return;
                e.stopPropagation();
                e.preventDefault();
                
                // open
                var offset = $container.offset();
                $('body').append($outerContainer);
                $li_first.css({ marginTop: 0 });
                $outerContainer.css({ 
                        position: 'absolute', 
                        left: offset.left, 
                        top: offset.top,
                        zIndex: ZINDEX_TOPMOST,
                        width: $container.width(),
                        height: $container.height()
                });
                $outerContainer.append($dropdown);
                $dropdown
                    .animate({ height: $dropdown.unabrigedHeight() }, { duration: 200, queue: false })
                    .addClass('expanded').removeClass('collapsed')
                ;
                
                // close when clicking elsewhere
                $(document).bind('click', closeTeaser);
                $dropdown.unbind('click', openTeaser);
        }
        
        var $li_selected = $dropdown.find('li.selected, li.replaced-dropdown-teaser, li').eq(0);
        $dropdown.add($container).css({ height: $li_selected.height() });
        $li_first.css({
                marginTop: $li_first.offset().top - $li_selected.offset().top
        });
        
        $dropdown.bind('click', openTeaser);
    });
 
 	    if ($.browser.msie) {
//			return;
			//alert('z');
 	    try {
		    $('#content').each(function() {
		    	curvyCorners(rounded10, this);
		    });
		    $('.ajaxified').each(function () {
		    	curvyCorners(rounded10, this);
		    });
		    $('.roundedCorners').each(function () {
		    	curvyCorners(rounded10, this);
		    });
		    $('#sidebar > h6').each(function() {
		    	curvyCorners(rounded5, this);
		    });
		    
		    $('#sidebar > div').each(function() {
		    	curvyCorners(rounded5, this);
		    });
		} catch(err) {};
	    }
	    
    /* Search form */
    $('#search input[type=text]').each(function () {
        var $input = $(this);
        $input
            .attr('value', $input.attr('title'))
            .focus(function () {
                if ($input.attr('value') == $input.attr('title')) $input.attr('value', '');
            })
            .blur(function () {
                if ($input.attr('value') == '') $input.attr('value', $input.attr('title'));
            })
        ;
    });
   
 });

