/*
*   @author     KeremCIU in YCEO
*   @date       July 2011
*   @license    www.keremciu.com
*/

//  Hashchange Author: "Cowboy" Ben Alman
(function($,e,b){var c="hashchange",h=document,f,g=$.event.special,i=h.documentMode,d="on"+c in e&&(i===b||i>7);function a(j){j=j||location.href;return"#"+j.replace(/^[^#]*#?(.*)$/,"$1")}$.fn[c]=function(j){return j?this.bind(c,j):this.trigger(c)};$.fn[c].delay=50;g[c]=$.extend(g[c],{setup:function(){if(d){return false}$(f.start)},teardown:function(){if(d){return false}$(f.stop)}});f=(function(){var j={},p,m=a(),k=function(q){return q},l=k,o=k;j.start=function(){p||n()};j.stop=function(){p&&clearTimeout(p);p=b};function n(){var r=a(),q=o(m);if(r!==m){l(m=r,q);$(e).trigger(c)}else{if(q!==m){location.href=location.href.replace(/#.*/,"")+q}}p=setTimeout(n,$.fn[c].delay)}$.browser.msie&&!d&&(function(){var q,r;j.start=function(){if(!q){r=$.fn[c].src;r=r&&r+a();q=$('<iframe tabindex="-1" title="empty"/>').hide().one("load",function(){r||l(a());n()}).attr("src",r||"javascript:0").insertAfter("body")[0].contentWindow;h.onpropertychange=function(){try{if(event.propertyName==="title"){q.document.title=h.title}}catch(s){}}}};j.stop=k;o=function(){return a(q.location.href)};l=function(v,s){var u=q.document,t=$.fn[c].domain;if(v!==s){u.title=h.title;u.open();t&&u.write('<script>document.domain="'+t+'"<\/script>');u.close();q.location.hash=v}}})();return j})()})(jQuery,this);

(function($){

  $.fn.ciuTab = function(options) {
    
    var opts = $.extend({}, $.fn.ciuTab.set, options);
	
        return this.each(function() {
            tab = $(this);
            var o = $.meta ? $.extend({}, opts, $(this).data()) : opts;
            // Degisken tanimlari
            tcont = o.contenttab;
            container = o.container;
            tselect = o.selectclass;
            tshowing = (o.showtab) -1; 
            type = o.type;
			var history = o.history;
			var magicline = o.magicline;
            content = $(this).parents(container).find(tcont);
            content.hide();
            // Hash algilama ve hash ile daha once tab degisimi yapildi mi sorgusu
            // Daha once tab degisimi yapildi ise o tab secili getirilir aksi takdirde tshowing degiskeninden gelen tab siralamasi gecerli olur
            clea = document.location.hash;
            if ((clea.indexOf('#!') != -1) && (history == true)) { 
                    clea = clea.replace('!','');
                    if ( $(container).find(clea).length ) {
                        $(clea).show();
                        $(window).load(function() {
                            $('html, body').delay(200).animate({scrollTop:$(clea).offset().top-100}, 200);
                        });
                        eshowing = $(clea).index();
                        $(clea).parents(container).find('li').eq(eshowing).addClass(tselect);
                    } else {
                        contID = content.eq(tshowing).attr('id');
                        content.eq(tshowing).show();
                        $(this).parents(container).find('li').eq(tshowing).addClass(tselect);
                    }
                    
            } else {
                contID = content.eq(tshowing).attr('id');
                content.eq(tshowing).show();
                $(this).parents(container).find('li').eq(tshowing).addClass(tselect);
            } 
               
				$.fn.ciuTab.setup(tab, container, tcont, type, tselect, history, magicline);
				if (history == true) {
					$.fn.ciuTab.hash(tab, container, tcont, type);
				}
        });
    };
    // Sayfa icerisinde ki bir link ile tabin degisimini saglar
    // Ornek link: <a href="#!PaymentOptions" class="scrolltab">Taksit Secenekleri</a>
    $.fn.scrollTab = function(container) {
        $(this).click(function() {
            cleac = $(this).attr('href');
            clea = cleac.replace('!','');
                if ( $(document).find(clea).length ) {
                    if( $(clea).is(':visible') ) {
                        $('html, body').animate({scrollTop:$(clea).offset().top-100}, 200);
                    } else {
                        window.location.hash = cleac;
                        window.location.reload();
                    }
                       
                }
        });
    };
	// Sayfanin hashi degistiginde yapacagi islem
	$.fn.ciuTab.hash = function(element, cont, content, type) {
    
        $(window).hashchange(function() {
            valId = window.location.hash;
            if (valId && valId != "") { 
                $.fn.ciuTab.changeTab(element, cont, content, type, valId);
            }
        });
    
	}
    // tab icerigi burada duzenlenir ve fonksiyonlar atanir
    $.fn.ciuTab.setup = function(element, cont, content, type, tselect, history, magicline) {
	
        // tab icerisinde magicline kullanimi icindir
		if (magicline == true) {
			element.append("<li id='magic-line'></li>");
			var $magicLine = $("#magic-line");
			if ($('.'+tselect).size()>0)	{	$magicLine.width(element.find('.'+tselect).width()).css("left", element.find('.'+tselect).position().left).data("origLeft", $magicLine.position().left).data("origWidth", $magicLine.width());	}
		
			// tab listesinde tabin uzerine gelince yapacagi islem
			element.find('li').hover(function(e) {
				t = $(this);
				leftPos = t.position().left;
				newWidth = t.width();
				$magicLine.stop().animate({left: leftPos, width: newWidth });
			}, function() {
				$magicLine.stop().animate({left: $magicLine.data("origLeft"), width: $magicLine.data("origWidth") });
			});
		}
        // tab listesinde tabin uzerine tiklaninca yapacagi islem
        element.find('li').click(function(e) {
			t = $(this);
			if (magicline == true) {
				leftPos = t.position().left;
				newWidth = t.width();
				$magicLine.stop().animate({
					left: leftPos,
					width: newWidth
				});
				$magicLine.data("origLeft", leftPos).data("origWidth", newWidth);
			}
            showed = t.prevAll().length;
            val = t.parents(cont).find(content);
			valId = '#!'+ val.eq(showed).attr('id');
			e.preventDefault();
            t.parents(cont).find('li').each(function() { if ($(this).hasClass(tselect)) { onshowing = $(this).index(); } });
                if (onshowing != showed) { 
                    $.fn.ciuTab.changeTab(t, cont, content, type, valId);
					if (history == true) {
						window.location.hash = valId;
					}
                }
       });
    }
    // tab degistirme fonksiyonu kendisi son fonksiyondur cok dikkatli olunmasi gerekir duzenlerken
    $.fn.ciuTab.changeTab = function(element, cont, content, type, valID) {
        // valID eger hashten geliyorsa #! seklinde gelecektir biz IDyi salt halde almak icin !ni ayristiriyoruz
        valID = valID.replace('!','');
        valCont = $(valID).parents(cont);
        val = valCont.find(content);
        valItem = valCont.find(valID);
        // index degerini alarak siralamayi diger listede kullanabilecegimiz sekilde elde ediyoruz
        valIndex = valItem.index();
        valTab = valItem.parents(cont).find('li');
        // listede secili classini bastiriyoruz yeni taba
        valTab.removeClass(tselect);
        valTab.eq(valIndex).addClass(tselect);
        // outerHeight ile padding ve margin bile olsa sayfa sekmeden bir diger taba gecimini sagliyoruz ve tum tablari kapatiyoruz
        valOffset = valItem.outerHeight();
        val.parent('div').height(valOffset);
        val.hide();
        
            if (type == 'fade' && !$.browser.msie) {
                valItem.fadeIn("fast");
            } else {
                valItem.show();
            }
    }
    // DEGISKENLER
    // type                     classic/fade olmak uzre show/fade efektlerini ifade etmektedir
    // container                tabin ul listesi ve tabin icerik listesini kapsayan div
    // contenttab               tabin icerik listesini iceren div
    // showtab                  sayfa ilk yuklenirken tabin ul listesinden hangi siradakini gosterecegini belirtir
    // selectclass              secili tabin hangi classi alacagini belirtir
	// history					backspace tusu ile secilen tablar arasi dolasimi saglar
  	$.fn.ciuTab.set = {
      'type' : 'classic',
      'container' : '.tabBox',
	  'contenttab' : '.tabcontent',
	  'showtab' : '1',
	  'history' : true,
	  'selectclass' : 'selected',
	  'magicline' : false
    };
  

})(jQuery);
