dom = (document.getElementById)? true : false;
nn4 = (document.layers)? true : false;
ie = (document.all)? true : false;
ie4 = (!dom && ie)? true : false;

var currGlasses = -1;
var currMoving = false;
var fadingAfterMoving = false;

var c_mainPhotosContainerWidth = 789;
var c_mainWidth = 1017;
var c_mainPhotosContainerPositionTop = 70;
var c_glassWidth = 99; 
var c_glassHeight = 65;
var c_firstWomenFrame = 1;
var c_firstMenFrame = 14;
var c_minimalDelay = 25;

var timers = new Array();
var eyes = new Array(); 
var models = new Array();
var scart = new Array();
var styles = new Array( 'fb001', 'fb002', 'fb003', 'fb004', 'fb005', 'fb006',
    'fb009', 'fb010', 'fb011', 'fb012', 'fb013', 'fb014', 'fb015', 'fb005', 
    'fb006', 'fb007', 'fb011', 'fb012', 'fb013', 'fb014' );

var fr_count = fr_left = fr_rght = fr_curr = 0;
var fr_fromCenter = false;
var fr_timeout = null;

var mouse_x = mouse_y = 0;
var scroll_x=0;
var movingTimeout = null;

function findObject( id, doc )
{
    var i;

    if( doc == null )
        doc = document;

    if( dom )
        return doc.getElementById( id );
    else
        {
        if( ie4 )
        return doc.all[ id ];
        }

    for( i=0; i<doc.forms.length; i++ )
        for( j=0; j<doc.forms[i].elements.length; j++ )
        if( id == doc.forms[i].elements[j].name )
            return doc.forms[i].elements[j];

    for( i=0; i<doc.images.length; i++ )
        if( id == doc.images[i].name )
        return doc.images[i];

    if( doc.layers == null )
        return null;

    for( i=0; i<doc.layers.length; i++ )
        {
        if( id == doc.layers[i].name )
        return doc.layers[i];
        var x = findObject( id, doc.layers[i].document );
        if( x != null )
        return x;
        } 
    return null;
}

function preloadImages()
{
    doc = document;
    if( doc.images )
        {
        if( !doc.preloadImgs )
            doc.preloadImgs = new Array();
        var i, j = doc.preloadImgs.length;
        var a = preloadImages.arguments;
        for(i=0; i<a.length; i++)
            {
            doc.preloadImgs[j] = new Image;
            doc.preloadImgs[j++].src = a[i];
            }
        }
}

function getScreenCoords( obj )
{
    var src_x = src_y = 0;
    src_x = obj.offsetLeft;
    src_y = obj.offsetTop;

    while( obj.offsetParent )
        {
        obj = obj.offsetParent;
        src_x += obj.offsetLeft;
        src_y += obj.offsetTop;
        }
    return { x: src_x, y: src_y };
}

function showElem(elemId)
{
    if (dom)
        document.getElementById(elemId).style.visibility = "visible";
    else
        if (ie4)
        document.all[elemId].style.visibility = "visible";
        else
        if (nn4)
            document.layers[elemId].visibility = "show";
}

function get_scroll_x()
{
   if (ie)
      scroll_x = document.body.scrollLeft ;
   else
      scroll_x = window.pageXOffset;
}

function hideElem(elemId)
{
    if (dom)
         document.getElementById(elemId).style.visibility = "hidden";
    else
        if (ie4)
        document.all[elemId].style.visibility = "hidden";
        else
        if (nn4) document.layers[elemId].visibility = "hide";
}

function turnElementOff( elemId )
{
    var elem = getElement( elemId );
    if( elem == null )
        return true;
    if( dom || ie4 )
        elem.style.display = "none";
    else
        {
        if( nn4 )
            elem.display = "none";
        }
}

function turnElementOn( elemId )
{
    var elem = getElement( elemId );
    if( elem == null )
        return true;
    if( dom || ie4 )
        elem.style.display = "";
    else
        {
        if( nn4 )
            elem.display = "";
        }
}

function toggleElement( elemId, toggleActive )
{
    var elem = getElement( elemId );
    if( elem == null )
        return true;
    if( toggleElement.arguments.length == 1 )
        toggleActive = true;

    if( dom || ie4 )
        (elem.style.display == "none")?elem.style.display="":elem.style.display="none";
    else
        {
        if( nn4 )
            (elem.display == "none")?elem.display="":elem.display="none";
        }
    activeElem = elemId;
    return false;
}

function getElement( elemId )
{
    if (dom)
        return document.getElementById(elemId);
    else
        if (ie4)
            return document.all[elemId];
        else
            if (nn4)
                return document.layers[elemId];
    return null;
}

//--------------------------------------------------------------------------
function addModel( modelUrl )
{
    for( i=0; i<models.length; i++ )
        if( models[i] == modelUrl )
            return i;
    models[i] = new String( modelUrl );
    return i;
}

function glassColor( imageUrl, colorName )
{
    this.img = imageUrl;
    this.color = colorName;
	this.loaded = false;
}

function glasses( imageUrl, modelUrl, sexValue, costValue, acetateFrameValue, polarizedLensValue, madeInJapanValue, gradatedLensValue, UVProtectionValue, glassName, glassColors )
{
    this.img = imageUrl;
    this.model = addModel( modelUrl );
    this.sex = sexValue;
    this.cost = costValue;
    this.currColor = 0;

    this.acetateFrame = acetateFrameValue;
    this.polarizedLens = polarizedLensValue;
    this.madeInJapan = madeInJapanValue;
    this.gradatedLens = gradatedLensValue;
    this.UVProtection = UVProtectionValue;

    this.name = glassName;
    this.colors = glassColors;
}

function sc_glasses( model, image, color, quantity, price )
{
    this.model = model;
    this.image = image;
    this.color = color;
    this.quantity = quantity;
    this.price = price;
}

function fadeStack( ra )
{
    this.currStep = 1;
    if( ra )
        this.runAfter = ra;
}

function fadeStack2( targetImage, swappingImage )
{
    this.targetObj = targetImage;
    this.newUrl = swappingImage;
    this.currStep = 8;
    this.stepShift = -1;
}

function movingStack( wholeValue, shiftValue, maxStepsValue )
{
    this.shift = shiftValue;
    this.whole = wholeValue;
    this.maxSteps = maxStepsValue;
    this.currStep = 0;

    ra = '';
    var cm = findObject( 'currmodel' );
    if( cm )
        {
        eyes_path = 'img/' + models[eyes[currGlasses-1].model] +'.jpg';
        cm_path = cm.src.substr( cm.src.length - eyes_path.length );
	//alert(cm_path);
        if( cm_path != eyes_path )
            ra = "addTimer( 1, 'fadeIt2(1);', 'new fadeStack2(\"currmodel\",\"" +eyes_path+"\");', c_minimalDelay );";
        }

    var ce = findObject( 'curreyes' );
    if( ce )
        {
        img_path = 'img/l/' + eyes[currGlasses-1].colors[ eyes[currGlasses-1].currColor ].img + '.jpg';
	cm_path = cm.src.substr( cm.src.length - img_path.length );
        //alert(cm_path);
	if( cm_path != img_path )
            ra += "addTimer( 2, 'fadeIt2(2);', 'new fadeStack2(\"curreyes\",\"" +img_path+"\");', c_minimalDelay );";
        }

    ra += "fadingAfterMoving = true;";
    this.runAfter = ra;
}

function numStr( num )
{
    var str = new String( num );
    if( str.length < 2 )
        str = '0'+str;
    return str;
}

function moveIt( idx )
{
    clearTimeout( timers[idx].timer );
    currMoving = true;
    timers[idx].stack.currStep++;

    var gs = findObject( 'glassslider' );
    if( !gs )
        return;
    processShift( gs, timers[idx].stack.whole, timers[idx].stack.shift );

    if( timers[idx].stack.currStep > timers[idx].stack.maxSteps )
        {
        ra = timers[idx].stack.runAfter;
        timers[idx] = null;
        eval( ra );
        currMoving = false;
        }
    else
        {
        if( timers[idx] )
            timers[idx].timer = setTimeout( timers[idx].callback, timers[idx].delay );
        }
}

