//core.js -- © 2010 Marc-Emmanuel Ramage & NVN STUDIOS
/* NOTICE : This JS file is the core JS for francoisjosephgraf.com website. */
//navigator section
var versiondev = 11;
var isIE = jCalix_checkNav('msie');
var isIE6 = jCalix_checkNav('msie 6');
var isIE7 = jCalix_checkNav('msie 7');
var isIE8 = jCalix_checkNav('msie 8');
var isChrome = jCalix_checkNav('chrome');
var isSafari = jCalix_checkNav('safari');
var isIphone = jCalix_checkNav('iphone');
var isChromeMac = function() {
    var mac = jCalix_checkNav('macintosh');
    var chrome = jCalix_checkNav('chrome');
    if (mac != 0 && chrome != 0)
    return true;
    else
    return false;
}

// end navigator section
// var section
var line1;
var line2;
var lis;
var loadindex = 0;
var slideindex = 0;
var slidepx = 0;
var imgindex = 0;
var enableclick = true;
var imgsloaded = false;
var slidewidth = 720;
var sliding = false;
var changeslide = false;
var reseting = false;
var sortednav;
var prevslide;
var prevz;
var objects;

//end var section
// jQuery section
function fadeinObject(obj, time) {
    var t = 500;
    if (time != null)
    t = time;
    $(obj).animate({
        opacity: 1
    },
    t);
}


function fadeoutObject(obj, time) {
    var op = 0;
    if (isIE)
    op = 0.01;
    var t = 200;
    if (time != null)
    t = time;
    $(obj).animate({
        opacity: op
    },
    t);
}


function fadeImages(transition) {
    var t = 130;   
    if (transition)
    t = transition;
    if (_$('#ajaxcontainer').getObject() == null)
    return false;
    var ulimgs = _$('.ulimgs', {
        index1: 0
    });
    var imgs = _$(':img', {
        node: ulimgs.getObject()
    });
    var line2 = _$('#line2');
    var bigimg = _$('#bigimg');
    var arrowright = false;
    objects = _$('#ajaxcontainer:li');
    if (objects.get(0)) {
        enableclick = false;
        for (var i = 0; i < objects.Length(); i++) {
            setTimeout('fadeinObject(objects.get(' + i + ').getObject());', (t * i));
            if (i == objects.Length() - 1) {
                setTimeout('enableclick=true;fadeoutlast(false);', t * (i + 1));
            }
        }
    }
    if (imgs.getObject() == null) return false;
    for (var i = 0; i < imgs.Length(); i++) {    	
        imgs.get(i).Click( function() {
             if (isIE)
            if (_$(this).Style().filter.indexOf('(opacity=1)') != -1)
            return false;
            if (_$(this).Opacity() == 0.01 || _$(this).Opacity() == 0)
            return false;
            if(bigimg.getObject() == null)
            return false;
            if(bigimg.getObject().src == this.getAttribute('src'))
            return false;
            if (isIE)
            _$('#line1:img', {
                index1: 0
            }).Opacity(0.01);
            else
            _$('#line1:img', {
                index1: 0
            }).Opacity('0');
            _$('#line1:img', {
                index1: 0
            }).Attributes('src', this.getAttribute('src'));            
            var tmp = new Image();
            tmp.onload = function(){            	
             fadeinObject(_$('#line1:img', {
                index1: 0
            }).getObject(), 400);            	
            };
            tmp.src = this.getAttribute('src');
        });
        if (line2.getObject() != null && arrowright == false)
        if (imgs.get(i).getPosition().x + imgs.get(i).getWidth() > line2.getPosition().x + line2.getWidth()) {
            if (isIE)
            _$('#albumright').Opacity(0.01);
            else
            _$('#albumright').Opacity(0);
            _$('#albumright').Display('block');
            fadeinObject(_$('#albumright').getObject(), 500);
            arrowright = true;
        }
    }
         if (bigimg.getObject())
        fadeinObject(bigimg.getObject(), 400);   
}


function fadegraf() {
    if (_$('#graf').getObject()) {
        _$('#fjg').Load(
        $(".container").animate({
            opacity: 1
        },
        1000));
    }
}


