// iBeatWalmart
// Evolve

var curNavSection = "";
var curSubNavSection = "";
var ajaxFormSuccess = "<div id='ajaxMessage' style='background-color: #fff; color: #7B7B7B; font-size: 20px; font-weight: bold; margin: 10px; text-align: center;'>Thank You!</div>"


 $(document).ready(function() {
 
 	//UI Fade RollOver - A lttle Tom Rankin magic
    $(".ui-fade-rollover").each(function() {
        if ($(this).attr("ui-fade-rollover-effect_loaded") == "true") return;
        $(this).attr("ui-fade-rollover-effect_loaded", "true");


        if ($(this).attr("alt") != null && curNavSection != "" && $(this).attr("alt").indexOf(curNavSection) != -1) {
            $(this).attr("src", $(this).attr("src").replace("_off", "_on"));
        }

        if ($(this).attr("alt") != null && curSubNavSection != "" && $(this).attr("alt").indexOf(curSubNavSection) != -1) {
            $(this).attr("src", $(this).attr("src").replace("_off", "_on"));
        }

        var wrapper = "<div style='display: block; height: auto; width: auto; background-repeat: no-repeat; background-image: url(" + $(this).attr("src").replace("_off", "_over") + ")'></div>";
        if ($(this).parent().get(0).tagName == "A") {
            $(this).parent().wrap(wrapper);

        }
        else {
            $(this).wrap(wrapper);
        }

        $(this).bind("mouseenter", function(e) {
            $(this).stop().animate({opacity: 0}, 300);
        });
        $(this).bind("mouseleave", function(e) {
            $(this).stop().animate({opacity: 1}, 300);
        });
    });

        // Fancybox city
        $(".fbImage").fancybox({
            'titlePosition': 'over',
            'showNavArrows': true,
            'transitionIn': 'elastic',
            'transitionOut': 'elastic',
            'overlayOpacity': .7,
            'overlayColor': '#000'
        });


        // Inline popin
        $(".fbInline").fancybox({
            'transitionIn': 'elastic',
            'transitionOut': 'elastic',
            'overlayOpacity': .7,
            'overlayColor': '#000',
            'scrolling': false,
            'padding': 20
        });

        // Meet Mike
        $("#meet_mike").fancybox({
            'transitionIn': 'none',
            'transitionOut': 'none',
            'overlayOpacity': .7,
            'overlayColor': '#000',
            'scrolling': false,
            'padding': 20,
            'width': 900,
            'height': 506
        });


        $(".staticPop").fancybox({
            'transitionIn': 'elastic',
            'transitionOut': 'elastic',
            'overlayOpacity': .7,
            'overlayColor': '#000',
            'scrolling': true,
            'padding': 20
        });
        
        $("#homeMascot a").fancybox({
            'transitionIn': 'elastic',
            'transitionOut': 'elastic',
            'overlayOpacity': .7,
            'overlayColor': '#000',
            'scrolling': true,
            'padding': 20,
            'width' : 900,
            'height' : 506,
            'onStart' : function() {
		changeBgColor("#fancybox-outer", "#000")
            },
            'onComplete' : function() {
		// After video is done close fb and show options
               $("#home_video").bind("ended", function() {
                   $("#home_video").after('<a id="homeVideoAction" href="#" onClick ="javascript:pageTracker._trackEvent();"><img src="/content/images/buttons/partnerup_video.png" /></a>');
                   $("#homeVideoAction").click(function(){
                        $.fancybox.close();
                        $("#expose").expose();
                    })
                });
            }
        });
        
        $("#features a").fancybox({
            'transitionIn': 'elastic',
            'transitionOut': 'elastic',
            'overlayOpacity': .7,
            'overlayColor': '#000',
            'scrolling': true,
            'padding': 20,
            'width' : 900,
            'height' : 506,
            'onStart' : function() {
		changeBgColor("#fancybox-outer", "#000")
            }
        });
        
        
        
        

        //Validate forms
        $("form.valid").validate();


	
	
	//Give me some default text in those fields
	
	$(".dt").focus(function (srcc) {
				if ($(this).val() == $(this)[0].title) {
					$(this).removeClass("dtActive");
					$(this).val("");
				}
			});
	
			$(".dt").blur(function () {
				if ($(this).val() == "") {
					$(this).addClass("dtActive");
					$(this).val($(this)[0].title);
				}
			});
	
			$(".dt").blur();


	//Side bar expand/collapse
	$(".sideDrop .oc").click(function(){
		//var selection = ".sideDrop#"+$(this).attr("id")+" ";
                var selection = ".sideDrop#"+$(this).parent().attr("id")+" ul";
                //alert(selection);
		$(selection).slideToggle("fast");
		if ($("#"+$(this).parent().attr("id")+" img:first").attr("alt") == "Expand"){
			$("#"+$(this).parent().attr("id")+" img:first").attr({src: "/content/images/sideBarMinus.jpg"});
			$("#"+$(this).parent().attr("id")+" img:first").attr({alt: "Collapse"});
		}
		else{
			$("#"+$(this).parent().attr("id")+" img:first").attr({src: "/content/images/sideBarPlus.jpg"});
			$("#"+$(this).parent().attr("id")+" img:first").attr({alt: "Expand"});
		}
	})

        //Profile rating setup
        $('div#profileRate').raty({
            scoreName:  'profileScore',
            number:     5,
            path: '/content/images/icons/',
            starOff:      'singleStar_off.jpg',
            starOn:      'singleStar_on.jpg',
            rWidth: 110,
            start: $('#average_rating').html(),
            hintList:     ['1 star', '2 stars', '3 stars', '4 stars', '5 stars'],
            onClick: function(score) {
                            //alert('score: ' + score);
                            $.post("/profile/ratingSubmit", {user_id: $('#userID').html(), rating: score} );
                            $.fn.raty.readOnly(true);
                        }
        });

        // Submit form via link
        $(".fsubmit").click(function(){
            $("#"+$(this).attr("rel")).submit();
        })
        
        // Submit form via image
        $(".fsubmitI").click(function(){
            $("#"+$(this).attr("title")).submit();
        })


        // Submit Popin form
        $(".ajaxSubmit").click(function(){
            var str = $("#"+$(this).attr("title")).serialize();
            var formID = "#"+$(this).attr("title");
           if ($("#"+$(this).attr("title")).valid() == true)
               {
                   $.ajax({
                        type: "POST",
                        url: "/ajax/"+$(this).attr("title"),
                        data: str,
                        success: function(){
                                $(formID)[0].reset();
                                $(formID + " .ajaxSubmit").before(ajaxFormSuccess);
                                $.fancybox.resize();
                                $("#ajaxMessage").delay(800).fadeOut('', function(){
                                    $("#ajaxMessage").remove();
                                    $.fancybox.close();
                                });
                                
                            }
                        });
               }
               else{
                   $.fancybox.resize();
               }

        })

        // Home Rollovers
        $("#businessBullets").hover(
            function(){
                $("#dst").fadeOut("fast");
                $("#businessBulletsList").fadeIn("fast");
            },
            function(){
                $("#dst").stop(true, true).fadeIn("fast");
                $("#businessBulletsList").fadeOut("fast");
            }
        );

        $("#personalBullets").hover(
            function(){
                $("#dst").fadeOut("fast");
                $("#personalBulletsList").fadeIn("fast");
            },
            function(){
                $("#dst").stop(true, true).fadeIn("fast");
                $("#personalBulletsList").fadeOut("fast");
            }
        );

        // Profile pages main nav

        // Lets start off by hiding all business section classes and then reveal comments
        $(".businessSections").each(function(){
            $(this).hide();
            $(".businessSections:first").fadeIn("slow");
        })

        // Profile Nav Action
        $("#profileArea li img").click(function(){
            $(".businessSections").hide();
            $(".businessSections#"+$(this).attr("alt")).fadeIn("slow");
        })

        // Change Location
        $("#changeLocal #change").click(function(){
            $("#changeLocal form").slideToggle("medium");
        })

        //Datepicker
        $(".dp").datepicker({
            beforeShow: function(input, inst){inst.dpDiv.css({marginTop: -input.offsetHeight + 'px', marginLeft: input.offsetWidth + 'px'});},
            defaultDate: +7,
            minDate: '0'
        });


        // Search autocomplete
        $("#swSearch").autocomplete({
            minLength: 1,
            source: function(req, add){
                $.ajax({
                    url: 'http://ibeatwalmart.com/sw_search',
                    dataType: 'json',
                    type: 'POST',
                    data: req,
                    success: function(data){
                        if(data.response =='true'){
                           add(data.message);
                        }
                    }
                });
            }
        });
        
        //Validate forms
        $(".validate").validate();
        
        //Validate forms
        $("#feedbackDoing").validate();
        $("#feedbackMerchant").validate();
        $("#feedbackBug").validate();
        $("#feedbackF").validate();
        
        
        
        // Load footer data
        $("#fBlog").load("/blog/recent-posts/index.php");
        $("#fUpdates").load("/blog/ibw-updates/index.php");
        
        
        
        
        // Sitewide ajax form submission, needs some major work still
        
        // Newsletter Submit
        $('form#newsletter').ajaxForm(function() { 
                var nContainer = "#fNewsletter div"; 
                $('footer form').fadeOut("fast");
                $(nContainer).hide();
                $(nContainer).html("<h2>Thank You</h2><h2>You will hear from us soon</h2>");
                $(nContainer).fadeIn("medium");
        });
        
        
        // Feedback
        $('form#feedbackDoing').ajaxForm(function() { 
                var nContainer = ".feedbackDoing form"; 
                $('form#feedbackDoing').fadeOut("fast");
                $(nContainer).hide();
                $(nContainer).html("<h2>Thank you for your feedback!</h2>");
                $(nContainer).fadeIn("medium");
        });
        $('form#feedbackMerchant').ajaxForm(function() { 
                var nContainer = ".feedbackMerchant form"; 
                $('form#feedbackMerchant').fadeOut("fast");
                $(nContainer).hide();
                $(nContainer).html("<h2>Thank you for your feedback!</h2>");
                $(nContainer).fadeIn("medium");
        });
        $('form#feedbackBug').ajaxForm(function() { 
                var nContainer = ".feedbackBug form"; 
                $('form#feedbackBug').fadeOut("fast");
                $(nContainer).hide();
                $(nContainer).html("<h2>Thank you for your feedback!</h2>");
                $(nContainer).fadeIn("medium");
        });
        
        // Support
        $('form#feedbackF').ajaxForm(function() { 
                var nContainer = "#feedbackSupport"; 
                $('form#feedbackF').fadeOut("fast");
                $(nContainer).hide();
                $(nContainer).html("<h2>Thank you for your message.  We will get back with you very soon!</h2>");
                $(nContainer).fadeIn("medium");
        });
        
        $('.ajform').ajaxForm(function() { 
                alert("Thank you!"); 
        });
        
        // END Sitewide ajax form submission.
        
        
        // Expose
        $("#signup").click(function() {
            $("#expose").expose();
        });
        
        // Feedback
        $("#feedback").show();
        $( "#accordion" ).accordion({ collapsible: true, active: false });
        $("#feedback #action").toggle(
           function()
           {
              $("#feedback").animate({"left": "+=372px"}, "medium", "jswing");
           },
           function()
           {
              $("#feedback").animate({"left": "-=372px"}, "medium", "jswing");
           }
        );
        
        // Change bg color of element
        function changeBgColor(element, hex)
        {
            $(element).css("background-color",hex);
        }
        
        
        
        




});