function fadeIt2( idx )
{
    clearTimeout( timers[idx].timer );

    pref = '';
    if( idx == 1 || idx == 2 )
        pref = 'g';

    var fo = findObject( 'fc_'+numStr(idx) );
    if( fo )
        fo.src = 'img/flash'+pref+'_'+numStr(timers[idx].stack.currStep)+'.png';

    timers[idx].stack.currStep += timers[idx].stack.stepShift;

    if( (timers[idx].stack.currStep == 2) && (timers[idx].stack.stepShift > 0) )
        {
        var obj = findObject( timers[idx].stack.targetObj );
        if( obj )
            obj.src = timers[idx].stack.newUrl;
        }

    if( (timers[idx].stack.currStep < 2) && (timers[idx].stack.stepShift < 0) )
        {
        timers[idx].stack.stepShift = 1;
        timers[idx].timer = setTimeout( timers[idx].callback, timers[idx].delay );
        }
    else
        {
        if( (timers[idx].stack.stepShift > 0) && (timers[idx].stack.currStep > 8) )
            {
            timers[idx] = null;
            fadingAfterMoving = false;
            }
        else
            timers[idx].timer = setTimeout( timers[idx].callback, timers[idx].delay );
        }
}


function fadeIt( idx )
{
    clearTimeout( timers[idx].timer );
    timers[idx].stack.currStep++;

    pref = '';
    if( idx == 1 || idx == 2 )
        pref = 'g';

    var fo = findObject( 'fc_'+numStr(idx) );
    if( fo )
        fo.src = 'img/flash'+pref+'_'+numStr(timers[idx].stack.currStep)+'.png';

    if( timers[idx].stack.currStep >= 8 )
        {
        if( timers[idx].stack.runAfter )
            eval( timers[idx].stack.runAfter );
        timers[idx] = null;
        }
    else
        timers[idx].timer = setTimeout( timers[idx].callback, timers[idx].delay );
}


function initTimer( timerValue, callbackValue, stackValue, delayValue )
{
    this.timer = timerValue;
    this.callback = callbackValue;
    this.stack = stackValue;
    this.delay = delayValue;
}

function addTimer( idx, callback, stack_callback, delay )
{
    var stack = eval( stack_callback );
    timer = setTimeout( callback, delay );
    timers[idx] = new initTimer( timer, callback, stack, delay );
}

function initSection( sectionName, x, y )
{
    var obj = findObject( sectionName );
    if( !obj )
        return;
    obj.style.left = x;
    obj.style.top = y;
}

function setColorsPanel( idx )
{
    var obj = findObject( 'm3colors' );
    if( !obj )
        return;

    s = '';
    for( i=0; i<eyes[idx].colors.length; i++ )
        s += '<span id=' + eyes[idx].colors[i].img + ' class=' +
            ( i== eyes[idx].currColor ? 'si' : 'ni' ) + 
            ' onclick="changeColor('+i+');"' +
            ' onmouseover="this.style.cursor=\'pointer\';"' + '>'+
            eyes[idx].colors[i].color+'</span><br>';
    obj.innerHTML = s;
}

function initMainScreen()
{
    var mm = findObject( 'menucontainer' );
    var sm = findObject( 'submenucontainer' );
    var gc = findObject( 'glasscontainer' );
    var m_Width = findObject( 'm_width' );

	var agent = navigator.userAgent;
	if (agent.indexOf('Safari') != -1) {
                var d1 = document.getElementById('m2about');
                var d2 = document.getElementById('m2press');
		var d3 = document.getElementById('m4');
		var d4 = document.getElementById('m2shoppingcart');
                d1.style.top = '320px';
                d2.style.top = '308px';
		d3.style.top = '172px';
		d4.style.top = '332px';
        }

	if( !m_Width)
        	return;

    start_x = Math.round( (m_Width.clientWidth - c_mainWidth )/2);
    if (start_x<0)
        start_x=0;
    start_y = c_mainPhotosContainerPositionTop;
    initSection( 'maintable2', start_x+90, start_y );
    initSection( 'maintable', start_x+90, start_y );
    initSection( 'menucontainer', start_x, start_y+23 ); //23
    initSection( 'submenucontainer', start_x+895, start_y+23 ); //23
    initSection( 'img3', start_x+75, start_y+440 );
    initSection( 'img4', start_x+880, start_y+440);
    initSection( 'glasscontainer', start_x+90, 480);
    initSection( 'glassflash', start_x+90, 480);
    initSection( 'glassprocessor', start_x+90, 480);
    initSection( 'glasshider', start_x+90, 480);
//    initSection( 'id_error', start_x+515, 340);

/*
    initSection( 'mainphotos', start_x, start_y );
    initSection( 'shoppingcart', start_x, start_y );
    initSection( 'myaccount', start_x, start_y );
    initSection( 'assistance', start_x, start_y );
    initSection( 'about', start_x, start_y );
    initSection( 'areahider', start_x, start_y+33 );
    initSection( 'createanaccount', start_x, start_y );
    initSection( 'press', start_x, start_y );
    initSection( 'contact', start_x, start_y );
*/
    setColorsPanel( 0 );
    showElem( 'mainphotos' );

    if( !mm )
        return;
    showElem( 'menucontainer' );
    showElem( 'submenucontainer' );

}

function initShowGlassesFromCenter()
{
    var m_Width = findObject( 'm_width' );
    if( !m_Width)
        return;
    start_x = Math.round( (m_Width.clientWidth - c_mainWidth )/2);
    if (start_x<0)
        start_x=0;
    var gc = findObject( 'glasscontainer' );
    if( !gc )
        retrun;
    showElem('glasshider');

    ShowStart();
    initSection( 'glasscontainer', start_x+90, 480);
    initSection( 'glassflash', start_x+90, 480);
    initSection( 'glassprocessor', start_x+90, 480);
    initSection( 'glasshider', start_x+90, 480);
    fr_count = Math.ceil( gc.offsetWidth / c_glassWidth );
    fr_left = Math.round( fr_count / 2 );
    fr_rght = Math.floor( fr_count / 2 )+1;
    fr_curr = 2;

    fr_timeout = setTimeout( 'showGlassesFromCenter();', c_minimalDelay*32 );
 }

function ShowStart()
{
    showElem('submenuhider');
    addTimer( 1, 'fadeIt(1);', 'new fadeStack();', c_minimalDelay );
    addTimer( 2, 'fadeIt(2);', 'new fadeStack();', c_minimalDelay );
    addTimer( 3, 'fadeIt(3);', 'new fadeStack( "hideElem(\'mf\');" );', c_minimalDelay );
    
    showElem('img1');
    
    //hideElem('glasscontainer');
    return true;
}

function showGlassesFromCenter()
{
    //showElem('glasscontainer');
    hideElem('glasshider');
    fr_fromCenter = true;
    clearTimeout( fr_timeout );

    var fg = findObject( 'fg' + numStr( fr_left ) );
    if( fg )
        fg.src = 'img/flash_' + numStr( fr_curr ) + '.png';

    if( fr_left != fr_rght )
        {
        fg = findObject( 'fg' + numStr( fr_rght ) );
        if( fg )
            fg.src = 'img/flash_' + numStr( fr_curr ) + '.png';
        }

    fr_curr++;
    if( fr_curr > 8 )
        {
        fr_curr = 2;
        fr_left--;
        fr_rght++;
        }

    if( fr_left )
        fr_timeout = setTimeout( 'showGlassesFromCenter();', c_minimalDelay );
    else
        {
        fr_fromCenter = false;
        hideElem( 'glassflash' );
        showElem('img3');
        showElem('img4');
        addTimer( 4, 'fadeIt(4);', 'new fadeStack( "hideElem(\'menuhider\');" );', c_minimalDelay*2 );
        addTimer( 5, 'fadeIt(5);', 'new fadeStack( "hideElem(\'submenuhider\');" );', c_minimalDelay*2 );

//        addTimer( 1, 'fadeIt(1);', 'new fadeStack();', c_minimalDelay );
//        addTimer( 2, 'fadeIt(2);', 'new fadeStack( "showElem(\'img3\');" );', c_minimalDelay );
//        addTimer( 3, 'fadeIt(3);', 'new fadeStack( "showElem(\'img4\');" );', c_minimalDelay );
//        addTimer( 4, 'fadeIt(4);', 'new fadeStack( "showElem(\'img1\');" );', c_minimalDelay );
//        addTimer( 5, 'fadeIt(5);', 'new fadeStack();', c_minimalDelay );
//        addTimer( 6, 'fadeIt(6);', 'new fadeStack( "hideElem(\'mf\');" );', c_minimalDelay );
        }
}

function processMouseMove( e )
{
    var eve = null;
    get_scroll_x();
    if( window.event )
        eve = window.event;
    if( e )
        eve = e;
    
    mouse_x = eve.clientX + scroll_x; 
    mouse_y = eve.clientY;
}

