var TINY={};

function T$(i){return document.getElementById(i)}
function T$$(e,p){return p.getElementsByTagName(e)}

TINY.dropdown=function(){
	var p={fade:1,slide:1,active:0,timeout:200}, init=function(n,o){
		for(s in o){p[s]=o[s]} p.n=n; this.build()
	};
	init.prototype.build=function(){
		this.h=[]; this.c=[]; this.z=1000;
		var s=T$$('ul',T$(p.id)), l=s.length, i=0; p.speed=p.speed?p.speed*.1:.5;
		for(i;i<l;i++){
			var h=s[i].parentNode; this.h[i]=h; this.c[i]=s[i];
			h.onmouseover=new Function(p.n+'.show('+i+',1)');
			h.onmouseout=new Function(p.n+'.show('+i+')')
		}
	};
	init.prototype.show=function(x,d){
		var c=this.c[x], h=this.h[x];
		clearInterval(c.t); clearInterval(c.i); c.style.overflow='hidden';
		if(d){
			if(p.active&&h.className.indexOf(p.active)==-1){h.className+=' '+p.active}
			if(p.fade||p.slide){
				c.style.display='block';
				if(!c.m){
					if(p.slide){
						c.style.visibility='hidden'; c.m=c.offsetHeight; c.style.height='0'; c.style.visibility=''
					}else{
						c.m=100; c.style.opacity=0; c.style.filter='alpha(opacity=0)'
					}
					c.v=0
				}
				if(p.slide){
					if(c.m==c.v){
						c.style.overflow='visible'
					}else{
						c.style.zIndex=this.z; this.z++; c.i=setInterval(function(){slide(c,c.m,1)},20)
					}
				}else{
					c.style.zIndex=this.z; this.z++; c.i=setInterval(function(){slide(c,c.m,1)},20)
				}
			}else{
				c.style.zIndex=this.z; c.style.display='block'
			}
		}else{
			c.t=setTimeout(function(){hide(c,p.fade||p.slide?1:0,h,p.active)},p.timeout)
		}
	};
	function hide(c,t,h,s){
		if(s){h.className=h.className.replace(s,'')}
		if(t){c.i=setInterval(function(){slide(c,0,-1)},20)}else{c.style.display='none'}
	}
	function slide(c,t,d){
		if(c.v==t){
			clearInterval(c.i); c.i=0;
			if(d==1){
				if(p.fade){c.style.filter=''; c.style.opacity=1}
				c.style.overflow='visible'
			}
		}else{
			c.v=(t-Math.floor(Math.abs(t-c.v)*p.speed)*d);
			if(p.slide){c.style.height=c.v+'px'}
			if(p.fade){var o=c.v/c.m; c.style.opacity=o; c.style.filter='alpha(opacity='+(o*100)+')'}
		}
	}
	return{init:init}
}();;
window.dropdown={};
jQuery.expr[':'].Contains = function(a, i, m) { 
    return jQuery(a).text().toUpperCase().indexOf(m[3].toUpperCase()) >= 0; 
};
function styleCommonLinks(target) {
    target.parentsUntil('ul').parent().addClass('common-links');
    if (jQuery('.region-sidebar-second li a:contains("Contact us")').length > 0) {
        if(jQuery('.region-sidebar-second li a:contains("Contact us")').parent().find('.icon').length == 0) {
            jQuery('.region-sidebar-second li a:contains("Contact us")').parent().prepend('<span class="sprite icon icon-qcontact"></span>');
        }
    }
    if (jQuery('.region-sidebar-second li a:Contains("print")').length > 0) {
        if(jQuery('.region-sidebar-second li a:Contains("print")').parent().find('.icon').length == 0) {
            jQuery('.region-sidebar-second li a:Contains("print")').parent().prepend('<span class="sprite icon icon-qprint"></span>');
            if(typeof(document.URL) != 'undefined') {
                var currHref = document.URL.replace('http://', '');
                var splitted = currHref.split('/');
                var newHref='';
                for(var x=0; x < splitted.length; x++) {
                    if(x == 0) {
                        newHref += splitted[0] + '/print';
                    } else if(splitted[x] != 'print' && splitted[x] != 'printmail') {
                        newHref += '/'+splitted[x];
                    }
                }
                jQuery('.region-sidebar-second li a:Contains("print")').attr('href', 'http://'+newHref);
            }
            if(typeof(document.title) != 'undefined') {
                jQuery('.region-sidebar-second li a:Contains("print")').attr('title', document.title);
            }
            jQuery('.region-sidebar-second li a:Contains("print")').addClass('print-page').click(function(e) {
                e.preventDefault();
                window.open(this.href);
                return false;
            });
        }
    }
    if (jQuery('.region-sidebar-second li a:contains("Share this")').length > 0) {
        if(jQuery('.region-sidebar-second li a:contains("Share this")').parent().find('.icon').length == 0) {
            if(typeof(document.URL) != 'undefined') {
                var currHref = document.URL.replace('http://', '');
                var splitted = currHref.split('/');
                var newHref='';
				for(var x=0; x < splitted.length; x++) {
                    if(x == 0) {
                        newHref += splitted[0] + '/printmail';
                    } else if(splitted[x] != 'printmail') {
                        newHref += '/'+splitted[x];
                    }
                }
                //jQuery('.region-sidebar-second li a:contains("Share this")').attr('href', 'http://'+newHref);
				//the two lines below are replaced with the commented line above for addtoany sharing popup window
				jQuery('.region-sidebar-second li a:contains("Share this")').attr('href', 'http://www.addtoany.com/share_save').addClass('a2a_dd');
				a2a.init('page')
            }
            jQuery('.region-sidebar-second li a:contains("Share this")').parent().prepend('<span class="sprite icon icon-qshare"></span>');
        }
    }
}
var resized = 0;
jQuery(document).ready(function($) {
    $('#navigation li a:Contains("home")').prepend('<span class="sprite icon icon-home"></span>');
    $('#navigation li a:Contains("about us")').prepend('<span class="sprite icon icon-smile"></span>');
    $('#navigation li a:Contains("our research")').prepend('<span class="sprite icon icon-fishead"></span>');
    $('#navigation li a:Contains("resources")').prepend('<span class="sprite icon icon-book"></span>');
    $('#navigation li a:Contains("news and media")').prepend('<span class="sprite icon icon-news"></span>');
	$('#navigation li a:Contains("contact us")').prepend('<span class="sprite icon icon-contact"></span>');
    
    if ($('.region-sidebar-second li a:contains("Contact us")').length > 0 || $('.region-sidebar-second li a:Contains("print")').length > 0 || $('.region-sidebar-second li a:contains("Share this")').length > 0)
    {
        if ($('.region-sidebar-second li a:contains("Contact us")').length > 0) {
            styleCommonLinks($('.region-sidebar-second li a:contains("Contact us")'));
        } else if ($('.region-sidebar-second li a:Contains("print")').length > 0) {
            styleCommonLinks($('.region-sidebar-second li a:Contains("print")'));
        } else if ($('.region-sidebar-second li a:contains("Share this")').length > 0) {
            styleCommonLinks($('.region-sidebar-second li a:contains("Share this")'));
        }  
    }
    
    $('#navigation li.expanded ul', this).hover(function() {
        $(this).parent().find('a:first').addClass('active');
    }, function() {
        $(this).parent().find('a:first').removeClass('active');
    });
    $('#navigation').find('ul.menu:first').attr('id','primary_links');
    var mnuSpeed = (jQuery.browser.msie) ? 2 : 10;
    dropdown = new TINY.dropdown.init("dropdown", {id:'primary_links', active:'menuhover', fade:true, slide:true, timeout:200});
    //log(jQuery('.front-main-content').length);
    if(jQuery('.front-main-content').length == 0) {
        jQuery('.region-sidebar-first, .region-sidebar-second').css('height', jQuery('.main-content').innerHeight()+'px');
        resized = 1;
    }
    setTimeout(function() {
        resizeContent();
    },2000);
});

