/*
 * .tabSwitch
 * Version: 1.0
 * http://www.hieu.co.uk/blog/index.php/tabswitch/
 *
 * Copyright (c) 2009 Hieu Pham - http://www.hieu.co.uk
 * COMMON DEVELOPMENT AND DISTRIBUTION LICENSE (CDDL)
 * http://www.opensource.org/licenses/cddl1.php
 *
 * Date: 14/04/2009
 */
(function(a){a.fn.tabSwitch=function(b,i,g){var c={type:"slide",cols:2,toggle:"fade",ease:40,easeType:"linear",loopback:1,width:400,height:400,index:0,speed:500,interval:5000,step:1,wrapperClass:"",viewportClass:""};var l=a.extend(c,i);var f=this;var d=f.eq(1);var e=f.eq(1).get(0);if(!a.isFunction(g)){var h=function(){a.data(e,"index",l.index)}}else{var h=function(){a.data(e,"index",l.index);g()}}StoreToCache=function(){a.data(e,"type",l.type);a.data(e,"toggle",l.toggle);a.data(e,"cols",l.cols);a.data(e,"ease",l.ease);a.data(e,"easeType",l.easeType);a.data(e,"index",l.index);a.data(e,"loopback",l.loopback);if(d.attr("style")){a.data(e,"orgAttr",d.attr("style"))}else{a.data(e,"orgAttr","")}};backFromCache=function(){f.attr("style",a.data(e,"orgAttr"));var m=a("#ViewPort"+a.data(e,"wraperindex"));m.replaceWith(f);stopAuto();a.removeData(e)};var j=function(){StoreToCache();if(l.width){f.width(l.width)}if(l.height){f.height(l.height)}var n=0;while(jQuery("#WideDiv"+n).length>0){n++}a.data(e,"wraperindex",n);var o="WideDiv"+a.data(e,"wraperindex");f.wrapAll("<div id='"+o+"'></div>");var p=a("#"+o);p.addClass(l.wraperClass);p.css({position:"relative"});p.wrap("<div id='"+o.replace("WideDiv","ViewPort")+"'></div>");var m=a("#"+o.replace("WideDiv","ViewPort"));m.width(l.width);m.height(l.height);m.css({display:"block",overflow:"hidden",position:"relative"});m.addClass(l.viewportClass);switch(l.type){case"slide":f.css({"float":"left"});p.width((l.width+2)*f.length);break;case"scroll":p.width(l.width);p.height((l.height+2)*f.length);break;case"toggle":p.width(l.width);p.height(l.height);f.css({position:"absolute",left:"-999px"});f.eq(l.index).css({left:"0px",top:"0px"});f.eq(l.index).css("opacity",1);break;case"table":p.width(l.width*l.cols);f.css("float","left")}k()};var k=function(){var r=0;if(!l.easeType){l.easeType=(e,"easeType")}if(l.index>f.length-1){if(a.data(e,"loopback")!=0){l.index=0;r=-a.data(e,"ease")}else{return}}if(l.index<0){if(a.data(e,"loopback")!=0){l.index=f.length-1;r=a.data(e,"ease")}else{return}}var p="WideDiv"+a.data(e,"wraperindex");var s=a("#"+p);switch(a.data(e,"type")){case"slide":if(r!=0){var m=(parseInt(s.css("left").replace("px",""))+r);s.animate({left:m+"px"},l.speed,function(){s.animate({left:-(f.outerWidth(true)*l.index)+"px"},l.speed,l.easeType,h())})}else{s.animate({left:-(f.outerWidth(true)*l.index)+"px"},l.speed,l.easeType,h())}break;case"scroll":if(r!=0){var m=(parseInt(s.css("top").replace("px",""))+r);s.animate({top:m+"px"},l.speed,function(){s.animate({top:-(f.outerHeight(true)*l.index)+"px"},l.speed,l.easeType,h())})}else{s.animate({top:-(f.outerHeight(true)*l.index)+"px"},l.speed,l.easeType,h())}break;case"toggle":f.eq(l.index).css({left:"0px",top:"0px"});switch(a.data(e,"toggle")){case"fade":f.eq(l.index).css({opacity:0});f.eq(l.index).animate({opacity:1},l.speed);if(a.data(e,"index")!=l.index){f.eq(a.data(e,"index")).animate({opacity:0},l.speed,function(){a(this).css("left",-999);h()})}break;case"toggle":if(a.data(e,"index")!=l.index){f.eq(l.index).css({display:"none"});f.eq(a.data(e,"index")).slideUp(l.speed,function(){f.eq(l.index).slideDown(l.speed,function(){h()});a(this).css("left",-999)})}break;case"show":if(a.data(e,"index")!=l.index){f.eq(l.index).css({display:"none"});f.eq(a.data(e,"index")).hide(l.speed,function(){f.eq(l.index).show(l.speed,function(){h()});a(this).css("left",-999)})}break;case"noeffect":if(a.data(e,"index")!=l.index){f.eq(a.data(e,"index")).css("left",-999);h()}break}break;case"table":var o=a.data(e,"cols");var q=-(l.index%o)*f.width();var n=-Math.floor(l.index/o)*f.height();s.animate({left:q},l.speed,l.easeType,function(){s.animate({top:n},l.speed,l.easeType,h())});break;default:a("html,body").animate({scrollTop:f.eq(l.index).offset().top},l.speed);break}};moveStep=function(){var m=a.data(e,"index");l.index=parseInt(m)+parseInt(l.step);k()};startAuto=function(){a.data(e,"AutoSwitch",setInterval(moveStep,l.interval))};stopAuto=function(){clearInterval(a.data(e,"AutoSwitch"));a.removeData(e,"AutoSwitch")};toggleAuto=function(){if(isAuto()){stopAuto()}else{startAuto()}};isAuto=function(){if(a.data(e,"AutoSwitch")){return true}else{return false}};if(!b){b=""}switch(b.toLowerCase()){case"index":if(a.data(e,"index")){return a.data(e,"index")}else{return 0}break;case"moveto":k();break;case"movestep":moveStep();break;case"destroy":backFromCache();break;case"create":j();break;case"isauto":return isAuto();break;case"toggleauto":toggleAuto();break;case"startauto":startAuto();break;case"stopauto":stopAuto();break}}})(jQuery);
