ytplayer = null;
menubasecolor = "#8B8B8D";
function onYouTubePlayerAPIReady() {
    alert("mjello!");
}


function youtubeId(orgurl){
    var re = new RegExp('.*(v=)([a-zA-Z0-9]*)');
    var result = re.exec(orgurl);
    if (result == null)
    {	
        return "Wrong URL";
    }
    return result[2];
}
youtubeactive = null;
$(document).ready(function() {
    // YOUTUBE BOX
    $(".playwrap").each(function(index){
        vidid = youtubeId($(this).attr("href"))
        /*    $(this).parent().attr("alt",
            '<iframe width="560" height="349" src="http://www.youtube.com/e/'+vidid+'?enablejsapi=1&version=3&playerapiid=ytplayer" frameborder="0" allowfullscreen></iframe>');*/
        $(this).parent().attr("alt",
            '<object id="ytplayer" width="560" height="349" data="http://www.youtube.com/v/'+vidid+'?color1=d6d6d6&amp;color2=f0f0f0&amp;border=0&amp;fs=1&amp;hl=en&amp;loop=0&amp;showinfo=0&amp;version=3&amp;enablejsapi=1&amp;iv_load_policy=3&amp;showsearch=0&amp;rel=0&amp;hd=1" type="application/x-shockwave-flash">'+'<param value="http://www.youtube.com/v/'+vidid+'?color1=d6d6d6&amp;color2=f0f0f0&amp;border=0&amp;fs=1&amp;hl=en&amp;loop=0&amp;showinfo=0&amp;version=3&amp;enablejsapi=1&amp;iv_load_policy=3&amp;showsearch=0&amp;rel=0&amp;hd=1" name="movie">'+
            '<param value="true" name="allowFullScreen">'+
            '<param value="always" name="allowScriptAccess">'+
            '<param value="transparent" name="wmode">'+
            '</object>');
        $(this).attr("href", "#youtube");
        $(this).parent().children(".snapshot").attr({
            "src":"http://img.youtube.com/vi/"+vidid+"/0.jpg",
            'attr':vidid
        });
    });
    $(".box").has(".playwrap").qtip({
        content: {
            text: $(this).attr("alt"),
            title: {
                text:"BPI Video:",
                button:"<img src='/style/button_close_01.png' style='height:20px' />"
            }
        },
        style:{
            width:570,
            height:349,
            name:'light',
            background:'#e32119',
            border:{
                width:3,
                radius:5,
                color:'#e32119'
            },
            padding:0,
            textAlign:'center',
            tip:true, 
            title : {
                background:'#e32119',
                color:"white",
                fontFamily : "Open Sans"
            }
        },
        position:{
            corner:{
                target: 'topLeft',
                tooltip: 'topRight'
            },
            adjust:{
                x:0,
                y:0,
                screen:true
            }
        },
        show:{
            when : {
                event : "click"
            },
            delay:20,
            effect:{
                type:'fade',
                length:250
            }
        },
        hide:{
            when : {
                event : "unfocus"
            },
            delay:100,
            effect:{
                type:'fade',
                length:100
            }
        },
        api : {
            beforeShow : function(e) {


            },
            beforeHide : function(e){

                document.getElementById("ytplayer").stopVideo();
            }
        }
    });

    // MENU ANIMATIONS
    $(document).ready(function() {
        if($("body").hasClass("front")){
            $(".box").has("a").each(function(){
                anchor = $(this).find("a");

                $(this).qtip({
                    content : {text:anchor.attr("title")},
                style:{
                    name:'light',
                    background:'#e32119',
                    border:{
                        width:3,
                        radius:5,
                        color:'#e30109'
                    },
                    padding:0,
                    textAlign:'center',
                    tip:true, 
                    color:"white",
                    fontFamily : "Open Sans",
                    fontSize : "11px"
                },
                position: {
                    corner: {
                        target: 'leftMiddle',
                        tooltip: 'rightMiddle'
                    }
                }
            });
            anchor.attr("title","");
                
            });
        }

    });
    $("#menuwrapper ul a").mouseenter(function(){
	
	if($("#active").length == 0){
		$("#menubg").fadeIn();
	}
        $("#menuwrapper ul a").clearQueue();
        $("#menubg").clearQueue();
        $("#menuwrapper ul a").not(this).not(".extramenu").animate({
            color:menubasecolor
        },300);
        $(this).animate({
            color :"#ffffff"
        },300);
			
        $("#menubg").animate( {
            width :$(this).width()+4
        },300);
        $("#menubg").animate({
            marginLeft : 22+$(this).position().left
        },300);
    });
    $("#menuwrapper ul a").mouseleave(function(){
        $("#menuwrapper a").clearQueue();
        $("#menuwrapper ul a").not("#active a").not(".extramenu").animate({
            color:menubasecolor
        },300);
        $(".extramenu").animate({
            color:"#E32119"
        },300);
	if($("#active").length != 0){
		$("#active a").animate({
		    color :"#ffffff"
		},300);
		$("#menubg").animate( {
		    width :$("#active a").width()+4
		},300);
		$("#menubg").animate({
		    marginLeft : 22+$("#active a").position().left
		},300);
	} else {
		$("#menubg").fadeOut();
	}


    });
    if($("body").hasClass("page")){
	if($("#active").length != 0){
		$("#active a").animate({
		    color :"#ffffff"
		},300);
		$("#menubg").css( {
		    width :$("#active a").width()+4
		},300);
		$("#menubg").css({
		    marginLeft : 22+$("#active a").position().left
		},300);
	} else {
		$("#menubg").fadeOut();
	}
    }
    // FRONTPAGE ANIMATIONS
    $(".front").find(".box.imgbox").animate({
        opacity:0.4
    },300);
    $(".textbox .br span h1").each(function(){
        $(this).css("margin-top", -($(this).height()/2));
    });
    $(".imgbox .br span h1").each(function(){
        $(this).css("margin-top", -($(this).height()/2));
    });
    $(".front").find(".box").mouseenter(function(){
        $(".front").find(".box.pair"+$(this).attr("attr")).each(function(i){
            $(this).clearQueue();
            if($(this).hasClass("textbox")){
                $(this).animate({
                    backgroundColor: "#e31818"
                }, 300);
                elh1 = $(this).find("h1");
                $(elh1).clearQueue();
                $(elh1).animate({
                    color: "#ffffff"
                }, 300);
            } else {
                $(this).animate({
                    opacity:1
                },300);
            } 
        });
    });
    $(".front").find(".box").mouseleave(function(){
        $(".front").find(".box.pair"+$(this).attr("attr")).each(function(i){
            $(this).clearQueue();
            if($(this).hasClass("textbox")){
                $(this).animate({
                    backgroundColor: "#dfe0e1"
                }, 300);
                elh1 = $(this).find("h1");
                $(elh1).clearQueue();
                $(elh1).animate({
                    color: "#aaaaac"
                }, 300);
            } else {
                $(this).animate({
                    opacity:0.4
                },300);
            } 
        });
    });
    if(window.location.hash) {
        hashel = $(window.location.hash).offset();
        if(hashel)
            $('html,body').scrollTop(hashel.top-250); 
    }
});
function scrolltoelement(el){


    $(el).click(function(e){
        e.preventDefault();
            if($.browser.msie && $.browser.version == "7.0") {

                $('html,body').animate({scrollTop: $($(this).attr("attr")).offset().top-250},'slow');    
            } else {
                $('html,body').animate({scrollTop: $($(this).attr("href")).offset().top-250},'slow');
            }
        });
}

