//Global Variables
var StopRightMotion = false;
var StopLeftMotion = false;
var final_left_position;

jQuery.fn.exists = function(){return jQuery(this).length>0;}

// Run the scripts
$(document).ready(function(){
	initLightbox();
	
	if($("#scroller").exists()){
	    $("#scroller").onImagesLoad({
	        selectorCallback: initSlider
	    });
	}
	
	if($("#gallery_roller").exists()){
	    $("#gallery_roller").onImagesLoad({
	        selectorCallback: initGallery
	    });
	}
	
	if($("#images_roller").exists()){
	    $("#images_roller").onImagesLoad({
	        selectorCallback: initImages
	    });
	}
	
	
	//initSlider();
	//initGallery();
	initVideos();
	initVideoSlider();
	//initImages();
	
	/*$(".col").columnize({
	    width: 330,
	    columns: 2
	});*/
	
});



// JQuery Lightbox
// http://leandrovieira.com/projects/jquery/lightbox/

function initLightbox(){
	$('a[rel=lightbox]').lightBox();
};

function initSlider(){
    if($("#scroller").exists()){
	    final_left_position =  $("#scroller").position().left;
	}
	var item_count = $("#scroller ul li.item").length;
	var scroller_width = (710 * item_count)+'px';
	var max_position_left = 0 - ((item_count - 1) * 710);
	var DoubleClicked = false;
	
	//Set Scroller Width
	$("#scroller").css('width', scroller_width);
	
	//Back Button
	$("#testimonials_container .back").click(function(){
	    if(final_left_position != 0){
	        RightMotion(false);
	        $("#scroller").animate({left: "+=710px"}, 500);
	        final_left_position += 710;
	    }else{
	        if(StopLeftMotion != true){
	            $("#scroller").effect("bounce", { direction:'right', times:2, distance:50 }, 200, LeftMotion(true));
	        }
	    }
	});
	
	//More Button
	$("#testimonials_container .more").click(function(){
	    if(final_left_position != max_position_left){
	        LeftMotion(false);
	        $("#scroller").animate({left: "-=710px"}, 500);
	        final_left_position -= 710;
	    }else{
	        if(StopRightMotion != true){
	            $("#scroller").effect("bounce", { direction:'left', times:2, distance:50 }, 200, RightMotion(true));
	        }
	    }
	});
}

function LeftMotion(val){
    StopLeftMotion = val;
}

function RightMotion(val){
    StopRightMotion = val;
}



function initGallery(){
    if($("#gallery_roller").exists()){
	    final_left_position =  $("#gallery_roller").position().left;
	}
	
	var item_count = $("#gallery_roller ul li.item").length;
	var scroller_width = 0;
	var max_position_left;
	var DoubleClicked = false;
	var window_width = 750;
	
	$("#gallery_roller ul li").each(function(){
	    //scroller_width += $(this).width() + 10;
	    //scroller_width += $(this).outerWidth(); // outerWidth() gets the width including padding and border.
	    scroller_width += $(this).find('img').width() + 15;
	    //console.log($(this).find('img').width() + 9);
	});
	
	max_position_left = window_width - scroller_width;
	var scroller_width_raw = scroller_width;
	scroller_width = scroller_width+'px';
	var division = Math.abs(max_position_left) / item_count;
	
	//console.log("division: " + division);
	//console.log("max_position_left: " + max_position_left);
	//console.log("scroller_width_raw: " + scroller_width_raw);
	//console.log("item_count: " + item_count);
	
	//Set Scroller Width
	$("#gallery_roller").css('width', scroller_width);
	
	//Init Slider
	/*$("#gallery_slider").slider({
	    min: 0,
	    max: item_count,
	    step: 1,
	    value: 0,
	    slide: function(event, ui){
	        //final_left_position = 0 - (ui.value * (scroller_width_raw / item_count));
	        //console.log(Math.floor(scroller_width_raw / item_count));
	        
            var movement = 0 - (division * ui.value);
            $("#gallery_roller").animate({left: movement+"px"}, 500);
            final_left_position = movement;
            //console.log(division);
	    }
	});*/
	
	
	//Back Button
	$("#gallery_container .back").click(function(){
	    if(final_left_position != 0){
	        //SLIDE
	        
	        if(final_left_position < -200){ // enough space for movement
	            RightMotion(false);
	            $("#gallery_roller").animate({left: "+=765px"}, 500);
	            final_left_position += 765;
	        }else{ // not enough space, lets fix
	            /*RightMotion(false);
	            var to_move = Math.abs(final_left_position);
	            $("#gallery_roller").animate({left: "+="+to_move+"px"}, 500);
	            final_left_position += to_move;*/
	            $("#gallery_roller").effect("bounce", { direction: 'right', times: 2, distance: 50 }, 200, LeftMotion(true));
	        }
	        $("#gallery_slider").slider('option', 'value', Math.floor(Math.abs(final_left_position) / division));
	    }else{
	        //BOUNCE
	        if(StopLeftMotion != true){
	            $("#gallery_roller").effect("bounce", { direction:'right', times:2, distance:50 }, 200, LeftMotion(true));
	        }
	    }
	});
	
	// More Button
	$("#gallery_container .more").click(function(){
	    if(final_left_position != max_position_left){
	        // SLIDE
	        
	        if((Math.abs(max_position_left) - Math.abs(final_left_position)) > 200){ // enough space for movement
	            LeftMotion(false);
	            $("#gallery_roller").animate({left: "-=765px"}, 500);
	            final_left_position -= 765;
	        }else{ // not enough space, lets fix space
	            /*LeftMotion(false);
	            var to_move = Math.abs(max_position_left) - Math.abs(final_left_position);
	            $("#gallery_roller").animate({left: "-="+to_move+"px"}, 500);
	            final_left_position -= to_move;*/
	            $("#gallery_roller").effect("bounce", { direction: 'left', times: 2, distance: 50 }, 200, RightMotion(true));
	        }
	        $("#gallery_slider").slider('option', 'value', Math.floor(Math.abs(final_left_position) / division));
	    }else{
	        //BOUNCE
	        if(StopRightMotion != true){
	            $("#gallery_roller").effect("bounce", { direction:'left', times:2, distance:50 }, 200, RightMotion(true));
	        }
	    }
	});
	
	//Hover Effect
	$("#gallery_roller .item").css("opacity", "0.8");
	$("#gallery_roller .item").hover(
	    function(){
	        $(this).stop().animate({ opacity: 1.0 }, "slow");
	    },
	    function(){
	        $(this).stop().animate({ opacity: 0.8}, "slow");
	    }
	);
	
	
}