function resizeContent() {
    if(document.getElementById('block-views-featured-slider-block')) {
        if(jQuery('.featured-image').length > 0) {
            var contentPadding = 52;
            jQuery('.views-slideshow-cycle-main-frame-row-item').each(function() {
                jQuery(this).css('width', jQuery(this).parent().width());
                if(jQuery.browser.msie) {
                    //jQuery('.featured-link-container').css('left', (jQuery(this).offset().left + jQuery(this).width()) - (jQuery('.featured-link-container').outerWidth() * 2) - (jQuery('.featured-link-container').outerWidth()/2) + 20);
                }
            });
            var defaultWidth = jQuery('.featured-image').eq(0).find('img').width();
            jQuery('.featured-image').each(function() {
                var curItem = jQuery(this);
                var targetWidth = curItem.parent().width()/2;
                if(jQuery.browser.msie) {
                    targetWidth -= contentPadding/2;
                }
                
                curItem.css('width', targetWidth);
                var contentWidth = targetWidth - contentPadding;
                if(jQuery.browser.msie) {
                    contentWidth -= 26;
                    curItem.css('width', targetWidth - contentPadding);
                    
                }
                
                curItem.parent().find('h2.title').css('width', contentWidth);
                curItem.parent().find('.more-link').css('width', contentWidth);
                curItem.parent().find('div.featured-body').css('width', contentWidth);
                var fImg = curItem.find('img');
                var w = curItem.outerWidth();
                if(jQuery.browser.msie) {
                    w = targetWidth - contentPadding;
                    if(w < 200) {
                        w = defaultWidth + contentPadding/2;
                        curItem.css('width', w);
                    }
                }
                fImg.removeAttr('height');//.parent().addClass('twelvecol');
                if(w > 50) {
                    fImg.css({
                        width: w - 26 + 'px',
                        overflow: 'hidden'
                    }).attr('width', w-26);
                }
            });
        }
    }
    jQuery('#content_wrapper').css('height', jQuery(document).height() + 'px');
    if(jQuery.browser.msie) {
        jQuery('#navigation').css('width', jQuery(window).width() - 40);
    }
}
function log(msg) {
    if(typeof(console) == 'object') { console.log(msg); }
}
// seems like we got problem to get element using jquery document ready
jQuery(window).load(function() {
    //Featured area need to be same height!
    resizeContent();
    if(!resized || jQuery('.front-main-content').length == 0) {
        log('resize');
        jQuery('.region-sidebar-first, .region-sidebar-second').css('height', jQuery('.frontpage-sidebar').innerHeight()+'px');
    }
});
jQuery(window).resize(function() {
    if(screen.width < 900) {
        resizeContent();
    }
});
;

