function Accordion(settingsAry) {	
	//vars
	this.settings = settingsAry;
	
	//functions
	this.init = initAccordion;
	this.setAccordionSettings = setAccordionSettings;
	this.applyBarMousoverEffect = applyBarMousoverEffect;
	this.applyAccordion = applyAccordion;
	this.startAccordion = startAccordion;
	
	//effects
	this.fadeEffect = fadeEffect;
	
	return this.init();
}

function initAccordion(){
	this.setAccordionSettings(this.settings[0]);
	this.applyBarMousoverEffect(this.settings[1]);
	this.applyAccordion();
	this.startAccordion();
}

function setAccordionSettings(amountOfSections){
	switch(amountOfSections){
		case 1:
			this.openSectionWidth = 816;
		return;
		case 2:
			this.openSectionWidth = 783;
		return;
		case 3:
			this.openSectionWidth = 750;
		return;
		default:
			this.openSectionWidth = 816;
		return;
	}
}

function applyBarMousoverEffect(effect){
	switch(effect){
		case "fade":
			this.fadeEffect();
		return;
		default:
			this.fadeEffect();
		return;
	}
}

function fadeEffect(){
	var mouseoverEl,
	mouseoutEl;
	//apply the mouseenter effect
	$(".accordion-col").bind("mouseenter", function(){
    	mouseoverEl = $(this);
    	// if this is already the active cell, don't do anything
		if (!mouseoverEl.hasClass("currSection")){
			//if class highlighted is not assigned animate to highlighted state	
			if (!mouseoverEl.hasClass("highlighted")){
		    	mouseoverEl.addClass("highlighted");
				mouseoverEl.children().children('.overlay').fadeTo(100, 0.6); 
			}
		}
	});
	//apply the mousout effect
	$(".accordion-col").bind("mouseout", function(){
    	mouseoutEl = $(this);
    	//check the current queue on the element
		// if this is already the active cell, don't do anything
		if (! mouseoutEl.hasClass("currSection")){
			//if class highlighted is assigned animate back to normal width
    		if ( mouseoutEl.hasClass("highlighted")){
    			mouseoutEl.removeClass("highlighted");
				mouseoutEl.children().children('.overlay').fadeTo(100, 0.8);
			}
    	}
    });
}
	
function applyAccordion(){
	var el,
	    otherSections,
	    currentId = "",
	    otherTextBoxes,
	    previousSection = $('#starter');
		openSectionWidth = this.openSectionWidth;
	//prevent the links from being clicked when the tab is not expanded
	$("#accordion a").click(function(e){
    	if (!$(this).parent().parent().hasClass("currSection")){ 
	    	e.preventDefault(); 
    	}
    });
	// clicking on the bars opens the image
    $(".accordion-col").bind("click", function(){
    	var q = $(this).queue(); 
        el = $(this);
        // if this is already the active cell, don't do anything
        if (!el.hasClass("currSection") && q.length == 0) {
        	/***
        	 * resize the links
        	 */
        	//check if the area has 1 or 2 links
        	var tmpChildren = el.children('span').children('a');
        	var linkCount = tmpChildren.length;
        	//apply the width according to the link count
        	if(linkCount == 2){
        		//el.children('span').addClass('currSection');
        		var accordionlinks = el.children('span').children('.accordion_area_link');
        		$(accordionlinks).css("width", "50%");
        	}else if(linkCount == 1){
        		//el.children('span').addClass('currSection');
        		var accordionlinks = el.children('span').children('.accordion_area_link');
        		$(accordionlinks).css("width", "100%");
        	}
        	//set all other 
        	$('.accordion_area_link').not(accordionlinks).css("width", "0%");
        	/**
        	 * resize the link container
        	 */
        	//get all other sections
        	otherSections = $(".accordion-col").not(el);
        	//set active class for the image link
            el.children('.image_link').addClass('active');
        	//fade the overlay
        	otherSections.children().children('.overlay').fadeTo(400, 0.8);
            el.children().children('.overlay').fadeOut();
            //do the animation
            el.animate({
                width: openSectionWidth }, 
                800                
            ).addClass("currSection");
            // make other images small
            otherSections.animate({
                width: 33 },
                800,
                function(){
            		otherSections.children('.image_link').removeClass('active');
            	}
            ).removeClass("currSection");
            //change the text box
            currentId = el.children('.image_link').attr('id');
            otherTextBoxes = $('#accordion_text div').not('#'+currentId+'_text');
            $(otherTextBoxes).hide();
            $('#'+currentId+'_text').fadeIn(600);
            //set previous section, can be used on next click
            previousSection = el;
        } 
    });
}

function startAccordion(){
    $('.overlay').fadeTo(100, 0.65);
    $(".starter .overlay").hide();
    //triggers the starter
    $(".starter").trigger("click");
    $(".starter").removeClass('starter');
}