function initModels()
{
    currGlasses = 1;
    var gc = findObject( 'glasscontainer' );
    var gs = findObject( 'glassslider' );
    if( !gc || !gs )
        return;

    init_x = - c_glassWidth*(eyes.length+1) + ( gc.clientWidth - c_glassWidth ) / 2;
    init_x -= init_x % c_glassWidth;
    gs.style.left = init_x;
}

function initDocument()
{
    	if( document.captureEvents )
        	document.captureEvents(Event.MOUSEMOVE);
    	document.onmousemove = processMouseMove;

    	initModels();   

    	initShowGlassesFromCenter();    
    	var sc;
	var agent = navigator.userAgent;
    	var scountry_x = 64, scountry_y = 150, bcountry_x = 152, bcountry_y = 179, cc_type_x = 234, cc_type_y = 65;

	if (agent.indexOf('Firefox') != -1) {
		scountry_x = 63;
                scountry_y = 138;
                bcountry_x = 151;
                bcountry_y = 179;
		cc_type_x = 233;
		cc_type_y = 71;
	} else if (agent.indexOf('Safari') != -1) {
		scountry_x = 69;
                scountry_y = 135;
                bcountry_x = 149;
                bcountry_y = 179;
		cc_type_x = 233;
		cc_type_y = 73;
	} 

	sc = new createComboBox( 'scountry_div', 'Select your country', shippingCosts, {x: scountry_x, y: scountry_y} );
	sc.onselectitem = updateShippingOptions;
	sc = new createComboBox( 'bcountry_div', 'Select your country', shippingCosts, {x: bcountry_x, y: bcountry_y} );	sc.enabled = false;
	sc = new createComboBox( 'cctype_div', '', cCards, {x: cc_type_x, y: cc_type_y} );
	sc.onselectitem = setCard;
	
	/*if (ie)
        	sc = new createComboBox( 'scountry_div', 'Select your country', shippingCosts, {x: 65, y: 150});
    	else{
	var ix = 76;
	var iy = 136;
	if (vendor.indexOf('Apple') != -1) {
 		ix = 70;
		iy = 134;
	}
       	sc = new createComboBox( 'scountry_div', 'Select your country', shippingCosts, {x: ix, y: iy});
	}
    sc.onselectitem = updateShippingOptions;
    if (ie)
        sc = new createComboBox( 'bcountry_div', 'Select your country', shippingCosts, {x: 153, y: 179} );
    else {
	var ix = 150;
	var iy = 178;
	if (vendor.indexOf('Apple') == -1) 
		ix = 147;

        sc = new createComboBox( 'bcountry_div', 'Select your country', shippingCosts, {x: ix, y: iy} );}
   	
	 sc.enabled = false;*/
}

function setCard() {
	var card = getComboByIDVal('cctype_div');
	if (card == 'MASTERCARD')
		card = 1;
 	else if (card == 'VISA')
		card = 2;
	else if (card == 'DISCOVER')
		card = 3;
	else if (card == 'AMERICAN EXPRESS')
		card = 4;
	else
		card = 0;
	selectCard(card);
}


function resizeDocument()
{
    hideElem( 'mainphotos' );
    initMainScreen();
}

//--------------------------------------------------------------------------

function calculateColor( value )
{
    var hex = value.toString( 16 );
    if( hex.length < 2 )
        hex = '0' + hex;
    return '#' + hex + hex + hex;
}

function showmenu( menu )
{
    if( currTimeout )
        clearTimeout( currTimeout );

    if( activeElem )
        {
        hideElem( activeElem );
        ae = findObject( activeElem );
        ae.style.left = (ae.offsetLeft + 30 - currStep) + 'px';
        }

    curr_menus = menu.charCodeAt( 0 );
    i = 1;
    var md = findObject( String.fromCharCode( curr_menus ) + i );
    while( md )
        {
        hideElem( String.fromCharCode( curr_menus ) + i );
        i++;
        md = findObject( String.fromCharCode( curr_menus ) + i );
        if( !md )
            {
            curr_menus++;
            i = 1;
            md = findObject( String.fromCharCode( curr_menus ) + i );
            }
        }

    activeElem = null;
    currStep = 0;

    var mc = findObject( menu );
    if( !mc )
        return;

    i = 1;
    var mi = findObject( menu + i );
    while( mi )
        {
        mi.style.color = calculateColor( 255 );
        i++;
        mi = findObject( menu + i );
        }

    mc.style.left = mc.offsetLeft - 30;
    showElem( menu );

    activeElem = menu;
    currStep = 0;
    currTimeout = setTimeout( "processMenu()", 50 );
}

function processMenu()
{
    var mc = findObject( activeElem );

    currStep++;
    mc.style.left = (mc.offsetLeft + 1) + "px"; 

    i = 1;
    var mi = findObject( activeElem + i );
    while( mi )
        {
        mi.style.color = calculateColor( Math.round( 255-currStep*4,25 ) );
        i++;
        mi = findObject( activeElem + i );
        }

    currTimeout = setTimeout( "processMenu()", c_minimalDelay );
    if( currStep > 30 )
        {
        activeElem = null;
        currStep = 0;
        clearTimeout( currTimeout );
        }
}

//--------------------------------------------------------------------------

