/*--- tabs ---*/
function initTabs(h_hold){
	$(h_hold).each(function(_ind,_el){
			var btn_h=$(_el);var _btn=$(_el).find('a.tab');var _a=0;
			_btn.each(function(_ind,_el){
					this._box=$('#'+_el.href.substr(_el.href.indexOf("#")+1));
					if ($(_el).hasClass('active')){this._box.css({display:'block'});_a=_ind;this._box.find(".hold").customScrollV();}else{this._box.css({display:'none'});}
					_el.onclick = function() {
							if (!$(this).hasClass('active')) {
									_btn.get(_a)._box.css({ display: 'none' });
									_btn.eq(_a).removeClass('active');
									this._box.css({ display: 'block' });
									$(this).addClass('active');
									_a = _ind;
									this._box.find(".hold").customScrollV();
							}
							return false;
					}
			});
	});
}
/*--- tab-container animation ---*/
function initNews(){
    var _speed=1000;
    $('div.tab-content').each(function(){
        var _hold=$(this);var _img=_hold.find('div.img-frame img');var _btn=_hold.find('div.hold li');var _a=(_btn.index(_btn.filter('.active:eq(0)'))!=-1)?(_btn.index(_btn.filter('.active:eq(0)'))):(0);
        _btn.removeClass('active').eq(_a).addClass('active');
        _img.parent().removeClass('active');
        _img.removeClass('active').css('opacity',0).eq(_a).addClass('active').css('opacity', 1);
        _img.eq(_a).parent().addClass('active');

        _btn.mouseenter(function(){changeEl(_btn.index(this));});
        function changeEl(_ind){
            if (_ind!=_a){
                _btn.eq(_a).removeClass('active');_btn.eq(_ind).addClass('active');
                _img.eq(_a).removeClass('active').animate({opacity:0},{queue:false,duration:_speed});
                _img.eq(_a).parent().removeClass('active');
                _img.eq(_ind).addClass('active').animate({opacity:1},{queue:false,duration:_speed});
                _img.eq(_ind).parent().addClass('active');
                $('div.tab-container div.scrollable').customScrollV();_a=_ind;
            }
        }
    });
}



