
var Alfa = function () {
	var $ = jQuery;
    
	return {
		init: function() {
			var instance = this;

			instance.dropDownMenu();
			instance.addClearFloats();
			instance.addMediaPlayer();
            instance.loadOpenSearch();
            instance.handleOneByOne();
		},

        handleOneByOne: function() {
        
            $("#onebyonenavigation").each( function(i) {

                $("div.article_newsticker").each( function(i) {
                
                    $(this).css("display", "none");
                });
                // $("#article_newsticker_1").css("display", "block");
                oboAction(0);
            });
        },
        
        loadOpenSearch: function() {

            $.each( $("div.opensearchbox"), function(i,box) {

                var instance = $(box);
                var ostype = instance.attr("ostype");
                var osurl = instance.attr("osurl");
                var osfullurl = instance.attr("osproxy")+"?"+osurl;
                var prefix = "<div class='searchresultfor'>Suchergebnisse auf <a class='searchblau' href='" + osurl + "'> " + instance.attr("osname") + "</a></div>";
                switch(parseInt(ostype)) {

                    case 1: //text/html
                        instance.html(prefix+"<iframe class='opensearchbox' src='" + osurl + "'></iframe>");
                        break;
                    case 2: //application/atom+xml
                        $.get(osfullurl,function(data) {

                            instance.html(prefix+data);
                        })
                        break;
                    case 3: //application/rss+xml
                        $.get(osfullurl,function(data) {

                            instance.html(prefix+data);
                        })
                        break;
                }
            })
        },

		dropDownMenu: function() {
			$(".bsf_navigation_level2_container").hoverIntent(
				{
					interval: 50,
					timeout: 100,
					over: function () {
						var instance = $(this);
						var child = $('div.bsf_navigation_level3', this);

						instance.addClass("bsf_navigation_level2_container_selected");
						child.slideDown(100);
					},
					out: function () {
						var instance = $(this);
						var child = $('div.bsf_navigation_level3', this);

						child.slideUp(50);
						instance.removeClass("bsf_navigation_level2_container_selected");
					}
				}
			);
		},

        addClearFloats: function() {
            // $("a.mediaobject_link").append( "<div class='clearfloats'></div>" );
        },
        
		addMediaPlayer: function() {
            $("a.mediaplayer").click( function() {

                var container = $("span." + this.id);
                var type = container.attr( "type" );
                var src = container.attr( "src" );
                var url = src.replace( /.tn=SMALL/, "" );
                var url = url.replace( /.tn=MEDIUM/, "" );
                var url = url.replace( /.tn=FILE/, "" );

                var exts = src.split( "." );
                var extl = exts.length;
                var ext = "";
                if( extl > 0 ) ext = exts[extl -1];
                if( url.search( "http://[^.]*\.youtube.com/" ) != -1 ) ext = "youtube";

                var instance = container.children( "img" );
                if( instance.length == 0 ) return;
                
                var classNames = instance.attr( "class" ).replace( "mediaobject_logo", "" );
                var width = instance.width();
                var height = instance.height();
                
                var player = "";

                switch( type ) {
                    case "audio":
                        height = 21;
                        switch( ext ) {
                            case "caf":
                                url = unescape( url );
                                player = "<embed class='" + classNames + "' type='video/quicktime' pluginspace='http://www.apple.com/quicktime/download/' src='" + url + "' height='" + height + "' width='" + width + "' autoplay='true' controller='true' />";
                                break;
                                
                            default:
                                url = escape( url );
                                player = "<object class='" + classNames + "' width='" + width + "' height='" + height + "'>";
                                player += "<param name='movie' value='/portalsuite/images/flash/mediaplayer.swf?file=" + url + "&width=" + width + "&height=" + height + "&overstretch=true&showdigits=total&autostart=true'></param>";
                                player += "<embed class='" + classNames + "'src='/portalsuite/images/flash/mediaplayer.swf?file=" + url + "&width=" + width + "&height=" + height + "&overstretch=false&showdigits=total&autostart=true' type='application/x-shockwave-flash' wmode='transparent' width='" + width + "' height='" + height + "'></embed>";
                                player += "</object>";
                                break;
                        }
                        break;
                        
                    case "video":
                        switch( ext ) {
                            case "wmv":
                                player = "<embed class='" + classNames + "' type='application/x-mplayer2' pluginspage='http://microsoft.com/windows/mediaplayer/en/download/' displaysize='4' autosize='0' bgcolor='darkblue' showcontrols='1' showtracker='0' showdisplay='0' showstatusbar='0' autosize='1' displaysize='0' videoborder3d='0' width='" + width + "' height='" + (height +10) + "' src='" + url + "' autostart='1' loop='0'></embed>";
                                break;
                                
                            case "mov":
                                player = "<embed class='" + classNames + "' type='video/quicktime' pluginspace='http://www.apple.com/quicktime/download/' src='" + url + "' height='" + height + "' width='" + width + "' autoplay='true' controller='true' />";
                                break;

                            case "youtube":
                                player = "<embed class='" + classNames + "' type='application/x-shockwave-flash' src='" + url + "&amp;hl=en&amp;fs=1&amp;autoplay=1' width='" + width + "' height='" + height + "' allowfullscreen='true'>";
                                break;
                                
                            default:
                                url = escape( url );
                                player = "<object class='" + classNames + "' width='" + width + "' height='" + height + "'>";
                                player += "<param name='movie' value='/portalsuite/images/flash/mediaplayer.swf?file=" + url + "&width=" + width + "&height=" + height + "&overstretch=true&showdigits=total&autostart=true'></param>";
                                player += "<embed class='" + classNames + "' type='application/x-shockwave-flash' src='/portalsuite/images/flash/mediaplayer.swf?file=" + url + "&width=" + width + "&height=" + height + "&overstretch=false&showdigits=total&autostart=true' wmode='transparent' width='" + width + "' height='" + height + "'></embed>";
                                player += "</object>";
                                break;
                        }
                        break;
                }
                container.html( player );
            });
		}
	};
}();