function fadepresentation() {
    if (_$('#presentation').getObject()) {
        $(".container").animate({
            opacity: 1
        },
        1000);
        $("a.n1").click(
        function() {
            $(".part1").toggle();
            $(".part2").fadeIn(1000);
            return false;
        });
        $("a.n2").click(
        function() {
            $(".part2").toggle();
            $(".part3").fadeIn(1000);
            return false;
        });
        $("a.p1").click(
        function() {
            $(".part2").toggle();
            $(".part1").fadeIn(1000);
            return false;
        });
        $("a.p2").click(
        function() {
            $(".part3").toggle();
            $(".part2").fadeIn(1000);
            return false;
        });
    }
}


function fadehome() {
    if (_$('#home').getObject()) {
        $(".container").animate({
            opacity: 1
        },
        1000);
        $("a").animate({
            opacity: 1
        },
        2000);
    }
}


function getSlideWidth(sleft) {
    var imgs = _$('.ulimgs album:img');
    if (imgs.get(0) == null || !imgs.isArray) return false;
    var line2 = _$('#line2');
    line2.Style().left = '0px';
    if (sleft) {
        slideindex -= 3;
        var line2limit = line2.getPosition().x;
        var tmpx = imgs.get(0).getPosition().x;
        var tmpw = tmpx + imgs.get(0).getWidth();
        //if(isIphone)
        tmpx = tmpw+160;
        var idx = 3;
        if (imgs.get(slideindex + 4) != null)
        idx = 4;
        if(slideindex <= 2){
      //  if (tmpx + imgs.get(slideindex + idx).getPosition().x + imgs.get(slideindex + idx).getWidth() > line2limit+100) {
            slidepx = 0;
            _$('#albumleft').Display('none');
            slideindex = 0;
        }
        else {
            var imgs = _$('.ulimgs album:img', {
                index1: 0
            });
            slidepx += (line2.getPosition().x + line2.getWidth()) - (imgs.get(slideindex + 3).getPosition().x + imgs.get(slideindex + 3).getWidth());
            if (imgs.get(slideindex + 3).getPosition().x >= line2.getPosition().x)
            slidepx += imgs.get(slideindex + 3).getWidth();
        }
    } else {
        slideindex += 3;
        var line2limit = line2.getPosition().x + line2.getWidth();
        var tmpx = imgs.get(imgs.Length() - 1).getPosition().x;
        var tmpw = tmpx + imgs.get(imgs.Length() - 1).getWidth();
        if (tmpx - (imgs.get(slideindex).getPosition().x - line2.getPosition().x) <= line2limit || countHiddenImgs(imgs) <= 6) {
            slidepx -= (tmpw - line2limit);
            _$('#albumright').Display('none');
        }
        else {
            var imgs = _$('.ulimgs album:img', {
                index1: 0
            });
            slidepx -= imgs.get(slideindex).getPosition().x - line2.getPosition().x;
            if (imgs.get(slideindex).getPosition().x + imgs.get(slideindex).getWidth() <= line2.getPosition().x + line2.getWidth()) {
                slidepx -= imgs.get(slideindex).getWidth();
                slideindex++;
            }
        }
    }
    return slidepx;
}


function countHiddenImgs(imgs) {
    var line2 = _$('#line2');
    var c = 0;
    for (var i = 0; i < imgs.Length(); i++) {
        if ((imgs.get(i).getPosition().x >= line2.getPosition().x + line2.getWidth()) || ((imgs.get(i).getPosition().x < line2.getPosition().x + line2.getWidth()) && (imgs.get(i).getPosition().x + imgs.get(i).getWidth() > line2.getPosition().x + line2.getWidth())))
        c++;
    }
    return c;
}


function nextimg() {
    var puces = _$('.ulimgs album:li');
    puces.get(imgindex).RemoveAttributes('class');
    imgindex++;
    puces.get(imgindex).Class('on');
    var img = _$('#line1:img', {
        index1: 0
    });
    img.Attributes('src', _$('#line2:img', {
        index1: imgindex
    }).Attributes('src'));
    if (isIE)
    img.Opacity(0.01);
    else
    img.Opacity(0);
    fadeinObject(img.getObject(), 600);
    if (imgindex > 0) {
        _$('#albumleft').Display('block');
        if (_$('#line2:img').Length() - 1 == imgindex)
        _$('#albumright').Display('none');
    }
    return false;
}


