var headID = document.getElementsByTagName("head")[0];

var carVars1 = document.createElement('script');
carVars1.type="text/javascript";
carVars1.src="//www.huawei.com/ucmf/groups/public/documents/webasset/swfobject.js";
headID.appendChild(carVars1);


var flash_params = {
    wmode: 'opaque',
    menu: false,
    allowFullscreen: "true",
    scale: 'Scale'
};
var flashvars = {};

//$(window).load(function(){
$(document).ready(function(){
	lightbox.worldwide = $("#worldWide")[0];
    lightbox.ini();

    
});

jQuery.fn.center = function(){
//alert(this.name);
    var top = ($(window).height() - this.height()) / 2 + $(window).scrollTop();    
    var left = ($(window).width() - this.width()) / 2 + $(window).scrollLeft();
//alert("window.width:"+$(window).width()+"------------this.width:"+this.width()+"--------------window.scrollLeft:"+$(window).scrollLeft());
   // alert("$(window).height()=="+$(window).height()+"/"+"this.height()="+this.height()+"$(window).scrollTop()="+ $(window).scrollTop());
    this.css("position", "absolute");
    this.css("top", top + "px");
    this.css("left", left + "px");
    return this;
};

var lightbox = {};
lightbox.status = 0;
lightbox.contentId = false;
lightbox.selector = 'a[rel=lightbox]';
lightbox.width = 706;
lightbox.height = 560;
lightbox.ini = function(){
   // $.getScript("http://10.88.48.179/ucmb/groups/public/documents/webasset/swfobject.js");
    $(lightbox.worldwide).click(function(e){
        e.preventDefault();
        
        lightbox.status = 1;
        lightbox.overlay();
        var hrefObj = this.href.substring(this.href.indexOf('#') + 1);
        var srcArray = hrefObj.split('&');
        var srcPath = srcArray[0];
         lightbox.width = srcArray[1];
         lightbox.height = srcArray[2];
		$(headID).append('<style>#overlay{ position:absolute; top:0px; left:0px; z-index:90; opacity:0.5; filter:alpha(opacity=50); background-color:#000; }#lightbox .videoHead{padding:0 0 0 5px; margin:0px auto; height: 16px; font-size:16px;}#lightbox .videoHead .Close{background: url("//www.huawei.com/ucmf/groups/public/documents/webasset/hw_000477.gif") no-repeat scroll -109px -43px transparent; height: 16px; margin-right: 0px; position: absolute; right: -6px; top: 0px; width: 16px; cursor:pointer;}#lightbox .video{ left:500px;background:none; padding:0 0 0 0;left:500px}</style>');
        $('<div id="lightbox" />').appendTo('body').css({
            'width': lightbox.width,
            'height': lightbox.height,
            'z-index': 99
        }).append('<div class="videoHead"><div class="Close"></div></div>').center();
        
        $('#lightbox .Close').click(lightbox.close);
        $('#lightbox').append('<div class="video"><div id="lightbox_flash"><div style="text-align:center;"><img src=g_HttpRelativeWebRoot + "groups/public/documents/webasset/hw_076709.gif" /></div></div></div>');
        $('#lightbox .heading div').removeClass("title");
        $('#lightbox').css({
            'width': parseInt(lightbox.width) + 10,
            'height': parseInt(lightbox.height) + 50
        }).find('.heading').css('width', lightbox.width);
        var flashvars = {};
        var flash_params = {
            menu: "false",
            scale: "noScale",
            allowFullscreen: "true",
            allowScriptAccess: "always",
            bgcolor: "#FFFFFF"
        };

  		 swfobject.embedSWF(srcPath, 'lightbox_flash', lightbox.width, lightbox.height, '9.0.0', "expressInstall.swf", flashvars, flash_params);

        $(window).unbind('scroll').resize(lightbox.relocate);
    });
    
};

lightbox.open = function(id, title){

    lightbox.status = 1;
    lightbox.overlay();
     
    $('<div id="lightbox" />').appendTo('body').css({

        'width': lightbox.width,
        'height': lightbox.height,
        'z-index': 99
    }).append('<div class="heading"><div class="title"></div><div class="close">Close X</div></div>').center();
    
    $('#lightbox .close').click(lightbox.close);
    
    $('#lightbox .title').html(title);
    
    $lightbox = $('#lightbox').append('<div id="lightbox_content" />');
    
    lightbox.contentId = id;
     
    var $content = $('#' + id);
    
    $lightbox.width($content.width()).height($content.height() + 50).center();
    
    if (parseInt($lightbox.css('top')) < 0) 
        $lightbox.css('top', '0px');
    
    $content.appendTo('#lightbox_content').show();
    
    if (id == 'inquiry_popup') {
        $(window).scroll(lightbox.relocateShift).resize(lightbox.relocateShift);
        lightbox.relocateShift();
    }
    else {
        $(window).unbind('scroll').resize(lightbox.relocate);
    };
    
    }


lightbox.relocate = function(){
    if (lightbox.status == 1) {
        lightbox.overlay();
        var $lightbox = $('#lightbox').center();
        if (parseInt($lightbox.css('top')) < 0) 
            $lightbox.css('top', '0px');
    };
    }

lightbox.relocateShift = function(){
    if (lightbox.status == 1) {
        lightbox.overlay();
        if (iPx()) 
            var top = $(window).scrollTop() + 30;
        else 
            var top = $(window).scrollTop() + 100;
        var $lightbox = $('#lightbox').center().css('top', top);
        if (parseInt($lightbox.css('top')) < 0) 
            $lightbox.css('top', '0px');
    };
    }

lightbox.overlay = function(){

    var w_width = $(document).width();
    var w_height = $(document).height();
    
    if ($.browser.msie && parseInt($.browser.version) == 6) 
        w_width = w_width - scrollbarWidth() - 8;
    
    $('#overlay').remove();
    $('<div id="overlay" />').appendTo('body').css({
        'width': w_width,
        'height': w_height
    }).click(lightbox.close);
    
    // IE6 select box fix
    if ($.browser.msie && parseInt($.browser.version) <= 6) {
        $('select').css('visibility', 'hidden');
        $('.popup select').css('visibility', 'visible');
    };
    
    }

lightbox.close = function(){

    if (lightbox.contentId != false) {
        $('#' + lightbox.contentId).hide().appendTo('body');
        lightbox.contentId = false;
    };
    
    
    $('#overlay, #lightbox').remove();
    
    // IE6 select box fix
    if ($.browser.msie && parseInt($.browser.version) <= 6) 
        $('select').css('visibility', 'visible');
    
    lightbox.status = 0;
    
}


function scrollbarWidth(){
    var div = $('<div style="width:50px;height:50px;overflow:hidden;position:absolute;top:-200px;left:-200px;"><div style="height:100px;"/></div>');
    // Append our div, do our calculation and then remove it
    $('body').append(div);
    var w1 = $('div', div).innerWidth();
    div.css('overflow-y', 'scroll');
    var w2 = $('div', div).innerWidth();
    $(div).remove();
    return (w1 - w2);
}

function getParameterByName(name){
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(l);
    if (results == null) 
        return "";
    else 
        return decodeURIComponent(results[1].replace(/\+/g, " "));
}

function iPx(){
    if ((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPad/i))) 
        return true;
    return false;
}


/** trim() method for String */  
String.prototype.trim=function() {  
    return this.replace(/(^\s*)|(\s*$)/g,'');  
};

