$.fn.hoverClass = function(c) {
    return this.each(function(){
        $(this).hover( 
            function() { $(this).addClass(c); },
            function() { $(this).removeClass(c); }
        );
        
    });
};

$.fn.clickClass = function(c) {
    return this.each(function(){
        $(this).click( 
            function() { 
            	if ($(this).attr('class') == c)
    			{
    				$(this).removeClass(c);
    			}
    			else
    			{
    				$(this).addClass(c);
    			}
             }
        );
        
    });
};

$(document).ready(
	
	function() 
	{
		/* KEEP CODE FROM HERE DOWN TO NEXT COMMENT BLOCK ON ALL PROJECTS */
		
		$('input#filename').keyup(function()
		{
			jQuery.ajax(
		   	{
		   		type: "GET",
		   		url: "/page/check_filename.html?filename=" + $(this).val() + '&old_filename='+$('#old_filename').val(),
		   		dataType: "html",
		   		timeout: 15000,
		   		error: function() {},
		   		success: function(r) {
		   								if (r == 'success')
		   								{
		   									$("#filename_status").attr({src: '/images/icons/accept.png'}); 
		   									if ($('input#title').val() != '') 
		   										$("#edit_submit").attr('disabled', false);
		   									$("#filename_text").html('');
		   								}
		   								else
		   								{
		   									$("#filename_status").attr({src: '/images/icons/delete.png'}); 
		   									$("#filename_text").html(r);
		   									$("#edit_submit").attr('disabled', true);
		   								}
		   							}
		   	});
		});
		
		$('input#title').keyup(function()
		{
			if ($(this).val() != '')
			{
				$("#title_status").attr({src: '/images/icons/accept.png'}); 
				if ($("#filename_status").attr('src') == '/images/icons/accept.png')
					$("#edit_submit").attr('disabled', false);
		   	}
		   	else
		   	{
		   		$("#title_status").attr({src: '/images/icons/delete.png'}); 
		   		$("#edit_submit").attr('disabled', true);
		   	}
		});
		
		$('.confirm_anchor').click(function()
		{
			return confirm('Are you SURE you want to do this???');
		});
		
		$('a.new_window').click(function()
		{
			window.open($(this).attr('href'));
			return false;
		});
		
		$("label.overlabel").overlabel();

		/* KEEP CODE FROM HERE UP TO PREVIOUS COMMENT BLOCK ON ALL PROJECTS*/		
		
		//$("h1").pir({font:"Georgia.ttf"});
});