function previmg() {
    var puces = _$('.ulimgs album:li');
    puces.get(imgindex).RemoveAttributes('class');
    imgindex--;
    puces.get(imgindex).Class('on');
    var img = _$('#line1:img', {
        index1: 0
    });
    img.Attributes('src', _$('#line2:img', {
        index1: imgindex
    }).Attributes('src'));
    if (isIE)
    img.Opacity(0.01);
    else
    img.Opacity(0);
    fadeinObject(img.getObject(), 600);
    if (imgindex == 0) {
        _$('#albumleft').Display('none');
    }
    if (_$('#line2:img').Length() > 1)
    _$('#albumright').Display('block');
    return false;
}


function slideleft() {
    if (sliding) return false;
    else sliding = true;
    resetimgs();
    var slide = getSlideWidth(true);
    $(".ulimgs").animate({
        left: slide
    },
    400,
    function() {
        fadeoutlast(true);
        _$('#albumright').Display('block');
        sliding = false;
    });
    return false;
}


function slideright() {
    if (sliding) return false;
    else sliding = true;
    resetimgs();
    var slide = getSlideWidth(false);
    $(".ulimgs").animate({
        left: slide
    },
    400,
    function() {
        fadeoutlast(false);
        _$('#albumleft').Display('block');
        sliding = false;
    });
    return false;
}


function fadeoutlast(sleft) {
    var ulimgs = _$('.ulimgs album', {
        index1: 0
    });
    if (ulimgs.getObject() == null) return false;
    var imgs = _$(':img', {
        node: ulimgs.getObject()
    });    
    var line2 = _$('#line2');
    var line2limit = line2.getPosition().x + line2.getWidth();
    if (!sleft) {
        for (var i = 0; i < imgs.Length(); i++) {
            var tmpw = imgs.get(i).getPosition().x + imgs.get(i).getWidth();
            if (tmpw > line2limit) {
                fadeoutObject(imgs.get(i).getObject(),400);
                break;
            }
        }
        if (isIE7) {
            if (slideindex == 3 && _$('#albumright').Attributes('style').display == 'none') {
                fadeoutObject(imgs.get(0).getObject());
                if (imgs.get(1).getPosition().x < line2.getPosition().x)
                fadeoutObject(imgs.get(1).getObject());
            }
        } else {
            if (slideindex == 3 && _$('#albumright').getStyle().toLowerCase().indexOf('display: none') != -1) {
                fadeoutObject(imgs.get(0).getObject());
                if (imgs.get(1).getPosition().x < line2.getPosition().x)
                fadeoutObject(imgs.get(1).getObject());
            }
        }
        if (imgs.get(imgs.Length() - 1).getPosition().x + imgs.get(imgs.Length() - 1).getWidth() <= line2limit) {
            for (var i = imgs.Length() - 1; i != 0; i--) {
                if (imgs.get(i).Opacity() == '0.01' || imgs.get(i).Opacity() == '0') {
                    slideindex = i + 1;
                    break;
                }
            }
        }        
        centerslide(false);
    } else {
        for (var i = 0; i < imgs.Length(); i++) {
            var tmpx = imgs.get(i).getPosition().x;
            var tmpw = imgs.get(i).getPosition().x + imgs.get(i).getWidth();
            if (tmpw > line2.getPosition().x && tmpx < line2.getPosition().x) {
                if (i != 0)
                fadeoutObject(imgs.get(i).getObject());
                break;
            }
        }
        for (var i = 0; i < imgs.Length(); i++) {
            var tmpw = imgs.get(i).getPosition().x + imgs.get(i).getWidth();
            if (tmpw > line2limit) {
                fadeoutObject(imgs.get(i).getObject());
                break;
            }
        }
        centerslide(true);
    }
    if (imgs.get(imgs.Length() - 1).getPosition().x + imgs.get(imgs.Length() - 1).getWidth() > line2limit)
    _$('#albumright').Display('block');
}


function resetimgsindex() {
    slideindex = 0;
    slidepx = 0;
    var ulimgs = _$('.ulimgs album', {
        index1: 0
    });
    if (ulimgs.get(0) == null) return false;
    var line2 = _$('#line2');
    var line2limit = line2.getPosition().x + line2.getWidth();
    var imgs = _$(':img', ulimgs.getObject());
    if (imgs.get(0) == null) return false;
}