jQuery(document).ready(

	/*
	This function gets loaded when all the HTML, not including the portlets, is
	loaded.
	*/

    function() {

        // if( themeDisplay.isSignedIn()) document.getElementById( "body" ).className = "signedIn";
        Alfa.init();
    }
);

Liferay.Portlet.ready(

	/*
	This function gets loaded after each and every portlet on the page.

	portletId: the current portlet's id
	jQueryObj: the jQuery wrapped object of the current portlet
	*/

		function(portletId, jQueryObj) {
			//alert(portletId);
			if ( portletId == 'DetailedStory_WAR_portalsuite' && findElementTagByAttrib (document, 'div', 'class', 'article_mainmedia') != null && document.getElementById("galerie_box") == null )
			{
				createPixBox();
			}
		}
);

jQuery(document).last(

	/*
	This function gets loaded when everything, including the portlets, is on
	the page.
	*/

	function() {
	}
);
function printArticle( printArticleUrl, width, height )
{
        if ( window.name == "Printstory" )
        {
                window.print();
        }
        else
        {
                theUrl = document.URL;
                Ergebnis = theUrl.match(/^http:\/\/[^\/]*/);
                if ( Ergebnis[0] )
                {
                        theUrl = Ergebnis[0] + printArticleUrl ;
                }
                mywindow = window.open(theUrl, "Printstory", "width=780, height=600, left=100, top=200, scrollbars=yes");
                //alert(mywindow.name);
        }
};

function oboAction(offset) {

    var oldact = obo_actno;
    obo_actno = obo_actno + offset;
    
    jQuery("span.obo_info").html( obo_actno + " von " + obo_maxno );
    jQuery("#article_newsticker_" + oldact).css("display", "none");
    jQuery("#article_newsticker_" + obo_actno).css("display", "block");
    
    if (obo_actno == 1) jQuery("a.obo_back").css("visibility", "hidden");
    else jQuery("a.obo_back").css("visibility", "visible");

    if (obo_actno == obo_maxno) jQuery("a.obo_forward").css("visibility", "hidden");
    else jQuery("a.obo_forward").css("visibility", "visible");
}

function changeSurveyLocaleBox(me) {

    me = jQuery(me);
    var locale = me.attr("name");
    jQuery("div.survey_edit_locale").each( function(i) {
    
        var instance = jQuery(this);
        instance.removeClass("multistories_tabsection_entryselect");
    });
    jQuery("#locale_" + locale).addClass("multistories_tabsection_entryselect");
    
    jQuery("a.multistories_tabsection_entry").each( function(i) {
    
        var instance = jQuery(this);
        instance.removeClass("multistories_tabsection_entryselect");
        instance.parent("li").removeClass("multistories_tabsection_entryselect");
    });
    me.addClass("multistories_tabsection_entryselect");
    me.parent("li").addClass("multistories_tabsection_entryselect");
}