function fillGlasses()
{
    eyes[0] = new glasses( 'fb01blk', 'h01', 1, '350.00', 1, 1, 1, 0, 0, 'FB 001', 
        new Array(
            new glassColor( 'fb01blk', 'black' ), 
            new glassColor( 'fb01pine', 'pine' ), 
            new glassColor( 'fb01red', 'red' ), 
            new glassColor( 'fb01tort', 'dark tortoise' ) 
            )
        );
    eyes[1] = new glasses( 'fb02blk', 'h02', 1, '350.00', 1, 1, 1, 0, 0, 'FB 002',
        new Array(
            new glassColor( 'fb02blk', 'black' ),
            new glassColor( 'fb02hav', 'havana' ),
            new glassColor( 'fb02pine', 'pine' ),
            new glassColor( 'fb02red', 'red' ),
            new glassColor( 'fb02mlgy', 'milky grey' )
            )
        );
    eyes[2] = new glasses( 'fb03blk', 'h03', 1, '350.00', 1, 1, 1, 0, 0, 'FB 003',
        new Array(
            new glassColor( 'fb03blk', 'black' ),
            new glassColor( 'fb03pine', 'pine' ),
            new glassColor( 'fb03grn', 'green' ),
            new glassColor( 'fb03mlgy', 'milky grey' ),
            new glassColor( 'fb03tort', 'dark tortoise' )
            )
        );
    eyes[3] = new glasses( 'fb04acorn', 'h04', 1, '350.00', 1, 1, 1, 0, 0, 'FB 004',
        new Array(
            new glassColor( 'fb04acorn', 'acorn' ),
            new glassColor( 'fb04blk', 'black' ),
            new glassColor( 'fb04red', 'red' ),
            new glassColor( 'fb04tort', 'dark tortoise' )
            )
        );
    eyes[4] = new glasses( 'fb05blk', 'h05', 1, '350.00', 1, 1, 1, 0, 0, 'FB 005',
        new Array(
            new glassColor( 'fb05blk', 'black' ),
            new glassColor( 'fb05pine', 'pine' ),
            new glassColor( 'fb05red', 'red' ),
            new glassColor( 'fb05tort', 'dark tortoise' )
            )
        );
    eyes[5] = new glasses( 'fb06acorn', 'h07', 1, '350.00', 1, 1, 1, 0, 0, 'FB 006',
        new Array(
            new glassColor( 'fb06acorn', 'acorn' ),
            new glassColor( 'fb06blk', 'black' ),
            new glassColor( 'fb06grn', 'green' ),
            new glassColor( 'fb06tort', 'dark tortoise' )
            )
        );
    eyes[6] = new glasses( 'fb09mblk', 'h_fb009', 1, '375.00', 0, 1, 1, 0, 0, 'FB 009',
        new Array(
            new glassColor( 'fb09mblk', 'matte black' ),
            new glassColor( 'fb09silv', 'silver' ),
            new glassColor( 'fb09brn', 'brown' ),
            new glassColor( 'fb09gld', 'gold' )
            )
        );
    eyes[7] = new glasses( 'fb10mblk', 'h_fb010', 1, '375.00', 0, 1, 1, 0, 0, 'FB 010',
        new Array(
            new glassColor( 'fb10mblk', 'matte black' ),
            new glassColor( 'fb10silv', 'silver' ),
            new glassColor( 'fb10brn', 'brown' ),
            new glassColor( 'fb10gld', 'gold' )
            )
        );
    eyes[8] = new glasses( 'fb11mblk', 'h_fb011', 1, '375.00', 0, 1, 1, 0, 0, 'FB 011',
        new Array(
            new glassColor( 'fb11mblk', 'matte black' ),
            new glassColor( 'fb11silv', 'silver' ),
            new glassColor( 'fb11dgrn', 'dark gun metal' ),
            new glassColor( 'fb11gld', 'gold' )
            )
        );
    eyes[9] = new glasses( 'fb12mblk', 'h_fb012', 1, '375.00', 0, 1, 1, 0, 0, 'FB 012',
        new Array(
            new glassColor( 'fb12mblk', 'matte black' ),
            new glassColor( 'fb12silv', 'silver' ),
            new glassColor( 'fb12brn', 'brown' ),
            new glassColor( 'fb12gld', 'gold' )
            )
        );
    eyes[10] = new glasses( 'fb13acorn', 'h10', 1, '350.00', 1, 1, 1, 0, 0, 'FB 013',
        new Array(
            new glassColor( 'fb13acorn', 'acorn' ),
            new glassColor( 'fb13blk', 'black' ),
            new glassColor( 'fb13grn', 'green' ),
            new glassColor( 'fb13tort', 'dark tortoise' )
            )
        );
    eyes[11] = new glasses( 'fb14acorn', 'h12', 1, '350.00', 1, 1, 1, 0, 0, 'FB 014',
        new Array(
            new glassColor( 'fb14acorn', 'acorn' ),
            new glassColor( 'fb14blk', 'black' ),
            new glassColor( 'fb14grn', 'green' ),
            new glassColor( 'fb14tort', 'dark tortoise' )
            )
        );
    eyes[12] = new glasses( 'fb15blk', 'h15', 1, '350.00', 1, 1, 1, 0, 0, 'FB 015',
        new Array(
            new glassColor( 'fb15blk', 'black' ),
            new glassColor( 'fb15mlgy', 'milky grey' ),
            new glassColor( 'fb15pine', 'pine' ),
            new glassColor( 'fb15red', 'red' )
            )
        );

    eyes[13] = new glasses( 'fb05blk', 'h06', 2, '350.00', 1, 1, 1, 0, 0, 'FB 005',
        new Array(
            new glassColor( 'fb05blk', 'black' ),
            new glassColor( 'fb05pine', 'pine' ),
            new glassColor( 'fb05red', 'red' ),
            new glassColor( 'fb05tort', 'dark tortoise' )
            )
        );
    eyes[14] = new glasses( 'fb06acorn', 'h08', 2, '350.00', 1, 1, 1, 0, 0, 'FB 006',
        new Array(
            new glassColor( 'fb06acorn', 'acorn' ),
            new glassColor( 'fb06blk', 'black' ),
            new glassColor( 'fb06grn', 'green' ),
            new glassColor( 'fb06tort', 'dark tortoise' )
            )
        );
    eyes[15] = new glasses( 'fb07acorn', 'h09', 2, '350.00', 1, 1, 1, 0, 0, 'FB 007',
        new Array(
            new glassColor( 'fb07acorn', 'acorn' ),
            new glassColor( 'fb07blk', 'black' ),
            new glassColor( 'fb07grn', 'green' ),
            new glassColor( 'fb07tort', 'dark tortoise' )
            )
        );
    eyes[16] = new glasses( 'fb11mblk', 'h_fb011m', 2, '375.00', 0, 1, 1, 0, 0, 'FB 011',
        new Array(
            new glassColor( 'fb11mblk', 'matte black' ),
            new glassColor( 'fb11silv', 'silver' ),
            new glassColor( 'fb11dgrn', 'dark gun metal' ),
            new glassColor( 'fb11gld', 'gold' )
            )
        );
    eyes[17] = new glasses( 'fb12mblk', 'h_fb012m', 2, '375.00', 0, 1, 1, 0, 0, 'FB 012',
        new Array(
            new glassColor( 'fb12mblk', 'matte black' ),
            new glassColor( 'fb12silv', 'silver' ),
            new glassColor( 'fb12brn', 'brown' ),
            new glassColor( 'fb12gld', 'gold' )
            )
        );
    eyes[18] = new glasses( 'fb13acorn', 'h11', 2, '350.00', 1, 1, 1, 0, 0, 'FB 013',
        new Array(
            new glassColor( 'fb13acorn', 'acorn' ),
            new glassColor( 'fb13blk', 'black' ),
            new glassColor( 'fb13grn', 'green' ),
            new glassColor( 'fb13tort', 'dark tortoise' )
            )
        );
    eyes[19] = new glasses( 'fb14acorn', 'h13', 2, '350.00', 1, 1, 1, 0, 0, 'FB 014',
        new Array(  new glassColor( 'fb14acorn', 'acorn' ),
            new glassColor( 'fb14blk', 'black' ),
            new glassColor( 'fb14grn', 'green' ),
            new glassColor( 'fb14tort', 'dark tortoise' )
            )
        );
}

function initGlasses()
{
    //fillGlasses();

    var gc = findObject( 'glassslider' );
    if( !gc )
        return;
        
    s1 = s2 = '<nobr>'; s3 = ''; j = 0; ip = '';
    for( i=0; i<eyes.length; i++, j++ )
        {
        s1 += '<div class=gl><img id=gl'+numStr(j+1)+' src=img/' + ip + eyes[i].img +'.png width=' + c_glassWidth + ' height=' + c_glassHeight + ' border=0></div>';
        s2 += '<img class=pngva id=fg'+numStr(j+1)+' src=img/flash_01.png width=' + c_glassWidth + ' height=' + c_glassHeight + ' border=0>';
        s3 += '<div class=gl><img id=gl'+numStr(j+1+eyes.length)+' src=img/' + ip + eyes[i].img +'.png width=' + c_glassWidth + ' height=' + c_glassHeight + ' border=0></div>';
        ip = 'p/';
        }
    s1 += s3+'</nobr>';
    s2 += '</nobr>';

    gc.innerHTML = s1;

    var gf = findObject( 'glassflash' );
    if( !gf )
        return;
    gf.innerHTML = s2;
}

function hideAll()
{
    var gi;
    for( i=0; i<eyes.length*2; i++ )
        {
        gi = findObject( 'gl' + numStr(i+1) );
        if( gi )
            {
            it = i;
            if( i >= eyes.length )
                it -= eyes.length;
            n_path = 'img/p/'+eyes[it].img+'.png';
            o_path = gi.src;
            c_path = o_path.substr( o_path.length-n_path.length );
            if( (c_path != n_path) && ((it+1) != currGlasses) )
                gi.src = n_path;
            }
        }
}

function processShift( gs, whole, shift )
{
    newPos = gs.offsetLeft + shift;
    if( shift < 0 )
        {
        if( newPos <= whole - c_glassWidth*eyes.length*2 )
            gs.style.left = (whole-c_glassWidth*eyes.length) + "px";
        else
            gs.style.left = newPos + "px";
        }
    else
        {
        if( newPos >= 0 )
            gs.style.left = -c_glassWidth*eyes.length + "px";
        else
            gs.style.left = newPos + "px";
    }
}

function processGlassesMoving()
{
    clearTimeout( movingTimeout );

    var gc = findObject( 'glasscontainer' );
    if( !gc )
        return;
    var gs = findObject( 'glassslider' );
    if( !gs )
        return;

    var m_Width = findObject( 'm_width' );
    if( !m_Width)
        return;

    start_x = Math.round( (m_Width.clientWidth - c_mainWidth )/2);
    if (start_x<0)
        start_x=0;
        
    // --- process shift of glass slider -----------------------------------
    whole = gc.offsetWidth;
    half = Math.round( whole / 2 );
    c_half = Math.round( c_mainPhotosContainerWidth / 2 + start_x + 90);
    gl_half = Math.round( c_glassWidth / 2 );

    mx = Math.abs( mouse_x - c_half );

    if( ( mx <= c_half ) && ( mx >= gl_half ) && !fr_fromCenter && !currMoving )
        {
        if( mouse_x > c_half )
            shift = -Math.max( 1, Math.round( (mouse_x - c_half) / 100 ) );
        else
            shift = Math.max( 1, Math.round( (c_half - mouse_x) / 100 ) );

        processShift( gs, whole, shift );
        }

    // --- process active glasses under mouse ---------------------------
    if( !fr_fromCenter && !currMoving )
        {
        curr_idx = Math.ceil((Math.abs(gs.offsetLeft)+mouse_x-start_x-90)/c_glassWidth);
        if( curr_idx > eyes.length )
            curr_idx -= eyes.length;

        var gi;
        for( i=0; i<eyes.length*2; i++ )
            {
            gi = findObject( 'gl' + numStr(i+1) );
            if( gi )
                {
                it = i;
                if( i >= eyes.length )
                    it -= eyes.length;
                a_pass = 'img/'+eyes[it].img+'.png';
                p_pass = 'img/p/'+eyes[it].img+'.png';
                o_path = gi.src;
                if( (curr_idx == (i+1)) || ((curr_idx+eyes.length) == (i+1)) || (currGlasses == (it+1)) )
                    n_path = a_pass;
                else
                    n_path = p_pass;
                c_path = o_path.substr( o_path.length-n_path.length );
                if( c_path != n_path )
                    gi.src = n_path;
                }
            }
        }

    // window.status = mouse_x + ','+mouse_y+','+curr_idx+','+n_path;
    
    movingTimeout = setTimeout( 'processGlassesMoving();', c_minimalDelay );
}

