$(document).ready(function(){
	
	var popupbox;
	var popupboxClon;
	var popupboxParent;

	var popUpLinks = $('a.poplight[href^=#]');
	$.each(popUpLinks, function(){
		var popData = $(this).attr('rel').split('|'); //Get Popup Name
		var popID = popData[0];

		popupbox = $('#' + popID);
	   popupboxClon = popupbox.clone();
	   popupbox.remove();
	   $('#bgcontainer').append(popupboxClon);
	});
	

    //When you click on a link with class of poplight and the href starts with a # 
    $('a.poplight[href^=#]').click(function() {
        var popData = $(this).attr('rel').split('|'); //Get Popup Name

        //var popID = $(this).attr('rel'); //Get Popup Name
        var popID = popData[0];
        var popDIR= popData[1]; //si foto -> url;   si video -> id_youtube;  si mapa -> null
        var popIDX= popData[2];  //si mapa -> null
        var popTYP= popData[3];
        var popURL = $(this).attr('href'); //Get Popup href to define size

        switch (popTYP){
            case "PHOTO":
                $('#'+popID+'_img').attr('src',popDIR);
                $('#'+popID+'_text').text($('#poptext_'+popIDX).text());
                break;
            case "SILUETA":
                var popNombre = popData[4];
                $('#'+popID+'_img').attr('src',popDIR);
                $('#'+popID+'_text').text($('#poptext_'+popIDX).text());
                $('#'+popID+'_nombre').html( popNombre+"<span>Nos cuenta su historia con Naranja</span>" );
                break;

            case "VIDEO":
                /*
                jwplayer("mediaplayer").setup({
                autostart:true,
                controlbar:"bottom",
                flashplayer:"../tools/mediaplayer-5.6/player.swf",
                allowscriptaccess:'always',
                allowfullscreen:true,
                width:$("#"+popID).width(),
                height:$("#"+popID).height() - 80});

                jwplayer('mediaplayer').load('http://tarjetanaranja.clientes.sg2.com.ar:8888/'+popDIR);
                //jwplayer('mediaplayer').resize( $("#"+popID).width(), $("#"+popID).height() - 80 );
                */ 
                $("#"+popID+"_iframe").attr("src","http://www.youtube.com/embed/"+popDIR+"?hl=es&fs=1"); 
                $("#"+popID+"_iframe").attr("height",$("#"+popID).height() - 80 ); 
                $("#"+popID+"_iframe").attr("width",$("#"+popID).width() ); 
                $('#'+popID+'_text').text($('#poptext_'+popIDX).text());
                break;
        }

        //Pull Query & Variables from href URL
        var query= popURL.split('?');
        var dim= query[1].split('&');
        var popWidth = dim[0].split('=')[1]; //Gets the first query string value
        var popHeight= (!dim[1])?0:dim[1].split('=')[1]; //Gets the first query string value

        //Fade in the Popup and add close button
        if (popHeight > 0)
             $('#' + popID).fadeIn().css({ 'width': Number( popWidth ),'height': Number( popHeight ) }).prepend('<a class="close close-lbox">cerrar</a>');
        else $('#' + popID).fadeIn().css({ 'width': Number( popWidth ) }).prepend('<a class="close close-lbox">cerrar</a>');
        //$('#'+popID).fadeIn().css({ 'width': Number( popWidth ) }).prepend('<a href="#" class="close"><img src="../images/icon_close.png" class="btn_close" title="Cerrar ventana" alt="Cerrar ventana" /></a>');
        //Define margin for center alignment (vertical + horizontal) - we add 80 to the height/width to accomodate for the padding + border width defined in the css

        if (document.getElementById('mcs_container')){
           $("#mcs_container").mCustomScrollbar("vertical",300,"easeOutCirc",1.05,"auto","yes","yes",15);
        }

        var popMargTop = ($('#' + popID).height() + 250) / 2;
        var popMargLeft = ($('#' + popID).width() + 80) / 2; 

        //Apply Margin to Popup

        $('#' + popID).css({ 
            'margin-top' : -popMargTop,
            'margin-left' : -popMargLeft
        });

		  popupbox = $('#' + popID).find('.popupbox');

        if (popTYP != "MAP"){
        }
        if (popHeight > 0){
		     slidescr = popupbox.find('#slide-scroll');
           popupbox.css({ 'width': Number( popWidth ),'height':Number(popHeight) });
           slidescr.css({ 'width': Number( popWidth ),'height':Number(popHeight) });
        }

		  popupboxClon = popupbox.clone();
		  popupboxParent = $('#' + popID);

        //Fade in Background
//	       popupboxParent.parents().css('z-index','1000');
		  popupboxParent.parents().addClass('superZindex');
        $('#' + popID).parent().append('<div id="fade"></div>'); //Add the fade layer to bottom of the body tag.
        $('#fade').fadeIn(); //Fade in the fade layer 
        return false;
    });

    //Close Popups and Fade Layer
    $('a.close, #fade').live('click', function() { //When clicking on the close or fade layer...
        $('#fade , .popup_block').fadeOut(function() {
            $('#fade, a.close').remove();  
            $("#popup1_iframe").attr("src",""); 
            popupbox.remove();
//			popupboxParent.parents().css('z-index','1');

			popupboxParent.parents().removeClass('superZindex');
			popupboxParent.append(popupboxClon);
        }); //fade them both out
		//document.getElementById('myytplayer').playVideo()
//		document.getElementById('myytplayer').stopVideo()
		
		
        return false;
    });
});

