var popup = {};
// Retrieve get variables
var get = [], hash;
var hashes = window.location.search.substring(1).split('&');
for (var i = 0; i < hashes.length; i++) 
{
	hash = hashes[i].split('=');
	get.push(hash[0]);
	get[hash[0]] = hash[1];
}
var anchor = window.location.hash;
if (anchor != '') {
    anchor = anchor.substring(1);
}
// Determine language
var language, langsel = false;
if (get.lang) {
    language = get.lang;
} else {
    if ($.cookie("lang")) {
        language = $.cookie("lang");
    } else {
        language = 'en_US';
        $.cookie("cookiecheck", null);
        $.cookie("cookiecheck", "1");
        if ($.cookie("cookiecheck")) {
            langsel = true;
            $.cookie("cookiecheck", null);
        }
    }
}



function displayMainContent(display, fromFlash)
{
	if (display == 'home')
	{
		if ($('#nav li.about a').hasClass('onstate'))
			$('#nav li.about a').removeClass('onstate');
		if ($('#content_secondary').css('display') != 'none')
			$('#content_secondary').hide('slow');
		if ($('#content_home').css('display') == 'none')
			$('#content_home').show('slow');
	}
	else if (display == 'secondary')
	{
		if (!$('#nav li.about a').hasClass('onstate'))
			$('#nav li.about a').addClass('onstate');
		if ($('#content_home').css('display') != 'none')
			$('#content_home').hide('slow');
		if ($('#content_secondary').css('display') == 'none')
			$('#content_secondary').show('slow');
	}
}

function get_image_browser_count(container_id){
    return parseInt($("#" + container_id + " .node").size());
}

function get_image_browser_movement_amount(container_id){
    var node = $("#" + container_id + " .node");
    var width = parseInt(node.css("width"));
    var marginLeft = parseInt(node.css("margin-left"));
	 var marginRight = parseInt(node.css("margin-right"));
	 totalMove = width + marginLeft + marginRight;
    return (totalMove);
}

function check_image_browser_arrows(container_id, nodesPerView) {
    var nodes = $("#" + container_id + " .node").size();
    var position = parseInt($("#" + container_id + " span.position").text());
    
    if(position == 0 || nodes <= nodesPerView )
        $("#" + container_id + "_wrapper .arrow_left").css("visibility", "hidden");
    else
        $("#" + container_id + "_wrapper .arrow_left").css("visibility", "visible");
 
    if(position + nodesPerView >= nodes || nodes <= nodesPerView )
        $("#" + container_id + "_wrapper .arrow_right").css("visibility", "hidden");
    else
        $("#" + container_id + "_wrapper .arrow_right").css("visibility", "visible");
}

function image_browser_animate(container_id, direction, nodesPerView ){

    var posDiv = $("#" + container_id + " span.position");
    var pos = parseInt(posDiv.text());

    if(direction == "right"){
        if(pos < get_image_browser_count(container_id)-nodesPerView){
            var nodes = $("#" + container_id/* + " div.nodes_inner"*/);
            var marginLeft = parseInt(nodes.css("margin-left").replace("px", ""));
            nodes.animate({
                marginLeft: "-=" + get_image_browser_movement_amount(container_id) + "px"
            });
            posDiv.html(pos+1);
        }
    }
    else if(direction == "left"){
        if(pos > 0){
            var nodes = $("#" + container_id/* + " div.nodes_inner"*/);
            var marginLeft = parseInt(nodes.css("margin-left").replace("px", ""));
            nodes.animate({
                marginLeft: "+=" + get_image_browser_movement_amount(container_id) + "px"
            });
            posDiv.html(pos-1);
        }
    }
   check_image_browser_arrows(container_id,nodesPerView);
}

function legalConfirm()
{
	return confirm(messages.download_legal_notice);
}

