var ITP = {
    photoGalleryDelay: 5000,
    assets: function(){
        return '/templates/ahlan_v3/components/'
        //return ''
    },
    hoverTip: {
        template1: '<div class="popup"><div class="inject-here"></div></div>'
    },
    youTubeVideo: function(o){
        return '<object height="250" width="288"><param value="http://www.youtube.com/v/' + o + '&amp;hl=en&amp;fs=1&amp;color1=0xcc2550&amp;color2=0xe87a9f" name="movie"/><param value="true" name="allowFullScreen"/><param value="always" name="allowscriptaccess"/><embed height="250" width="288" allowfullscreen="true" allowscriptaccess="always" type="application/x-shockwave-flash" src="http://www.youtube.com/v/' + o + '&amp;hl=en&amp;fs=1&amp;color1=0xcc2550&amp;color2=0xe87a9f"/></object>'
    },
    youtubeVideo: function(o){
        return '<object height="250" width="288"><param value="http://www.youtube.com/v/' + o + '&amp;hl=en&amp;fs=1&amp;color1=0xcc2550&amp;color2=0xe87a9f" name="movie"/><param value="true" name="allowFullScreen"/><param value="always" name="allowscriptaccess"/><embed height="250" width="288" allowfullscreen="true" allowscriptaccess="always" type="application/x-shockwave-flash" src="http://www.youtube.com/v/' + o + '&amp;hl=en&amp;fs=1&amp;color1=0xcc2550&amp;color2=0xe87a9f"/></object>'
    },
    flvVideo: function(o){
        return '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" width="288" height="250" id="http://' + document.domain + '/images/videos/player" align="middle"><param name="allowScriptAccess" value="always" /><param name="allowFullScreen" value="false" /><param name="movie" value="http://' + document.domain + '/images/videos/player.swf" /><param name="quality" value="high" /><param name="bgcolor" value="#ffffff" /><param name="FlashVars" value="playerOpts=targetClip*http://' + document.domain + '/images/videos/' + o + '.flv*s||autoLoad*true*b||showReplay*false*bshowReplay||pauseAtFirstFrame*true*b||sndVolume*0*i"	 /><embed src="http://' + document.domain + '/images/videos/player.swf"  			quality="high" bgcolor="#ffffff" width="288" height="250" name="http://' + document.domain + '/images/videos/player"  			align="middle"  allowscriptaccess="always" allowfullscreen="false"  type="application/x-shockwave-flash"  	pluginspage="http://www.macromedia.com/go/getflashplayer"  flashvars="playerOpts=targetClip*http://' + document.domain + '/images/videos/' + o + '.flv*s||autoLoad*true*b||showReplay*false*b||pauseAtFirstFrame*true*b||sndVolume*0*i" /></embed></object>'
    },
    swfVideo: function(o){
        return '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" width="288" height="250" id="flashWindow" align="middle"><param name="allowScriptAccess" value="sameDomain" /><param name="allowFullScreen" value="false" /><param name="movie" value="' + o + '.swf" /><param name="menu" value="false" /><param name="quality" value="high" /><param name="wmode" value="opaque" /><param name="bgcolor" value="#ffffff" /><embed src="' + o + '.swf" menu="false" quality="high" bgcolor="#ffffff" width="288" height="250" name="flashWindow" 	align="middle" 			allowScriptAccess="sameDomain" allowFullScreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /></object>'
    },
    transformFlash: function(theName){
        if (swfobject.getFlashPlayerVersion().major >= 10) {
            if (theName === 'top_nav' || theName === 'bot-nav') {
                $('#' + theName).each(function(i){
                    var t = $(this), newIndex = -1, menu = [], link = [], setbg = '#ffffff', td = t.find('td:first'), activemenu
                    var buttonMargin = parseInt(td.css('paddingLeft')) + parseInt(td.css('paddingRight'));
                    t.find('a').each(function(i){
                        var p = $(this);
                        if (p.find('.new').length > 0) {
                            newIndex = i;
                        }
                        var tempM = $.trim(p.text()).replace(/.\x2b/, "%2b")
                        menu.push(tempM);
                        link.push(p.attr('href'));
                        if (p.hasClass('active')) {
                            activemenu = i
                        }
                    })
                    if (t.attr('id') === 'bot-nav') {
                        setbg = '#222222'
                    }
                    //t.html('').append('<div id="'+theName+i+'"></div>');
                    var flashvars = {
                        menuItems: menu,
                        menuLinks: link,
                        margin: buttonMargin,
                        newIndex: newIndex,
                        selectedmenu: activemenu,
                        activeColor: 'cc0000'
                    };
                    var flashpara = {
                        quality: "high",
                        bgcolor: '#ff00ff',
                        wmode: 'transparent'
                    };
                    swfobject.switchOffAutoHideShow();
                    swfobject.embedSWF(ITP.assets() + 'flash/menu.2.3.swf', theName, "954", "42", "10", ITP.assets() + "flash/expressInstall.2.2.swf", flashvars, flashpara);
                });
            }
            if (theName === 'loves-hates') {
                $('.ahate,.alove').each(function(i){
                    var t = $(this), file = 'flash/ahlan_loves.swf';
                    if (t.hasClass('alove')) {
                        file = 'flash/ahlan_hates.swf';
                    }
                    t.attr('id', 'lovehate' + i);
                    var flashpara = {
                        quality: "high",
                        bgcolor: '#ffffff',
                        wmode: 'transparent'
                    };
                    swfobject.embedSWF(ITP.assets() + file, 'lovehate' + i, "282", "55", "10", null, null, flashpara);
                });
            }
        }
    },
    gallerySlide: function(){
        $('#photo-galleries').each(function(){
            var that = $(this).find('ul'), liObj, timer, curPage = 0, auto
            $(this).find('ol').each(function(){
                var butMem = {};
                liObj = $(this).find('li').each(function(i){
                    var t = $(this), z = 1, h = -(parseInt(t.css('height')) * (i + 1));
                    that.find('li:eq(' + i + ')').css({
                        'z-index': z,
                        opacity: 0,
                        'visibility': 'visible'
                    });
                    t.css('text-indent', '-500px');
                    if (i === 0) {
                        butMem.o = t;
                        butMem.i = i;
                        butMem.h = h;
                        t.css({
                            'background-position': 'right ' + h + 'px'
                        }).addClass('active');
                        that.find('li:eq(' + i + ')').css({
                            'z-index': z + 1,
                            opacity: 1
                        });
                    }
                    else {
                        t.css({
                            'background-position': 'left ' + h + 'px'
                        });
                    }
                    t.hover(function(){
                        if (!t.hasClass('active')) {
                            t.css({
                                'background-position': 'center ' + h + 'px'
                            });
                        }
                    }, function(){
                        if (!t.hasClass('active')) {
                            t.css({
                                'background-position': 'left ' + h + 'px'
                            });
                        }
                    });
                    t.mousedown(function(){
                        butMem.o.css({
                            'background-position': 'left ' + butMem.h + 'px'
                        }).removeClass('active');
                        that.find('li:eq(' + butMem.i + ')').fadeTo('normal', 0).css('z-index', z);
                        butMem.o = t;
                        butMem.i = i;
                        butMem.h = h;
                        butMem.o.css({
                            'background-position': 'right ' + butMem.h + 'px'
                        }).addClass('active');
                        that.find('li:eq(' + i + ')').fadeTo('normal', 1).css('z-index', z + 1);
                    });
                    t.mouseup(function(){
                        clearTimeout(timer);
                        auto = null
                    })
                });
            });
            /*auto scroll*/
            auto = function(){
                timer = setTimeout(function(){
                    if (curPage != $(liObj).length - 1) {
                        curPage++;
                        $(liObj[curPage]).trigger('mousedown');
                    }
                    else {
                        curPage = 0;
                        $(liObj[curPage]).trigger('mousedown');
                    }
                    auto();
                }, ITP.photoGalleryDelay);
            };
            auto();
        });
    }
};