function resetimgs() {
    var ulimgs = _$('.ulimgs album', {
        index1: 0
    });
    if (ulimgs.get(0) == null) return false;
    var line2 = _$('#line2');
    var line2limit = line2.getPosition().x + line2.getWidth();
    var imgs = _$(':img', {
        node: ulimgs.getObject()
    });
    if (imgs.getObject() == null) return false;
    if (isIE) {
        for (var i = 0; i < imgs.Length(); i++) {
            imgs.get(i).Opacity(1);
        }
    } else {
        for (var i = 0; i < imgs.Length(); i++) {
            if (imgs.get(i).Opacity() == '0.01' || imgs.get(i).Opacity() == '0')
            imgs.get(i).Opacity(1);
        }
    }
}


// end jQuery section
// callbacks section
var prevw = 0;
function centerslide(sleft) {
    if (_$('.creations').getObject())
    return false;
    var ulimgs = _$('.ulimgs album', {
        index1: 0
    });
    if (ulimgs.getObject() == null) return false;
    var container = _$('#ajaxcontainer');
    var line2 = _$('#line2');
    var line2limit = line2.getPosition().x + line2.getWidth();
    var imgs = _$(':img', {
        node: ulimgs.getObject()
    });
    if (imgs.get(0) == null || !imgs.isArray) return false;
    var first = slideindex;
    var last = imgs.Length() - 1;
    var f = true;
    for (var i = 0; i < imgs.Length(); i++) {
        if (imgs.get(i).getPosition().x + imgs.get(i).getWidth() > line2limit) {
            last = i - 1;
            break;
        }
    }
    if (!sleft) {
        if (first > 0)
        if ((imgs.get(first - 1).getPosition().x < line2.getPosition().x) && (imgs.get(first - 1).getPosition().x + imgs.get(first - 1).getWidth() > line2.getPosition().x))
        fadeoutObject(imgs.get(first - 1).getObject());
        if (imgs.get(first) != null)
        if ((imgs.get(first).getPosition().x < line2.getPosition().x) && (imgs.get(first).getPosition().x + imgs.get(first).getWidth() > line2.getPosition().x)) {
            first++;
            fadeoutObject(imgs.get(first - 1).getObject(), 150);
        }
        else {
            if (isIE7) {
                if (slideindex == 3 && _$('#albumright').Display() == 'none') {
                    first = 1;
                    for (var i = 1; i < imgs.Length(); i++) {
                        if (imgs.get(i).getPosition().x < line2.getPosition().x)                        
                        if (imgs.get(i).getPosition().x >= line2.getPosition().x)
                        break;
                    }
                }
            } else {
                if (slideindex == 3 && _$('#albumright').Display() == 'none') {
                    first = 1;
                    for (var i = 1; i < imgs.Length(); i++) {
                        if (imgs.get(i).getPosition().x < line2.getPosition().x)
                        first++;
                        if (imgs.get(i).getPosition().x >= line2.getPosition().x)
                        break;
                    }
                }
            }
        }
    }
    if (sleft) {
        for (var i = 0; i < imgs.Length(); i++) {
            if (imgs.get(i).getPosition().x >= line2.getPosition().x) {
                first = i;
                break;
            }
        }
    }
    var w = (imgs.get(last).getPosition().x + imgs.get(last).getWidth()) - imgs.get(first).getPosition().x;
    var dif = 785 - w;
    line2.Style().position = 'relative';
    $(line2.getObject()).animate({
        left: container.getPosition().x + (dif / 2) - imgs.get(first).getPosition().x - 4
    },
    500);
    prevw = line2.Style().left;
}