function signupFriend()
{
	var errorCount = 0;
	if(!(/^[_A-Za-z0-9-]+(\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\.[A-Za-z0-9-]+)*(\.[A-Za-z]{2,4})$/.test($('#friend_email').val())))
	{
		$('#friend_email_error').css('visibility','visible');
		errorCount++;
	}
	else
		$('#friend_email_error').css('visibility','hidden');

	if(!(/^[_A-Za-z0-9-]+(\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\.[A-Za-z0-9-]+)*(\.[A-Za-z]{2,4})$/.test($('#user_email').val())))
	{
		$('#user_email_error').css('visibility','visible');
		errorCount++;
	}
	else
		$('#user_email_error').css('visibility','hidden');
	
	if (errorCount == 0)
	{
		var dataString = 'email=' + $('#friend_email').val() + '&from=' + $('#user_email').val();
		$.ajax({
			type: 'GET',
			url: 'sendtofriend.aspx',
			data: dataString
		});
        $('#send_to_friend_submit').hide();
        $('#send_to_friend_submitsuccess').show();
		setTimeout(function() { $('#send_to_friend').slideUp('slow'); }, 1000);
	}
}

    function openSendToFriend(){
            flashLink("flash_tellafriend");
            var popup = $('#popup_home_assistance');
            if(popup.length > 0) {
                $('a', popup).click(function() {
                    popup.slideUp('slow');
                    return false;   
                });
                popup.slideDown('slow');                
            } else {
                $('#friend_email').val("");
                $('#user_email').val("");
                $('#send_to_friend_submitsuccess').hide();
                $('#send_to_friend_submit').show();
     		    $('#send_to_friend').slideDown('slow', function(){ document.getElementById('friend_email').focus(); });
            }
    }
    
function tokenReplace(el, obj, html) {
    var tkn = $(el);
    if(html == true) {
        tkn.next().html($.token(tkn.html(), obj));
    } else {
        tkn.next().text($.token(tkn.text(), obj));
    }
}

var util = {
    loadtable: function(tbody, lst, decorator) {
        tbody = $(tbody);
        var tmpl = $('.token_template', tbody);
        $(tbody).children().not('.static').remove();
        if (lst != null) {
            var s = tmpl.html();
            $.each(lst, function(idx) {
                var rw = $('<tr>' + $.token(s, this) + '</tr>');
                if (decorator) {
                    decorator(rw, this, idx);
                }
                rw.insertBefore(tmpl);
            });
        }
    },
    buildlist: function(parent, lst, decorator) {
        parent = $(parent);
        var tmpl = $('.token_template', parent);
        if (tmpl != null) {
            var dup = tmpl.clone().removeClass('token_template').removeClass('static');
            $(parent).children().not('.static').remove();
            if (lst != null) {
                var s = tmpl.html();
                $.each(lst, function(idx) {
                    var el = dup.clone().html($.token(s, this));
                    if (decorator) {
                        decorator(el, this, idx);
                    }
                    el.insertBefore(tmpl);
                });
            }
        }
    },
    checkinput: function(f, sel) {
        var valid = true;
        $(sel || "input:text:visible:enabled,input:password:visible:enabled", f).each(function() {
            var v = jQuery.trim($(this).val());
            if (v.length == 0) {
                valid = false;
            }
        });
        if (!valid) {
            omniSetPage(s_initPageName + 'ERROR');
        }
        omniSetPageTypeId(s_initPageName);
        return valid;
    },
    validations: {
        required: function(v) {
            return (jQuery.trim(v).length == 0) ? messages.value_required : null;
        },
        regextest: function(re, msg) {
            return function(v) {
                return (!re.test(v)) ? (msg || messages.invalid_value) : null;
            }
        }
    },
    msgbox: {
        show: function(c, f_result, obj) {
            if (typeof c !== "object" && c != null) {
                c = confirmForm[c];
            }
            $('#msgbox_title').text(c.title || '');
            $('#msgbox_text').html($.token(c.text, obj));
            $('#msgbox_accept_button_text').text(c.accept);
            $('#msgbox_decline_button_text').text(c.decline);
            $('#msgbox_accept_button_text,#msgbox_decline_button_text').each(function() {
                Cufon.replace(this);
            });
            Cufon.now();
            $('#msgbox_accept_button').toggle(c.accept != '').unbind().click(function() {
                util.msgbox.hide(f_result, true);
            });
            $('#msgbox_decline_button,.msgbox_close_button').unbind().click(function() {
                util.msgbox.hide(f_result, false);
            });
            $('#msgbox_decline_button').toggle(c.decline != '');
            $('div.opaque').fadeIn('fast', function() {
                $('div.opaque_msgbox').fadeIn('fast');
            });
        },
        hide: function(f_result, result) {
            $('div.opaque_msgbox').fadeOut('fast', function() {
                $('div.opaque').fadeOut('fast', function() {
                    if (f_result != null) {
                        f_result(result);
                    }
                });
            });
        }
    },
    radioSelect: function(name, val) {
        var sel = $('input[name=' + name + '][value=' + val + ']');
        if (sel.length == 0) {
            sel = $('input[name=' + name + ']:first');
        }
        sel.click();
    }
}

