//# jQuery - Horizontal Accordion
//# Version 2.00.00 Alpha 1
//#
//# portalZINE(R) - New Media Network
//# http://www.portalzine.de
//#
//# Alexander Graef
//# portalzine@gmail.com
//#
//# Copyright 2007-2009

(function($) {
	
	var HashToId = [];
	HashToId['home'] = '1';
	HashToId['about'] = '2';
	HashToId['records'] = '3';
	HashToId['d2f'] = '4';
	HashToId['management'] = '5';
	HashToId['contact'] = '6';
	
	var IdToHash = [];
	IdToHash[1] = 'home';
	IdToHash[2] = 'about';
	IdToHash[3] = 'records';
	IdToHash[4] = 'd2f';
	IdToHash[5] = 'management';
	IdToHash[6] = 'contact';
	
	$.hrzAccordion = {
		setOnEvent: function(i, container, finalWidth, settings) {
			$("#"+container+"Handle"+i).bind(settings.eventTrigger,function() {
				var status = $('[rel='+container+'ContainerSelected]').data('status');
						
				if (status ==1 && settings.eventWaitForAnim === true) {
					return false;	
				}

				if( $("#"+container+"Handle"+i).attr("rel") != container+"HandleSelected"){
					  settings.eventAction;
						
						$('[id*='+container+'Handle]').attr("rel","");

						$('[id*='+container+'Handle]').attr("class",settings.handleClass);

			   			$("#"+container+"Handle"+i).addClass(settings.handleClassSelected);
			
						document.title = "+1 > "+IdToHash[i+1].toUpperCase();
			   		
						// $("."+settings.contentWrapper).css({width: finalWidth+"px" });

						switch(settings.closeOpenAnimation) {
							case 1:
								if($('[rel='+container+'ContainerSelected]').get(0)) {

									$('[rel='+container+'ContainerSelected]').data('status',1);
							
									current_width = $('[rel='+container+'ContainerSelected]').width();
							
									$('[rel='+container+'ContainerSelected]').animate({width: "0px", opacity:"0"}, {
										queue:true,
										duration:settings.closeSpeed,
										easing:settings.closeEaseAction,
										complete: function(){
											$('[rel='+container+'ContainerSelected]').data('status',0);
										}, step: function(now){
											width = $(this).width();
											//new_width = finalWidth- (finalWidth  * (width/current_width));
											console.log("finalWidth: "+finalWidth)
											// console.log("current_width: "+current_width);
											if (finalWidth <= 780) {
												new_width = finalWidth - width;
											} else if (finalWidth >= 780 && finalWidth < 880) {
												new_width = finalWidth - width - 0;
											} else if(finalWidth >= 880 && finalWidth < 980) {
												new_width = finalWidth - width - 0;
											} else if (finalWidth >= 980 && finalWidth < 1180) {
												new_width = finalWidth - width - 80;
											} else if (finalWidth > 1180) {
												new_width = finalWidth - width - 60;
											};
											// new_width = finalWidth - width - 140;
											// new_width = finalWidth - 60;
											// new_width = 1000;
											// console.log("new_width: "+new_width);
											$('#'+container+'Content'+i).width(Math.ceil(new_width)).css("opacity","1");
										}
									});
								} else {
									$('[rel='+container+'ContainerSelected]').data('status',1);
										$('#'+container+'Content'+i).animate({width: finalWidth+"px",opacity:"1"}, {
										queue:false,
										duration:settings.closeSpeed,
										easing:settings.closeEaseAction,
										complete: function(){
											$('[rel='+container+'ContainerSelected]').data('status',0);
										}
									});
								}
							break;

						case 2:
							$('[id*='+container+'Content]').css({width: "0px"});
							$('#'+container+'Content'+i).animate({width: finalWidth+"px",opacity:"1"}, {
									queue:false,
									duration:settings.openSpeed, 
									easing:settings.openEaseAction,
									// complete: settings.completeAction
									complete: function() {
									}
							});
							break;
						}

						$('[id*='+container+'Content]').attr("rel","");			
						$("#"+container+"Handle"+i).attr("rel",container+"HandleSelected");
						$("#"+container+"Content"+i).attr("rel",container+"ContainerSelected");					
					}
				});	
			}
		};

	$.fn.extend({
		hrzAccordion: function(options) {
			this.settings = {
			eventTrigger	   		: "click",
			containerClass     		: "container",
			listItemClass      		: "listItem",					
			contentContainerClass  	: "contentContainer",
			contentWrapper     		: "contentWrapper",
			contentInnerWrapper		: "contentInnerWrapper",
			handleClass        		: "handle",
			handleClassOver    		: "handleOver",
			handleClassSelected		: "handleSelected",
			handlePosition     		: "right",
			handlePositionArray		: "", // left,left,right,right,right
			closeEaseAction    		: "swing",
			closeSpeed     			: 500,
			openEaseAction     		: "swing",
			openSpeed      			: 500,
			openOnLoad		   		: 2,
			hashPrefix		   		: "!/",
			eventAction		   		: function(){
								 	//add your own extra clickAction function here
								 	},
			completeAction	   		: function(){
								 	//add your own onComplete function here
								 	},
			closeOpenAnimation 		: 1,// 1 - open and close at the same time / 2- close all and than open next
			fixedWidth				: "",
			eventWaitForAnim		: true
				
		};
		if(options){
			$.extend(this.settings, options);
		}
			var settings = this.settings;

			return this.each(function(a){
				
				var container = $(this).attr("id") || $(this).attr("class");
				
				$(this).data('settings', settings);
				
				$(this).wrap("<div class='"+settings.containerClass+"'></div>");
			
				var elementCount = $('#'+container+' > li, .'+container+' > li').size();
												
				var containerWidth =  $("."+settings.containerClass).width();
				
				var handleWidth = $("."+settings.handleClass).css("width");
		
				handleWidth =  handleWidth.replace(/px/,"");
			    var finalWidth;
				var handle;
				
				if(settings.fixedWidth){
					finalWidth = settings.fixedWidth;
				}else{
					// console.log("containerWidth: "+containerWidth);
					// if (containerWidth >= 1280) {
						// finalWidth = (containerWidth-(elementCount*handleWidth)-handleWidth);
					// } else {
						finalWidth = (containerWidth-(elementCount*handleWidth)-handleWidth)+20;
					// }
					// console.log("containerWidth: "+containerWidth);
					
				}
				
				$('#'+container+' > li, .'+container+' > li').each(function(i) {
					$(this).attr('id', container+"ListItem"+i);
			   		$(this).attr('class',settings.listItemClass);
		       		$(this).html("<div class='"+settings.contentContainerClass+"' id='"+container+"Content"+i+"'>"
								 +"<div class=\""+settings.contentWrapper+"\">"
								 +"<div class=\""+settings.contentInnerWrapper+"\">"
								 +$(this).html()
								 +"</div></div></div>");
								
					if ( $("div",this).hasClass(settings.handleClass) ) {
						
						var html = $("div."+settings.handleClass,this).attr("id",""+container+"Handle"+i+"").html();
						$("div."+settings.handleClass,this).remove();
					 	handle = "<div class=\""+settings.handleClass+"\" id='"+container+"Handle"+i+"'>"+html+"</div>";
					} else {
						handle = "<div class=\""+settings.handleClass+"\" id='"+container+"Handle"+i+"'></div>";
					}

					if(settings.handlePositionArray) {
						splitthis = settings.handlePositionArray.split(",");
						settings.handlePosition = splitthis[i];
					}

					switch(settings.handlePosition ){
						case "left":
							$(this).prepend( handle );
							break;
						case "right":	
							$(this).append( handle );	
							break;
						case "top":	
							$("."+container+"Top").append( handle );	
							break;
						case "bottom":	
							$("."+container+"Bottom").append( handle );	
							break;
					}					
				
					$("#"+container+"Handle"+i).bind("mouseover", function(){
						$("#"+container+"Handle"+i).addClass(settings.handleClassOver);
					});
			    
					$("#"+container+"Handle"+i).bind("mouseout", function(){
						if( $("#"+container+"Handle"+i).attr("rel") != "selected"){
							$("#"+container+"Handle"+i).removeClass(settings.handleClassOver);
						}
					});

					$.hrzAccordion.setOnEvent(i, container, finalWidth, settings);

					$(window).bind("resize", resizeWindow);
					function resizeWindow( e ) {
						var newWindowWidth = $(window).width();
						var newWindowHeight = $(window).height();
						// console.log(newWindowWidth+"x"+newWindowHeight);
						if (newWindowWidth >= 1000 && newWindowHeight >= 640) {
							// console.log('show big');
							finalWidth = newWindowWidth;
						} else {
							// console.log('show small');
							finalWidth = newWindowWidth-100;
						}
						// console.log(settings.contentWrapper);
						// $.hrzAccordion.setOnEvent(i, container, finalWidth, settings);
							// $("#container").css({width: finalWidth+"px" });
					}
					
					if(i == elementCount-1){
						$('#'+container+",."+container).show();
					}

					if(settings.openOnLoad !== false && i == elementCount-1){
							var location_hash = location.hash;
							location_hash  = location_hash.replace("#", "");	
							var pagename = location_hash.replace("!/", "");
							if(location_hash.search(settings.hashPrefix) != '-1' ) {
							// if(location.hash && $('a[href=' + location.hash + ']').length > 0) {
								var tab = 1;
								location_hash  = location_hash.replace(settings.hashPrefix, "");
								var hash_parts = location_hash.split("/");
								if (hash_parts[0] == 'artists') {
									location_hash = HashToId[1];
									showArtist(hash_parts[1]);
								} else {
									location_hash = HashToId[location_hash];
								};
							}
							
							if(location_hash && tab ==1){
						 		$("#"+container+"Handle"+(location_hash)).attr("rel",container+"HandleSelected");
								$("#"+container+"Content"+(location_hash)).attr("rel",container+"ContainerSelected");
								$("#"+container+"Handle"+(location_hash-1)).trigger(settings.eventTrigger);
												
							}else{
								$("#"+container+"Handle"+(settings.openOnLoad)).attr("rel",container+"HandleSelected");
							    $("#"+container+"Content"+(settings.openOnLoad)).attr("rel",container+"ContainerSelected");	
								$("#"+container+"Handle"+(settings.openOnLoad-1)).trigger(settings.eventTrigger);
							}
					}
				});	
			});				
		}
	});
})(jQuery);	