function initGlassesMoving()
{
    movingTimeout = setTimeout( 'processGlassesMoving();', c_minimalDelay );
}

function doneGlassesMoving()
{
    clearTimeout( movingTimeout );
    if( !fr_fromCenter && !currMoving )
        hideAll();
    movingTimeout = null;
}

function setGlassPanel( newGlass )
{
    var obj;

    setColorsPanel( newGlass-1 );

    obj = findObject( 'mw' );
    if( obj )
        obj.className = ( eyes[newGlass-1].sex == 1 ) ? 'si' : 'ni';
    obj = findObject( 'mm' );
    if( obj )
        obj.className = ( eyes[newGlass-1].sex == 2 ) ? 'si' : 'ni';

    obj = findObject( 'ccost' );
    if( obj )
        obj.innerHTML = '$' + eyes[newGlass-1].cost;
    

    obj = findObject( 'm5' );
    if( !obj )
        return;
    s = '<span class=si>'+eyes[newGlass-1].name+'</span><br>';
    s += eyes[newGlass-1].acetateFrame ? 'Acetate Frame<br>' : 'Metal Frame<br>';
    s += eyes[newGlass-1].polarizedLens ? 'Polarized Lens<br>' : '';
    s += eyes[newGlass-1].madeInJapan ? 'Handmade in Japan<br>' : '';
    s += eyes[newGlass-1].gradatedLens ? 'Gradated Lens<br>' : '';
    s += eyes[newGlass-1].UVProtection ? 'UV Protection<br>' : '';
    obj.innerHTML = s;
}

function selectGlasses()
{
    if( fadingAfterMoving )
        return;

    var gs = findObject( 'glassslider' );
    if( !gs )
        return;


    var m_Width = findObject( 'm_width' );
    if( !m_Width)
        return;

    start_x = Math.round( (m_Width.clientWidth - c_mainWidth )/2);
    if (start_x<0)
        start_x=0;
    c_half = Math.round( c_mainPhotosContainerWidth / 2 + start_x + 90);
        
        
    curr_idx = Math.ceil((Math.abs(gs.offsetLeft)+mouse_x-start_x-90)/c_glassWidth);
    if( curr_idx > eyes.length )
        curr_idx -= eyes.length;

    if( currGlasses == curr_idx )
        return;

	var col = eyes[curr_idx-1].colors;
	for( j=0; j<col.length; j++ )
		{
		if( !col[j].loaded )
			{
			preloadImages( 'img/l/'+col[j].img+'.jpg' );
			col[j].loaded = true;
			}
		}

    setGlassPanel( curr_idx );
    currGlasses = curr_idx;

    var gc = findObject( 'glasscontainer' );
    if( !gc )
        return;

    half = Math.round( gc.offsetWidth / 2 );
    mx = Math.abs( mouse_x - c_half );

    maxSteps = Math.ceil( mx/100 )*10;

    if( mouse_x > c_half )
        shift = -Math.max( 1, Math.round( (mouse_x - c_half) / maxSteps ) );
    else
        shift = Math.max( 1, Math.round( (c_half - mouse_x) / maxSteps ) );

    hideAll();
    addTimer( 1, 'moveIt(1);', 'new movingStack('+gc.offsetWidth+','+shift+','+maxSteps+');' );
}

function showMoney( cost )
{
    var rs = '$' + Math.floor( cost ) + '.';
    dec = Math.round( (cost-Math.floor(cost))*100 );
    rs += dec.toString();
    if( dec.toString().length < 2 )
        rs += '0';
    return rs;
}

function deleteFromShoppingCart( idx )
{
    if( scart[idx].quantity > 1 )
        scart[idx].quantity--;
    else
        {
        for( i=idx; i<scart.length-1; i++ )
            scart[ i ] = scart[ i+1 ];
        scart.pop();
        }
    prepareShoppingCart( true );
}

function AddQuantity( idx )
{
    scart[idx].quantity++;
    prepareShoppingCart( true );
}

function prepareShoppingCart( updatePos )
{
    var sc = '<table class=sci width=100% cellpadding=0 cellspacing=0 border=0>';
    totalPrice = 0;
    for( i=0; i<scart.length; i++ )
        {
        sc += '<tr><td width=12%>' + scart[i].model + '</td>';
        sc += '<td width=100><img src=' + scart[i].image + ' width=100 height=60 border=0></td>';
        sc += '<td width=16% align=center>' + scart[i].color + '</td>';
        sc += '<td width=12% align=center>' + scart[i].quantity + '</td>';
        sc += '<td width=120 align=center style="padding-right: 10px;"><span class=h1 style=\'cursor: pointer;\' onclick="AddQuantity(' + i + '); return false;">ADD</span>&nbsp;&nbsp;<span class=h1 style=\'cursor: pointer;\' onclick="deleteFromShoppingCart(' + i + '); return false;">REMOVE</span></td>';
            //<img onmouseover="this.style.cursor=\'pointer\';" onclick="deleteFromShoppingCart(' + i + ');" src=img/scdel.gif width=16 height=16 border=0></td>';
        sc += '<td width=15%>' + showMoney( scart[i].price ) +'</td></tr>';
        totalPrice += scart[i].quantity*scart[i].price;
        }
    sc += '</table>';

    var obj = findObject( 'sc_slider' );
    if( obj )
        {
        obj.innerHTML = sc;
        if( updatePos )
            obj.style.top = 0;
        }

    obj = findObject( 'dtp' );
    if( obj )
        obj.innerHTML = showMoney( totalPrice );

    obj = findObject( 'sc_container' );
    if( !obj )
        return;

    if( i*63 > obj.offsetHeight )
        {
        var scrl = findObject( 'scup' );
        if( scrl )
            {
            scrl.style.left = obj.offsetWidth-15;
            scrl.style.top = 25;
            }
        scrl = findObject( 'scdn' );
        if( scrl )
            {
            scrl.style.left = obj.offsetWidth-15;
            scrl.style.top = obj.offsetHeight;
            }
        showElem( 'scup' );
        showElem( 'scdn' );
        }
    else
        {
        hideElem( 'scup' );
        hideElem( 'scdn' );
        }
}

function addToShoppingCart()
{
    model = eyes[currGlasses-1].name;
    color = eyes[currGlasses-1].colors[ eyes[currGlasses-1].currColor ].color;
    found = false;
    for( i=0; i<scart.length && !found; i++ )
        if( scart[i].model == model && scart[i].color == color )
            {
            scart[i].quantity++;
            found = true;
            }
    if( !found )
        scart[ scart.length ] = new sc_glasses( model, 
            'img/sc/'+eyes[currGlasses-1].colors[eyes[currGlasses-1].currColor].img+'.png', color, 1, 
            eyes[currGlasses-1].cost );
        
	prepareShoppingCart();
	currPurchaseStep = 0;
    var o;
    for( i=2; i<=7; i++ )
        {
        o = findObject( 'sc_cart_0'+i );
        if( o )
            o.style.left = 582;
        }
    o = findObject( 'sc_cart_01' );
	if( o )
        	o.style.left = 0;
    showElem( 'sc_cart_01' );
    
    switchPanel( 'shoppingcart' );
}

function hi( what, suffix, ext )
{
    if( arguments.length<2 )
        suffix = '_img';
    if( !ext )
        ext = 'gif';
    var obj = findObject( what+suffix );
    if( !obj )
        return;
    obj.src = 'img/'+what+'_a.'+ext;
}