var formprm = {};
var formmgr = {
    _I: null,
    active: null,
    forms: null,
    init: function(frm) {
        _I = this;
        _I.forms = frm;
        for (var f in _I.forms) {
            _I.forms[f].el = $('#' + f);
            _I.forms[f].id = f;
        }
        $.each($('#script_form_prm').text().split('^'), function() {
            var c = this.split('~');
            formprm[c[0]] = {
                id: c[0],
                omnipage: c[1]
            };
        });
    },
    show: function(frm, obj) {
        if (this.active != frm) {
            $('#pageforms > div').hide();
            frm.init(obj);
            if (frm.validation != null) {
                $('error', frm.el).hide();
            }
            $(frm.el).show();
            formmgr.invalid();
            formmgr.status();
            var pg = $('#omnipage:visible', frm.el).text() || '';
            if (pg != '') {
                omniSetPage(pg);
            }
            this.active = frm;
        }
    },
    isvalid: function(frm, val) {
        var cnt = 0;
        for (var i in val || frm.validation || {}) {
            var id = (val) ? val[i] : i;
            var el = $('#' + id + ':visible:enabled');
            if (el.length > 0) {
                var txt = frm.validation[id](el.val(), el);
                var msg = $('#' + id + '_error');
                if (txt != null) {
                    msg.text(txt).slideUp().slideDown();
                    cnt++;
                } else {
                    msg.slideUp().text('');
                }
            }
        }
        if (cnt != 0) {
            omniSetPage(s_initPageName + 'ERROR');
        }
        omniSetPageTypeId(s_initPageName);
        return cnt == 0;
    },
    status: function(msg, busy) {
        var bx = $('#statusbox');
        if (msg == null) {
            bx.fadeOut(500);
        } else {
            $('span', bx).text(msg);
            $('img', bx).toggle(busy == true);
            bx.fadeIn(250, (busy == true) ? null : function() {
                setTimeout(function() {
                    bx.fadeOut(500);
                }, 5000);
            });
        }
    },
    invalid: function(msg, el) {
        if (formmgr.active == null) {
            return;
        }
        var bx = el || $('div.validation', formmgr.active.el);
        if (bx.length > 0) {
            if (msg == null) {
                bx.slideUp();
            } else {
                bx.html(msg).slideDown();
            }
        }
    },
    responsecheck: function(res, msg, el) {
        formmgr.status();
        if (!res.Success) {
            if (res.ResultCode == 'RC_AuthTimeout') {
                alert(messages.RC_AuthTimeout);
                window.location.reload(true);
            } else {
                var msg = msg || messages[res.ResultCode] || messages.RC_GeneralFailure;
                formmgr.invalid(msg, el);
            }
            return false;
        } else {
            return true;
        }
    }
}

