// Pseudo global space
var zml = {
    /*
     * IE6 tab fix
     *
     * Redraws tabs to prevent visual artifacts on tabs (IE6)
     *
     * @param   object  current element (usually currently clicked <li>)
     * @return  void
     */
    
    ieTabFix: function(el) {
        var normalBgr = '/content/zml/english/images/tab.png';
        var activeBgr = '/content/zml/english/images/tab-a.png';
        
        var customBgr = el.parent().parent().find('span.custom-bgr').attr('title');
        if(customBgr) {
            bgrs = customBgr.split('|');
            normalBgr = bgrs[0];
            activeBgr = bgrs[1];
        }

        el.parent().find('li').each(function(){
            var curBgr = $(this).hasClass('active') ? activeBgr : normalBgr;
            $(this).css('background-image', 'url(\'' + curBgr + '\')');
            $(this).find('.wrapper').css('background-image', 'url(\'' + curBgr + '\')');
        });
    },
    
	changeSearchType: function(sType, sFormId) {
		var $f = $(sFormId), a = '';
		switch(sType) {
			case 'movies': a = '/search/advanced/'; break;
			case 'series': a = '/tvshows/search/advanced/'; break;
		}
		$f.attr('action', a);
	},
	jquery_FixDollarBrowser: function() {
		var userAgent = navigator.userAgent.toLowerCase();
		var ieVersion = -1;
		if (navigator.appName == 'Microsoft Internet Explorer') {
			var re = new RegExp("msie ([0-9]{1,}[\.0-9]{0,})");
			if (re.exec(userAgent) != null) {
				ieVersion = parseFloat(RegExp.$1);
			}
		}
		return {
			version: (ieVersion>-1)?ieVersion:(userAgent.match( /.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/ ) || [0,'0'])[1],
			safari: /webkit/.test( userAgent ),
			opera: /opera/.test( userAgent ),
			msie: /msie/.test( userAgent ) && !/opera/.test( userAgent ),
			mozilla: /mozilla/.test( userAgent ) && !/(compatible|webkit)/.test( userAgent )
		};
	}
};

function ie6pngFix(arr) {
    for (var i = 0; i < arr.length; i++) {
        img = document.getElementById(arr[i]);        
        pngFix(img, img.src);
    }
}

function pngFix(obj, src) {    
    if (document.all){        
        obj.parentNode.style.width = obj.offsetWidth;
        obj.parentNode.style.height = obj.offsetHeight;                        
        obj.parentNode.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, sizingMethod=crop src='" + src + "')";            
        obj.parentNode.style.margin = '22px 0 0 23px';
    } else {
        obj.style.visibility = 'visible';
    }
}

$(document).ready(function(){
	$.browser = zml.jquery_FixDollarBrowser();

    // Tabs png fix for IE6
    if($.browser.msie && $.browser.version <= 6) {
        DD_belatedPNG.fix('.tabs li');
        DD_belatedPNG.fix('.tabs .wrapper');
        $('#promo img').each(function() {                    
            src = this.src;
            this.src = '/content/zml/english/images/blank.gif';
            pngFix(this, src);
        });
    }
    // Click on container (event pool)
    $('#container').click(function(ev){
        if(ev.button == 0) $(this).trigger('SPLASH_CLICKED');
    });

    $('#promo img').css('visibility', 'visible');
    
    // Tabs: default action for icon click
    $('.tabs .ico').click(function() {
        if($(this).parent().find('a').attr('href')) document.location = $(this).parent().find('a').attr('href');
    });
    
    $('#auth-tabs-login a').click(function(ev){
        ev.stopPropagation();
        $('#auth-login-form').toggle();
        $('#auth-tabs-login').toggleClass('active');
        return false;
    });
    
    $('#auth-login-form').bind('rightclick', function(ev) {
        ev.stopPropagation();
    });
    
    $('#search-tabs li').click(function(){
	var token = $(this).attr('id').replace('search-tabs-', '');

	if($(this).hasClass('active')) {
	    $('#menu-' + token).hide();
	    $(this).removeClass('active');
	} else {
	    $('#menu .menu').hide();
	    $('#search-tabs li.active').removeClass('active');
	    $('#menu-' + token).show();
	    $(this).addClass('active');
	}
			        
        if($.browser.msie && $.browser.version <= 6) zml.ieTabFix($(this));

        return false;
    });
    
    $(document).bind('SPLASH_CLICKED', function(ev){
        // Hide auth menu
        $('#auth-tabs .active').removeClass('active');
        $('#auth-login-form').hide();
    });
    
    // Prevent auth menu close when user clicks inside login form
    $('#auth-login-form').click(function(ev){
        ev.stopPropagation();
    });

	$('#search-params input').bind('click', function(){
		zml.changeSearchType($(this).val(), '#top-search-form');
	});
	
	$('select.customselect').customSelect();
});
