var dbooomSettings = {
    packed: true,
    partners: 5,
    analyticsId: "UA-7587752-4",
    floatingHeader: true,
    googleMap: {
        address: "1000 Chopper Circle Denver, CO 80204",
        latitude: 0,
        longitude: 0,
        title: "Main office",
        baloonText: "Here we are!",
        zoom: 15
    },
    tweet: {
        username: "envato",
        count: 3
    },
    rss: {
        feed: "http://themeforest.net/feeds/new-themeforest-items.atom",
        count: 2
    },
    flickr: {
        query: "22980156@N03",
        count: 12
    }
};

$(function() {
	/* Theme backgrounds and elements colors
        --------------------------------------------------------------------- */
        if($("#theme-backgrounds").length) {
            var dbooomSlides = [];
            $("<div id='body-texture'>").appendTo("body");

            // Get images
            $("#theme-backgrounds img").each(function() {
                dbooomSlides.push({image: $(this).attr("src")});
            });

            // Initialize plugin
            $.supersized({
                fit_portrait: 1,
                start_slide: 0,
                image_protect: 1,
                slide_links: 0,
                thumb_links: 0,
                slides: dbooomSlides
            });

            // Process links color animation
            var colorReference = $("#theme-backgrounds img:eq(" + $.supersized.vars.current_slide + ")").data("color");

            if(colorReference.length) {
                animateLinks();
                animateElements();
            }
        }

	if($('#contacts').length) {
		$("#contacts input").tipsy({fade: true, gravity: 'w'});
	}
	if($('#request').length) {
		$('input[name="request[scope_other]"]').parent().parent().prev().find('em').hide();
		$('#request-scope').change(function() {
			if($(this).val() == 'other') {
				$(this).parent().parent().parent().next().slideDown('fast');
			} else {
				$(this).parent().parent().parent().next().slideUp('fast');
			}
		});
		$('#request-scope').last().change();

		$('#request-carbon_model').change(function() {
			var val = $(this).val();
			var carbonChilds = $('#request-carbon_model').children();
			
			if(val) {
				carbonChilds.each(function() {
					var child = $(this).val();
					if(val.indexOf(child) != -1) {
						$('#request-volume_'+child).parent().parent().parent().slideDown('fast');
					} else {
						$('#request-volume_'+child).parent().parent().parent().slideUp('fast');
					}
				});
			} else {
				carbonChilds.each(function() {
					$('#request-volume_'+$(this).val()).parent().parent().parent().slideUp(0);
				});
			}
		});
		$('#request-carbon_model').change();
	}

	/* Accordions
        --------------------------------------------------------------------- */
        if($(".accordion-block").length) {
            $(".accordion-block").each(function() {
                // Hide all accordions content
                $(this).find(".accordion-content").css({display: "none"});

                // Click on accordion item trigger and show the content
                $(this).find(".accordion-trigger").click(function() {
                    // On click we close all slides and removes "on" class
                    $(".accordion-trigger").removeClass("active");
                    $(".accordion-content").slideUp();

                    // Open next to this trigger slider
                    if($(this).next().is(":hidden") == true) {
                        $(this).addClass("active");
                        $(this).next().slideDown();
                    }
                    // Disable link click
                    return false;
                });
            });
        }

	/* Top navigation
        --------------------------------------------------------------------- */
        $('.main-nav ul > li:last-child').addClass('last-child');

        /* Second level offset ---------------------------------------------- */
        $(".main-nav > li > ul").each(function() {
            var offset = $(this).offset();
            var width  = $(this).children('li').width();
            var parent = $(this).parent("li").width();

            if((offset.left + width) > screen.width) {
                $(this).css({left: -(width - parent - 10)});
            }
        });

        /* Third level+ offset ---------------------------------------------- */
        $(".main-nav ul ul").each(function() {
            var offset = $(this).offset();
            var width  = $(this).children('li').width();

            if((offset.left + width) > screen.width) {
                $(this).css({left: -(width + 2), width: width + 2});
            }
        });

        /* Some init tuning ------------------------------------------------- */
	$(".main-nav ul").css({display: "none"});
        $(".main-nav li").css({backgroundColor: "rgba(0,0,0,0.01)"});
        $(".main-nav li ul").parent("li").addClass("dropdown");
        $(".main-nav a").removeAttr("title");

        /* Dropdown animation ----------------------------------------------- */
	$(".main-nav li").hover(function(){
	    // IE7 Fix
	    if($.browser.msie && $.browser.version == 7.0) {
		$(this).children("ul").css({display: "block"});
	    }

	    // Main animation
	    $(this).children("ul")
		.css({visibility: "visible"})
		.stop(true, true)
		.delay(200)
		.slideDown(400, "easeOutSine");

	    // RGBA background animation
	    if(Modernizr.rgba) {
		$(this).stop(true, true).animate({backgroundColor: "rgba(0,30,90,0.6)"});
	    }
	},function(){
	    // Main animation
	    $(this).children("ul")
		.stop(true, true)
		.delay(100)
		.fadeOut("fast");

	    // RGBA background animation
	    if($(Modernizr.rgba).length) {
		$(this).stop(true, true).animate({backgroundColor: "rgba(0,0,0,0.01)"});
	    }
	});

        /* Text indentation animation --------------------------------------- */
        $(".main-nav li li").hover(function(){
            $(this).children("a").animate(
                {textIndent: "5px"},
                {duration: "fast", queue: false}
            );
        }, function() {
            $(this).children("a").animate(
                {textIndent: "0px"},
                {duration: "fast", queue: false}
            );
        });



//	$('.slider-nivo .slider-container').nivoSlider({
//		pauseTime: 10000,
//		directionNavHide: false,
//		controlNav: false
//        });

//	$(".main-nav li").hover(function(){
//            $(this).children("a").animate(
//                {color: "#3490c3"},
//                {duration: "fast", queue: false}
//            );
//        }, function() {
//            $(this).children("a").animate(
//                {color: "#ffffff"},
//                {duration: "fast", queue: false}
//            );
//        });

	/* Featured news block
        --------------------------------------------------------------------- */
        if($(".featured-news-text").length) {
            $(".featured-news-text").each(function() {
                var boxElement = $(this);
                var boxHeight = boxElement.height();

                boxElement.children('.text').css({display: "none"});
                var boxMinHeight = boxElement.height();

                // Animation
                $(this).parent().hover(function() {
                    boxElement.stop(true, true).animate({height: boxHeight});
                    $(this).find('.text').stop(true, true).fadeIn();


                }, function() {
                    boxElement.stop(true, true).animate({height: boxMinHeight});
                    $(this).find('.text').stop(true, true).fadeOut();
                });
            });
        }

	// Zoom frame ----------------------------------------------------------
        $(".media-image,.media-link,.media-video").live("mouseover mouseout", function(event) {
            var thisItem = $(this);

            var zoomFrame = thisItem.find(".zoom-frame");
            if(zoomFrame.length == 0) {
		    thisItem.append('<span class="zoom-frame" ><span class="zoom-frame-icon" ></span></span>');
		    zoomFrame = thisItem.find(".zoom-frame");
	    }
	    var zoomFrameIcon = thisItem.find(".zoom-frame-icon");

            // Animation
            if (event.type == "mouseover") {
                 zoomFrame.stop().fadeTo("slow", 1);
                 zoomFrameIcon.stop().animate({top: "50%"});
            } else {
                zoomFrame.stop().fadeTo("slow", 0);
                zoomFrameIcon.stop().animate({top: "70%"});
            }
        });

	/* Elements wrapper
        --------------------------------------------------------------------- */
        // Add wrapper to buttons, form submits and pager elements
        $(".btn,.btn-s,.btn-l,.btn-xl,#pager li > *,input[type=submit],input[type=reset],input[type=button]")
        .not(".yaform__submit, #subheader-search-submit,.container-header .btn-xl,.site-search input,#site-newsletter input").wrap("<div class='btn-wrapper' />");

	/* Elements background animation
        ----------------------------------------------------------------------------- */
	if(Modernizr.rgba) {
            // Set default rgba color value
            $('.tags-floated-list a,.calendar-holder,.widget-tabs .widget-content,.banner-150,.banner-190,.banner-250,.post-content-tags a,.btn-wrapper,.toggle-block,.tabs-block,.accordion-block,.widget-hint,.widget-tweets li').each(function() {
		if($(this).closest('.dark-block').length == 0) {
			    $(this).css({backgroundColor: "rgba(0,0,0,0.1)", color: "#333"});

			    // Animation
			    $(this).hover(function() {
				$(this).stop(true, true).animate({backgroundColor: "rgba(0,0,0,0.15)"});
			    }, function() {
				$(this).stop(true, true).animate({backgroundColor: "rgba(0,0,0,0.1)"});
			    });
		}
            });
        }

	/* Widgets hint
        --------------------------------------------------------------------- */
        if($(".widget-hint").length) {
            $(".widget-hint").css({display: "none"});

            // Hover animation
            $(".widget-hint").parent().css({position: "relative"}).hover(function() {
                $(this).find(".widget-hint").stop(true).fadeTo("fast", 1);
            }, function() {
                $(this).find(".widget-hint").stop(true).fadeTo("normal", 0);
            });
        }

	 /* Related item
        --------------------------------------------------------------------- */
        if($(".post-related-item").length) {
            $(".post-related-item").tipsy({fade: true, gravity: 'n'});
        }

	/* Process images and elements decorations
        --------------------------------------------------------------------- */
        $("a[rel^='prettyPhoto']").prettyPhoto({
            deeplinking: false,
            social_tools: false,
	    ie6_fallback: false,
            overlay_gallery: false,
	    markup:'<div class="pp_pic_holder"><div class="pp_top"><div class="pp_left"></div><div class="pp_middle"></div><div class="pp_right"></div></div><div class="pp_content_container"><div class="pp_left"><div class="pp_right"><div class="pp_content"><div class="pp_loaderIcon"></div><div class="pp_fade"><a href="#" class="pp_expand" title="Expand the image">Expand</a><div class="pp_hoverContainer"><a class="pp_next" href="#">next</a><a class="pp_previous" href="#">previous</a></div><div id="pp_full_res"></div><div class="pp_details"><div class="pp_nav"><a href="#" class="pp_arrow_previous">Previous</a><p class="currentTextHolder">0/0</p><a href="#" class="pp_arrow_next">Next</a></div><div class="ppt">&nbsp;</div><p class="pp_description"></p>{pp_social}<a class="pp_close" href="#">Close</a></div></div></div></div></div></div><div class="pp_bottom"><div class="pp_left"></div><div class="pp_middle"></div><div class="pp_right"></div></div></div><div class="pp_overlay"></div>'
        });

	 /* Partners
        --------------------------------------------------------------------- */
	if($(".our-partners").length) {
		var $partnersBlock = $(".our-partners").find('.partner-blocks');
		var partnersItems = $partnersBlock.children().length;
		if(partnersItems > dbooomSettings.partners) {
			var blockWidth = $partnersBlock.children().first().outerWidth(true);
			$partnersBlock.parent().parent().append('<a class="bx-prev" href="">prev</a><a class="bx-next" href="">next</a>');
			$partnersBlock.width(blockWidth*partnersItems + 'px');
			$partnersBlock.css('left', 0);
			var partnersActive = 0;
			var partnersTimer = false;

			function startPartnersAnimate() {
				if(partnersTimer != false) {
					window.clearInterval(partnersTimer);
					partnersTimer = false;
				}
				partnersTimer = window.setInterval(function(){moveNextPartner('next')}, 15000);
			}

			function stopPartnersAnimate() {
				window.clearInterval(partnersTimer);
			}

			function moveNextPartner(side, number) {
				if(typeof(number) == 'undefined') {
					number = dbooomSettings.partners;
				}
				var timeAnimate = 1000;
				switch (side) {
					case 'next':
						if((partnersActive + number + dbooomSettings.partners) <= partnersItems) {
							partnersActive += number;
						} else if ((partnersActive + dbooomSettings.partners) == partnersItems) {
							partnersActive = 0;
							timeAnimate = partnersItems*100;
						} else {
							partnersActive = partnersItems - dbooomSettings.partners;
						}
						break;
					case 'prev':
						partnersActive = (partnersActive - number >= 0) ? partnersActive - number : 0
						break;
				}

				$partnersBlock.stop().animate({'left': -(blockWidth*partnersActive)}, timeAnimate, 'easeOutQuint');
			}

			$partnersBlock.parent().parent().children('.bx-prev, .bx-next').click(function() {
				if($(this).hasClass('bx-prev')) {
					moveNextPartner('prev', 1);
				} else if($(this).hasClass('bx-next')) {
					moveNextPartner('next', 1);
				}

				return false;
			});

			startPartnersAnimate();

			$partnersBlock.parent().parent().hover(function() {
				stopPartnersAnimate();
			}, function() {
				startPartnersAnimate();
			});
		}
	}
	 /* Floating header
        --------------------------------------------------------------------- */
        if(dbooomSettings.floatingHeader === true) {
            var showHeight = $('.page-content').offset();
            var floatLogo = $('.logo a');
            var floatNav = $('.container-header .main-nav');

            // Build Floated header
            $("<div id='floated-header'>\
                <div class='container-content-inner'>\
                    <div id='floated-logo'>"+floatLogo.html()+"</div>\
                    <ul id='floated-nav'>"+floatNav.html()+"<li id='scrollTop'><a href='#'>Top</a></li></ul>\
                </div>\
               </div>").appendTo("body");

            $("#floated-header #floated-nav li").css({background: "none"});
	    var $img = $('#floated-header').find('img');
	    $img.attr('src', $img.attr('src').replace('.png', '_white.png'));

            // Check on page load
            if($(window).scrollTop() > (showHeight.top + 80)) {
                $('#floated-header').fadeIn();
            }

            // Check on scroll event
            $(window).scroll(function () {
                if($(window).scrollTop() > (showHeight.top + 80)) {
                    $('#floated-header').fadeIn();
                } else {
                    $('#floated-header').fadeOut();
                }
            });

            // Scroll top link
            var scrollTop = $("#scrollTop a").css({opacity: .3});

            scrollTop.hover(function() {
                scrollTop.stop(true, true).animate({opacity: 1});
            }, function() {
                scrollTop.stop(true, true).animate({opacity: .3});
            });

            scrollTop.click(function() {
                $("html,body").animate({scrollTop: 0}, 2000, "easeOutQuint", function() {
                    $('#floated-header').fadeOut();
                });
                return false;
            });
        }

	 /* Tabs
        --------------------------------------------------------------------- */
        if($(".tabs-block, .widget-tabs").length) {
            $(".tabs-block, .widget-tabs").tabs({
                fx: {opacity: 'toggle'}
            });
        }

	/* Gallery fading --------------------------------------------------- */
        if($(".gallery-fading").length) {
		$(document).ready(function() {
			$(".gallery-fading").wtRotator({
				width:990,
				height:350,
				button_width:30,
				button_height:30,
				button_margin:0,
				auto_start:true,
				delay: 5000,
				play_once:false,
				transition_speed:800,
				auto_center:true,
				easing: "",
				cpanel_position:"inside",
				cpanel_align:"BR",
				timer_align:"top",
				display_thumbs:false,
				display_dbuttons:true,
				display_playbutton:true,
				display_numbers:true,
				display_timer:true,
				mouseover_pause:true,
				cpanel_mouseover:false,
				text_mouseover:false,
				text_effect:"fade",
				text_sync:true,
				tooltip_type:"text",
				lock_tooltip:true,
				shuffle:false,
				block_size:75,
				vert_size:55,
				horz_size:50,
				block_delay:25,
				vstripe_delay:75,
				hstripe_delay:180
			});
		});
//            $(".gallery-fading ul").bxSlider({
//                mode: 'fade',
//                controls: true,
//                pager: false,
//                auto: true,
//                speed: 1000,
//                pause: 7000,
//                autoHover: true,
//                autoDelay: 5000
////                onAfterSlide: function(currentSlideNumber, totalSlideQty, currentSlideHtmlObject) {
////                    $(".gallery-fading .slide-caption-left, .gallery-fading .slide-caption-right").fadeOut();
////                    currentSlideHtmlObject.find(".slide-caption-left, .slide-caption-right").delay(100).fadeIn(1000);
////                }
//            });
        }


	
});

// Animate links
function animateLinks() {
    //if(colorReference.length) {
        $("a").each(function() {
            // Get link's default state color
            var linkDefaultColor = $(this).css("color");
            $(this).css({color: linkDefaultColor}).addClass("link-processed");

            // Animate link on hover
            $(this).hover(function() {
                $(this).stop(true).animate({color: '#3490c3'}, "fast");
            }, function() {
                $(this).stop(true).animate({color: linkDefaultColor}, "normal");

            });
        });
    //}
}


// Animate elements
function animateElements() {
    $(".btn, .btn-s, .btn-l, .btn-xl, #pager a, .accordion-trigger, .toggle-trigger, button,input[type='submit'],input[type='reset'],input[type='button']").not("#subheader-search-submit,.site-search-submit,#subscribe-submit").each(function() {
        // Get link's default state color
        var elementDefaultBackground = $(this).css("backgroundColor");
        $(this).css({backgroundColor: elementDefaultBackground});

        // Animate link on hover
        $(this).hover(function() {
            $(this).stop(true, true).animate({backgroundColor: '#3490c3'}, "fast");
        }, function() {
            $(this).stop(true, true).animate({backgroundColor: elementDefaultBackground}, "normal");
        });
    });
}




