/*
 * @author			Daniyal Hamid
 * 
 * @license			This JavaScript file is a commercial file, available for purchase at 
 *					http://codecanyon.net/user/daniyal/portfolio. Any illegal copying, 
 *					distribution, packaging or re-production of this script for commercial or 
 *					personal use is strictly prohibited and will be considered theft.
 *
 * @copyright		The author is the first owner of copyright and reserves all rights to
 *					all written work contained in this file. Distribution, re-production
 *					or commercial use of the written work in this file, without the author's 
 * 					signed permission, prior consent or a valid license, is strictly prohibited.
 *					The author is protected by the "Copyright, Designs and Patents Act 1988" of 
 *					the United Kingdom. Any infringement of the copyright, in or outside of the
 *					United Kingdom, may result in a lawsuit.
 */

var Swap=new Class({Implements:[Options],options:{index:0,normalFlowAtStart:false,activeLinkClass:"active"},initialize:function(b,a){this.setOptions(a);try{if(null==document.id(b)){throw new Error("Specified links container object not found!")}else{this.array=document.id(b).getElements("a")}if(this.array==""){throw new Error("No anchor element(s) found in the specified links container object.")}else{if(null==this.array[this.options.index]){throw new Error('Expecting an element (at index "'+this.options.index+"\") which doesn't exist.")}}this.linksContainerID=b;this.array.each(function(f,e,d){var g=this.toID(f.get("href"));if(null==document.id(g)){throw new Error('Expecting an anchor element (with id "'+g+"\") which doesn't exist!")}f.addEvent("click",function(i){if(!i.target){var h=new Event(i);h.target=i;i=h}else{i.preventDefault()}this.array[this.options.index].removeClass("active");this.array[e].addClass("active");this.options.index=e;if(this.isNormalFlow){this.normalFlow()}else{this.requestOnly()}}.bind(this))}.bind(this));document.id(document.body).addEvent("keydown",function(d){if(d.key=="left"){if(this.options.index>0){document.id(this.array[this.options.index-1]).fireEvent("click",this.array[this.options.index-1])}}else{if(d.key=="right"){if(this.options.index<(this.array.length-1)){document.id(this.array[this.options.index+1]).fireEvent("click",this.array[this.options.index+1])}}}}.bind(this));this.prev=null;this.next=null}catch(c){alert(c.name+': "'+c.message+'"');this.error=true;return}},run:function(){if(this.error){return}if(this.options.normalFlowAtStart){this.normalFlow()}else{this.requestOnly()}this.array[this.options.index].addClass("active")},attachToggle:function(a,c){try{if(document.getElements(a)==""){throw new Error('No elements matching the selector "'+a+'" found in the document!')}document.getElements(a).each(function(d){document.id(d).addEvent("click",function(f){f.preventDefault();if(this.isNormalFlow){this.requestOnly()}else{this.normalFlow()}if(typeof c=="function"){c(f.target,this.isNormalFlow)}}.bind(this))}.bind(this))}catch(b){alert(b.name+': "'+b.message+'"');return}},attachNormalFlow:function(a,c){try{if(document.getElements(a)==""){throw new Error('No elements matching the selector "'+a+'" found in the document!')}document.getElements(a).each(function(d){document.id(d).addEvent("click",function(g){g.preventDefault();var f=false;if(!this.isNormalFlow){this.normalFlow();f=true}if(typeof c=="function"){c(f)}}.bind(this))}.bind(this))}catch(b){alert(b.name+': "'+b.message+'"');return}},attachRequestOnly:function(a,c){try{if(document.getElements(a)==""){throw new Error('No elements matching the selector "'+a+'" found in the document!')}document.getElements(a).each(function(d){document.id(d).addEvent("click",function(g){g.preventDefault();var f=false;if(this.isNormalFlow){this.requestOnly();f=true}if(typeof c=="function"){c(f)}}.bind(this))}.bind(this))}catch(b){alert(b.name+': "'+b.message+'"');return}},attachPrevious:function(a){try{if(document.getElements(a)==""){throw new Error('No elements matching the selector "'+a+'" found in the document!')}this.prev=a;document.getElements(a).each(function(c){c.addEvent("click",function(d){d.preventDefault();if(this.options.index>0){document.id(this.array[this.options.index-1]).fireEvent("click",this.array[this.options.index-1])}}.bind(this));c.setStyle("display",((this.isNormalFlow)?"none":""))}.bind(this))}catch(b){alert(b.name+': "'+b.message+'"');return}},attachNext:function(a){try{if(document.getElements(a)==""){throw new Error('No elements matching the selector "'+a+'" found in the document!')}this.next=a;document.getElements(a).each(function(c){c.addEvent("click",function(d){d.preventDefault();if(this.options.index<(this.array.length-1)){document.id(this.array[this.options.index+1]).fireEvent("click",this.array[this.options.index+1])}}.bind(this));c.setStyle("display",((this.isNormalFlow)?"none":""))}.bind(this))}catch(b){alert(b.name+': "'+b.message+'"');return}},attachGoto:function(a,b){try{if(document.getElements(a)==""){throw new Error('No elements matching the selector "'+a+'" found in the document!')}document.getElements(a).each(function(d){d.addEvent("click",function(f){f.preventDefault();if(b){document.id(this.linksContainerID).scrollIntoView(true)}else{document.id(this.toID(d.get("href"))).scrollIntoView(true)}}.bind(this))}.bind(this))}catch(c){alert(c.name+': "'+c.message+'"');return}},normalFlow:function(){if(null!=this.prev&&null!=this.next){document.getElements(this.prev).setStyle("display","none");document.getElements(this.next).setStyle("display","none")}this.array.each(function(b,a){document.id(this.toID(b.get("href"))).setStyle("display","")}.bind(this));document.id(this.toID(this.array[this.options.index].get("href"))).scrollIntoView(true);this.isNormalFlow=true},requestOnly:function(){if(null!=this.prev&&null!=this.next){document.getElements(this.prev).setStyle("display",(((this.options.index-1)==-1)?"none":""));document.getElements(this.next).setStyle("display",(((this.options.index+1)==this.array.length)?"none":""))}this.array.each(function(b,a){if(a!=this.options.index){document.id(this.toID(b.get("href"))).setStyle("display","none")}else{document.id(this.toID(b.get("href"))).setStyle("display","")}}.bind(this));this.isNormalFlow=false},toID:function(a){return a.substring(a.lastIndexOf("#")+1,a.length)},getNormalFlow:function(){return this.isNormalFlow}});