function autocenter() {
    if (_$('#ajaxcontainer').getObject())
    _$('#ajaxcontainer').Display('block');
    line1 = _$('#line1');
    line2 = _$('#line2');
    var uls = _$('.ulimgs');
    var firsts = _$('.firstelem');
    var ends = _$('.endlist');
    var slides = _$('#nav:li');
    var add = 10;
    if (isIE7 && _$('.ulimgs dynamic').get(0) != null) {
        add = 25;
        var lis = _$('.ulimgs:li');
        if (lis.get(0) != null) {
            for (var i = 0; i < lis.Length() - 1; i++) {
                var img = _$(':img', {
                    node: lis.get(i).getObject(),
                    index1: 0
                });
                lis.get(i).Style().width = img.getWidth() + 15;
            }
        }
        lis = _$('.ulimgs:li', {
            index1: 1
        });
        if (lis.get(0) != null) {
            for (var i = 0; i < lis.Length() - 1; i++) {
                var img = _$(':img', {
                    node: lis.get(i).getObject(),
                    index1: 0
                });
                lis.get(i).Style().width = img.getWidth() + 15;
            }
        }
    }
    if(firsts)
    if (line1.getObject() && firsts.get(0)) {
    	if(uls.isArray)
        uls.get(0).Style().width = (ends.get(0).getPosition().x + ends.get(0).getWidth() - firsts.get(0).getPosition().x + add) + 'px'; 
        else
        uls.Style().width = (ends.getPosition().x + ends.getWidth() - firsts.getPosition().x + add) + 'px'; 
    }
    if(ends)
    if (line2.getObject() && ends.get(0) && ends.isArray) {
    	if(uls.isArray)
        uls.get(1).Style().width = (ends.get(1).getPosition().x + ends.get(1).getWidth() - firsts.get(1).getPosition().x + add) + 'px';
        else
        uls.Style().width = (ends.getPosition().x + ends.getWidth() - firsts.getPosition().x + add) + 'px';
    }
       if (line2.getObject() && _$('.presse').getObject() && slides.get(0)) {    
        line2.Style().width = (slides.get(slides.Length() - 2).getPosition().x + slides.get(slides.Length() - 2).getWidth()) - line2.getPosition().x + 'px';
        sortednav = jCalix_toArray(slides).sort(sortimg);
    }
}


function ajaxResponse(url, parameters) {
    var receiver = _$('#content-receiver');
    var tmp = http_request.responseText.replace('ajax', 'globalajax').replace('bodyclass', 'bodyclassajax');
    receiver.html(tmp);
    var bodyb = _$(':body', {
        index1: 0
    });
    var bodyclass = _$('#bodyclassajax').html();
    bodyb.Class(bodyclass);
    _$('#ajax').html(_$('#globalajax').html());
    _$('#bodyclassajax').Id('bodyclass');
    receiver.html(' ');
    if (!isIE6)
    jCalix.ajaxSetLinks({
        callback_in: ajaxResponse
    });    
    var langid = jCalix.getCookie('lang');
    if (langid == null)
    langid = 'FR';
    var html = _$(':html');
    html.Attributes('xml:lang', langid.toLowerCase());
    html.Attributes('lang', langid.toLowerCase());
    if (langid == 'AR')
    html.Attributes('dir', 'rtl');
    else
    html.Attributes('dir', 'ltr');
    centermenu();   
    var imgs = _$(':img');   
    if (imgs.get(2) && imgs.isArray) {
        loadindex = 0;
        imgsloaded = false;
        for (var i = 0; i < imgs.Length(); i++) {        	
            if (imgs.get(i).Id() != 'fjg') {
                imgs.get(i).Load(function() {
                    loadindex++;
                    if (loadindex == imgs.Length() - 1 && imgsloaded == false) {
                        imgsloaded = true;
                        setTimeout(onLoad, 150);
                    }
                });
            }
        }
        for (var i = 0; i < imgs.Length(); i++) {
            if (imgs.get(i).Id() != 'fjg') {
                if (imgs.get(i).getObject().complete) loadindex++;
                if (loadindex >= imgs.Length() && imgsloaded == false) {
                    imgsloaded = true;
                    setTimeout(onLoad, 150);
                }
            }
        }
    } else
    setTimeout(onLoad, 150);
    jCalix.addHistoryEntry(url, parameters);
    return false;
}


function afterload() {	 
    var bodyclass = _$('#bodyclass').html();
    if (_$('#ajaxcontainer').getObject())
    _$('#ajaxcontainer').Display('block');
    if (bodyclass.indexOf('creations') == -1 && bodyclass.indexOf('contact') == -1 && bodyclass.indexOf('presse') == -1) {    	
        slideindex = 0;
        fadeImages();
    } else
    if (bodyclass.indexOf('creations') != -1 || bodyclass.indexOf('contact') != -1 || bodyclass.indexOf('presse') != -1) {    	
        imgindex = 0;
        if (bodyclass.indexOf('creations') != -1) {
            var ul = _$('.ulimgs album');
            _$(':li', {
                node: ul.getObject(),
                index1: 0
            }).Class('on');
            ul.Style().width = (_$('.ulimgs album:li').Length() - 1) * 37 + 'px';
            ul.Style().left = '13px';
        }         
        var bigimg = _$('#bigimg').getObject();
        if (bigimg)
        fadeinObject(bigimg, 300);
        if (bodyclass.indexOf('presse') != -1)
        fadeImages(100);
        else
        fadeImages(50);
        setTimeout(showarrow, 500);
    }
}