/*--- custom scroll ---*/
var types = ['DOMMouseScroll', 'mousewheel'];
$.event.special.mousewheel = {
    setup: function() {
        if (this.addEventListener) for (var i = types.length; i; ) this.addEventListener(types[--i], handler, false);
        else this.onmousewheel = handler;
    },
    teardown: function() {
        if (this.removeEventListener) for (var i = types.length; i; ) this.removeEventListener(types[--i], handler, false);
        else this.onmousewheel = null;
    }
};
$.fn.extend({
    mousewheel: function(fn) {
        return fn ? this.bind("mousewheel", fn) : this.trigger("mousewheel");
    },
    unmousewheel: function(fn) {
        return this.unbind("mousewheel", fn);
    }
});
function handler(event) {
    var args = [].slice.call(arguments, 1), delta = 0, returnValue = true;
    event = $.event.fix(event || window.event);
    event.type = "mousewheel";
    if (event.wheelDelta) delta = event.wheelDelta / 120;
    if (event.detail) delta = -event.detail / 3;
    args.unshift(event, delta);
    return $.event.handle.apply(this, args);
}
jQuery.fn.customScrollV = function(_options) {
    var _options=jQuery.extend({lineWidth:15},_options);
    return this.each(function(){
        var _box = jQuery(this);
				
        if (_box.is(':visible')) {
            if (_box.children('.scroll-content').length == 0) {
                var line_w = _options.lineWidth;
                /*--- init part ---*/
                var scrollBar = jQuery('<div class="scroll-bar"><div class="scroll-up"></div><div class="scroll-line"><div class="scroll-slider"></div></div><div class="scroll-down"></div></div>');
                _box.wrapInner('<div class="scroll-content"><div class="scroll-hold"></div></div>').append(scrollBar);
                var scrollContent = _box.children('.scroll-content');
                var scrollSlider = scrollBar.find('.scroll-slider');
                var scrollSliderH = scrollSlider.parent();
                var scrollUp = scrollBar.find('.scroll-up');
                var scrollDown = scrollBar.find('.scroll-down');
                /*--- different variables ---*/
                var box_h = _box.height();
                var slider_h = 0;
                var slider_f = 0;
                var cont_h = scrollContent.height();
                var _f = false;
                var _f1 = false;
                var _f2 = true;
                var _t1, _t2, _s1, _s2;
                /*--- set styles ---*/
                _box.css({
                    position: 'relative',
                    overflow: 'hidden',
                    height: box_h
                });
                scrollContent.css({
                    position: 'absolute',
                    top: 0,
                    left: 0,
                    zIndex: 1,
                    height: 'auto'
                });
                scrollBar.css({
                    position: 'absolute',
                    top: 0,
                    right: 0,
                    zIndex: 2,
                    width: line_w,
                    height: box_h,
                    overflow: 'hidden'
                });
                scrollUp.css({
                    width: line_w,
                    height: line_w,
                    overflow: 'hidden',
                    cursor: 'pointer'
                });
                scrollDown.css({
                    width: line_w,
                    height: line_w,
                    overflow: 'hidden',
                    cursor: 'pointer'
                });
                slider_h = scrollBar.height();
                if (scrollUp.is(':visible')) slider_h -= scrollUp.height();
                if (scrollDown.is(':visible')) slider_h -= scrollDown.height();
                scrollSliderH.css({
                    position: 'relative',
                    width: line_w,
                    height: slider_h,
                    overflow: 'hidden'
                });
                slider_h = 0;
                scrollSlider.css({
                    position: 'absolute',
                    top: 0,
                    left: 0,
                    width: line_w,
                    height: slider_h,
                    overflow: 'hidden',
                    cursor: 'pointer'
                });
                box_h = _box.height();
                cont_h = scrollContent.height();
                if (box_h < cont_h) {
                    _f = true;
                    slider_h = Math.round(box_h / cont_h * scrollSliderH.height());
                    if (slider_h < 5) slider_h = 5;
                    scrollSlider.height(slider_h);
                    slider_h = scrollSlider.outerHeight();
                    slider_f = (cont_h - box_h) / (scrollSliderH.height() - slider_h);
                    _s1 = (scrollSliderH.height() - slider_h) / 10;
                    _s2 = (scrollSliderH.height() - slider_h) / 3;
                    scrollContent.children('.scroll-hold').css('padding-right', scrollSliderH.width());
                }
                else {
                    _f = false;
                    scrollBar.hide();
                    scrollContent.css({ width: _box.width(), top: 0, left: 0 });
                    scrollContent.children('.scroll-hold').css('padding-right', 0);
                }
                var _top = 0;
                /*--- element's events ---*/
                scrollUp.mousedown(function() {
                    _top -= _s1;
                    scrollCont();
                    _t1 = setTimeout(function() {
                        _t2 = setInterval(function() {
                            _top -= 4 / slider_f;
                            scrollCont();
                        }, 20);
                    }, 500);
                }).mouseup(function() {
                    if (_t1) clearTimeout(_t1);
                    if (_t2) clearInterval(_t2);
                }).mouseleave(function() {
                    if (_t1) clearTimeout(_t1);
                    if (_t2) clearInterval(_t2);
                });
                scrollDown.mousedown(function() {
                    _top += _s1;
                    scrollCont();
                    _t1 = setTimeout(function() {
                        _t2 = setInterval(function() {
                            _top += 4 / slider_f;
                            scrollCont();
                        }, 20);
                    }, 500);
                }).mouseup(function() {
                    if (_t1) clearTimeout(_t1);
                    if (_t2) clearInterval(_t2);
                }).mouseleave(function() {
                    if (_t1) clearTimeout(_t1);
                    if (_t2) clearInterval(_t2);
                });
                scrollSliderH.click(function(e) {
                    if (_f2) {
                        _top = e.pageY - scrollSliderH.offset().top;
                        /*
                        if(scrollSlider.offset().top + slider_h < e.pageY) _top += _s2;
                        else if(scrollSlider.offset().top > e.pageY) _top -= _s2;
                        */
                        scrollCont();
                    }
                    else {
                        _f2 = true;
                    }
                });
                var t_y = 0;
                scrollSlider.mousedown(function(e) {
                    t_y = e.pageY - $(this).position().top;
                    _f1 = true;
                }).mouseup(function() {
                    _f1 = false;
                });
                $('body').mousemove(function(e) {
                    if (_f1) {
                        _f2 = false;
                        _top = e.pageY - t_y;
                        scrollCont();
                    }
                }).mouseup(function() {
                    _f1 = false;
                });
                document.body.onselectstart = function() {
                    if (_f1) return false;
                }
                _box.bind('mousewheel', function(event, delta) {
                    if (_f) {
                        _top -= delta * _s1;
                        scrollCont();
                        if ((_top > 0) && (_top + slider_h < scrollSliderH.height())) return false;
                    }
                });
                function scrollCont() {
                    if (_top < 0) _top = 0;
                    else if (_top + slider_h > scrollSliderH.height()) _top = scrollSliderH.height() - slider_h;
                    scrollSlider.css('top', _top);
                    scrollContent.css('top', -_top * slider_f);
                }
                this.scrollResize = function() {
                    box_h = _box.height();
                    cont_h = scrollContent.height();
                    if (box_h < cont_h) {
                        _f = true;
                        scrollBar.show();
                        slider_h = Math.round(box_h / cont_h * scrollSliderH.height());
                        if (slider_h < 5) slider_h = 5;
                        scrollSlider.height(slider_h);
                        slider_h = scrollSlider.outerHeight();
                        slider_f = (cont_h - box_h) / (scrollSliderH.height() - slider_h);
                        if (cont_h + scrollContent.position().top < box_h) scrollContent.css('top', -(cont_h - box_h));
                        _top = -scrollContent.position().top / slider_f;
                        scrollSlider.css('top', _top);
                        _s1 = (scrollSliderH.height() - slider_h) / 15;
                        _s2 = (scrollSliderH.height() - slider_h) / 3;
                        scrollContent.children('.scroll-hold').css('padding-right', scrollSliderH.width());
                    }
                    else {
                        _f = false;
                        scrollBar.hide();
                        scrollContent.css({ top: 0, left: 0 });
                        scrollContent.children('.scroll-hold').css('padding-right', 0);
                    }
                }
            }
            else {
                this.scrollResize();
            }
        }
    });
}



