var lastClickedDownload = false;

$(document).ready(function(){
    //manipulating catmenu for tt_news
    $('ul.fakemenu').each(function(){
    	var elems = $(this).find('li');

  		var ulid = $(this).attr('id');
			if(!ulid) {
				ulid = parseInt(Math.random()*10000);
			}


    	var formid = 'fake-'+ ulid;
    	var formTemplate = '<form class="jNice" action="" id="'+formid+'"" ><select onchange="if(this.options[this.selectedIndex].value){document.location.href = this.options[this.selectedIndex].value}"></select></form>';

    	//append form after ul
    	$(this).after(formTemplate);
    	var select = $('#'+formid+ ' select')[0];

    	var activeOption = 0;
    	elems.each(function(i){
    		var link = $(this).find('a');
    		var selected = ($(this).hasClass('active')) ? 1 : 0;
    		selected == 1 ? activeOption = i : '';

    		var href = $(link).attr('href') ? $(link).attr('href') : '';  		
    		if(href != ''){
      		//var myNewOption = new Option($(this).text(), href, selected);
    			//select.options[select.options.length] = myNewOption;
    			var testoption = '<option value="'+href+'"';
          if(selected == 1) {
            testoption += ' selected="selected" '
          }
          testoption += '>'+$(this).text()+'</option>';
    			$(select).append(testoption);
    		}
				//$.jNice.SelectUpdate(element);
				$(this).remove();
			});
			//select.selectedIndex = activeOption;

		});

    //prepare flv links for the lightbox
    $('#main-content a[href$=flv], #main-content a[href$=FLV]').each(function(){
        $(this).addClass('lightbox-link').click(function(){
				return false;
				});
    });

    //transform download links to show a modal dialog page
    if(!$.cookies.get('acceptCopyright')) {
	    $('#main-content p.download-link a').each(function(){
	        $(this).attr('savedhref', $(this).attr('href'));
	        $(this).attr('href',  location.href.toString().split('#')[0]+'#modal-dialog').addClass('lightbox-modal');
	        $(this).attr('rel',  'nofollow');
	        $(this).attr('target', '');
	        $('#modal-dialog').css({
						'width': '340px',
						'visibility': 'hidden'
						});
					$(this).click(function(){
						lastClickedDownload = $(this);
					});
	    });
	  }
});