function bannerPresent(divElm) {
	var banner = $(divElm).descendants().find(function (elm) {
		if (elm.nodeType != 1) { // not an element
			return false;
		}
		if (elm.tagName.toUpperCase().indexOf('SCRIPT')>=0 ||
				elm.tagName.toUpperCase() == 'A') {
			return false;
		}
		
		if (elm.tagName.toUpperCase() == 'IMG') {
			return elm.width > 10 && elm.height > 10;
		}
		
		return true;
	});
	
	if (banner) {
		return true;
	}
	return false;
}


function checkAllBanners() {
	$$('.box_Banner').each(function (elm) {
		var hiddenParent = elm.hasClassName('bnr_Hidden') ? elm : $(elm).up('.bnr_Hidden');
		if (hiddenParent) {
			if (bannerPresent(elm)) {
				hiddenParent.removeClassName('bnr_Hidden');
			} else {
				hiddenParent.hide();
			}
		}
	});
	$$('.banner_dx1').each(function (elm) {
		var table = elm.getElementsByTagName('table')[0];
		var td = elm.getElementsByTagName('td')[0];
		
		if (!bannerPresent(td)) {
			elm.hide();
			return;
		}
		
		var width = table.scrollWidth;
		var height = table.scrollHeight;
		
		//alert(width + 'x' + height);
		
		var finalW = width < 305 ? 300 : 336;
		var finalH = height > 255 ? 600 : 250;
		
		$(table).up().addClassName('bnr_' + finalW + 'x' + finalH);
		elm.addClassName('box_Banner');
		
		if (finalH == 600) {
			elm.up().addClassName('mostViewed_Hidden');
		}
	});
}

Event.observe(window, 'load', checkAllBanners);

// cerca nel mediacenter
var cerca_mcn_init = false;
function cerca_mcn_onfocus () {
	if (!cerca_mcn_init) {
		$('form_SearchBar')['Search'].value = "";
		cerca_mcn_init = true;
	}
}
function cerca_mcn_submit() {
	var elm = $('form_SearchBar')['Search'];
	if (elm.value == '') {
		elm.focus();
		return;
	}
	document.location = "/ricercavideo/" + elm.value;
}

function bottoneCondividi() {
	var url = document.location.href;
	document.write('<a href="javascript:;" id="condividi_button" onclick="openBoxCondividi()" class="btn_Share" title="Condividi"><span class="box_Ico">Share</span></a>');
	document.write(
		'<div id="condividi_box" style="display:none;" class="box_Lightbox">' +
			'<a href="javascript:;" onclick="closeBoxCondividi()" class="btn_Close" title="Chiudi">Chiudi</a>' +
			'<ul class="list_Lightbox">' +
				'<li><a href="http://www.facebook.com/share.php?u=' + url + '" onclick="return fbs_click()" target="_blank" class="fb_share_link">Facebook</a></li>' +
				'<li><img src="/res/imgs/imgs_mcn/ico_wikio.gif" alt="Wikio" /><a href="http://www.wikio.it/vote" title="Condividi su Wikio">Wikio</a></li>' +
				'<li><img src="/res/imgs/imgs_mcn/ico_OKNO.png" alt="OKNOtizie" /><a href="http://oknotizie.alice.it/post?url=' + url + '" title="Condividi su OKNOtizie">OKNOtizie</a></li>' +
			'</ul>' +
			'<div class="clear">&nbsp;</div>' +
		'</div>'		
	);
}
function openBoxCondividi() {
	Effect.Appear('condividi_box', { duration: 0.6 });
}
function closeBoxCondividi() {
	Effect.Fade('condividi_box', { duration: 0.6 });
}

function closePrimoPiano() {
	var lis = $$('#box_PrimoPiano li');
	for (var i = 5; i < lis.length; i++) {
		$(lis[i]).hide();
	}
}

