$(document).ready(function() {
	
	// show clients
	
	$('.btn-showclients').bind({
		'click': function(e) {
			e.preventDefault();
			if ($("#cc-prop-clientlist").css('height') == '44px') {
				$("#cc-prop-clientlist").animate(
				    { height: clientsFullHeight }, 
				    { 
				        queue: false, 
				        duration: 400, 
				        easing: 'easeOutQuad', 
				        step: function() { 
				            $(window).trigger('resize'); 
				        } 
				    }
				);
				$(".btn-showclients").addClass('btn-hideclients');
			} else {
				$("#cc-prop-clientlist").animate(
				    { height:"44px" }, 
				    { 
				        queue: false, 
				        duration: 400, 
				        easing: 'easeOutQuad', 
				        step: function() { 
				            $(window).trigger('resize'); 
				        } 
				    }
				);
				$(".btn-showclients").removeClass('btn-hideclients');
			}
		}
	});
	
	
	// news wheel
	
	var npSel = 1;
	
	// update image sizes
	function updateNewsImageSize(img) {
	    var minWidth = 290;
	    var minHeight = 290;
	    var w = img.width();
	    var h = img.height();
	    var ratio = w / h;
	    if (ratio > 1) {
	        img.css("height", minHeight + "px");
	        img.css("width", (minHeight * ratio) + "px");
	    } else {
	        img.css("width", minWidth + "px");
	        img.css("height", (minWidth / ratio) + "px");
	    }
	}
	
	$(".npimg img").each(function() {
	    var img = $(this);
	    img.bind("load", function() {
            updateNewsImageSize($(this));
        });
        
	    if (img.width() > 290 && img.height() > 290) {
	        updateNewsImageSize(img);
	    }
	    
	})
	
	$('#news-arr-l').bind({
		'click': function(e) {
			e.preventDefault();
			if (npSel < npNum) {
				// remove link class
				$("#news-arr-r")
					.removeClass("linkoff");
				// fade out current text
				curTxt = $("#nptxt"+(npSel));
				newTxt = $("#nptxt"+(npSel+1)); 
				curTxt
					.animate({ opacity:"0" }, 200, function() {
						curTxt
							.css("display","none");
						// fade in new text
						newTxt
							.css("opacity","0")
							.css("display","block");
						newTxt
							.animate({ opacity:"1" }, { queue:false, duration:100 } )
					});
				// slide out current image
				$("#npimg"+(npSel))
					.animate( { left:"341px", top:"-320px" }, { queue:false, duration:300 } )
				// slide in new image
				$("#npimg"+(npSel+1))
					.css("left","-341px");
				$("#npimg"+(npSel+1))
					.css("top","320px")
					.animate( { left:"0px", top:"0px" }, { queue:false, duration:300 } )
				// update link
				npSel++;
				if (npSel == npNum) {
					$("#news-arr-l")
						.addClass("linkoff");
				}
			}
		}
	});
	
	$('#news-arr-r').bind({
		'click': function(e) {
			e.preventDefault();
			if (npSel > 1) {
				// remove link class
				$("#news-arr-l")
					.removeClass("linkoff");
				// fade out current text
				curTxt = $("#nptxt"+(npSel));
				newTxt = $("#nptxt"+(npSel-1)); 
				curTxt
					.animate({ opacity:"0" }, 200, function() {
						curTxt
							.css("display","none");
						// fade in new text
						newTxt
							.css("opacity","0")
							.css("display","block");
						newTxt
							.animate({ opacity:"1" }, { queue:false, duration:100 } )
					});
				// slide out current image
				$("#npimg"+(npSel))
					.animate( { left:"-341px", top:"320px" }, { queue:false, duration:300 } )
				// slide in new image
				$("#npimg"+(npSel-1))
					.css("left","341px");
				$("#npimg"+(npSel-1))
					.css("top","-320px").animate( { left:"0px", top:"0px" }, { queue:false, duration:300 } )
				// update link
				npSel--;
				if (npSel == 1) {
					$("#news-arr-r")
						.addClass("linkoff");
				}
			}
		}
	});
	
	
	// our work mouseovers
	
	$('.box-werktb a').each(
		function() {
			$(this).bind({
				'mouseenter': function() {
					$(this).children(".werktb-over")
						.stop(true)
						.animate({ opacity:"1" }, 200);
				},
				'mouseleave': function() {
					$(this).children(".werktb-over")
						.stop(true)
						.animate({ opacity:"0" }, 200);
				}
			})
		}
    );
    
    // removing errant lines from the twitter feed
    
    var tweetsCurHeight = 0;
    var tweetsMaxHeight = parseInt($('.news-subposts').css('height'));
    var tweetsPad = 0;
    
    $('.news-subposts li').each(
		function() {
			thisHeight = $(this).innerHeight();
			if ((tweetsCurHeight + thisHeight) < (tweetsMaxHeight + 1)) {
			    // all good
			    tweetsCurHeight = tweetsCurHeight + thisHeight;
			} else {
			    $(this).remove();
			}
		}
    );
    
    // modify the padding in the twitter feed
    
    if (tweetsCurHeight < 119) {
        tweetsPad = parseInt(((tweetsMaxHeight - tweetsCurHeight) / 2));
        tweetsPad = tweetsPad - 1;
        $('.news-subposts').css('padding-top', tweetsPad + 'px');
        $('.news-subposts').css('height', (tweetsMaxHeight - tweetsPad) + 'px');
    }
    
    // messaging
    
    function decoderRing(str) {
        var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
        var invalid = {
            strlen: (str.length % 4 != 0),
            chars:  new RegExp('[^' + chars + ']').test(str),
            equals: (/=/.test(str) && (/=[^=]/.test(str) || /={3}/.test(str)))
        };
        if (invalid.strlen || invalid.chars || invalid.equals)
            throw new Error('Invalid base64 data');
        var decoded = [];
        var c = 0;
        while (c < str.length) {
            var i0 = chars.indexOf(str.charAt(c++));
            var i1 = chars.indexOf(str.charAt(c++));
            var i2 = chars.indexOf(str.charAt(c++));
            var i3 = chars.indexOf(str.charAt(c++));
            var buf = (i0 << 18) + (i1 << 12) + ((i2 & 63) << 6) + (i3 & 63);
            var b0 = (buf & (255 << 16)) >> 16;
            var b1 = (i2 == 64) ? -1 : (buf & (255 << 8)) >> 8;
            var b2 = (i3 == 64) ? -1 : (buf & 255);
            decoded[decoded.length] = String.fromCharCode(b0);
            if (b1 >= 0) decoded[decoded.length] = String.fromCharCode(b1);
            if (b2 >= 0) decoded[decoded.length] = String.fromCharCode(b2);
        }
        return decoded.join('');
    }
    
    if (Math.round(Math.random() * 10) == 5) {
        if ($("#cc-msg").html() != "") {
            var msg = $("<div>"+decoderRing($('#cc-msg').html())+"</div>");
            var wrap = $("<div></div>");
            $("#cc-msg").remove();

            wrap.css({
                "position": "fixed",
                "display":"none",
                "top": "0",
                "left": "0",
                "z-index": "1000",
                "width": "100%",
                "height": "100%"
            })
            msg.css({
                "display":"table-cell",
                "background-color": "#d3af08",
                "color": "#000",
                "font-size":"40px",
                "text-transform":"uppercase",
                "vertical-align":"middle",
                "text-align":"center"
            })
            $("body").append(wrap.append(msg));

            window.setTimeout(function() {
                wrap.css("display", "table");
                window.setTimeout(function() {
                    wrap.remove();
                }, 150)
            }, Math.random() * 5000 + 5000);
        }
    }
});