function lo( what, suffix, ext )
{
    if( arguments.length<2 )
        suffix = '_img';
    if( !ext )
        ext = 'gif';
    var obj = findObject( what+suffix );
    if( !obj )
        return;
    obj.src = 'img/'+what+'.'+ext;
}

var scrl_shift;
var scrl_timeout = null;
var scrl_timeout_stop = null;

function scroll_sc( shift, prefix )
{
    if( shift > 0 )
        hi( 'sc'+prefix+'dn' );
    else
        hi( 'sc'+prefix+'up' );
    scrl_shift = shift;
    scrl_timeout = setTimeout( 'scrollIt("'+prefix+'");', c_minimalDelay );
}

function scroll_ab( shift, prefix )
{
    if( shift > 0 )
        hi( 'ab'+prefix+'dn' );
    else
        hi( 'ab'+prefix+'up' );
    scrl_shift = shift;
    scrl_timeout = setTimeout( 'scrollIt2("'+prefix+'");', c_minimalDelay );
}


function scroll_ab_to( to, prefix )
{
    if( scrl_timeout )
        clearTimeout( scrl_timeout );
    var objContainer = findObject( 'ab'+prefix+'_container' );
    var objSlider = findObject( 'ab'+prefix+'_slider' );
    if( !objContainer || !objSlider )
        return;
    if (to>0) 
    {
        scrl_shift=28;
        curr_y = objSlider.offsetTop + to;
        if (curr_y==0){
            return;
	}
    }
    else
        scrl_shift = -28;
        
    curr_y = objSlider.offsetTop + to;

    if (Math.abs(curr_y)==14)
        scrl_shift = scrl_shift/2; 
    curr_y = objSlider.offsetTop - scrl_shift;   
    if( (-curr_y + objContainer.offsetHeight <= objSlider.offsetHeight) && curr_y <= 0)
    {
        curr_y = objSlider.offsetTop - scrl_shift;
	objSlider.style.top = curr_y+'px';   
        /* alert ("oTop=" + objSlider.offsetTop + ", top=" + objSlider.style.top + ", to=" + to + ", contH=" + objContainer.offsetHeight + ", slidH=" +
 objSlider.offsetHeight + ", curr_y=" + curr_y); */
        scrl_timeout = setTimeout( 'scroll_ab_to(' + to + ',"' + prefix + '");', c_minimalDelay );
    }
}

function scrollIt( prefix )
{
    if( scrl_timeout )
        clearTimeout( scrl_timeout );

    var sc = findObject( 'sc'+prefix+'_container' );
    var ss = findObject( 'sc'+prefix+'_slider' );
    if( !sc || !ss )
        return;

    curr_y = ss.offsetTop - scrl_shift;
    if( (-curr_y + sc.offsetHeight <= ss.offsetHeight) && curr_y <= 0 )
        ss.style.top = curr_y+'px';

    scrl_timeout = setTimeout( 'scrollIt("'+prefix+'");', c_minimalDelay ); 
}

function scrollIt2( prefix )
{
    if( scrl_timeout )
        clearTimeout( scrl_timeout );

    var objContainer = findObject( 'ab'+prefix+'_container' );
    var objSlider = findObject( 'ab'+prefix+'_slider' );
    if( !objContainer || !objSlider )
        return;

    curr_y = objSlider.offsetTop - scrl_shift;
    if( (-curr_y + objContainer.offsetHeight <= objSlider.offsetHeight) && curr_y <= 0 )
        objSlider.style.top = curr_y+'px';

    scrl_timeout = setTimeout( 'scrollIt2("'+prefix+'");', c_minimalDelay ); 
}

function stop_scroll_ab( shift, prefix )
{
    clearTimeout(scrl_timeout);
    if( scrl_timeout_stop )
        clearTimeout( scrl_timeout_stop );    
    var objContainer = findObject( 'ab'+prefix+'_container' );
    var objSlider = findObject( 'ab'+prefix+'_slider' );
    if( !objContainer || !objSlider )
        return;
        
    curr_y = (objSlider.offsetTop % 14);
    if (curr_y<0)
    {
        curr_y = objSlider.offsetTop - scrl_shift;
        if( (-curr_y + objContainer.offsetHeight <= objSlider.offsetHeight) && curr_y <= 0 )
            objSlider.style.top = curr_y+'px';   
        scrl_shift = shift;
        scrl_timeout_stop = setTimeout( 'stop_scroll_ab(' + shift + ',"' + prefix + '");', c_minimalDelay );
    }
    //alert (objSlider.offsetTop + " " + curr_y);

}

var currPage = 'mainphotos';

function fadePanelStack( targetPanel, sourcePanel, hiderPanel, ra )
{
    this.targetPnl = targetPanel;
    this.sourcePnl = sourcePanel;
    this.hiderPnl = hiderPanel;
    this.currStep = 8;
    this.stepShift = -1;
    if( ra )
        this.runAfter = ra;
}

function showPanelStack( targetPanel, initialStep, step, ra )
{
    this.targetPnl = targetPanel;
    this.currStep = initialStep;
    this.stepShift = step;
    if( ra )
        this.runAfter = ra;
}

function showPanel( idx )
{
    clearTimeout( timers[idx].timer );
    timers[idx].stack.currStep += timers[idx].stack.stepShift;

    pref = '';
    if( idx == 1 )
        pref = 'g';

    var fo = findObject( timers[idx].stack.targetPnl+'_img' );
    if( fo )
        fo.src = 'img/flash'+pref+'_'+numStr(timers[idx].stack.currStep)+'.png';

    if( (timers[idx].stack.currStep == 8) && (timers[idx].stack.stepShift > 0) )
        hideElem( timers[idx].stack.targetPnl );

    if( ( (timers[idx].stack.currStep == 8) && (timers[idx].stack.stepShift > 0) ) ||
        ( (timers[idx].stack.currStep == 1) && (timers[idx].stack.stepShift < 0) ) )
        {
        if( timers[idx].stack.runAfter )
            eval( timers[idx].stack.runAfter );
        timers[idx] = null;
        }
    else
        timers[idx].timer = setTimeout( timers[idx].callback, timers[idx].delay );
}

function fadePanel( idx )
{
    clearTimeout( timers[idx].timer );

    if( (timers[idx].stack.currStep == 1) && (timers[idx].stack.stepShift > 0) )
        {
        showElem( timers[idx].stack.targetPnl );
        hideElem( timers[idx].stack.sourcePnl );
        }

    pref = '';
    if( idx == 1 )
        pref = 'g';

    var fo = findObject( timers[idx].stack.hiderPnl+'_img' );
    if( fo )
        fo.src = 'img/flash'+pref+'_'+numStr(timers[idx].stack.currStep)+'.png';
    timers[idx].stack.currStep += timers[idx].stack.stepShift;

    if( (timers[idx].stack.currStep < 2) && (timers[idx].stack.stepShift < 0) )
        {
        timers[idx].stack.stepShift = 1;
        timers[idx].timer = setTimeout( timers[idx].callback, timers[idx].delay );
        }
    else
        {
        if( (timers[idx].stack.stepShift > 0) && (timers[idx].stack.currStep > 8) )
            {
            if( timers[idx].stack.runAfter )
                eval( timers[idx].stack.runAfter );
            timers[idx] = null;
            }
        else
            timers[idx].timer = setTimeout( timers[idx].callback, timers[idx].delay );
        }

}

function switchPanel( newPage )
{
    fadingAfterMoving = true;
    if( newPage == currPage )
        return;

    var omi = findObject( 'mm_'+currPage );
    var nmi = findObject( 'mm_'+newPage );
    if( omi )
    {
        omi.className = 'ni';
        var omi2 = findObject ( 'mm_'+currPage + '2');
        omi2.className = 'ni';
    }
    if( nmi )
    {
        nmi.className = 'si';
        var nmi2 = findObject ( 'mm_'+newPage + '2');
        nmi2.className = 'si';
    }
    hideElem( 'searchnotfound' );
    hideElem('id_error');
    showElem( 'areahider' );
    showElem( 'submenuhider' );
    addTimer( 1, 'fadePanel(1);', 'new fadePanelStack("'+newPage+'","'+currPage+'","areahider","hideElem(\'areahider\');");', c_minimalDelay );
    addTimer( 2, 'fadePanel(2);', 'new fadePanelStack("m2'+newPage+'","m2'+currPage+'","submenuhider","hideElem(\'submenuhider\');");', c_minimalDelay );


    if( (newPage != 'mainphotos') && (currPage == 'mainphotos') ) 
        {
        
           showElem( 'glasshider' );
           addTimer( 3, 'showPanel(3);', 'new showPanelStack("glasshider",8,-1);', c_minimalDelay );
           showElem( 'mf' );
           addTimer( 4, 'showPanel(4);', 'new showPanelStack("mf",8,-1);', c_minimalDelay );
           hideElem('m1');
           showElem( 'm1_dubl' );
           hideElem('img1');
           hideElem('img3');
           hideElem('img4');
        }
    else
        {
        if( newPage == 'mainphotos' )
            {
              addTimer( 3, 'showPanel(3);', 'new showPanelStack("glasshider",1,1);', c_minimalDelay );
              addTimer( 4, 'showPanel(4);', 'new showPanelStack("mf",1,1,"fadingAfterMoving = false;");', c_minimalDelay );
              hideElem ('m1_dubl');
              showElem( 'm1' );
              showElem( 'img1' );
              showElem( 'img3' );
              showElem( 'img4' );
            }
        }
    /*if (currPage=='shoppingcart')
    {
        hideElem('navSelect');
    }
    else
    {
        if ((newPage=='shoppingcart') && (scart.length>0))
            showElem('navSelect');
    }*/        
    currPage = newPage;
}