function setPremiumToken(obj) {
	var cookieName = "TSpremiumtoken";
	var regexp = new RegExp(cookieName + '=([^;]+)');
	var result = regexp.exec(document.cookie);
	
	if (result == null) {
		return;
	}
	obj.addVariable("token", result[1]);
}

function onLinearAdFinish() {
    $('vast_companion_container').hide();
    $('vast_companion_bottom').hide();
}

function getSWFPlayer(options) {
  
  FLVPlayer = (function(options) {

    function FLVPlayer(options) {
      this.wSize = "610";
    	this.hSize = "502";
    	this.data=options.file.substring(0,options.file.lastIndexOf("/"));
    	this.cdn_file=options.file.substring(options.file.lastIndexOf("/")+1);
    	this.zona=options.streamer.substring(options.streamer.lastIndexOf("/"));
    	this.cdn_streamer = "rtmp://video.tuttosport.com"+this.zona+"/streams/_definst_"+this.data;  
     	this.dartVTag = getVideoAd({format : '450x340',full:'y', 'tipo':'video',pos : options.pos});
     	this.logo = options.logo;
     	this.image = options.image;
    }

    FLVPlayer.prototype.write = function(target) {
      
      $(target).insert({after:
      '<div id="vast_companion_container" style="text-align:center;height:0px;width:100%;position:relative;top:-90px;clear:both">'+
      '	<div id="vast_companion_bottom" style="margin:0 auto;width:320px;height:60px"></div>'+
      '</div><div style="clear:both;height:20px;width:100%;"></div>'}
      );

      jwplayer(target).setup({
      	modes: [ {type:"flash", src:"/res/swf/player.swf"} ],
      	provider: "rtmp",
        streamer: this.cdn_streamer,
        file: this.cdn_file,
      	logo: this.logo,
      	image: this.image,
      	width: this.wSize,
      	height: this.hSize,
      	backcolor: '3B94CC',
      	frontcolor: 'f0f0f0',
      	'controlbar.position': 'bottom',
      	autostart: ( ((document.location.href.indexOf("?cdo") <= 0) && (document.location.href.indexOf("#invioCommenti") <= 0)) ? 'true':'false'),
      	allowscriptaccess:"always",
      	plugins: {
      		"/res/swf/ova.swf": {
            "debug": {
              "debugger": "firebug",
              "levels": "none"
            },
            "ads": {
              "notice" : {
                "show": true, 
                "message": "Il tuo video sta arrivando tra _countdown_ secondi",
                "region": "top-notice"         
              },
              "canFireApiCalls": true,
              "pauseOnClickThrough": true,
              "deliveryType": "progressive",
              "servers": [ { "type": "DART", "tag": this.dartVTag } ],
              "schedule": [ {
                  "position": "pre-roll"
                }, {
                  "position":"auto:bottom",
                  "startTime": "00:00:07",
                  "duration": "7"
                } ],
              "displayCompanions": true,
              "restoreCompanions": false,
              "companions": [ 
                { "id":"vast_companion_bottom", "width":"320", "height":"60" },
                { "id":"vast_companion_square", "width":"300", "height":"250" }
			  ],
              "additionalParamsForSWFCompanions": [ { "name": "wmode", "value": "transparent" } ]
            },
            "overlays": {
              "regions": [ {
                  "id": "top-notice",
                  "verticalAlign": "top",
                  "horizontalAlign": "left",
                  "backgroundColor": "transparent",
                  "opacity": 0.5,
                  "width": "400px",
                  "style": ".smalltext { font-style: italic; font-size:10; }"
                } ]
            }		
          }
      	}
      });      

    };//write

    return FLVPlayer;
    
  })();

  return new FLVPlayer(options);
}

function HighlightMenu(idCat) {
	var menuElm = $('menu_cat_' + idCat);
	if (menuElm) {
		menuElm.addClassName('li_Selected');
		var parentElm = menuElm.up('li', 0);
		for (var i = 1; parentElm; i++) {
			parentElm.addClassName('li_Selected');
			parentElm = menuElm.up('li', i);
		}
	}
}