function showarrow() {
    var bodyclass = _$('#bodyclass').html();
    if (bodyclass.indexOf('creations') == -1 && bodyclass.indexOf('contact') == -1 && bodyclass.indexOf('presse') == -1)
    return false;
    if (bodyclass.indexOf('presse') == -1) {
        if (_$('#albumright').getObject() != null) {
            if (isIE)
            _$('#albumright').Opacity(0.01);
            else
            _$('#albumright').Opacity(0);
            _$('#albumright').Display('block');
            fadeinObject(_$('#albumright').getObject(), 350);
        }
    } else {
        if (_$('#nav').getObject() == null)
        return false;
        if (_$('#nav:li').Length() < 3)
        return false;
        if (_$('#albumright').getObject() != null) {
            _$('#albumright').Opacity(0.01);
            _$('#albumright').Display('block');
            fadeinObject(_$('#albumright').getObject(), 350);
        }
        if (bodyclass.indexOf('presse') != -1) {
            if (_$('#albumleft').getObject() != null) {
                if (isIE)
                _$('#albumleft').Opacity(0.01);
                else
                _$('#albumleft').Opacity(0);
                _$('#albumleft').Display('block');
                fadeinObject(_$('#albumleft').getObject(), 350);
                _$('#albumleft').getObject().onclick = function() {
                    shuffle(findlastli().id);
                    return false;
                }
            }
            if (_$('#albumright').getObject() != null) {
                _$('#albumright').getObject().onclick = function() {
                    shuffle(sortednav[1].id);
                    return false;
                }
            }
        }
    }
}


function devalert() {
    var version = _$('#version').html();
    if (parseInt(version) != parseInt(versiondev))
    _$('#devalert').Display('block');
}


function centermenu() {
    var menu = _$('.menu-container', {index1: 0});
    if (menu.getObject() == null) return false;
    if (jCalix.getCookie('lang') == 'AR')
    reverseMenu();
    var menuwidth = menu.getPosition().x + menu.getWidth();
    var menuimgs = _$(':a', {node: menu.getObject()});
    var rightimg = menuimgs.get(menuimgs.Length() - 1);
    var leftimg = menuimgs.get(menuimgs.Length() - 2);
    var innerleft = leftimg.getPosition().x + leftimg.getWidth();
    var innerright = rightimg.getPosition().x;
    var innercenter = innerright + 2 - innerleft;
    var marginright = innercenter / 5;
    for (var i = 0; i < menuimgs.Length() - 2; i++) {
        menuimgs.get(i).Style().marginRight = marginright + 'px';
    }
    centersubmenu();
}


function reverseMenu() {
    var wrapper = _$('#mainmenu:a');
    var newMenu = new Array(wrapper.Length());
    var l = wrapper.Length() - 1;
    for (var i = 0; i < wrapper.Length(); i++) {
        newMenu[i] = wrapper.get(l - i).Clone();
    }
    _$('#mainmenu').html('');
    var paren = _$('#mainmenu');
    var w = paren;
    for (var i = 0; i < newMenu.length; i++) {
    	newMenu[i].html(newMenu[i].getObject().title);
        paren.Append(newMenu[i]);
        if (i == newMenu.length - 1) {
            var on = '';
            if (newMenu[i].Class().indexOf('on') != -1)
            on = 'on';
            newMenu[i].Class('right clearmargin ' + on);
            w.Append(newMenu[i]);            
        }
        else
        if (i == 0) {
        	var on = '';
        	if (newMenu[i].Class().indexOf('on') != -1)
        	on = 'on';
            newMenu[i].RemoveClass('class');
            newMenu[i].Class(on);
            paren.Append(newMenu[i]);
            var e = jCalix.CreateElement('div');
            e.Class('wrapper');
            paren.Append(e);
            var ee = jCalix.CreateElement('div');
            ee.Class('center');
            e.Append(ee);
            paren = ee;
        }
    }
}