var currTextPage = 1;
var newTextPage = 0;
var slidingShift = slidingStep = 0;
var slidingTimeout = null;
var sliding = false;

function nextPage( shift )
{
    if( sliding )
        return false;
    newTextPage = currTextPage + shift;
    if( newTextPage < 1 || newTextPage > 3 )
        return;

    slidingShift = 31*shift;
    slidingStep = 0;
    sliding = true;
    slidingTimeout = setTimeout( 'slideText();', c_minimalDelay );
}

function slideText()
{
    clearTimeout( slidingTimeout );
    var p1 = findObject( 'p'+currTextPage );
    var p2 = findObject( 'p'+newTextPage );
    if( !p1 || !p2 )
        return;
    x1 = p1.offsetLeft;
    p1.style.left = x1 - slidingShift;
    x2 = p2.offsetLeft;
    p2.style.left = x2 - slidingShift;
    slidingStep++;
    if( slidingStep < 20 )
        setTimeout( 'slideText();', c_minimalDelay );
    else
        {
        currTextPage = newTextPage;
        sliding = false;
        }
}


var assPages = new Array( 
    { pages: 13, sufix: 'faq', prefix: 'faq_', currTextPage: 1, newTextPage: 0, slidingShift: 0, slidingStep: 0, slidingTimeout: null, sliding: false },
    { pages: 8, sufix: 'terms', prefix: 'terms_', currTextPage: 1, newTextPage: 0, slidingShift: 0, slidingStep: 0, slidingTimeout: null, sliding: false },
    { pages: 7, sufix: 'privacy', prefix: 'privacy_', currTextPage: 1, newTextPage: 0, slidingShift: 0, slidingStep: 0, slidingTimeout: null, sliding: false }
    );
var currAssPage = 0;

function nextAssPage( shift )
{
    if( assPages[ currAssPage ].sliding || !shift )
        return false;
    assPages[ currAssPage ].newTextPage = assPages[ currAssPage ].currTextPage + shift;
    if( assPages[ currAssPage ].newTextPage < 1 || assPages[ currAssPage ].newTextPage > assPages[ currAssPage ].pages )
        return;

    sh = 1;
    if( shift < 0 )
        sh = -sh;

    var p2 = findObject( assPages[ currAssPage ].prefix + 'p' + assPages[ currAssPage ].newTextPage );
    if( (shift > 0) && p2 )
        p2.style.left = 620;
    if( (shift < 0) && p2 )
        p2.style.left = -620;    

    assPages[ currAssPage ].slidingShift = 31*sh;
    assPages[ currAssPage ].slidingStep = 0;
    assPages[ currAssPage ].sliding = true;
    assPages[ currAssPage ].slidingTimeout = setTimeout( 'slideAssText();', c_minimalDelay );
}

function slideAssText()
{
    clearTimeout( assPages[ currAssPage ].slidingTimeout );
    var p1 = findObject( assPages[ currAssPage ].prefix + 'p' + assPages[ currAssPage ].currTextPage );
    var p2 = findObject( assPages[ currAssPage ].prefix + 'p' + assPages[ currAssPage ].newTextPage );
    if( !p1 || !p2 )
        return;
    x1 = p1.offsetLeft;
    p1.style.left = x1 - assPages[ currAssPage ].slidingShift;
    x2 = p2.offsetLeft;
    p2.style.left = x2 - assPages[ currAssPage ].slidingShift;
    assPages[ currAssPage ].slidingStep++;
    if( assPages[ currAssPage ].slidingStep < 20 )
        setTimeout( 'slideAssText();', c_minimalDelay );
    else
        {
        assPages[ currAssPage ].currTextPage = assPages[ currAssPage ].newTextPage;
        assPages[ currAssPage ].sliding = false;
        }
}

function switchSex( newSex )
{
    if( newSex == eyes[currGlasses-1].sex )
        return;
    var obj = findObject( 'mw' );
    if( obj )
        obj.className = (newSex == 1 ) ? 'si' : 'ni';
    obj = findObject( 'mm' );
    if( obj )
        obj.className = (newSex == 2 ) ? 'si' : 'ni';

    newIdx = (newSex == 1) ? c_firstWomenFrame : c_firstMenFrame;

    turnActiveOnScroller( currGlasses, false );
    currGlasses = newIdx;
    setGlassPanel( currGlasses );
    turnActiveOnScroller( currGlasses, true );

    var gc = findObject( 'glasscontainer' );
    var gs = findObject( 'glassslider' );
    if( !gc || !gs )
        return;

    addon = currGlasses-1;
    if( addon*c_glassWidth + gc.clientWidth < gs.clientWidth )
        addon += eyes.length;
    to_x = - c_glassWidth*addon + ( gc.clientWidth - c_glassWidth ) / 2;
    to_x -= init_x % c_glassWidth;
    if( -to_x > c_glassWidth*eyes.length )
        to_x += c_glassWidth*eyes.length;

    shift = 15;
    maxSteps = Math.abs( Math.round( (gs.offsetLeft - to_x)/shift ) );
    shift = ( gs.offsetLeft < to_x ) ? shift : -shift;

    hideAll();
    addTimer( 1, 'moveIt(1);', 'new movingStack('+gc.offsetWidth+','+shift+','+maxSteps+');' );
}

// Function for moving on arrows and respective displacement in slider glasses 
function moveGlass(moveStep)
{
if( fadingAfterMoving )
        return;

    var gs = findObject( 'glassslider' );
    if( !gs )
        return;

    var m_Width = findObject( 'm_width' );
    if( !m_Width)
        return;

    start_x = Math.round( (m_Width.clientWidth - c_mainWidth )/2);
    if (start_x<0)
        start_x=0;
    c_half = Math.round( c_mainPhotosContainerWidth / 2 + start_x + 90);
    //alert (start_x +"<-start_x + c_half->" + c_half);
    curr_idx = currGlasses + moveStep;
    if( curr_idx > eyes.length )
        curr_idx -= eyes.length;
       
    if( curr_idx == 0)
        curr_idx = eyes.length;
	var col = eyes[curr_idx-1].colors;
	for( j=0; j<col.length; j++ )
		{
		if( !col[j].loaded )
			{
			preloadImages( 'img/l/'+col[j].img+'.jpg' );
			col[j].loaded = true;
			}
		}
	turnActiveOnScroller( currGlasses, false );	
    currGlasses = curr_idx;
    setGlassPanel( currGlasses );
    turnActiveOnScroller( currGlasses, true );
    var gc = findObject( 'glasscontainer' );
    if( !gc )
        return;
        
    half = Math.round( gc.offsetWidth / 2 );
    
    var pmouse_x = half + c_glassWidth*moveStep;
    
    //alert ("half:" + half + " pmouse_x:" + pmouse_x + " mouse_x:" + mouse_x);
    mx = Math.abs( pmouse_x  - half );
    maxSteps = 10;//Math.ceil( mx/100 )*10;

    if( pmouse_x > half )
        shift = -9
    else
        shift = 9;

    hideAll();
    //alert ("gc.offsetWidth:" + gc.offsetWidth + " shift:" + shift + " maxSteps:" + maxSteps);
    addTimer( 1, 'moveIt(1);', 'new movingStack('+gc.offsetWidth+','+shift+','+maxSteps+');' );
 }