$(document).ready(function(){

    /* going out module headlines */
    $('#going-out-big-mod').each(function(){
        $(this).find('h3 > a').each(function(i){
            var t = $(this);
            t.attr('id', 'going-tempID' + i).find('span').remove();
            var flashvars = {
                headline: $.trim(t.text()),
                headlink: t.attr('href')
            };
            var flashpara = {
                quality: "high",
                bgcolor: "#f8f8f8"
            };
            swfobject.embedSWF(ITP.assets() + "flash/headline.swf", 'going-tempID' + i, "100%", "20", "9.0.0", ITP.assets() + "flash/expressInstall2.swf", flashvars, flashpara);
        });
    });
    
    
    
    
    
    
    
    /* green radio buttons */
    $(".green-radio").each(function(){
        var radio = $(this), radio_name = radio.attr('name'), radio_id = radio.attr('id'), hov, ov, rep = $('<span class="' + radio_name + ' green_radio"></span>').hover(hov = function(l){
            rep.addClass('hover');
            $('label[for=' + radio_id + ']').addClass('lhov');
        }, ov = function(l){
            rep.removeClass('hover');
            $('label[for=' + radio_id + ']').removeClass('lhov');
        }).click(function(){
            $('span.' + radio_name).removeClass('select');
            $('label.' + radio_name).removeClass('lsel');
            rep.addClass('select');
            radio.attr('checked', true);
            $('label[for=' + radio_id + ']').addClass('lsel');
        });
        radio.bind('change', function(){
            rep.trigger('click');
        });
        radio.hide();
        radio.after(rep)
        if (radio.attr('checked') === true) {
            rep.trigger('click')
        }
        $('label[for=' + radio_id + ']').addClass(radio_name).hover(function(){
            hov();
        }, function(){
            ov();
        }).click(function(){
            rep.trigger('click')
        });
    });
    
    
    /* ellipse submit buttons */
    $('.ellipse-button').each(function(){
        var b = $(this);
        b.addClass('addimg').hover(function(){
            b.addClass('hover');
        }, function(){
            b.removeClass('hover');
        }).mouseup(function(){
            $(this).attr('class', 'ellipse-button-anim')
            
        }).mousedown(function(){
            b.addClass('press').blur();
        });
    });
    
    
    /* init tabs */
    $('.tabview').each(function(){
        $(this).find('.tab-hold:first').tabs({
            fx: {
                opacity: 'toggle'
            }
        }, 'fast');
        $(this).find('ul a').mouseup(function(){
            $(this).blur();
        })
    });
    
    /* VIDEOS */
    $('#ahlan-tv').each(function(){
        $(this).find('.ui-tabs-panel').each(function(tabEach){
            var that = $(this);
            var v = []
            var currentVideo = 0;
            var videoContainer = $(this).find('.video-embed');
            var videoMarker = $(this).find('.video-marker');
            var videoSwitch = $(this).find('ul > li > a');
            
            videoMarker.show();
            $(this).find('.v0').show();
            $(videoSwitch).each(function(i){
                var dest = this;
                v.push(that.find('.v' + i))
                if (i === 0 && tabEach === 0) {
                    $('.video-marker').css({
                        'display': 'block',
                        'left': $(dest).position().left + (($(dest).width() - $('.video-marker').width()) / 2)
                    })
                }
                var videoId = $(this).attr('title').split(',')
                $(this).attr('title', '');
                $(this).mousedown(function(){
                    videoContainer.html(ITP[videoId[0]](videoId[1]));
                    $(videoMarker).animate({
                        left: $(dest).position().left + (($(dest).width() - videoMarker.width()) / 2)
                    }, 'normal');
                    $(v[currentVideo]).slideUp('fast');
                    $(v[i]).slideDown('fast');
                    currentVideo = i;
                });
                
            });
            
        });
        $(this).find('ul > li > a > img').hoverTip(null, true)
    });
    
    
    /* competitions scroller */
    $('#competitions-mod').each(function(){
        var show = 4; //number of competations you want to show at 1 time...
        var xFact = 4; //number of items slide per click
        var u = $(this).find('ul:first'), l = u.find('li'), p = parseInt, o = $('#comp-nav'), count = $('#ccount'), mt = p($(l[0]).css('marginTop')), max, pages, curPage = 1, slide, pAdd, al, updatePage;
        var h = p($(l[0]).css('height')) + p($(l[0]).css('paddingTop')) + p($(l[0]).css('paddingBottom')) + p($(l[0]).css('marginBottom')) + mt + 2;
        u.css({
            'height': show * h,
            'overflow': 'hidden'
        });
        pages = Math.ceil(l.length / show);
        if (pages > 1) {
            o.css({
                'display': 'block'
            });
            max = (l.length * h) - (show * h);
            count.text(curPage + ' of ' + pages);
            $('#cupbut, #cdownbut').hover(function(i){
                al = 'left';
                if (i.target.id === "cdownbut") {
                    al = 'right';
                }
                $(this).css({
                    'background-position': al + ' bottom'
                });
            }, function(i){
                al = 'left';
                if (i.target.id === "cdownbut") {
                    al = 'right';
                }
                $(this).css({
                    'background-position': al + ' top'
                });
            }).click(function(i){
                slide = xFact * h;
                pAdd = -1;
                if (i.target.id === "cdownbut") {
                    slide *= -1;
                    pAdd = 1;
                }
                if ((p($(l[0]).css('marginTop')) >= mt && i.target.id === "cupbut") || (p($(l[0]).css('marginTop')) <= -max && i.target.id === "cdownbut")) {
                    slide = pAdd *= 0;
                }
                $(l[0]).animate({
                    marginTop: p($(l[0]).css('marginTop')) + slide
                }, 'slow');
                count.text((curPage += pAdd) + ' of ' + pages);
            });
        }
    });
    
    /* back to top slide */
    $('#backtotop').find('a:first').each(function(){
        $(this).attr('href', '#:here').click(function(){
            $('html,body').animate({
                scrollTop: 100
            }, 1000);
        });
    });
    
    /* article thumbs scroller */
    $('#thumb-scroll').each(function(){
        var thumbs = 6; // set thumbs
        var th = $(this), sh = th.find('.scroll-hold:first'), ul = $(this).find('ul:first'), li = ul.find('li'), im = ul.find('img'), cP = 1, maxP = Math.ceil(li.length / thumbs), p = parseInt, upBack, rb, lb, liW, showImg, maxCount, expand = {
            state: false
        }, countText;
        im.hide();
        th.slideDown('normal');
        liW = $(li[0]).outerWidth();
        sh.css('width', liW * thumbs);
        ul.css('width', liW * li.length);
        showImg = function(){
            if (cP * thumbs < $(im).length) {
                maxCount = cP * thumbs
            }
            else {
                maxCount = $(im).length
            }
            for (z = (cP * thumbs) - thumbs; z < maxCount; z++) {
                $(im[z]).show();
            }
        };
        if ($(im).length === 1) {
            $(this).after('<div class="spacer"></div>')
            th.stop().hide();
            
        }
        else 
            if ($(im).length > thumbs + 1) {
                showImg();
            }
            else {
                $('#lbut, #rbut, #expandit').hide();
                $(sh).css('width', 'auto');
                $(im).show();
                
            }
        function scrollCount(){
            var topIt = $(im).length
            if (thumbs * cP < $(im).length) {
                topIt = thumbs * cP
            }
            countText = ((thumbs * cP) - thumbs + 1) + ' to ' + topIt + ' of ' + $(im).length
            $('#count').text(countText)
        };
        scrollCount();
        $('#lbut').fadeTo('fast', .2);
        lb = true;
        $('#lbut, #rbut').click(function(i){
            if (i.target.id === 'rbut') {
                if (!(cP === maxP)) {
                    if (lb) {
                        $('#lbut').fadeTo('fast', 1);
                        lb = false
                    }
                    cP++;
                    var k = p(ul.css('marginLeft')) - (liW * thumbs);
                    ul.animate({
                        marginLeft: k
                    }, 'slow');
                    showImg();
                    $(i.target).css('background-position', 'right bottom');
                    if (cP === maxP) {
                        $(i.target).fadeTo('fast', .2)
                    };
                    rb = true
                }
            }
            if (i.target.id === 'lbut') {
                if (!(cP === 1)) {
                    if (rb) {
                        $('#rbut').fadeTo('fast', 1);
                        rb = false
                    }
                    cP--;
                    var k = p(ul.css('marginLeft')) + (liW * thumbs);
                    ul.animate({
                        marginLeft: k
                    }, 'slow');
                    $(i.target).css('background-position', 'right -304px');
                    if (cP === 1) 
                        $(i.target).fadeTo('fast', .2);
                    lb = true
                }
            };
            setTimeout(function(){
                upBack(i);
            }, 150);
            scrollCount();
        }).hover(upBack = function(i){
            if (i.target.id === 'lbut') {
                $(i.target).css('background-position', 'center -304px');
            }
            if (i.target.id === 'rbut') {
                $(i.target).css('background-position', 'center bottom');
            }
        }, function(i){
            if (i.target.id === 'lbut') {
                $(i.target).css('background-position', 'left -304px');
            }
            if (i.target.id === 'rbut') {
                $(i.target).css('background-position', 'left bottom');
            }
        });
        $('#expandit').click(function(){ //expand - collapse
            if (!expand.state) {
                expand.state = true;
                th.find('span').hide('fast');
                expand.ul = {
                    width: ul.css('width'),
                    marginLeft: ul.css('marginLeft')
                };
                ul.css({
                    'width': ($('#article-body').outerWidth() + 18),
                    'margin-left': '0'
                });
                expand.th = th.css('height');
                th.css('height', 'auto');
                expand.sh = {
                    width: sh.css('width'),
                    height: sh.css('height')
                };
                sh.animate({
                    width: ($('#article-body').outerWidth()),
                    height: ul.outerHeight()
                }, 'normal', function(){
                    $(im).show()
                });
                expand.txt = $(this).html()
                $(this).css('text-align', 'left').html('Collapse &uarr;');
                $('#count').fadeOut('normal');
                $('#expandit').css({
                    'float': 'left'
                });
            }
            else {
                expand.state = false;
                th.find('span').show('slow');
                ul.animate({
                    width: expand.ul.width,
                    marginLeft: expand.ul.marginLeft
                });
                th.animate({
                    height: expand.th
                });
                sh.animate({
                    width: expand.sh.width,
                    height: expand.sh.height
                }, 'normal');
                $(this).css('text-align', 'right').html(expand.txt);
                $('#count').fadeIn('normal');
                $('#expandit').css({
                    'float': 'right'
                });
            }
        });
        //add custom tooltip
        $(im).hoverTip();
    });
    
    
    $(".pr_img").each(function(){
        $(this).hoverTip();
    })
    
    
    $("#gallerythumb").each(function(){
        var that = this, p = parseInt, ul = $(this).find('ul'), li = ul.find('li'), cP = 0;
        var liWidth = $(li[0]).outerWidth() + p($(li[0]).css('margin-right')) + p($(li[0]).css('margin-left')), liHeight = $(li[0]).outerHeight();
        
        ul.css({
            'width': liWidth * $(li).length
        })
        
        rBut = $(this).find("div#rbut");
        lBut = $(this).find("div#lbut");
        
        var maxP = Math.ceil($(li).length / 11)
        
        rBut.css({
            'position': 'absolute',
            'left': $(this).position().left + $(this).outerWidth() - rBut.outerWidth()
        })
        lBut.css({
            'position': 'absolute'
        })
        
        $("div#rbut , div#lbut").click(function(but){
            var direction = -1
            if (but.target.id === "lbut") {
                direction = 1
            }
            
            cP += direction
            if (cP === 0) {
                lBut.hide()
            }
            else {
                lBut.show()
            }
            var liMargin = (liWidth * 11) * cP
            $(ul).animate({
                marginLeft: liMargin
            }, 'slow');
            
            
        });
        
    })
    
    
    
    
    $('#add-comment').each(function(){
        var maxMsgDisplay = 5; // initial msg count
        var that = $(this), dd, k = function(){
            $('#com-name').prev('label').animate({
                color: '#f00'
            }, 1000, function(){
                $('#com-name').prev('label').animate({
                    color: '#666'
                }, 1000, function(){
                    k();
                });
            })
        };
        k();
        
        $('#com-name, #slidecomment').bind('click', function(e){
            k = function(){
                k = null;
            };
            that.animate({
                height: 430,
                backgroundColor: '#f7f7f7',
                borderLeftColor: '#ccc',
                borderTopColor: '#ccc',
                borderRightColor: '#ccc',
                borderBottomColor: '#ccc'
            }, 'slow');
            $('#com-name, #slidecomment').unbind('click');
            $('#add-comment').find('h3').slideDown('normal');
        });
        
        $(this).find('input, textarea').each(function(){
            $(this).focus(function(){
                $(this).animate({
                    backgroundColor: '#FFFFDF'
                }, 'normal');
                $(this).prev('label').stop().animate({
                    color: '#f00'
                }, 'normal');
            });
            $(this).blur(function(){
                $(this).animate({
                    backgroundColor: '#fff'
                }, 'fast', function(){
                    $(this).css({
                        'background-color': '#fff'
                    });
                });
                $(this).prev('label').stop().animate({
                    color: '#666'
                }, 'normal', function(){
                    $(this).css({
                        'color': '#666'
                    });
                });
                if (!$(this).attr('value')) {
                    $(this).attr('value', $(this).attr('title'));
                    $(this).attr('title', '');
                }
            });
        });
        
        $('#article-comments').each(function(){
            dd = $(this).find('dl dd').each(function(i){
                if (i < maxMsgDisplay) {
                    $(this).slideDown('normal');
                }
            });
            $(this).find('a.more, h3 > span > a').attr('href', '#:here').click(function(){
                dd.slideDown('normal');
                $(this).slideUp();
            });
        });
    });
    
    $('body').each(function(){
        var scrollTo = self.location.toString().split('#');
        if (scrollTo[1] && (scrollTo[1] !== 'here')) {
            $('#article-comments dl dd').slideDown('normal');
            $('#article-comments dl dd a.more, #article-comments dl dd h3 > span > a').slideUp()
            var scrollThru = parseInt($('#' + scrollTo[1]).position().top);
            $('html,body').animate({
                scrollTop: (scrollThru - 120)
            }, 800);
        }
    });
    
    $('#celeb-list').each(function(){
        var setHeight = 66
        var t = $(this), tempArr = [], database = {}, result;
        var dtext = t.find('dt:first > span:first').text()
        var dd = t.find('dd').each(function(){
            this.h = $(this).height();
            this.open = false;
            $(this).css('height', setHeight);
        })
        t.find('dt').each(function(i){
            var o = dd[i];
            var span = $(this).find('span:first');
            $(this).mousedown(function(){
                switch (o.open) {
                    case false:
                        $(o).stop().animate({
                            height: o.h
                        }, 'normal', function(){
                            span.text('Click to minimize');
                        });
                        o.open = true;
                        break;
                    case true:
                        $(o).stop().animate({
                            height: setHeight
                        }, 'normal', function(){
                            span.text(dtext);
                        });
                        o.open = false;
                        break;
                }
            });
        });
        /*
         t.append('<div id="searchresult"></div>')
         t.find('a').each(function(iter){
         tempArr = $.trim($(this).text()).toLowerCase().split(' ')
         this.unique = 'elem'+iter
         for (j=0; j<tempArr.length+1;j++){
         var kw = tempArr[j]
         if (database[kw]){
         database[kw].push($(this))
         } else {
         database[kw] = $(this);
         }
         }
         
         });
         var writeArr = []
         $('#celebsearch').keyup(function(k){
         t.find('dl').show()
         $('#searchresult').find('a').remove();
         var search = (k.target.value).split('')
         if(search != ''){
         t.find('dl').hide();
         $('#searchresult').find('a').remove();
         }
         for (obj in database){
         for(i = 0; i<search.length;i++){
         if(obj.indexOf(k.target.value) > -1){
         result = database[obj];
         for(di=0;di<result.length;di++){
         if(!result[di].unique){
         result.splice(di)
         }
         writeArr.push(result[di]);
         for (wA=0;wA<writeArr.length;wA++){
         if($(writeArr[wA]).text() == $(result[di]).text()){
         writeArr.splice([wA-1])
         }
         }
         }
         }
         }
         }
         })
         */
        /* adjust article image caption */
    
    });
    /*
     $('#article-body').each(function(){
     if(!$(this).hasClass('landscape')){
     var img = $('#img-holder');
     $(this).find('cite').each(function(){
     $(this).css({'position':'absolute','margin-top':img.height()-$(this).height()})
     });
     }
     });
     */
    $("#system-message").each(function(){
        $(this).animate({
            backgroundColor: "#ff9"
        }, 4000, function(){
            $(this).slideUp('slow')
        })
    })
    
    $('.more-news').each(function(){
        $(this).find('ul>li').hover(function(){
            $(this).animate({
                backgroundColor: '#e0ebf5'
            }, 'fast')
        }, function(){
            $(this).animate({
                backgroundColor: '#ffffff'
            }, 'fast')
        })
        
    })
    
    $('body.gallery').each(function(){
        var morepics = $('#moreinpictures').html();
        $('#moreinpictures').remove();
        $('#gallery-brand').append('<div id="topmoreinpictures">' + morepics + '</div>');
        $('#previous').hoverTip('Previous image', false);
        $('#next').hoverTip('Next image', false);
    })
    
});

