/* 
    Document   : slide_control.js
    Created on : 04-Mar-2011
    Author     : Matthew Asbury http://madesignuk.com/
    Description:
        jQuery class to handle slide gallery in header section
*/

$(function(){

    Gallery = new function(){
        
        var slideObject = $(".slide-gallery");
        var leftControl;
        var rightControl;
        var imageWidth;
        var imageCount;
        var imageArray = [];
        var pos;
        var sliderID;
        
        this.construct = function(){
            Gallery.fetchImages();
            Gallery.addControls();
            Gallery.setVars();
            Gallery.hideControls(800);
            Gallery.bindActions();
            Gallery.startSlider();
        }
        
        this.addControls = function(){
            slideObject.prepend('<a href="" class="slide-control left-control">Previous</a>\n<a href="" class="slide-control right-control">Next</a>');
        }
        
        this.setVars = function(){
            Gallery.leftControl = $(".left-control");
            Gallery.rightControl = $(".right-control");
            Gallery.imageWidth = $(slideObject, "ul li:first-child img").width();
            Gallery.pos = 0;
        }
        
        this.bindActions = function(){
            slideObject.live('mouseenter', function(){
                Gallery.pauseSlider();
                Gallery.showControls();
            });
            slideObject.live('mouseleave', function(){
                Gallery.startSlider();
                Gallery.hideControls();
            });
            Gallery.leftControl.live('click', function(event){
                Gallery.slideRight();
                event.preventDefault();
            });
            Gallery.rightControl.live('click', function(event){
                Gallery.slideLeft();
                event.preventDefault();
            });
        }
        
        this.showControls = function(){
            Gallery.leftControl.stop().animate({
                left: '0px'
            }, 200);
            Gallery.rightControl.stop().animate({
                right: '0px'
            }, 200);
        }
        
        this.hideControls = function(setDelay){
            if(setDelay === undefined){
                setDelay = 0;
            }
            Gallery.leftControl.stop().delay(setDelay).animate({
                left: '-' + Gallery.leftControl.width() + 'px'
            }, 200);
            Gallery.rightControl.stop().delay(setDelay).animate({
                right: '-' + Gallery.rightControl.width() + 'px'
            }, 200);
        }
        
        this.startSlider = function(){
            Gallery.sliderID = setInterval("Gallery.slideLeft()", 5000);
        }
        
        this.pauseSlider = function(){
            clearInterval(Gallery.sliderID);
        }
        
        this.slideLeft = function(skipAnimate){
            if($("ul:animated", slideObject).length == 0){
                currPos = $("ul", slideObject).css('left');
                currPos = currPos.substring(0, (currPos.length - 2));
                if(isNaN(currPos)){
                    currPos = 0;
                }
                newPos = (currPos - Gallery.imageWidth);
                if((newPos + $(".slide-gallery ul").width()) != 0){
                    if(skipAnimate !== undefined){
                        $("ul", slideObject).css({
                            left: newPos + 'px'
                        });
                    } else {
                        $("ul", slideObject).stop(false, true).animate({
                            left: newPos + 'px'
                        }, 300);
                    }
                } else {
                    Gallery.slideRight(false, true);
                }
            }
        }
        
        this.slideRight = function(skipAnimate, toStart){
            if($("ul:animated", slideObject).length == 0){
                currPos = $("ul", slideObject).css('left');
                currPos = currPos.substring(0, (currPos.length - 2));
                newPos = ((currPos * 1) + (Gallery.imageWidth * 1));
                if(newPos <= 0){
                    if(toStart != undefined){
                        $("ul", slideObject).stop(false, true).animate({
                                left: '0px'
                            }, 300);
                    } else {
                        if(skipAnimate !== undefined){
                            $("ul", slideObject).css({
                                left: newPos + 'px'
                            });
                        } else {
                            $("ul", slideObject).stop(false, true).animate({
                                left: newPos + 'px'
                            }, 300);
                        }
                    }
                }
            }
        }
        
        this.fetchImages = function(){
            $.get("assets/js/ajax_image_fetch.php", function(data){
                if(data.length > 0){
                    ulWidth = 696 * data.length;
                    slideObject.append('<ul style="width: ' + ulWidth + 'px;" class="clearfix">');
                    $.each(data, function(i, v){
                        $("ul", slideObject).append('<li><img src="assets/images/gallery/' + v + '" alt="' + v + '" /></li>')
                    });
                    slideObject.append('</ul>');
                }                   
            }, 'json');
        }
        
    }
    
    Gallery.construct();

});

//Copyright ? 2004 Angela C. Buraglia & DWfaq.com
//All Rights Reserved. Not for distribution. support@dwfaq.com
//Support Newsgroup: news://support.dwfaq.com/support

function dwfaq_getCSSPropertyValue(obj,cP,jP){
	if(typeof(obj)!='object'){var obj=document.getElementById(obj);}
	if(typeof(obj.currentStyle)!='object'){
		return (typeof(document.defaultView)=='object')?
		document.defaultView.getComputedStyle(obj,'').getPropertyValue(cP):
		obj.style.getPropertyValue(cP);}
	else{
		return (navigator.appVersion.indexOf('Mac')!=-1)?
		obj.currentStyle.getPropertyValue(cP):
		obj.currentStyle.getAttribute((jP)?jP:cP);}
}

function dwfaq_ToggleOMaticDisplay(){
	var obj,cS,args=dwfaq_ToggleOMaticDisplay.arguments;document.MM_returnValue=(typeof(args[0].href)!='string')?true:false;
	for(var i=1;i<args.length;i++){obj=document.getElementById(args[i]);
		if(obj){cS=dwfaq_getCSSPropertyValue(obj,'display');
			if(!obj.dwfaq_OD){obj.dwfaq_OD=(cS!='none'&&cS!='')?cS:(obj.tagName.toUpperCase()=='TR' && cS!=='none')?'':
			(obj.tagName.toUpperCase()=='TR' && typeof(obj.currentStyle)!='object')?'table-row':'block';}
			obj.style.display=(cS!='none')?'none':obj.dwfaq_OD}}
}

function dwfaq_ToggleOMaticClass(){
	var obj,cN,args=dwfaq_ToggleOMaticClass.arguments;document.MM_returnValue=(typeof(args[0].href)!='string')?true:false;
	for(var i=1;i<args.length-1;i+=2){obj=document.getElementById(args[i]);
		if(obj){cN = (typeof(obj.currentStyle)!='object')?'class':'className';
			if(!obj.dwfaq_OC){obj.dwfaq_OC=(obj.className=='')?true:obj.className;}
			if(obj.dwfaq_OC&&obj.className==args[i+1]){
				(obj.dwfaq_OC==true)?obj.className='':obj.className=obj.dwfaq_OC;}
			else{obj.className=args[i+1];}}}
}