function initVideos(){
    //Hover Effect
    $("#videos .item").css("opacity", "0.75");
	$("#videos .item").hover(
	    function(){
	        $(this).stop().animate({ opacity: 1.0 }, "slow");
	    },
	    function(){
	        $(this).stop().animate({ opacity: 0.75}, "slow");
	    }
	);
	
	//Click
	$("#videos .item").click(function(){
	    if($(this).attr('title') != ''){
	        window.location = $(this).attr('title');
	    }
	});
}

function initImages(){
    if($("#images_roller").exists()){
	    final_left_position =  $("#images_roller").position().left;
	}
	
	var item_count = $("#images_roller ul li.item").length;
	var scroller_width = (254 * item_count)+'px';
	var max_position_left = 0 - ((item_count - 3) * 254);
	var DoubleClicked = false;
	
	//Set Scroller Width
	$("#images_roller").css('width', scroller_width);
	
	
	//Back Button
	$("#images_container .back").click(function(){
	    if(final_left_position != 0){
	        RightMotion(false);
	        $("#images_roller").animate({left: "+=254px"}, 500);
	        final_left_position += 254;
	    }else{
	        if(StopLeftMotion != true){
	            $("#images_roller").effect("bounce", { direction:'right', times:2, distance:50 }, 200, LeftMotion(true));
	        }
	    }
	});
	
	//More Button
	$("#images_container .more").click(function(){
	    if(final_left_position != max_position_left){
	        LeftMotion(false);
	        $("#images_roller").animate({left: "-=254px"}, 500);
	        final_left_position -= 254;
	    }else{
	        if(StopRightMotion != true){
	            $("#images_roller").effect("bounce", { direction:'left', times:2, distance:50 }, 200, RightMotion(true));
	        }
	    }
	});
    
    //Hover Effect
    $("#images .item .row").css("opacity", "0.75");
    $("#images .item .row").hover(
        function(){
            $(this).stop().animate({ opacity: 1.0 }, "slow");
        },
        function(){
            $(this).stop().animate({ opacity: 0.75 }, "slow");
        }
    );
    
    //Click
	$("#images .item .row").click(function(){
	    window.location = $(this).attr('title');
	});
}


function initVideoSlider(){
    if($("#video_scroller").exists()){
	    final_left_position =  $("#video_scroller").position().left;
	}
	//var item_count = $("#scroller ul").length;
	var item_count = 2;
	var scroller_width = (750 * item_count)+'px';
	var max_position_left = 0 - ((item_count - 1) * 750);
	var DoubleClicked = false;
	
	//Set Scroller Width
	$("#video_scroller").css('width', scroller_width);
	
	//Back Button
	$("#video_container .back").click(function(){
	    if(final_left_position != 0){
	        RightMotion(false);
	        $("#video_scroller").animate({left: "+=750px"}, 500);
	        final_left_position += 750;
	    }else{
	        if(StopLeftMotion != true){
	            $("#video_scroller").effect("bounce", { direction:'right', times:2, distance:50 }, 200, LeftMotion(true));
	        }
	    }
	});
	
	//More Button
	$("#video_container .more").click(function(){
	    if(final_left_position != max_position_left){
	        LeftMotion(false);
	        $("#video_scroller").animate({left: "-=750px"}, 500);
	        final_left_position -= 750;
	    }else{
	        if(StopRightMotion != true){
	            $("#video_scroller").effect("bounce", { direction:'left', times:2, distance:50 }, 200, RightMotion(true));
	        }
	    }
	});
}