/* tooltip plugin */
$.fn.extend({
    hoverTip: function(k, mouse){
        return this.each(function(){
            if ($(this).attr('title') || k) {
                var timer = 400
                var popTip, message, _x, _y, delay, el = $(this);
                $(this).hover(function(elem){
                    popTip = $(ITP.hoverTip.template1).css('opacity', 0);
                    message = el.attr('title');
                    el.attr('title', '');
                    if (k) {
                        message = k
                    }
                    popTip.find('.inject-here').text(message);
                    $('body').append(popTip);
                    popTip.css({
                        'height': 'auto'
                    });
                    if (mouse) {
                        delay = setTimeout(function(){
                            popTip.stop().css({
                                'display': 'block'
                            }).animate({
                                opacity: 1
                            }, 'normal');
                        }, timer)
                        el.bind('mousemove.popTip', function(e){
                            _x = e.pageX;
                            _y = e.pageY + 20;
                            popTip.css({
                                'left': _x,
                                'top': _y
                            });
                        })
                    }
                    else {
                        delay = setTimeout(function(){
                            _x = Math.round(el.position().left + ((el.outerWidth() - popTip.outerWidth()) / 2));
                            _y = el.position().top - popTip.outerHeight();
                            popTip.stop().css({
                                'left': _x - 20,
                                'top': _y,
                                'display': 'block'
                            }).animate({
                                left: _x,
                                opacity: 1
                            }, 'fast');
                        }, timer);
                    }
                }, function(){
                    clearTimeout(delay);
                    if (popTip) {
                        if (mouse) {
                            $(this).unbind('mousemove.popTip');
                        }
                        $(this).attr('title', message);
                        popTip.stop().animate({
                            left: _x + 15,
                            opacity: 0
                        }, timer, function(){
                            $(this).remove();
                        });
                    }
                });
            }
        });
    }
});

