/** Position Kiss logo to bottom left of page and reposition of page resize.
 *  Done to get rid of any flakiness caused by position absolute madness!
 *  - jonesy 02-02-2011
**/
  $(document).ready(function() {
    $("#pageFooterOuter").stickyFooter();
  });
  (function($){
    var footer;
    $.fn.extend({
      stickyFooter: function(options) {
        footer = this;
        positionFooter();
        $(window)
          .scroll(positionFooter)
          .resize(positionFooter);
        function positionFooter() {
          var docHeight = $(document.body).height() - $("#footer-push").height();
          if(docHeight < $(window).height()){
            var diff = $(window).height() - docHeight;
            if (!$("#footer-push").length > 0) {
              $(footer).before('<div id="footer-push"></div>');
            }
            $("#footer-push").height(diff);
            $("#footer-push").css("min-height", "400px");
          }
        }
      }
    });
  })(jQuery);
  

$(document).ready(function() {
    $("#homepage #right").fadeIn(500);
});

$(".toggle_container").hide();
$(".trigger").click(function(){
        $(this).toggleClass("active");
        $(".toggle_container").slideToggle("slow");
        $(".twtr-tweet-text p").css("cssText","line-height: 0.9em !important;");
//        var elem = $('.twtr-tweet-text p');
//        elem.text(elem.text().replace(/noticed/g, ""));
});


        $("#home-img").css("width", $(window).width());
        $("#home-img").css("height", $(window).height());
         $(window).resize(function(){
                $("#home-img").css("width", $(window).width());
                $("#home-img").css("height", $(window).height());
         });

var remove_these = "#right .sub-menu, #abouthere, #work-scroll-wrapper, #workhere, #contacthere, #home-img, #company, #scroll-wrapper, #recruitment, #fullservicehere, #fullservice-wrapper";

// ABOUT SECTION //
// About section links
$("a.aboutlinks").live('click', function(){
    $(".slideinmenu").animate({marginLeft:'-500px'}, 1000, function() {

    })
});

// About section back button
$("a.back").live('click', function(){
	$("a").removeClass('selected');
	$("a.aboutmenu").addClass("selected");
        $(".back-button, #company, #scroll-wrapper, #recruitment").fadeOut(500);
        $(".slideinmenu").delay(1000).animate({marginLeft:'0px'}, 1000, function(){
          //  $('#about-content').hide();
        });
        Cufon.refresh();
});

// About section. Gets URL, if URL is about.php then changes function of clicking "about", if not then calls "about" via ajax.
$(function(){
	var url = location.pathname;
	if(url.indexOf("about") > -1){
            // Need to redo this for its seperate page.
        }
	  else {
                    $("a.aboutmenu").live('click', function(){
                            $(".kiss").css("color", "#cccccc").removeClass("kiss");
                            $("a, li").removeClass("selected");
                            $('a.aboutmenu').addClass("selected");
                            Cufon.refresh();
                            $("#right").animate({'opacity': 0}, 1000, function(){
                                    $(this).children().remove();
                                    $(".ketchup-error").remove();
                                    $("<div id = 'abouthere'/>").load("about.php #about-wrapper", function (){
                                            $(this).appendTo("#right");
                                            Cufon.refresh();
                                            $(this).parent().animate({'opacity': 1}, 500, function(){
                                                    // empty
                                            });
                                    });
                            })
                        })
                }
});


// Fades in the Company information
$("a.aboutcompany").live('click', function(){
        $(remove_these).stop(true, true);
        //Had to add a delay as the other click function needs to finish before hand, is there a way to wait for global transitions to end first?
        $("#recruitment, #scroll-wrapper").fadeOut(1000, function(){
                    $("#about-content").delay(1500).css('display', 'block');
                    $(".back-button, #company").delay(1000).fadeIn(1000);
                    $("a").removeClass("selected");
                    $("a.aboutcompany").addClass("selected");
                    Cufon.refresh();
        })
});