function checkTheVal(){
	var cansubmitt = true
	for ( i = 0; i <= document.form.length; i++){
			var el = document.getElementsByTagName('input')[i];
			if ( el ){
				if ( el.getAttribute('name' )!= null && el.getAttribute('name' ).substring(0, 6 )== 'needed' ){
					if (el.value == null || el.value == "" )
					{
						el.style.border = ""
						cansubmitt = false
						//var err = document.createAttribute("style");
						//err.nodeValue = "background-color:red";
						//el.setAttributeNode(err);
						//el.style.backgroundColor = "red";
						el.style.border = "1px solid red"
					}
					else{
						el.style.border = ""
						el.style.border = "1px solid"
						el.style.borderColor =  "#BFBFBF #DEDEDE #DEDEDE #BFBFBF"
					}
				}
			}
	}
	if ( cansubmitt == true )
	{
		document.form.submit();
	}
	else{
		document.getElementById('errortext').style.display = "block";
	}
}

// function for resizing images and imageboxes in detailedimage create a dummy function to get rid of it. 
function resizePicture (item) {
	var maxsize = 180; // defines the max size of box and image.
	//var jetzt = new Date();
  	//var Zeit = jetzt.getTime();
	//alert("-- " + Zeit +" -- " + Zeit2);
	var theDiv = document.getElementById("resizeBox");
	if ( theDiv == null ){
	}
	else{
		var myimg = theDiv.getElementsByTagName("img")[0];
		var height = myimg.height;
		var width = myimg.width;
		if ( width == 0){
		}
		else if(width >= height) {
			myimg.height = Math.round(maxsize*height/width);
			myimg.width = maxsize;
			var mybr = myimg.width + 'px';
			theDiv.style.width = mybr;
		} else if(height > width ) {
			myimg.height = maxsize;
			myimg.width = Math.round(maxsize/height*width);      
			var mybr = myimg.width + 'px';
			theDiv.style.width = mybr;
		} 
	}
}
function findElementTagByAttrib (el, tag, attrName, attrValue) {
	try{
		var i = 0;
		var theEl = el.getElementsByTagName(tag)[i];
		while ( theEl != null){
			var theEl = el.getElementsByTagName(tag)[i];
			if ( theEl.getAttributeNode(attrName)!= null && ( theEl.getAttributeNode(attrName).value.match(attrValue + ".*") )){
				return 	theEl;
			}
			i++;
			theEl = el.getElementsByTagName(tag)[i];
		}
		return null;	
	}catch (e){
		return null;	
	}	
}

function setattribute (el, attr, val){
	if ( el != null ){
		if (window.navigator.userAgent.indexOf("MSIE ") > -1 && window.navigator.userAgent.substr(window.navigator.userAgent.indexOf("MSIE ") + 5, 1) < 8 ){
			switch (attr){
				case "onmouseover":
					el.onmouseover = function() { eval(val) };
					break;
				case "onmouseout":
					el.onmouseout = function() { eval(val) };
					break;
				case "onclick":
					el.onclick = function() { eval(val) };
					break;
				case "class":
					el.className = val;
					break;
				case "style":
					if ( val.match(/=/) ){
						el.style = val;
					}else{
						el.style.cssText = val;
					}
					break;
				default:
					el.setAttribute( attr, val );
			 }
		} else{
			el.setAttribute( attr, val )
		}
	}
}

function createPixBox(){
	theEl = document.getElementById('_DetailedStory_WAR_portalsuite_content');
	theMoEl = findElementTagByAttrib (theEl, 'div', 'class', 'article_mainmedia');
	
	theMl = findElementTagByAttrib (theMoEl, 'a', 'class', 'mediaobject_link');
	href = theEl.getElementsByTagName("img")[0].getAttribute("src").replace('SMALL', 'MEDIUM');
	callJs = " createSecondPixBox(theMoEl, theMl );divlayer('Bilder', href);return false;";
	setattribute (theMl, 'onclick', callJs)

}

function createSecondPixBox(theMoEl, theMl ){
	theTi = findElementTagByAttrib (theMoEl, 'div', 'class', 'mediaobject_title');
	//theEl = findElementTagByAttrib (theTi, 'a', 'class', 'mediaobject_link');
	setattribute (theMl, 'onclick', callJs);
	theTitle = theTi.getElementsByTagName('a')[0];
	theTitleText = theTitle.firstChild.nodeValue;
	
	var gal = document.createElement("div");
	setattribute (gal, 'id', 'galerie_box');
	setattribute (gal, 'class', 'galerie_box');
	var img = document.createElement("img");
	setattribute (img, 'id', 'galerie_0');
	setattribute (img, 'class', 'gallery_img');
	setattribute (img, 'picbox', 'Bilder');
	setattribute (img, 'src', href);
	setattribute (img, 'gross', href);
	setattribute (img, 'style', 'display:none;');
	setattribute (img, 'title', theTitleText);
	gal.appendChild(img);
	theMoEl.appendChild(gal);
}