/* colour shift plugin */
(function(jQuery){
    jQuery.each(['backgroundColor', 'borderBottomColor', 'borderLeftColor', 'borderRightColor', 'borderTopColor', 'color', 'outlineColor', 'borderColor'], function(i, attr){
        jQuery.fx.step[attr] = function(fx){
            if (fx.state == 0) {
                fx.start = getColor(fx.elem, attr);
                fx.end = getRGB(fx.end);
            }
            fx.elem.style[attr] = "rgb(" +
            [Math.max(Math.min(parseInt((fx.pos * (fx.end[0] - fx.start[0])) + fx.start[0]), 255), 0), Math.max(Math.min(parseInt((fx.pos * (fx.end[1] - fx.start[1])) + fx.start[1]), 255), 0), Math.max(Math.min(parseInt((fx.pos * (fx.end[2] - fx.start[2])) + fx.start[2]), 255), 0)].join(",") +
            ")";
        }
    });
    function getRGB(color){
        var result;
        if (color && color.constructor == Array && color.length == 3) 
            return color;
        if (result = /rgb\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*\)/.exec(color)) 
            return [parseInt(result[1]), parseInt(result[2]), parseInt(result[3])];
        if (result = /rgb\(\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*\)/.exec(color)) 
            return [parseFloat(result[1]) * 2.55, parseFloat(result[2]) * 2.55, parseFloat(result[3]) * 2.55];
        if (result = /#([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})/.exec(color)) 
            return [parseInt(result[1], 16), parseInt(result[2], 16), parseInt(result[3], 16)];
        if (result = /#([a-fA-F0-9])([a-fA-F0-9])([a-fA-F0-9])/.exec(color)) 
            return [parseInt(result[1] + result[1], 16), parseInt(result[2] + result[2], 16), parseInt(result[3] + result[3], 16)];
        return colors[jQuery.trim(color).toLowerCase()];
    }
    function getColor(elem, attr){
        var color;
        do {
            color = jQuery.curCSS(elem, attr);
            if (color != '' && color != 'transparent' || jQuery.nodeName(elem, "body")) 
                break;
            attr = "backgroundColor";
        }
        while (elem = elem.parentNode);
        return getRGB(color);
    };
    var colors = {};
})(jQuery);

