/* MIGRATING v3.0 URLs */
if (location.href.match(/\.ro(\:\d+)?\/[^#]*#\/(.*)/)) {
    location.href = location.href.match(/\.ro(\:\d+)?\/[^#]*#(.*)/)[2];
}

// Ajaxifying forms
$.fn.ajaxify = function () {
    var $form = this;
    var $div = this.closest("div");
    $form.ajaxForm({
    	target: $div,
    	success: function() { 
            $div.fadeIn('slow');
            $("form.ajaxified").ajaxify(); 
            }
    });
}


/* GALLERY */
function showGallery(href, initialImageId, fullSizeVideo) {
    var $popup = $('.gallery-popup');
    if ($popup.size() < 1) $popup = $('<div class="gallery-popup"></div>');
    
    $('body').append($popup);
    $popup
        .css({
            position: 'absolute', 
            top: 0,
            left: ($(document).width() - 950) / 2,
            width: 950,
            height: 700
        })
        .expose({ api: true, color: '#DDDDDD', opacity: 0.9 })
            .onClose(function () { $popup.remove(); })
            .load()
    ;
    
    var swfoGallery = new SWFObject(RESOURCES_PATH + '/flash/photogallery/photogallery.swf', 'photogallery', '950', '100%', '8', '#ffffff'); 
    swfoGallery.addParam("wmode", "transparent");
    swfoGallery.addParam("salign", "tl");
    swfoGallery.addParam("scale", "noScale");
    swfoGallery.addParam("base", RESOURCES_PATH + '/flash/photogallery/');
    swfoGallery.addParam("allowScriptAccess", "sameDomain"); 
    swfoGallery.addVariable("href", href);
    if (initialImageId != undefined) swfoGallery.addVariable("initialImageId", initialImageId); 
    if (fullSizeVideo != undefined) swfoGallery.addVariable("fullSize", fullSizeVideo); 
    swfoGallery.write($popup[0]); 
}
function doHideGallery() {
    var $popup = $('.gallery-popup');
    if ($popup.size() < 1) return;
    $popup.expose().close();
}

/* OVERLAY */
function showOverlay(href) {
    var $popup = $('.overlay-popup');
    if ($popup.size() < 1) $popup = $('<div class="overlay-popup"></div>');
    
    $('body').append($popup);
    $popup
        .css({
            position: 'absolute', 
            top: 0,
            left: ($('body').width() -760 -60) / 2,
            width: 760
        })
        .expose({ api: true, color: '#DDDDDD', opacity: 0.9 })
            .onClose(function () { $popup.remove(); })
            .load()
    ;
    
    $popup
        .append(
            $('<img src="'+RESOURCES_PATH+'/images/close.jpg" alt="închide" />')
                .click(function () { $popup.expose().close() })
                .css({ float: 'right', cursor: 'pointer' })
        )
    ;

    $.get(href, function (data, textStatus) {
        $popup.append(data);
        $popup.expose().fit();
    });
}
function doHideOverlay() {
    var $popup = $('.overlay-popup');
    if ($popup.size() < 1) return;
    $popup.expose().close();
}

$(function () {

    /* Ajaxified */
    $('.ajaxified').ajaxify();

	$('a.gallery').click(function() {
		showGallery($(this).attr("href"));
		return false;
	});
    /* THERE IS NOT target="_blank" */
    $('a.open-new-window').click(function (e) {
        e.stopPropagation();
        window.open(this.href, '_blank');
        return false;
    });
    
    /* FLASH MOVIE CONTAINER */
    $('.flash_movie_container').each(function () {
        var $c = $(this);
        var src = $c.attr('title');
        var swfid = src.replace(/[\/\.]/g, '-').replace(/^-+|-+$/g, '');

        $c.removeAttr('title');
        
        var swfo = new SWFObject(src, swfid, '530', '270', '8', '#ffffff'); 
        swfo.addParam("wmode", "transparent"); 
        swfo.addParam("allowScriptAccess", "always"); 
        setTimeout(function () { swfo.write($c[0]);  }, 33);
    });
    
    /* DATE PICKER */
    if ($.datepicker) {
        var datepickerOptions = $.extend({}, $.datepicker.regional["ro"], {
            showOn: "both", 
            duration: "", 
            showStatus: true, 
            dateFormat: "dd.mm.yy", 
            buttonImage: "/images/calendar.png", 
            buttonImageOnly: true
        }), 
        datepickerImageStyle = { position: "relative", margin: "-2px -19px 0 0", top: 0, left: "-17px"  }; 
        $('input[type=text].date').datepicker(datepickerOptions);
        $('input[type=text].date').find("+ img").css(datepickerImageStyle);
    }
});
    
$(window).load(function () {
    /* FOOTER */
    $('#footer .footer-entry').each(function () {
        var $footer = $(this);
        var $a = $footer.find('a');
        var $img = $footer.find('img');
        
        // Is it a SWF?
        if ($img.attr('src').match(/\.swf$/) != null) {
            var swfid = $img.attr('src').replace(/[\/\.]/g, '-').replace(/^-+|-+$/g, '');
            
            var $swfc = $('<div />').css({ position: 'absolute', width: '100%' });
            $footer.append($swfc);
            $a.remove();
            
            var swfo = new SWFObject($img.attr('src'), swfid, '100%', '100%', '8', '#ffffff'); 
            swfo.addParam("wmode", "transparent"); 
            swfo.addParam("allowScriptAccess", "always"); 
            swfo.addVariable("linkTo", $a.attr('href')); 
            setTimeout(function () { swfo.write($swfc[0]); }, 33);
            
            if ($footer.hasClass('expandToFull')) {
                $footer.css({ position: 'relative' });
                var footerFlashLeft = null;
                var footerFlashWidth = null;
                $footer.hover(
                    function () {
                        if (footerFlashWidth == null) footerFlashWidth = $footer.width(); 
                        if (footerFlashLeft == null) footerFlashLeft = -($footer.offset().left - $('#footer').offset().left); 
                        $('#footer object').hide();
                        $swfc.stop(true, true).show().find('object').show(); 
                        $swfc.css({ left: footerFlashLeft, width: 950, zIndex: 5 }); 
                        $('#'+swfid).get(0).playForward(); 
                    }, 
                    function () {
                        $('#footer object').show(); 
                        $swfc.animate({ opacity: 1 }, {
                            duration: 1000, 
                            queue: false, 
                            complete: function () { 
                                $swfc.css({ left: 0, width: footerFlashWidth, zIndex: 1 }); 
                            }
                        });
                        $('#'+swfid).get(0).playBackward(); 
                    }
                );
            }
        }
        
    });
});

