// 
//  application.js
//  «People Design»
//  
//  Created by People Design on 2008-12-18.
//  Copyright 2008 People Design. All rights reserved.


Portfolio = {
    setup: function(){
        // When click on tag, make that tag active
        $(".thumb").addClass('slideAble');

        // Only pass lightBox images that are tagged by selected tag
        $('#gallery .slideAble').lightBox();
 
        Portfolio.setupTags();
	if(location.search)Portfolio.activateItem(location.search.split("=")[1]);
		
    },
		activateItem: function(whichItem){
			 $("."+whichItem+" .thumb").click();
		},
		activateTag: function(whichTag){
			$(".tag[tname="+whichTag+"]").click();
		},
    setupTags: function(){
        // Insert comma after tags in blog
        // $("body.blog .tags a").after(",");
    
        var tag = '';
        $(".tag").click(function() {
            if($(this).hasClass("current_tag"))return false;
            $('.tag').removeClass("current_tag");
            $(this).addClass("current_tag");

            // Which tag is it?
            var tag = $(this).attr("tname");
       		whichTag=tag;
			
            // If it is all, forget other rules and display all
            if(tag == 'all'){
                $(".thumb").fadeTo("slow", 1).addClass('slideAble').removeClass('notSlideAble').unbind('click');
            } else{
                $(".thumb").removeClass('slideAble').addClass('notSlideAble').bind('click', function(){
                    return false;
                });
                $("."+tag).addClass('slideAble').removeClass('notSlideAble').unbind('click');
                $(".notSlideAble").fadeTo("slow", .1);

                // AV Note: if there are two tags on a single item, fadeTo('slow', 1) wont work correctly
                // $(".slideAble").css({opacity: 1});
                $(".slideAble").fadeTo("slow", 1);

            }

            // Redefine lightbox array to only include images with the chosen tag
			$('#gallery .slideAble').lightBox();

            return false;
        });
    }
}

Comments = {
    //colors: ['996633','006633','006633'],  
    //colors: ['66A9AD','DBC31C','EDA084'],
    colors: ['399499','B9A518','CC6E4D'],
    setup: function(){
    
        // Recent Comments colors
        colors = this.colors;
        $(".comment").each(function(){
            $this = $(this);
      
            // Choose a random color
            color = colors[Utils.rand(3)];
      
            // Set text color
            $this.css({
                "color": '#' + color
            });
            $this.find('a').css({
                "color": '#' + color
            });
      
        });
	
        $(".swatch").click(function(){
            // Color picker event
            $(".swatch").css("border-color","#fff");
            mycolor=$(this).attr("class").split("-")[0];
            $(this).css("border-color",$(this).css("background-color"));
            $("#commentstyle").attr("value","comment-"+mycolor);
        });
        Comments.userCreation();
        Comments.validator();
    },
    userCreation: function(){
        // Add a comment functionality
        // Click 'Add a comment' to open comment form
        $(".respondoption").click(function(){
            $(this).slideUp(function(){
                $(this).next(".respondForm").slideDown("slow");
            });
        });
    },
    validator: function(){
        // Make sure content is actually imput
        var originalFieldContent = [];
    
        $('.commentform').find('input,textarea').not(':submit,:hidden').each(function(i){
            var firstContent = $(this).val();
            originalFieldContent.push(firstContent);
        });
    
         $('.commentform').submit(function(){
            var correct = true;
/*	
            // Make sure they have entered something
            $(this).find('input,textarea').not(':submit,.comment_url').each(function(){
                var content = $(this).val();
                $(this).css({
                    'background-color':'white'
                });
                if(originalFieldContent.in_array(content, false)){
                    $(this).css({
                        'background-color':'#FFCCCC'
                    });
                    correct = false;
                }
            });
      
            // Make sure it's a good email
            var emailValue = $('.comment_email',this).val();
            if(!emailValue.match(/@/) || !emailValue.match(/\./)){
                $('.comment_email',this).css({
                    'background-color':'#FFCCCC'
                });
                correct = false;
            }		
            var urlValue = $('#url').val();
            // Make sure URL has '.' in it
            if(!urlValue.match(/\./)){
                $('#url').css({
                    'background-color':'#FFCCCC'
                });
                correct = false;
            }
 */     
            if(correct==false){
                return false;
            }else{
                return true;
            }
        });
    }
}