function reverseSubMenu() {
    var wrapper = _$('#submenu:a');
    var newMenu = new Array(wrapper.Length());
    var l = wrapper.Length() - 1;
    for (var i = 0; i < wrapper.Length(); i++) {
        newMenu[i] = wrapper.get(l - i).Clone();
    }
    _$('#submenu').html('');
    var paren = _$('#submenu');
    var w = paren;
    for (var i = 0; i < newMenu.length; i++) {
    	newMenu[i].html(newMenu[i].getObject().title);
        paren.Append(newMenu[i]);
        if (i == newMenu.length - 1) {
            var on = '';
            if (newMenu[i].Class().indexOf('on') != -1)
            on = 'on';
            newMenu[i].Class('right clearmargin ' + on);
            w.Append(newMenu[i]);
        }
        else
        if (i == 0) {
        	var on = '';
        	if (newMenu[i].Class().indexOf('on') != -1)
        	on = 'on';
            newMenu[i].RemoveAttributes('class');
            newMenu[i].Class(on);
            paren.Append(newMenu[i]);
            var e = jCalix.CreateElement('div');
            e.Class('wrapper');           
            paren.Append(e);
            var ee = jCalix.CreateElement('div');
            ee.Class('center');
            e.Append(ee);
            paren = ee;
        }
    }

    
}

function centersubmenu() {
    var menu = _$('.submenu-container', {index1: 0});
    if (menu.getObject() == null) return false;
    if (jCalix.getCookie('lang') == 'AR')
    reverseSubMenu();
    var menuwidth = menu.getPosition().x + menu.getWidth();
    var menuimgs = _$(':a', {node: menu.getObject()});
    var rightimg = menuimgs.get(menuimgs.Length() - 1);
    var leftimg = menuimgs.get(menuimgs.Length() - 2);
    var innerleft = leftimg.getPosition().x + leftimg.getWidth();
    var innerright = rightimg.getPosition().x;
    var innercenter = innerright + 2 - innerleft;
    var marginright = innercenter / (menuimgs.Length()-1);
    for (var i = 0; i < menuimgs.Length() - 2; i++) {
        menuimgs.get(i).Style().marginRight = marginright + 'px';
    }
}


function reversefjg() {
    var fjg = _$('#fjg');
    if (fjg.getObject() == null) {
        return false;
    }
    else {
        if (jCalix.getCookie('lang') == 'AR')
        fjg.Attributes('src', 'css_imgs/fjg-ar.jpg');
    }
}


function checkreload() {
    var loc = document.location.href;
    var params = '';
    var splitedloc = loc.split('#');
    if (splitedloc[1]) {
        var bhash = splitedloc[1].split('html/');
        if (bhash[1]) {
            splitedloc[1] = bhash[0] + 'html';
            params = decode64(bhash[1]).replace('//', '/').replace('mi', 'menu_id').replace('di', 'dyn_id').replace('ai', 'alb_id').replace('?', '');
        }
        else
        splitedloc[1] = splitedloc[1].replace('//', '/').replace('mi', 'menu_id').replace('di', 'dyn_id').replace('ai', 'alb_id').replace('?', '');
        if (jCalix.getCookie('lang'))
        jCalix.ajaxRequest(base_url + splitedloc[1].substr(1), params, true, ajaxResponse);
        else
        jCalix.ajaxRequest(base_url, '', true, ajaxResponse);
    }
}


function fixcss() {	
	if(navigator.appVersion.indexOf('Win') == -1)
	return false;
    var l = document.createElement('link');
    l.setAttribute('rel', 'stylesheet');
    l.setAttribute('href', 'css/windows.css');
    l.setAttribute('type', 'text/css');
    document.getElementsByTagName('html')[0].appendChild(l);
}


function fixiphone() {
    if (isIphone) {
        var l = document.createElement('link');
        l.setAttribute('rel', 'stylesheet');
        l.setAttribute('href', 'css/iphone.css');
        l.setAttribute('type', 'text/css');
        document.getElementsByTagName('html')[0].appendChild(l);
    }
}


jCalix.onLoad();
if (!isIE6) {
    jCalix.addOnLoad(fixiphone);
    jCalix.addOnLoad(fixcss);
    jCalix.addOnLoad(jCalix.initAjax);
    jCalix.addOnLoad(function(){jCalix.ajaxSetLinks({callback_in: ajaxResponse});});
    jCalix.addOnLoad(checkreload);
}
jCalix.addOnLoad(onLoad);
if (!isIE6) {
    jCalix.addOnLoad(centermenu);
    jCalix.addOnLoad(jCalix.initAjaxHistory);
    jCalix.addOnLoad(jCalix.addHistoryEntry);
}