/*--- IE6 hover ---*/
function ieHover(h_list, h_class) {
    if ($.browser.msie && $.browser.version < 7) {
        if (!h_class) var h_class = 'hover';
        $(h_list).mouseenter(function() {
            $(this).addClass(h_class);
        }).mouseleave(function() {
            $(this).removeClass(h_class);
        });
    }
}


$('document').ready(function() {
	$.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?id=63395829@N05&lang=en-us&format=json&jsoncallback=?", displayImages);
	function displayImages(data) {																																   
		var iCount = 0;								
		var htmlString = '<ul class="flickr-pictures">';
		var htmlStr = '';
		$.each(data.items, function(i,item){
				if (iCount==0){var addCl=' class="active"';}else{var addCl=' class=""';}
				var sourceSquare = (item.media.m).replace("_m.jpg", "_s.jpg");		
				var sourceBig = (item.media.m).replace("_m.jpg", ".jpg");		
				htmlString += '<li'+addCl+'><a id="flickr" href="' + sourceBig + '" rel="flickr" title="' + item.title + '">';
				htmlString += '<img class="last" src="' + sourceSquare + '" alt="' + item.title + '" title="' + item.title + '" width="60" height="60" />';
				htmlString += '</a></li>';
				htmlStr += '<a id="flickr" href="' + sourceBig + '" rel="flickr" title="' + item.title + '"><img '+addCl+' alt="'+item.title+'" src="'+sourceBig+'"/><span class="corn"></span></a>'
			iCount++;
		});		

	$('#tab4 .box').html(htmlStr);
	$('#tab4 .hold').html(htmlString + "</ul>");
	$("a#flickr").fancybox({'titlePosition':'over','overlayColor':'#000','overlayOpacity': 0.8});
  initNews();
	}

	$("#flickr").fancybox({'titlePosition':'over','overlayColor':'#000','overlayOpacity':0.8});
	$(".gallery").fancybox({'titlePosition':'over','overlayColor':'#000','overlayOpacity':0.8});
	
	getTwitters('twitters',{id:'karacigernakli',clearContents:true,count:50,withFriends:true,ignoreReplies:false,
			template:'<div class="social-frame"><div class="t">&nbsp;</div><div class="c"><div class="bx"><span class="avatar"><img height="50" width="50" src="%user_profile_image_url%" /></span><span class="tweets"><p>%text%</p><em class="date-tweet"><a href="http://twitter.com/%user_screen_name%/statuses/%id%" target="_blank">%time%</a></em></div></div><div class="b">&nbsp;</div></div>'
	});

    initTabs('.tab-titles');
		ieHover('.scroll-bar .scroll-slider');
    initNews();
    $('div.tab-container div.scrollable').customScrollV();
});