$(document).ready(function() {


    if (langsel) {
        $('#language_select').show();
    }
    if (language) { // Select current language
        var c = language.substring(0, 2).toLowerCase();
        if (codes.language[c]) {
            $('#language_options_active').text(codes.language[c]);
            Cufon.replace('#language_options_active');
        }
    } else {
        language = 'en_US';
    }
    $.each($('#popup_param').text().split('^'), function() {
        var p = this.split('~');
        popup[p[0]] = {
            id: p[0],
            interval: p[1] - 0,
            cookie: 'pop_' + p[0],
            el: $('#pop_' + p[0])
        };
    });

    $.preloadCssImages();

    var loadopts = {};
    switch (anchor) {
        case "newsletter":
            anchor = '';
            loadopts['news'] = true;
            break;
    }

    $('#major_popup_selector').click(function() {
        if ($('#major_popup_options').css('display') == 'block') $('#major_popup_options').hide();
        else $('#major_popup_options').show();
        return false;
    });


    $('#language_selector').click(function() {
        $('#language_select').show();
        //if ($('#language_options').css('display') == 'none')
        //    $('#language_options').show('fast');
        //else
        //    $('#language_options').fadeOut('fast');
    });
    //$('#all_content').click(function() {
    //    $('#language_options').fadeOut('fast');
    //});
    /*
    $('#main a.home').click(function() {
    if ($('#nav li.about a').hasClass('onstate'))
    $('#nav li.about a').removeClass('onstate');
    displayMainContent('home');
    return false;
    });
    */
    /*
    $('#nav ul.local li.about a').click(function() {
    displayMainContent('secondary');
    return false;
    });
    */
    $('.temp_secondary').click(function() {
        displayMainContent('secondary');
        return false;
    });


    $('a.question').click(function() {
        $(this).next('div.answer').slideToggle('fast');
        return false;
    });

    $('a.readmore').click(function() {
        $(this).prev('p.more').slideToggle('fast');
        if ($(this).html() == 'Read More') {
            $(this).html('Less');
            $(this).addClass('action_read_more_less');
            $(this).removeClass('action');
        }
        else if ($(this).hasClass('action_read_more_less')) {
            $(this).html('Read More');
            $(this).addClass('action');
            $(this).removeClass('action_read_more_less');
        }
        else if ($(this).html() == 'Learn More') {
            $(this).html('Less');
            $(this).addClass('action_learn_more_less');
            $(this).removeClass('action');
        }
        else if ($(this).hasClass('action_learn_more_less')) {
            $(this).html('Learn More');
            $(this).addClass('action');
            $(this).removeClass('action_learn_more_less');
        }
        return false;
    });

    $('a.extlink').click(function() {
        return (confirm(messages.confirm_external_link));
    });

    $('#wallpaper_left').click(function() {
        image_browser_animate("wallpaper_img", "left", 3);
        return false;
    });

    $('#wallpaper_right').click(function() {
        image_browser_animate("wallpaper_img", "right", 3);
        return false;
    });

    check_image_browser_arrows("wallpaper_img", 3);

    $('#screensaver_left').click(function() {
        image_browser_animate("screensaver_img", "left", 3);
        return false;
    });

    $('#screensaver_right').click(function() {
        image_browser_animate("screensaver_img", "right", 3);
        return false;
    });

    check_image_browser_arrows("screensaver_img", 3);

    $('#toys_left').click(function() {
        image_browser_animate("toys_img", "left", 4);
        return false;
    });

    $('#toys_right').click(function() {
        image_browser_animate("toys_img", "right", 4);
        return false;
    });

    check_image_browser_arrows("toys_img", 4);

    /* TOYS SLIDER */
    $('#toys_img_wrapper div.node a').click(function() {
        $('#toys_img_wrapper div.node').removeClass('onstate');
        $(this).closest('.node').addClass('onstate');
        var toy = $('span', this).text().split('~');
        $('#toy_image').attr('alt', toy[0]).attr('title', toy[0]).attr('src', toy[1]).attr('alt', toy[0]);
        $('#toy_title').text(toy[0]);
        Cufon.replace('#toy_title');
        $('#toy_description').html(toy[2]);
        $('#tag_detail').toggle((toy.length < 4) || (toy[3] != 'false'));
        return false;
    }).eq(0).click(); ;

    //    $('#toys_img a').click(function() {
    //        var toy = $('span', this).text().split('~');
    //        $('#toy_image').attr('alt', toy[0]).attr('title', toy[0]).attr('src', toy[1]).attr('alt', toy[0]);
    //        $('#toy_title').text(toy[0]);
    //        $('#toy_description').html(toy[2]);
    //        //tokenReplace('#toy_description_template', { description: toy[2] });
    //        return false;
    //    }).eq(0).click();

    $('#blog_left').click(function() {
        image_browser_animate("slide_window", "left", 1);
        return false;
    });

    $('#blog_right').click(function() {
        image_browser_animate("slide_window", "right", 1);
        return false;
    });

    check_image_browser_arrows("slide_window", 1);

    $('#blog_window .node a[target=_popimg]').each(function() {
        var el = $(this);
        var img = new Image();
        var imgLoaded = function() {
            var width = img.width;
            el.data('img', $('<img/>').attr('src', img.src).css(
                (width > 500)
                    ? { 'width': '500px', 'height': 'auto' }
                    : { 'position': 'relative', 'left': ((500 - width) / 2) + 'px' }
            ));
        };
        img.src = el.attr('href');
        if (!img.complete) {
            $(img).bind('error load onreadystatechange', imgLoaded);
        } else {
            imgLoaded();
        }
        el.click(function() {
            $('#img_popbox .popup_overlay_innermost').html(el.data('img'));
            $('#img_popbox').slideDown();
            return false;
        });
    });

    $('#img_popbox').click(function() {
        $('#img_popbox').slideUp();
    });

    $('#os_selector .selector_active').click(function() {
        $('#os_selector').fadeOut('slow');
    });

    $('#screensaver_img div.node a, #wallpaper_img div.node a').click(function(e) {
        $("#os_selector").css("left", e.pageX - 88);
        $("#os_selector").css("top", e.pageY + 14);
        var title = $(this).next().attr('title');
        $('#os_selector img').each(function() {
            this.alt = title;
            this.title = title;
        });
        $('#os_selector #os_options_active').text(title);
        $('#os_options').html($(this).next().html());
        if ($('#os_selector').css('display') == 'none') {
            $('#os_selector').fadeIn('slow');
            $('#os_options').fadeIn('slow');
        }
        /*else
        {
        $('#os_selector').fadeOut('slow');
        }*/
        return false;
    });

    $('ul#os_options').click(function() {
        $('#os_selector').fadeOut();
        if (legalConfirm() == true)
            return true;
        else
            return false;
    });

    $('p.answer').hide();
    $('p.more').hide();

    $('#footer_sign_up_now').click(function() {
        openPositionedWindow(this.href, 'lpso-newsletter-signup', 800, 680, 0, 0, true, true);
        return false;
    });

    $('a.language_country_select').click(function() {
        var pop = $($(this).attr('href'));
        var vis = pop.is(':visible');
        $('div.language_popup').hide();
        if (!vis) {
            var p = $(this).offset();
            pop.css({ "left": (p.left - 28) + "px", "top": (p.top + 12) + "px" }).show();
        }
        return false;
    });

    $('a.language_locale_select').click(function() {
        if ($(this).closest('div.language_popup').length == 0) {
            $('div.language_popup').hide();
        }
        var lnk = $(this).attr('href');
        var locale = lnk.split('=')[1];
        $("body").css("cursor", "wait");
        preferenceProxy.geoIpCheck(locale, function(res) {
            $("body").css("cursor", "auto");
            if (formmgr.responsecheck(res)) {
                window.location.assign(lnk);
            } else {
                util.msgbox.show(geowarn[locale], function(res) {
                    if (res == true) {
                        window.location.assign(lnk);
                    }
                });
            }
        });
        return false;
    });

    // process tab    
    var t = get['tab'];
    if (t != 'home' && t != 'meet' && t != 'explore' && t != 'play' && t != 'video') {
        t = null;
    }
    displayMainContent(t == null || t == 'home' ? 'home' : 'secondary', true);

    // init flash
    if (typeof (flashObjs) != "undefined") {
        jQuery.each(flashObjs, function(i, f) {
            if (t != null) {
                f.vars.section = t;
            }
            swfobject.embedSWF(f.url, f.el, f.width, f.height, "9.0.0.0", "", f.vars, f.prms);
            $('#' + f.el + ' #altcontent').show();
        });
    }

    if (typeof (flashLinks) != "undefined") {
        jQuery.each(flashLinks, function(i, l) {
            $("#" + l.id).click(function(e) {
                displayMainContent(l.method == "flashHome" ? "home" : "secondary");
                var f = document.getElementById(l.flashObj);
                if (f != null) {
                    e.preventDefault();
                    f[l.method]();
                    return false;
                }
            });
        });
    }

    /* SEND TO FRIEND */
    $('#send_to_friend_submit').click(function() {
        signupFriend();
        return false;
    });
    $('#send_to_friend_cancel').click(function() {
        $('#send_to_friend').slideUp('slow');
        return false;
    });
    $('#friend_email').keypress(function(e) {
        if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
            signupFriend();
            return false;
        }
    });
    $('#user_email').keypress(function(e) {
        if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
            signupFriend();
            return false;
        }
    });

    $('div.popup_question_wrapper a.popup_question_link').mouseenter(function() {
        $(this).closest('div.popup_question_wrapper').find('p.form_decoration').fadeIn('fast');
    });
    $('div.popup_question_wrapper a.popup_question_link').mouseleave(function() {
        $(this).closest('div.popup_question_wrapper').find('p.form_decoration').fadeOut('fast');
    });
    $('div.popup_question_wrapper a.popup_question_link').click(function() {
        return false;
    });

    $('a.simple_button').mouseenter(function() {
        Cufon.replace('div.simple_button_inner');
    });
    $('a.simple_button').mouseout(function() {
        setTimeout(function() {
            Cufon.replace('div.simple_button_inner');
        }, 100);
    });

    $("input#parent_email,input#parent_password").keypress(function(e) {
        if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
            $('#login_submit').click();
            return false;
        } else {
            return true;
        }
    });

    if ($('div#day_countdown').length == 1) {
        var today = new Date();
        var countdownTo = new Date("September 8, 2009");
        var msPerDay = 24 * 60 * 60 * 1000;
        var remaining = (countdownTo.getTime() - today.getTime());
        var setDayCountdown = Math.floor(remaining / msPerDay) + 1;

        if (setDayCountdown == 1)
            var daysVar = "ONLY 1 MORE DAY";
        else if (setDayCountdown == 0)
            var daysVar = "FINAL DAY";
        else
            var daysVar = "ONLY " + setDayCountdown + " MORE DAYS";
        $('div#day_countdown').html(daysVar + " OF THE SNEAK PEEK BETA!");
        Cufon.replace("div#day_countdown");
    }

    var popsel = null;
    if (get.popup && popup[get.popup]) {
        popsel = popup[get.popup];
    } else {
        for (var id in popup) {
            if (popup[id].interval > 0 && !($.cookie(popup[id].cookie))) {
                popsel = popup[id];
                break;
            }
        }
    }
    if (popsel != null) {
        $('.popup_overlay_close a,.popup_overlay_inner', popsel.el).click(function() {
            $('#popup_shim').hide();
            $(popsel.el).hide();
            return false;
        });
        if (popsel.interval > 0) {
            $.cookie(popsel.cookie, '1', { expires: popsel.interval, path: '/' });
        }
        $(popsel.el).show();
        $('#popup_shim').show();
        setTimeout(function() {
            $('#popup_shim').height($('.popup_overlay_innermost', popsel.el).outerHeight() + 25);
        }, 500);
    }
    $('#lpso_frame').each(function() {
        var src = $('#lpso_game_url').text();
        if (anchor != '') {
            src += '&playMode=' + anchor;
        }
        if (get['trackingCode'] != null) {
            src += '&trackingCode=' + get['trackingCode'];
        }
        $(this).attr('src', src);
    });


    //	var el = $('#play_popup');
    //	if(el.length > 0) {
    //	    var p = $('#play_popup_param').text().split('~');
    //	    if(!($.cookie(p[0]))) {
    //	        $('#bff_popup_close,#play_popup').click(function() {
    //		        $('#play_popup_shim').hide();
    //		        $('#play_popup').hide();
    //	        });
    //	        $.cookie(p[0], '1', { expires: p[1] - 0, path: '/' });
    //	        $('#play_popup').show();
    //	        $('#play_popup_shim').show();
    //	        setTimeout(function() {
    //	            $('#play_popup_shim').height($('.popup_overlay_innermost', el).outerHeight() + 25);
    //	        }, 500);
    //	    }
    //	}

    // service error handler
    preferenceProxy.error = function(err) {
        formmgr.status(messages.unexpected_error, false);
    };
    // init proxy
    preferenceProxy.init(g_httpPath);

    // populate newsletter list
    if (g_httpPath == '') {
        preferenceProxy.gameNewsList(function(res) {
            if (res.Success) {
                var news_url = "";
                var news_tmpl = $('.flash_newsletter').html();
                var loadNews = function(url) {
                    if (news_url != url) {
                        if (news_url != '') {
                            $('.flash_newsletter').html(news_tmpl);
                        }
                        swfobject.embedSWF(url + 'news.swf', 'flash_news_swf', 950, 550, "9.0.0.0", "", {
                            baseURL: url,
                            lang: language
                        }, {
                            wmode: 'transparent',
                            allowScriptAccess: 'always'
                        });
                        $('#flash_news_swf #altcontent').show();
                        news_url = url;
                    }
                };
                var l = $('.major_selector_options li a').text();
                $('.major_selector_options').empty();
                for (var i in res.Assets) {
                    var itm = res.Assets[i];
                    $('.major_selector_options').append($('<li/>').append($('<a href="#"/>').text(l + ' ' + itm.Name).data('url', itm.Url).click(function() {
                        loadNews($(this).data('url'));
                        $('#major_popup_options').hide();
                        return false;
                    })));
                }
                var showNews = function() {
                    if (res.Assets.length > 0) {
                        loadNews(res.Assets[0].Url);
                        $('div.opaque,div.major_popup').show();
                    }
                };
                $('.news_popup_link').click(function() {
                    showNews();
                    return false;
                });
                if (loadopts.news) {
                    showNews();
                }
                window.showNews = showNews;
            }
        });
    }
    return false;

});

