if (!window.console) {
    if (!window.console || !console.firebug) {
        var names = ["log", "debug", "info", "warn", "error", "assert", "dir", "dirxml",
        "group", "groupEnd", "time", "timeEnd", "count", "trace", "profile", "profileEnd"];

        window.console = {};
        for (var i = 0; i < names.length; ++i) {
            window.console[names[i]] = function() {};
        }
    }
}

function empty() { console.log('empty function call'); }

(function($) {
    $.fn.newsFeed = function(options) {

        var i=0;

        var defaults = {
            'delay':2000
        };
        options = $.extend(defaults, options || {});
        options.list = [];

        function clear() {
            window.clearInterval(options.timerId);
        };

        return this.each( function() {
            options.parentId = $(this).parent().attr('id');

            $.fn.newsFeed.reload = function() {
            };

            $.fn.newsFeed.reStartAt = function(e) {
                element = e.target;

                while(/^article_/.test(element.id)==false) {
                    element = $(element).parent()[0];
                }
                for(var i in options.list) $(options.list[i]).removeClass('selected').css({'opacity':'0.7'});
                $(element).addClass('selected').css({'opacity':'1.0'});

                $.fn.newsFeed.load();
            };

            $.fn.newsFeed.interval = function() {
                if($('#'+options.parentId+' > li.selected')[0] == undefined) {
                    $(options.list[0]).addClass('selected').css({'opacity':'1.0'});
                }
                else {
                    var selected = $('#'+options.parentId+' > li.selected');
                    $(selected).removeClass('selected').css({'opacity':'0.7'});

                    if($(selected).next()[0] == undefined) {
                        $(options.list[0]).addClass('selected').css({'opacity':'1.0'});
                    }
                    else {
                        $(selected).next().addClass('selected').css({'opacity':'1.0'});
                    }
                }
                $.fn.newsFeed.load();
            };

            $.fn.newsFeed.load = function() {
                var selected = $('#'+options.parentId+' > li.selected')[0];
                
                if(selected == undefined) return;
                
                $('#teaserWait').show();
                $.post('/article/load-head-teaser/', {'articleId':selected.id.replace(/article_/, '')}, function(data) {
                    $('#headTeaserLeftCol').replaceWith(data);
                    clear();
                    options.timerId = window.setInterval($.fn.newsFeed.interval, options.delay);
                    $('#teaserWait').hide();
                });
            };

            options.list.push(this);

            if(!$(this).attr('id'))
                $(this).attr('id','article_' + i);
            $(this).click( function(e) { $.fn.newsFeed.reStartAt(e); });

            if(i === 0) {
                if(options.timerId) clear();
                options.timerId = window.setInterval($.fn.newsFeed.interval, options.delay);
            }
            else {
                $(this).css({'opacity':'0.7'});
            }

            i++;
        });
    }
}(jQuery));

