$(document).ready(function() {
	var visitorStr = getQueryVariable("visitor");
	if (visitorStr == "presse") {
		$("#form866 div input").each(function(i) {
			$(this).val("Test");
		});
		$("#form866").submit();
	}

	$('a.iframe').iframe();

	$("#deFrame").attr("href","javascript:void(0);").click(function() {
		$("#deIFrame").toggle();
	});
	
	$("#siteSelect > a").hover(function(){
	  $("#siteDiv").addClass("sitediv-" + $(this).parent().attr("class"));
      $("#siteDiv").show();
    },function(){
      $("#siteDiv").hide();
    });

	$("#siteDiv").hover(function(){
      $("#siteDiv").show();
	  $("#siteSelect > a").addClass("selected");
    },function(){
      $("#siteDiv").hide();
	  $("#siteSelect > a").removeClass();
    });
	
	$("ul.collapseText > li > a").attr('href','javascript:void(0);').click(function() {
		var selItem = $(this).parent();
		selItem.children("div").toggle();
		if ($(this).attr("class") == "selected") {
			$(this).removeClass("selected");
		} else {
			$(this).addClass("selected");
		}
	});
	
	$("p.visibleText > a").parent().parent().children("ul").hide();
	
	$("p.visibleText > a").attr('href','javascript:void(0);').click(function() {
		var selItem = $(this).parent().parent();
		selItem.children("ul").toggle();
		if ($(this).attr("class") == "selected") {
			$(this).removeClass("selected");
		} else {
			$(this).addClass("selected");
		}
	});
	
	
	buildVideos();
});

/* Main SWF/FLV Creation */
function buildVideos() {
	$("#rightColumnContainer > div").find("a").each(function(i) {
		if (($(this).attr("href").indexOf("flashtype=swf") >= 0)) {
			buildSWF($(this));
		}
		
		if (($(this).attr("href").indexOf("flashtype=flv") >= 0)) {
			buildFLV($(this));
		}
	});
	
	$(".contentArea").find("a").each(function(i) {
		if (($(this).attr("href").indexOf("flashtype=swf") >= 0)) {
			buildSWF($(this));
		}
		
		if (($(this).attr("href").indexOf("flashtype=flv") >= 0)) {
			buildFLV($(this));
		}
	});
}

/* SWF/FLV Helper Functions */
function getWidthHeight(theHref) {
	var infoArray = new Array(3);
	var urlArray = new Array();
	var sHeight = 0;
	var sWidth = 0;

	urlArray = theHref.split('?');
	var strURL = urlArray[0];
	var strQuery = urlArray[1];

	if(typeof(strQuery) != 'undefined') {
		var queryArray = new Array();
		queryArray = strQuery.split("&");
		
		var typeArray = new Array();
		typeArray = queryArray[0].split("=");
		linkType = typeArray[1];
		
		var widthArray = new Array();
		widthArray = queryArray[1].split("=");
		sWidth = widthArray[1];
		
		var heightArray = new Array();
		heightArray = queryArray[2].split("=");
		sHeight = heightArray[1];
	}

	var sHeightF = parseInt(sHeight) + 60;
	var sWidthF = parseInt(sWidth) + 30;
	
	infoArray[0] = strURL;
	infoArray[1] = sHeightF;
	infoArray[2] = sWidthF;
	infoArray[3] = sHeight;
	infoArray[4] = sWidth;
	
	return infoArray;
}
function buildSWF(theHref) {
	var infoArray = getWidthHeight(theHref.attr("href"));
			
	theHref.attr("href","/flash/dynamic-flash-player-overlay.asp?FileId="+ infoArray[0] +"&amp;FileType=swf&amp;FileWidth="+ infoArray[4] +"&amp;FileHeight="+ infoArray[3] +"&amp;TB_iframe=true&amp;height="+ infoArray[1] +"&amp;width="+ infoArray[2] +"&amp;modal=true");
	theHref.attr("onclick","");

	theHref.addClass("thickbox");
}
function buildFLV(theHref) {
	var infoArray = getWidthHeight(theHref.attr("href"));

	theHref.attr("href","/flash/dynamic-flash-player-overlay.asp?FileId="+ infoArray[0] +"&amp;FileType=flv&amp;FileWidth=512&amp;FileHeight=288&amp;TB_iframe=true&amp;height=351&amp;width=504&amp;modal=true");
	theHref.attr("onclick","");

	theHref.addClass("thickbox");
}

/**
 *  JQuery iFrame plugin for converting a link into an iframe.
 *  @author:  Vlada Misic / http://www.lucidcrew.com
 *  @version: 1.0
 *  http://33rockers.com/2006/12/05/unobtrusive-iframe-with-jquery
 *
 *  Thanks to M. Alsup http://www.malsup.com for his SWF plugin that was the basis for this code
 *
 *  This plugin converts anchor tags into iframes.
*/
 
 jQuery.fn.iframe = function(options) {
    return this.each(function() {
        var $this = jQuery(this);
        var cls = this.className;
        
        var opts = jQuery.extend({
            frameborder:  ((cls.match(/fb:(\d+)/)||[])[1]) || 0,
            marginwidth:  ((cls.match(/wm:(\d+)/)||[])[1]) || 0,
            marginheight: ((cls.match(/hm:(\d+)/)||[])[1]) || 0,
            width:        ((cls.match(/w:(\d+)/)||[])[1]) || 640,
            height:       ((cls.match(/h:(\d+)/)||[])[1]) || 480,
            scrolling:    ((cls.match(/sc:(\w+)/)||[])[1]) || "auto",
            version:     '1,0,0,0',
            cls:          cls,
            src:          $this.attr('href') || $this.attr('src'),
			id:			  $this.attr('id'),
            caption:      $this.text(),
            attrs:        {},
            elementType:  'div',
            xhtml:        true
        }, options || {});
        
        var endTag = opts.xhtml ? ' />' : '>';

        var a = ['<iframe src="' + opts.src + '"'];
		if(opts.id){
			a.push(' id="' + opts.id + '"');
		}else{
			a.push(' id="content_iframe"');
		}
		a.push(' frameborder="' + opts.frameborder + '"');
		a.push(' marginwidth="' + opts.marginwidth + '"');
		a.push(' marginheight="' + opts.marginheight + '"');
		a.push(' width="' + opts.width + '"');
		a.push(' height="' + opts.height + '"');
		a.push(' scrolling="' + opts.scrolling + '"');
		a.push(endTag);
        
        // convert anchor to span/div/whatever...
        var $el = jQuery('<' + opts.elementType + ' class="' + opts.cls + '"></' + opts.elementType + '>');
        $el.html(a.join(''));

        $this.after($el).remove();
    });
};

function closeDeFrame() {
	$("#deIFrame").hide();
}

function getQueryVariable(variable) {
	var query = window.location.search.substring(1);
	var vars = query.split("&");
	var resultStr = "";
	for (var i=0;i<vars.length;i++) {
		var pair = vars[i].split("=");
		if (pair[0] == variable) {
			resultStr = pair[1];
		}
	}
	return resultStr;
}