function omniCall(element, omniTag) {
    omniLinkCall(element, omniTag);
}

function omniSetPage(pageName) {
    // create pseudo request with new pagename
    setOmniValues('', '', '', '', '', '', 1, '', '', omniSetPageTypeId(pageName));
}

function openPositionedWindow(url, name, width, height, x, y, status, scrollbars, moreProperties, openerName) {
	openPopup(url, name, width, height, status, scrollbars, moreProperties);
}

// GLOBAL WINDOW POPUP (Used everywhere there's a popup.)
function openPopup(url, name, width, height, status, scrollbars, moreProperties) {
	var agent = navigator.userAgent.toLowerCase();
	var x, y = 0;
	if (screen) {
      x = (screen.availWidth - width) / 2;
      y = (screen.availHeight - height) / 2;
   }
   if (!status) status = '';

	// Adjust width if scrollbars are used (pc places scrollbars inside the content area; mac outside) 
	width += (scrollbars != '' && scrollbars != null && agent.indexOf("mac") == -1) ? 16 : 0;

	var properties = 'width=' + width + ',height=' + height + ',screenX=' + x + ',screenY=' + y + ',left=' + x + ',top=' + y + ((status) ? ',status' : '') + ',scrollbars' + ((scrollbars) ? '' : '=no') + ((moreProperties) ? ',' + moreProperties : '');
   
	window.open(url, name, properties);
	return false;
} 
    // flash callbacks