$(function() {
    $('.slideshow').cycle({
        fx: 'fade'
    });


  //remove js-disabled class
    $("#is_viewer").removeClass("is_js-disabled");

  //create new container for images
    $("<div>").attr("id", "is_container").css({ position:"absolute"}).width(1020).height(172).appendTo("div#is_viewer");
    
    //add images to container
    $(".is_wrapper").each(function() {
        $(this).appendTo("div#is_container");
    });
    
    //work out duration of anim based on number of images (1 second for each image)
    var duration = $(".is_wrapper").length * 2200;
    
    //store speed for later (distance / time)
    var speed = (parseInt($("div#is_container").width()) + parseInt($("div#is_viewer").width())) / duration;
                    
    //set direction
    var direction = "rtl";
    
    //set initial position and class based on direction
    (direction == "rtl") ? $("div#is_container").css("left", $("div#is_viewer").width()).addClass("is_rtl") : $("div#is_container").css("left", 0 - $("div#is_container").width()).addClass("is_ltr") ;
    
    //animator function
    var animator = function(el, time, dir) {

     
        //which direction to scroll
        if(dir == "rtl") {
          
          //add direction class
            el.removeClass("is_ltr").addClass("is_rtl");
                
            //animate the el
            el.animate({ left:"-" + el.width() + "px" }, time, "linear", function() {
                                    
                //reset container position
                $(this).css({ left:$("div#is_imageScroller").width(), right:"" });
                
                //restart animation
                animator($(this), duration, "rtl");
                
                //hide controls if visible
                ($("div#is_controls").length > 0) ? $("div#is_controls").slideUp("slow").remove() : null ;			
                                
            });
        } else {
        
          //add direction class
            el.removeClass("is_rtl").addClass("is_ltr");
        
            //animate the el
            el.animate({ left:$("div#is_viewer").width() + "px" }, time, "linear", function() {
                                    
                //reset container position
                $(this).css({ left:0 - $("div#is_container").width() });
                
                //restart animation
                animator($(this), duration, "ltr");
                
                //hide controls if visible
                ($("div#is_controls").length > 0) ? $("div#is_controls").slideUp("slow").remove() : null ;			
            });
        }
    }
    
    //start anim
    animator($("div#is_container"), duration, direction);
    
    //pause on mouseover
    $("a.is_wrapper").live("mouseover", function() {
      
        //stop anim
        $("div#is_container").stop(true);
        
        //show controls
        //($("div#is_controls").length == 0) ? $("<div>").attr("id", "is_controls").appendTo("div#is_outerContainer").css({ opacity:0.7 }).slideDown("slow") : null ;
        //($("a#is_rtl").length == 0) ? $("<a>").attr({ id:"is_rtl", href:"#", title:"rtl" }).appendTo("#is_controls") : null ;
        //($("a#is_ltr").length == 0) ? $("<a>").attr({ id:"is_ltr", href:"#", title:"ltr" }).appendTo("#is_controls") : null ;
        
        //variable to hold trigger element
        var title = $(this).attr("title");
        
        //add p if doesn't exist, update it if it does
        ($("p#is_title").length == 0) ? $("<p>").attr("id", "is_title").text(title).appendTo("div#is_controls") : $("p#is_title").text(title) ;
    });
    
    //restart on mouseout
    $("a.is_wrapper").live("mouseout", function(e) {
      
        //hide controls if not hovering on them
        //(e.relatedTarget == null) ? null : (e.relatedTarget.id != "is_controls") ? $("div#is_controls").slideUp("slow").remove() : null ;
        
        //work out total travel distance
        var totalDistance = parseInt($("div#is_container").width()) + parseInt($("div#is_viewer").width());
                                            
        //work out distance left to travel
        var distanceLeft = ($("div#is_container").hasClass("is_ltr")) ? totalDistance - (parseInt($("div#is_container").css("left")) + parseInt($("div#is_container").width())) : totalDistance - (parseInt($("div#is_viewer").width()) - (parseInt($("div#is_container").css("left")))) ;
        
        //new duration is distance left / speed)
        var newDuration = distanceLeft / speed;
    
        //restart anim
        animator($("div#is_container"), newDuration, ($("div#is_container").attr("class") == "is_ltr") ? "ltr" : "rtl");

    });
                                    
    //handler for ltr button
    $("#is_ltr").live("click", function() {
                        
        //stop anim
        $("div#is_container").stop(true);
    
        //swap class names
        $("div#is_container").removeClass("is_rtl").addClass("is_ltr");
                            
        //work out total travel distance
        var totalDistance = parseInt($("div#is_container").width()) + parseInt($("div#is_viewer").width());
        
        //work out remaining distance
        var distanceLeft = totalDistance - (parseInt($("div#is_container").css("left")) + parseInt($("div#is_container").width()));
        
        //new duration is distance left / speed)
        var newDuration = distanceLeft / speed;
        
        //restart anim
        animator($("div#is_container"), newDuration, "ltr");
    });
    
    //handler for rtl button
    $("#is_rtl").live("click", function() {
                            
        //stop anim
        $("div#is_container").stop(true);
        
        //swap class names
        $("div#is_container").removeClass("is_ltr").addClass("is_rtl");
        
        //work out total travel distance
        var totalDistance = parseInt($("div#is_container").width()) + parseInt($("div#is_viewer").width());

        //work out remaining distance
        var distanceLeft = totalDistance - (parseInt($("div#is_viewer").width()) - (parseInt($("div#is_container").css("left"))));
        
        //new duration is distance left / speed)
        var newDuration = distanceLeft / speed;
    
        //restart anim
        animator($("div#is_container"), newDuration, "rtl");
    });
});