Utils = {  // All random items that need to run
    onLoad: function(){ // Actions that need to happen on the ready state
	  
        $('#li--11 input').attr('value', $("#label--11 span").text());
	  
        // IMAGE RANDOMIZER CALL
        // This makes it so images loaded in the blog section are randomized
        $('.blog .randomizable').randomImage({
            imageNames:    ['rndm_01.jpg',
            'rndm_02.jpg',
            'rndm_03.jpg',
            'rndm_04.jpg'],
            imageData: ['Thanks for the photo PinkMoose! Flickr.com',
            'Thanks for the photo CheekyRabbit! Flickr.com',
            'Thanks for the photo SimplePlat! Flickr.com',
            'Thanks for the photo KillerCroc! Flickr.com'],
            imagePath: template_directory + '/images/ideas/',
            dataDisplay:'alt'
        });
    
    
    
    
    
        //  SPECIFIC RESIZABLE CALLS
        //  Note:
        //    These are seperated due to their placement in 'column position'.
        //    Their horizontalOffset changes per what column they are in.
        //  Recognition page and blog
        $('.recognition .resizable, .blog .resizable, .contact .resizable').maxImage({
            horizontalOffset: 980,
            verticalOffset: 0,
            topSpace:0,
            position: '',
            maxFollows: 'width'
        });
        // Careers page
        $('.overview .resizable, .team .resizable, .careers .resizable').maxImage({
            horizontalOffset: 810,
            verticalOffset: 0,
            topSpace:0,
            position: '',
            maxFollows: 'width'
        });
        // Homepage
        $('.homepage .resizable').maxImage({
            horizontalOffset: 350,
            verticalOffset: 0,
            topSpace:0,
            rightSpace:10,
            position: '',
            maxFollows: 'both'
        });
    
        $('.services div.sizablediv').maxImage({
            horizontalOffset: 450,
            verticalOffset: 119,
            position: '',
            maxFollows: 'both'
        });
        $('.free_advice .resizable').maxImage({
            horizontalOffset: 760,
            verticalOffset: 29,
            topSpace:29,
            position: '',
            maxFollows: 'both'
        });
    
    
    
        $('.empathy .resizer').maxImage({
            horizontalOffset: 350,
            verticalOffset: 29,
            topSpace:29,
            position: '',
            maxFollows: 'both'
        });
    
        // MAX GRID CALLS
        // This sets up the portfolio grid to resize automatically with the browser
        $('.portfolio .maxgrid').maxGrid({
            numberOfColumns: 10,
            horizontalOffset: 320
        });
    
    
        // FOCUS MAGIC CALLS
        // This sets the contact form to use focus magic
        $('form').focusMagic(); // JQUERY PLUGIN: AV 01.28.09 found in /script/jquery.focusformmagic.js


        // SOCIAL HOVERS
        // wrench sharethis into the same dom layout as facebook and twitter
        $('a.stbutton').html('<img src="http://www.peopledesign.com/wp-content/themes/people_design/images/share_bw.jpg" />')  
        $('.social a').hover(function(){
            $("img", this).attr('src',    $("img", this).attr('src').replace(/_bw/, "")   )
        },
        function(){ 
            $("img", this).attr('src',    $("img", this).attr('src').replace(/(\.jpg)/, "_bw$1")   )
        })
        // sharethis popup alignment
        if( $.browser.msie ){
            $('span#sharethis_0 a').click(function(){
                top_string = $( '#stwrapper' ).css('top') ;
                numbers = parseInt( top_string.substr(0,3) );
                new_top = numbers - 300;
                $( '#stwrapper' ).css('top', new_top+'px') ;
            });
        }else{
            $('span#sharethis_0 a').click(function(){
                top =  $( '#stwrapper' ).css('top') ;
                new_top = parseInt( top ) - 310 ;
                new_top_string = new_top.toString() + 'px';
                $('#stwrapper').css('top',  new_top_string );
            })
        }	
try{
			if($.fragment().whichTag)Portfolio.activateTag($.fragment().whichTag);
			if($.fragment().whichItem)Portfolio.activateItem($.fragment().whichItem);
    }catch(e){}    
    },
    rand: function(n){        // Find a random number
        return ( Math.floor ( Math.random ( ) * n ) );
    },
    debug: function($obj) {
        if (window.console && window.console.log) {
            window.console.log($obj);
        }
    }
}



$(document).ready(function(){
    Utils.onLoad();
    Portfolio.setup();      // Setup the portfolio
    Comments.setup();       // Setup comments
	

	
    if($("#post-81").length>0)$("#miniform").show("fast"); // show the free advice form on the free advice page
    
    $(".newsletter").toggle(function(){
        $("#miniform").show("fast");
    },function(){
        $("#miniform").hide("fast");
    });
	
    $("#sendbutton2").click(function(){
        $(this).oneTime(4000, function() {
            // if the miniform has successfully submitted, hide it.
            if($("#usermessage2b").hasClass("success")){
                $("#miniform").hide();
            }
        });
    });
	
    $("#sendbutton,#sendbutton2").click(function(){
        // highlight required fields if form submit fails
        $(this).oneTime(1000, function() {

            $("input, textarea").css("background-color","#ffffff");
            if($("#usermessagea").hasClass("failure")){
                badElement="#cformsform "+location.hash.toString();
                $(badElement).css("background-color","#ffcccc");
            }
        });
    });
	
});