// Fades in the Team information
$("a.aboutteam").live('click', function(){
        $(remove_these).stop(true, true);
        //Had to add a delay as the other click function needs to finish before hand, is there a way to wait for global transitions to end first?
        $("#company, #recruitment").fadeOut(1000, function(){
                    $("#about-content").delay(1500).css('display', 'block');
                    $(".back-button, #scroll-wrapper").delay(1000).fadeIn(1000, function(){
                        $.getScript('about/teamscroll.js', function() {
                        });
                    })
                    $("a").removeClass("selected");
                    $("a.aboutteam").addClass("selected");
                    Cufon.refresh();
        })
});

// Fades in the Company information
$("a.aboutrecruitment").live('click', function(){
        $(remove_these).stop(true, true);
        //Had to add a delay as the other click function needs to finish before hand, is there a way to wait for global transitions to end first?
        $("#company, #scroll-wrapper").stop().fadeOut(1000, function(){
                    $("#about-content").delay(1500).css('display', 'block');
                    $(".back-button, #recruitment").delay(1000).fadeIn(1000);
                    $("a").removeClass("selected");
                    $("a.aboutrecruitment").addClass("selected");
                    Cufon.refresh();
        })
});




// FULL SERVICE SECTION //
// tested and working one
$(function(){
	var url = location.pathname;
	if(url.indexOf("full-service") > -1){

	}
	else {
		$("a.fullservicemenu").live('click', function(){
			$(".kiss").css("color", "#cccccc").removeClass("kiss");
			$("a, li").removeClass("selected");
			$('a.fullservicemenu').addClass("selected");
			Cufon.refresh();

			$("#right").animate({'opacity' : 0}, 1000, function(){
				$(this).children().remove();
				$(".ketchup-error").remove();
				$("<div id = 'fullservicehere' style='opacity: 0'/>").load("full-service.php #fullservice-wrapper", function (){
                                                        $.getScript('js/jquery.mCustomScrollbar.js', function() {
                        $.getScript('js/work.js', function() {});
                    });
					$(this).appendTo("#right");
					Cufon.refresh();
					$(this).parent().animate({'opacity' : 1}, 1, function(){
                                            $('#fullservicehere').animate({'opacity': 1}, 500);
					});
				});
			})
		}
	)
}
});



// WORK SECTION //
$(function(){
	var url = location.pathname;
	if(url.indexOf("work") > -1){

	  }
	  else {
		$("a.work").live('click', function(){
			$(".kiss").css("color", "#cccccc").removeClass("kiss");
			$("a, li").removeClass("selected");
			$('a.work').addClass("selected");
			Cufon.refresh();

			$("#right").animate({'opacity' : 0}, 1000, function(){
				$(this).children().remove();
				$(".ketchup-error").remove();
				$("<div id = 'workhere' style='opacity: 0'/>").load("work.php #work-scroll-wrapper", function (){
                    $.getScript('js/jquery.mCustomScrollbar.js', function() {
                        $.getScript('js/work.js', function() {});
                    });
                    $(".client-link a", this).hoverIntent({
                        sensitivity: 7,
                        interval: 50,
                        over: function(){
  //                         $(".client-link a").stop(true, true);
//                                               $(".client-over", this).stop().animate({'opacity' : 1}, 1500);
                           $(".client-over", this).fadeIn(500);
                        },
                        timeout: 0,
                        out: function(){
//                           $(".client-link a").stop(true, true);
//                                               $(".client-over", this).stop().animate({'opacity' : 0}, 1500);
                           $(".client-over", this).fadeOut(500);
                        }
                    });
                    $(this).appendTo("#right");
					Cufon.refresh();
					$(this).parent().animate({'opacity' : 1}, 500, function(){
                        $('#workhere').animate({'opacity': 1}, 1000);
					});
				});
			})
                        // if the container is already in the dom then fade it in.
			/*$("#right").animate({'opacity' : 0}, 1000, function(){
                                $(this).children().remove();
				$("<div id = 'workhere' style='opacity: 0'/>").load("work.php #work-scroll-wrapper", function (){
                                        $.getScript('js/jquery.mCustomScrollbar.js', function() {
                                            $.getScript('js/work.js', function() {});
                                        });
                                        $(".client-link a", this).hoverIntent({
                                            sensitivity: 7,
                                            interval: 50,
                                            over: function(){
                                               $(".client-link a").stop(true, true);
                                               $(".client-over", this).stop().animate({'opacity' : 1}, 1500);
                                            },
                                            timeout: 0,
                                            out: function(){
                                               $(".client-link a").stop(true, true);
                                               $(".client-over", this).stop().animate({'opacity' : 0}, 1500);
                                            }
                                        });
					$(this).appendTo("#right");
					Cufon.refresh();
					$(this).parent().animate({'opacity' : 1}, 500, function(){
                                            $('#workhere').animate({'opacity': 1}, 1000);
					});
				});
			})*/
		})

          }
});