(function($) {
    $.fn.myTab = function(options) {

        var i=0;
        var defaults = {
            'remote':false
        };
        options = $.extend(defaults, options || {});
        options.plist = [];
        options.list = [];

        function remote(element) {
            return $.get(element.href,'',function(data) { return data; })
        };

        return this.each( function() {
            
            $.fn.myTab.load = function(e) {
                
                if($(e.target).attr('disabled') == 'disabled') return;

                $(options.plist).removeClass('selected');
                $(e.target).parent().addClass('selected');

                if(options.remote == true) {
                    $(options.targetId).html('').html(remote(e.target));
                }
                else {
                    $('#' + options.targetId + ' > div').hide();
                    $(e.target.href.replace(/^.*(#.*)$/,'$1')).show();
                }
            };

            var lnk = $(this).find('a');
            options.list.push(lnk);
            options.plist.push(this);
            
            if($(lnk).attr('disabled') == 'disabled') $(lnk).css({'color':'#ccc'});

            if(!$(lnk).attr('id')) $(lnk).attr('id','myTabLnk'+i);
            $(lnk).click(function(e) { e.preventDefault(); $.fn.myTab.load(e);});

            if(options.target == undefined || options.target.length <= 0) {
                if(i>0) {
                    if(options.remote==false) $($(lnk).attr('href').replace(/^.*(#.*)$/,'$1')).hide();
                    $(this).removeClass('selected');
                }
            }
            else {

                $('#' + options.targetId + ' > div').hide();
                $('#' + options.target).show();

                if(lnk.attr('href') == '#' + options.target) {
                    $(this).addClass('selected');
                }
                else {
                    $(this).removeClass('selected');
                }

            }
            i++;
        });
    }
}(jQuery));


(function($) {
    $.fn.pcImage = function(options) {

        var defaults = {
            'delay':90,
            'intervalId':false,
            'step':38
        };
        options = $.extend(defaults, options || {});


        return this.each( function() {
            
            var count = 1;

            var element = this;

            $.fn.pcImage.Zoom = function() {
                window.clearInterval(options.intervalId);

                if(options.step > 0) {

                    $(element).attr('width', $(element).attr('width')-1);
                    $(element).attr('height', $(element).attr('height')-1);
    
                    options.intervalId = window.setInterval($.fn.pcImage.Zoom, options.delay);

                    options.delay += 2;
                    options.step--;
                }
            }
            $.fn.pcImage.Zoom();
        });
    }

}(jQuery));

(function($) {
    $.fn.pcCountdown = function(options) {

        var defaults = {
            'delay':1000,
            'intervalId':false
        };
        options = $.extend(defaults, options || {});

        return this.each( function() {

            options.current = this;
            options.strongs = $(options.current).find('strong');

            $.fn.pcCountdown.Step = function() {

                current_date = new Date();
                current_date_utc = Date.UTC(current_date.getFullYear(), current_date.getMonth(), current_date.getDay(), current_date.getHours(), current_date.getMinutes(), current_date.getSeconds());
                until_date_utc   = Date.UTC(options.until.getFullYear(), options.until.getMonth(), options.until.getDay(), options.until.getHours(), options.until.getMinutes(), options.until.getSeconds());

                current_diff = ((Date.parse(options.until.toGMTString()) - Date.parse(current_date.toGMTString()))/1000);

                if(parseInt(current_diff % 60) < 0) {
                    window.clearInterval(options.intervalId);
                    return;
                }

                $(options.strongs[0]).html('').html(parseInt(current_diff/86400));
                $(options.strongs[1]).html('').html(parseInt(current_diff/60/60 % 24) < 10 ? '0' + parseInt(current_diff/60/60 % 24) : parseInt(current_diff/60/60 % 24));
                $(options.strongs[2]).html('').html(parseInt(current_diff/60 % 60) < 10 ? '0' + parseInt(current_diff/60 % 60) : parseInt(current_diff/60 % 60));
                $(options.strongs[3]).html('').html(parseInt(current_diff % 60) < 10 ? '0' + parseInt(current_diff % 60) : parseInt(current_diff % 60));

                if(options.intervalId == false)
                    options.intervalId = window.setInterval($.fn.pcCountdown.Step, options.delay);
            }

            $.fn.pcCountdown.Step();

        });
    }

}(jQuery));

(function($) {
    $.fn.pcCarousel = function(options) {

        var defaults = {
            'displayItems':3,
            'moveItemCount':1,
            'duration':2000,
            'fancybox':false,
            'slideshow':false
        };
        var options = $.extend(defaults, options || {});
        options.check = true;

        function triggerFancybox(o) {

            var tpl;
            
            $('#fbTmpContent').remove();
            $('<div id="fbTmpContent"></div>').hide().appendTo('body');
            $(o.w).find('img').clone().each( function() {
                $(this).attr('src', $(this).attr('src').replace(/(\d+)(s|c|sic)(\d+)/, '$1o$3'));
                tpl = $('<a rel="fb">').append(this).append('</a>');
                $(tpl).appendTo('#fbTmpContent');
            });
            
            while($(o.w).find('img').length != $("#fbTmpContent a[rel='fb']").length) { setTimeout("empty", 700); }
            $("#fbTmpContent a[rel='fb']").fancybox({});
            $(tpl).trigger('click');
            $.fancybox.resize();
        }

        function move(o) {
            var count=1;
            if(options.check == false) return;
            options.check=false;
            $('img', o.w).css({'position':'relative'}).animate({"left": "-=" + o.iw + 'px'}, o.duration, function() {
                if(count == $('img', o.w).length) { options.check=true; clear(o.w, o) };
                count++;
            });
        };

        function clear(w,o) {
            $('img', w).css({'left':'0px'});

            var newElement = $('img:first', w).clone();
            $(w).append($(newElement));

            if(o.fancybox == true) {
                $(newElement).bind('click', function() {triggerFancybox(o)});
            } else {
                $(newElement).bind('click', function() {move(o)});
            }
            $(newElement).css({'left':'-' + o.iw + 'px'});
            $('img:first-child', w).remove();

            if(o.slideshow == true) move(o);
        };

        return this.each( function() {

            options.p  = this;
            options.w  = $('div:first-child', this)[0];
            options.iw = $('img:first-child', options.w).width();

            $('img', options.w).each( function() { $(this).hover(function(){$(this).css({'cursor':'pointer'});}); });

            $(options.w).css({'width':$('img', options.w).length*options.iw+'px'});

            $('img', options.w).each( function() {

                $(this).css({'float':'left'});

                if(options.fancybox == true) {
                    $(this).bind('click', function() {triggerFancybox(options)});
                } else {
                    $(this).bind('click', function(e) { window.location.href=e.target.rel });
                }
            });
            $(options.p).css({width: options.displayItems*options.iw+'px'});
            if(options.slideshow == true) {
                move(options);
            }
            else {
                $(options.p).css({width: options.displayItems*options.iw+28+'px'});
            }

        });
    }

}(jQuery));


(function($) {
    $.fn.pcBoxScale = function(options) {

        var defaults = {
            'diff':false
        };
        var options = $.extend(defaults, options || {});
        options.check = true;


        return this.each( function() {
            
            options.o = this;
            
            $.fn.pcBoxScale.scale = function(e) {
                e.preventDefault();

                if(e.pageX > options.pageX) {
                    options.diff = options.diff == false ? e.pageX - options.pageX : options.diff;
                    $(options.o).css({'width':(parseInt($(options.o).width()) + (options.diff)) + 'px'});
                    $(options.ref.o).css({'margin-left':(parseInt($(options.o).width()) + 7) + 'px'});
                } else {
                    options.diff = options.diff == false ? options.pageX - e.pageX : options.diff;
                    $(options.o).css({'width':(parseInt($(options.o).width()) - (options.diff)) + 'px'});
                    $(options.ref.o).css({'margin-left':(parseInt($(options.o).width()) + 7) + 'px'});
                }
            }
            
            $.fn.pcBoxScale.mdHandler = function(e) {
                e.preventDefault();
                options.pageX=e.pageX;
                $(document).bind('mousemove', function(e){$.fn.pcBoxScale.scale(e)});
                
                $(document).bind('mouseup', function(e) {
                    options.pageX=false;
                    options.diff =false;
                    $(document).unbind();
                    $.post('/admin/setting/set', {'setting':{'admin_panel_left_width':$(options.o).width()}}, function() {});
                });
            }
            
            $(this).bind('mousedown', function(e){$.fn.pcBoxScale.mdHandler(e)});
            
        });
    }

}(jQuery));


$(document).ready(function() {
    $('a:not(:has(img))').filter(function() {

      if(this.hostname && this.hostname !== location.hostname) {
          $(this).attr('target','_blank');
          if ($(this).hasClass('hide_external_link_ico')) return ;
          return this.hostname && this.hostname !== location.hostname;
      }
    }).after(' <img src="/system/external.png" alt="external link" />');
    $('a:has(img)').filter(function() {

        if(this.hostname && this.hostname !== location.hostname) {
            $(this).attr('target','_blank');
            return this.hostname && this.hostname !== location.hostname;
        }
    });
    //document.oncontextmenu=false;
});