function onLoad() {
   // autocenter();
   // devalert();
    reversefjg();
    fadegraf();
    fadepresentation();    
    fadehome();    
    resetimgs();
    resetimgsindex();
    afterload();
    autocenter();
}


function findfirstli() {
    var nav = _$('#nav:li');
    var obj = nav.get(0);
    for (var i = 1; i < nav.Length() - 1; i++) {
        if (obj.Style().zIndex < nav.get(i).Style().zIndex)
        obj = nav.get(i);
    }
    return obj.getObject();
}


function findlastli() {
    var nav = _$('#nav:li');
    var obj = nav.get(0);
    for (var i = 1; i < nav.Length() - 1; i++) {
        if (obj.Style().zIndex > nav.get(i).Style().zIndex)
        obj = nav.get(i);
    }
    return obj.getObject();
}

function sortimg(a, b) {
    if (a.style.zIndex < b.style.zIndex)
    return 1;
    else
    return - 1;
}


function shuffle(ids) {
    if (changeslide) return false;
    changeslide = true;
    var obj = _$('#' + ids);
    var nav = _$('#nav:li');
    var first = findfirstli();
    var last = findlastli();
    if (ids == jCalix_getId(first)) {
        changeslide = false;
        return false;
    }
    reseting = true;
    $('#' + ids).animate({
        left: '+' + 350
    },
    300,
    function() {
        obj.Style().zIndex = parseInt(first.style.zIndex) + 1;
        first.style.zIndex = parseInt(last.style.zIndex) - 1;
        var l = 40;
        sortednav = jCalix_toArray(nav).sort(sortimg);
        for (var i = 0; i < sortednav.length; i++) {
            if (obj.getObject() == sortednav[i]) {
                continue;
            }
            $('#' + jCalix_getId(sortednav[i])).animate({
                left: '+' + l
            },
            200);
            l += 40;
        }
        $('#' + ids).animate({
            left: -40
        },
        300,
        function() {
            $('#' + sortednav[0].id).animate({
                left: 0
            },
            100);
            changeslide = false;
        });
    });
}

function coupurehover(ids) {
    var fl = findfirstli();
    if (fl.id == ids)
    return false;
    if (changeslide) return false;

    var val = getcoupureindex(ids) * 40;
    $('#' + ids).animate({
        left: parseInt(val) + 20
    },
    300);
}


function coupureout(ids) {
    var val = getcoupureindex(ids) * 40;
    $('#' + ids).animate({
        left: val
    },
    300,
    function() {
        changeslide = false;
    });

}

function getcoupureindex(ids) {

    for (var i = 0; i < sortednav.length; i++) {

        if (sortednav[i].id == ids)
        return i;
    }

}


function partialfade(obj) {
    if (changeslide)
    return false;
    if (reseting && obj != sortednav[0])
    return false;
    reseting = false;
    var nav = jCalix.toArray(_$('#nav:li').getObject()).sort(sortimg);
    for (var i = 0; i < nav.length - 1; i++) {
        if (obj == nav[i]) {
            if (nav[i].style.zIndex == 1000)
            return false;
            jCalix_setOpacity(nav[i], 1);
            prevz = nav[i].style.zIndex;
            nav[i].style.zIndex = 1000;
            prevslide = jCalix_getId(nav[i]);
        }
        else
        jCalix_setOpacity(nav[i], 0.1);
    }
    if (obj != sortednav[0])
    sortednav[0].style.zIndex = 100;
}


function resetslides() {
    if (changeslide)
    return false;
    var nav = _$('#nav:li');
    if (prevslide) {
        if (prevslide == sortednav[0])
        _$('#' + prevslide).Style().zIndex = 500;
        else
        _$('#' + prevslide).Style().zIndex = prevz;
    }
    for (var i = 0; i < nav.Length() - 1; i++) {
        if (jCalix_getOpacity(nav.getObject(i)) == 0.1)
        jCalix_setOpacity(nav.getObject(i), 1);
    }
}


function resetallslides() {
    var cn = 40;
    var z = 500;
    for (var i = 0; i < sortednav.length - 1; i++) {
        sortednav[i].style.zIndex = (z - cn);
        jCalix_setOpacity(sortednav[i], 1);
        cn += 40;
    }
}

// end callbacks section