// Load in gallery of work when logo is clicked.
$(".client-link a").live('click', function(){
	var className = $(this).attr('class')
	
	if($(this).find("span:first").is(".noworkexample")){
		alert("There are currently no examples available for this client.");
	} else {
		var folderName = className.replace(/-/g, "%20");
		$("<div id='" +className+ "' />").load("work/" + folderName + "/index.php .clientwork",
	//	$("<div id='" +className+ "' />").load("testgallery.php .clientwork",
			function(responseText, textStatus, XMLHttpRequest){
				if(textStatus == 'error') {
					//alert("No work examples to display!");
		    	}
				else {
					$.getScript('js/bgstretcher.js', function() {
						$.getScript("work/" + folderName + "/gallery.js", function() {
		                    Cufon.refresh();
		                });
					});
					$(".clientwork").remove();
					$("#item3").css("opacity", "0");
					$(this).appendTo("#item3");
					$("#item3").animate({'opacity' : 1}, 2000);
                                        $("#item2").animate({marginLeft:'-980px'}, 1000);
					Cufon.refresh();
                                        $("#navigation li a h2").hover(
                                          function () {
                                            $(this).css("color","#ccc");
                                            Cufon.refresh();
                                          },
                                          function () {
                                            $(this).css("color","#646464");
                                            Cufon.refresh();
                                          }
                                        );
				}
		});
	}
});


// Back button to scroll back to the work page.
$("a.back-work").live('click', function(){
    $("#item2").animate({marginLeft:'0'}, 1000);
    $('#item3').animate({'opacity' : 0}, 1000, function() {
                $('#item3 div').remove();
    });
});

$(function(){
	var url = location.pathname;
	if(url.indexOf("contact") > -1){

	}
	else {
		$("a.contact").live('click', function(){
			$(".kiss").css("color", "#cccccc").removeClass("kiss");
			$("a, li").removeClass("selected");
			$('a.contact').addClass("selected");
			Cufon.refresh();

			$("#right").animate({'opacity' : 0}, 1000, function(){
				$(this).children().remove();
				$("<div id = 'contacthere' style='opacity: 0'/>").load("contact.php #contact-wrapper", function (){
					$.getScript("js/jquery.ketchup.all.min.js", function(){
						jQuery(document).ready(function($){
							$("#contactForm").ketchup();
							
							$("#contactForm").submit(function(){
								var formValues = $(this).serialize();
				
								$.ajax({
									url: "form.php",
									type: 'POST',
									data: formValues,
									dataType: "json",
									success: function(data, textStatus, jqXHR){
										console.info("data", data);
									},
									error: function(jqXHR, textStatus, errorThrown){
										
									}
								});
								
								return false;
							});							
						});
					});
					
					$(this).appendTo("#right");
					Cufon.refresh();
					$(this).parent().animate({'opacity' : 1}, 1, function(){
                    	$('#contacthere').animate({'opacity': 1}, 500);
					});
				});
			})
		}
	)
}
});