function changeColor( newColor )
{
    var obj;
    if( newColor == eyes[currGlasses-1].currColor )
        return;

    obj = findObject( eyes[currGlasses-1].colors[ eyes[currGlasses-1].currColor ].img );
    if( obj )
        obj.className = 'ni';

    obj = findObject( eyes[currGlasses-1].colors[ newColor ].img );
    if( obj )
        obj.className = 'si';

    eyes[currGlasses-1].currColor = newColor;

    var ce = findObject( 'curreyes' );
    if( ce )
        {
        img_path = 'img/l/' + eyes[currGlasses-1].colors[ eyes[currGlasses-1].currColor ].img + '.jpg';
        addTimer( 2, 'fadeIt2(2);', 'new fadeStack2("curreyes",\"" +img_path+"\");', c_minimalDelay );
        fadingAfterMoving = true;
        }
}

function turnActiveOnScroller( idx, active )
{
    prefix = active ? '' : 'p/';
    var gl01 = findObject( 'gl'+numStr(idx) );
    var gl02 = findObject( 'gl'+numStr(idx+eyes.length) );
    if( gl01 && gl02 )
        {
        gl01.src = 'img/'+prefix+eyes[idx-1].img+'.png';
        gl02.src = 'img/'+prefix+eyes[idx-1].img+'.png';
        }
}

function searchByStyleNumber()
{
    found = -1;
    var obj = findObject( 'stylenumber' );
    if( !obj || obj.value == '' )
        return;

    searchString = '';
    onlynumbers = true;
    for( i=0; i<obj.value.length; i++ )
        if( !(obj.value.charAt( i ) >= '0' && obj.value.charAt( i ) <= '9') )
            onlynumbers = false;

    if( onlynumbers )
        {
        searchString = 'fb';
        for( i=0; i<3-obj.value.length; i++)
            searchString += '0';
        searchString += obj.value;
        }
    else
        {
        for( i=0; i<obj.value.length; i++ )
            if( obj.value.charAt(i) != ' ' )
                searchString += obj.value.charAt(i);
        searchString = searchString.toLowerCase();
        if( searchString.substr( 0, 2 ) == 'fb' )
            {
            numbers = searchString.substr( 2 );
            
            onlynumbers = true;
            for( i=0; i<numbers.length; i++ )
                if( !(numbers.charAt( i ) >= '0' && numbers.charAt( i ) <= '9') )
                    onlynumbers = false;
            if( onlynumbers )
                {
                searchString = 'fb';
                for( i=0; i<3-numbers.length; i++)
                    searchString += '0';
                searchString += numbers;
                }    
            }
        }

    for( i=0; i<styles.length; i++ )
        if( styles[i] == searchString )
            {
            found = i;
            break;
            }

    if( found == -1 )
        {
        showElem( 'searchnotfound' );
        return;
        }

    hideElem( 'searchnotfound' );
    var curr_eyes = findObject( 'curreyes' );
    var curr_model = findObject( 'currmodel' );
    if( !curr_eyes || !curr_model )
        return;

    turnActiveOnScroller( currGlasses, false );

    currGlasses = found+1;
    curr_eyes.src = 'img/l/'+eyes[found].colors[ eyes[found].currColor ].img+'.jpg';
    curr_model.src = 'img/'+models[found]+'.jpg';
    setGlassPanel( currGlasses );

    turnActiveOnScroller( currGlasses, true );

    var gc = findObject( 'glasscontainer' );
    var gs = findObject( 'glassslider' );
    if( !gc || !gs )
        return;

    addon = currGlasses;
    if( addon*c_glassWidth + gc.clientWidth < gs.clientWidth )
        addon += eyes.length;
    init_x = - c_glassWidth*addon + ( gc.clientWidth - c_glassWidth ) / 2;
    init_x -= init_x % c_glassWidth;

    if( -init_x > c_glassWidth*eyes.length )
        init_x += c_glassWidth*eyes.length;

    gs.style.left = init_x + c_glassWidth / 2;

    switchPanel( 'mainphotos' );
}

var currAbout = 1;
var aboutSlidingStep = 19;
var aboutSlidingTimer = null;
var currAboutSlidingStep = 0;

function switchAbout( what )
{
    if( currAboutSlidingStep > 0 || currAbout == what )
        return;
    
    var o1 = findObject( 'aboutfb' );
    var o2 = findObject( 'aboutfbeyewear' );
    if( !o1 || !o2 )
        return;
    o1.className = ( what == 1 ) ? 'ni' : 'si';
    o2.className = ( what == 1 ) ? 'si' : 'ni';
    currAbout = what;
    currAboutSlidingStep = 0;
    setTimeout( 'slideAboutSection();', c_minimalDelay );
}

function slideAboutSection()
{
    clearTimeout( aboutSlidingTimer );
    var o1 = findObject( 'p_aboutfb' );
    var o2 = findObject( 'p_aboutfbeyewear' );
    if( !o1 || !o2 )
        return;
    y1 = o1.offsetTop+aboutSlidingStep*currAbout;
    y2 = o2.offsetTop+aboutSlidingStep*currAbout;
    o1.style.top = y1;
    o2.style.top = y2;
    currAboutSlidingStep++;
    if( currAboutSlidingStep >= 20 )
        currAboutSlidingStep = 0;
    else
        setTimeout( 'slideAboutSection();', c_minimalDelay );        
}

var assistanceSlidingStep = 19;
var assistanceSlidingTimer = null;
var currAssistanceSlidingStep = prevAssPage = 0;
var assistanceDir = 1;

function switchAssistance( what )
{
    if( currAssistanceSlidingStep > 0 || currAssPage == what )
        return;
    
    var o1 = findObject( 'faq' );
    var o2 = findObject( 'terms' );
    var o3 = findObject( 'privacy' );
    if( !o1 || !o2 || !o3 )
        return;
    o1.className = ( what == 0 ) ? 'si' : 'ni';
    o2.className = ( what == 1 ) ? 'si' : 'ni';
    o3.className = ( what == 2 ) ? 'si' : 'ni';
    prevAssPage = currAssPage;
    currAssPage = what;
    if( prevAssPage > currAssPage )
        assistanceDir = 1;
    else
        assistanceDir = -1;
    currAssistanceSlidingStep = 0;

    var o1 = findObject( 'p_'+assPages[prevAssPage].sufix );
    var o2 = findObject( 'p_'+assPages[currAssPage].sufix );
    if( !o1 || !o2 )
        return;
    o1.style.top = 0;
    if( assistanceDir < 0 )
        o2.style.top = 381;
    else
        o2.style.top = -381;

    setTimeout( 'slideAssistanceSection();', c_minimalDelay );
}

function slideAssistanceSection()
{
    clearTimeout( assistanceSlidingTimer );
    var o1 = findObject( 'p_'+assPages[prevAssPage].sufix );
    var o2 = findObject( 'p_'+assPages[currAssPage].sufix );
    if( !o1 || !o2 )
        return;
    y1 = o1.offsetTop+assistanceSlidingStep*assistanceDir;
    y2 = o2.offsetTop+assistanceSlidingStep*assistanceDir;
    o1.style.top = y1;
    o2.style.top = y2;
    currAssistanceSlidingStep++;
    if( currAssistanceSlidingStep >= 20 )
        currAssistanceSlidingStep = 0;
    else
        assistanceSlidingTimer = setTimeout( 'slideAssistanceSection();', c_minimalDelay );        
}


var currLoadingPercentage = 0;
var readyToStart = false;

function startPreloading()
{
	preloadImages( 
	    'img/flash_01.png', 'img/flash_02.png', 'img/flash_03.png',
	    'img/flash_04.png', 'img/flash_05.png', 'img/flash_06.png',
	    'img/flash_07.png', 'img/flash_08.png', 'img/flashg_01.png', 
	    'img/flashg_02.png', 'img/flashg_03.png', 'img/prev.gif',
	    'img/flashg_04.png', 'img/flashg_05.png', 'img/flashg_06.png',
	    'img/flashg_07.png', 'img/flashg_08.png', 'img/next.gif' );

	fillGlasses();

	for( i=0; i<eyes.length; i++ )
		{
		preloadImages( 'img/l/'+eyes[i].img+'.jpg' );
		if( !i )
			{
			for( j=1; j<eyes[i].colors.length; j++ )
				{
				preloadImages( 'img/l/'+eyes[i].colors[j].img+'.jpg' );
				eyes[i].colors[j].loaded = true;
				}
			}
		preloadImages( 'img/'+eyes[i].img+'.png' );
		preloadImages( 'img/'+models[ eyes[i].model ]+'.jpg' );
		eyes[i].colors[0].loaded = true;
		}
}

startPreloading();
