var ie6 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 6.0") != -1);
jQuery.noConflict();
jQuery(document).ready(function() {
    //Main navigation:
    navRollover();
    
    //Language dropdown list:
    langDropdown();
    
    //PNG transparency fix for ie6:
    jQuery('.pngfix').pngFix();

    //Adjust top margin of rte content:
    //adjustRte();
    
    //Show telephone/email form fields:
    choiceFields();
    
    //Fix for buttons spoiled by IE7.js:
    fixIeButtons();
    
    //fix for clipping navigations list
    naviClip();
    

});
jQuery(function(jQuery){
    jQuery(".peKenBurns").peKenburnsSlider()
})
jQuery(window).load(function() {
    //Equalize height of representatives blocks:
    var pList = jQuery('.representants-category ul');
    if(pList.length)
        pList.each(function() {
            equalizeHeight(jQuery(this).children('li'));
        });
    var p2List = jQuery('.case-study div.item');
    if(p2List.length) {
    	for(var i = 0; i < p2List.length; i++) {
    		if(i%2 == 0) {
    			continue;
    		}
    		if(p2List.eq(i).height() < p2List.eq(i-1).height())
    			p2List.eq(i).find('.in .in-in').height(p2List.eq(i-1).height() - 39);
    		else if(p2List.eq(i).height() > p2List.eq(i-1).height())
    			p2List.eq(i-1).find('.in .in-in').height(p2List.eq(i).height() - 39);
    	}
    }
});



/*
 * --------------------------------------------------------------------
 *  Main navigation:
 * --------------------------------------------------------------------
 */
function navRollover() {
    
    jQuery('#main-nav > li').each(function() {
        var childUl = jQuery(this).children('ul').css('width','auto');
        var thisLeft = jQuery(this).position().left; 
        if(945 - thisLeft >= childUl.width()) {
            jQuery(this).addClass('relative');
        }
        else {
            childUl.css({width: '945px', textAlign: 'right'});

        }
    });
    
    jQuery('#main-nav li li').each(function() {
        var childUl = jQuery(this).children('ul').css('width','auto');
        var thisLeft1 = jQuery(this).position().left;
        var thisLeft2 = thisLeft1;
        var shiftLeft = 15;
        if(jQuery(this).parents('li.relative').length) {
            thisLeft2 += jQuery(this).parents('#main-nav > li').position().left ;
            shiftLeft = 30;
        }

        /*if(jQuery(this).parents('li.relative').length) {//jQuery(this).attr('data-ble',thisOffsetParent.position().left);
            thisLeft += jQuery(this).parents('li.relative').position().left;
        }*/
        if(945 - thisLeft2 >= childUl.width()) {

        }
        else {
            childUl.css({width: '945px', textAlign: 'right'});
            if(jQuery(this).parent().attr('id') != 'main-nav') {
                childUl.css({marginLeft: -thisLeft2 +shiftLeft + 'px'});
            }
        }
    });    

    /*jQuery('#main-nav ul ul').each(function() {
        jQuery(this).css('paddingLeft', jQuery(this).parent('li').prevAll().andSelf().first().outerWidth() + 13 + 'px');
    });*/


    var currentOption = jQuery('#main-nav > li.current'); 
    if(currentOption.length) {
        jQuery('#main-nav > li').hover(
            function() {
                if(ie6 && !jQuery(this).hasClass('current')) {
                    currentOption.children().addClass('reset');
                }
                currentOption.removeClass('current');
            },
            function() {
                currentOption.addClass('current');           
                if(ie6) {
                    currentOption.children().removeClass('reset');
                }
            }        
        );
    }
    
    jQuery('#main-nav ul li li').hover(
        function() {
            jQuery(this).parent('ul').parent('li').nextAll('li').addClass('goback');
        },
        function() {
            jQuery(this).parent('ul').parent('li').nextAll('li').removeClass('goback');
        }
    );
    
}

function naviClip() {
    
    var level_0_li = jQuery('#main-nav > li');
    nameLiTree(level_0_li,0);
    var i = 0;
    
    jQuery('.hasChildren').mouseover(function(){
        jQuery('.hasChildren').removeClass('clip');
        jQuery(this).addClass('clip');
    })
    
    jQuery('.hasChildren_1').mouseover(function(){
        jQuery('.hasChildren_1').removeClass('clip_1');
        jQuery(this).addClass('clip_1');
    })
    
    jQuery('.noChildren').mouseover(function(){
        jQuery('.hasChildren').removeClass('clip');
        jQuery('.hasChildren_1').removeClass('clip_1');
    })
    
}

function nameLiTree(listRow,level) {        
    listRow.each(function(index){
        if(jQuery(this).find('ul > li').length > 0) {
            jQuery(this).addClass('hasChildren');
            jQuery(this).addClass('hasChildren_'+level);
            nameLiTree(jQuery(this).find('ul > li'),level+1)    
        } else {
            jQuery(this).addClass('noChildren');
        }
    })
}

/*
 * --------------------------------------------------------------------
 *  Language dropdown list:
 * --------------------------------------------------------------------
 */
 
 function langDropdown() {
    var opener = jQuery('.languages');
    if(opener.length)
        opener.click(function() {
            jQuery(this).find('.lang-list').slideToggle('fast');
        });
 }

/*
 * --------------------------------------------------------------------
 *  Adjust top margin of rte content:
 * --------------------------------------------------------------------
 */
function adjustRte() {
    jQuery('.rte').each(function() {
        var firstEl = jQuery(this).children().first();
        if(firstEl.is(':header'))
            firstEl.addClass('no-margin-top');
    });
}


/*
 * --------------------------------------------------------------------
 *  Show telephone/email form fields:
 * --------------------------------------------------------------------
 */
function choiceFields() {
    var choiceFields = jQuery('.choice-field');
    if(choiceFields.length) {
        choiceFields.click(function() {
            jQuery('.field-' + jQuery(this).attr('id').replace('show_', '')).toggle();
        })
    }
}


/*
 * --------------------------------------------------------------------
 *  Fix for buttons spoiled by IE7.js
 * --------------------------------------------------------------------
 */
function fixIeButtons() {
    if(ie6 && jQuery('button').length) {
        setTimeout(function() {
            jQuery('button').unwrap();
        }, 100);
    }
}

/*
 * --------------------------------------------------------------------
 *  Equalize height of items:
 * --------------------------------------------------------------------
 */
function equalizeHeight(pItems) {
    var pHeight = 0;
    for(i=0; i <pItems.length; i++) {
        if(pItems.eq(i).height() > pHeight)
            pHeight = pItems.eq(i).height();
    } 

    pItems.height(pHeight);
}