function flashOmniEvent(moduleId, objectId, position, link, omnitag) {
    if ((omnitag || '') != '') {
        omniCall($('#flash_swf'), omnitag);
    }
    var l = (link || '').split('|');
    if (l[0].substring(0, 1) == '#') {
        switch (l[0].substring(1)) {
            case "newsletter":
                if(window.showNews) {
                    window.showNews();
                }
                break;
            case "/video":
                $('#flash_swf').get(0).loadAboutThegame();
                break;
            case "/home":
                $('#flash_swf').get(0).loadHome();
                break;
        }
    } else {
        if (l[0].length > 0) {
            var target = (l.length > 1) ? l[1] : null;
            var external = (l.length > 2) ? l[2] : null;
            if (external == 'true') {
                if (confirm(messages.confirm_external_link) != true) {
                    return;
                }
            }
            if ((target || '') != '') {
                window.open(l[0], target);
            } else {
                setTimeout(function() {
                    window.location.assign(l[0]);
                }, 500);
            }
        }
    }
}
    
//    function flashLink(id) {
//        var l = link[id];
//        if ((l.omnitag || '') != '') {
//            omniCall($('#flash_swf'), l.omnitag);
//        }
//        if ((l.flashmethod || '') != '') {
//            $('#flash_swf').get(0)[l.flashmethod]();
//        } else if((l.link || '') != '') {
//            if(l.external == 'true') {
//                if(confirm(messages.confirm_external_link) != true) {
//                    return;
//                }
//            }
//            if((l.target || '') != '') {
//                window.open(l.link, l.target);
//            } else {
//                setTimeout(function() {
//                    window.location.assign(l.link);
//                }, 500);
//            }
//        }
//    }

    function closeNewsContent() {
        $('div.major_popup,div.opaque').hide();
    }
     
    
    
