﻿var related_page_products = "";
var image_num_slide_show = 0;
var slide_show_speed = 5000;
var slide_show_transition = 300;

$(document).ready(function(){ 
    //add google maps support
    if($("#map_canvas").size()>0){
        initialize();
    }
    
    $("#nav ul li").each(function(){
        if($(this).children("ul").size()>0){
            $(this).hover(function(){
                $(this).children("ul").css("height","auto");
                $(this).children("ul").css("padding","20px");
                $(this).children("ul").css("display","none");
                $(this).children("ul").slideDown(200);
            },function(){
                $(this).children("ul").stop();
                $(this).children("ul").css("height","auto");
                $(this).children("ul").css("padding","20px");
                $(this).children("ul").hide();
            });
        }
    });
    
    //sort sub images on sub pages
	if($("div.extra_imgs").size()>0){
		var img_width = 0;
		$("div.extra_imgs").each(function(){
			img_width = img_width + $(this).children("img").width() + 10;
			if(img_width>=700){
				img_width = $(this).children("img").width() + 10;;
				$(this).before('<div style="clear:both";><!-- EMPTY --></div>');
			}
			$(this).children("p").css("width",$(this).children("img").width() + "px");
			
		});
	}
    
    //add fancy box
    if($("a.popup_guide").size()>0){
        $("a.popup_guide").fancybox({
            overlayOpacity:'0.7'
        });
    }
    
    //add home page logo support
    if($("#fcs-ad").size()>0){
        $("#fcs-ad").css("overflow","hidden");
        $("#fcs-ad .logo_item").css("overflow","hidden");
        $("#fcs-ad .logo_item").css("width","265px");
        $("#fcs-ad .logo_item").css("height","190px");
        $("#fcs-ad .logo_item img").css("width","auto");
        //loop through each image and center it in the div
        imageloop_forslideshow();
        
        $("#fcs-ad .logo_item").each(function(ich){
            $(this).css("position","absolute");
            if(ich!=0){
                $(this).hide();
            }
        });
        
        //set the slideshow in motion
        window.setTimeout(nextslide,slide_show_speed);
    }
    
    //add testimonials on home page
    $("#quote_title_controls").append("<ul><li id=\"prev\"><a href=\"JavaScript:loadTestimonial('prev');\"><span>Previous</span></a></li><li id=\"next\"><a href=\"JavaScript:loadTestimonial('next');\"><span>Next</span></a></li></ul>");
   
    //add product support
    if($("a.products_special").size()>0){
        $("a.products_special").each(function(){
            //get all related products when page loads and also set their javascript
            if(related_page_products==""){
                related_page_products = $(this).attr("href").replace("product.asp?id=",""); 
            }else{
                related_page_products = related_page_products + "|" + $(this).attr("href").replace("product.asp?id=","");
            }
                
            $(this).attr("target","_self");
            $(this).attr("href","JavaScript:loadProduct(" + $(this).attr("href").replace("product.asp?id=","") + ");");
        });
    }
    
    //format the related products to the appropriate option bit
    if(related_page_products!=""){
        $.get("format_input.asp?id=" + related_page_products,"",function(d){
            related_page_products = d;
        });
    }
    
    //add blog/twitter support on home page
    if($("#blogposts").size()>0){
        //load BLOG to page
        $.get("_feed_blog.asp",'',function(d){
            $("#blogposts_div").html(d);
        });
        
        //get the blog/twitter buttons working
        $("#blogposts a").attr("href","JavaScript:void(0);");
        $("#blogposts a").attr("target","");
        $("#twitter_active a").attr("href","JavaScript:void(0);");
        $("#blogposts").click(function(){
            $(this).attr("id","blogposts_active");
            $("#twitter_active").attr("id","twitter_id");
            $("#twitter_div").hide();
            $("#blogposts_div").show();
        });
        $("#twitter_active").click(function(){
            $(this).attr("id","twitter_active");
            $("#blogposts_active").attr("id","blogposts");
            $("#blogposts_div").hide();
            $("#twitter_div").show();
        });
        
        //load twitter to the page
        $("body").append('<script type="text/javascript" src="http://twitter.com/javascripts/blogger.js"></script><script type="text/javascript" src="http://twitter.com/statuses/user_timeline/hweltd.json?callback=twitterCallback2&amp;count=2"></script>');
        
        //load mailchimp scripts
        //$("body").append('<script type="text/javascript" src="http://highlandwoodenergy.us1.list-manage.com/js/jquery-1.2.6.min.js"></script><script type="text/javascript" src="http://highlandwoodenergy.us1.list-manage.com/js/jquery.validate.js"></script><script type="text/javascript" src="http://highlandwoodenergy.us1.list-manage.com/js/jquery.form.js"></script><script type="text/javascript" src="http://highlandwoodenergy.us1.list-manage.com/subscribe/xs-js?u=79b97491ddec91cedada6a4b1&amp;id=f0e2344a94"></script>');
    }
    
});

function loadTestimonial(dir){
    $("#client-quote blockquote").html("Loading...");
    $.get("randomTestimonial.asp?dir=" + dir + "&cache=" + Math.random(),"",function(d){
        $("#client-quote").html(d);
    });
}

function imageloop_forslideshow(){
    $("#fcs-ad .logo_item img").each(function(ieach){
        if($(this).height()>$(this).width()){
            $(this).css("height","150px");
            if($(this).width()>210){
                $(this).css("width","210px");
                $(this).css("height","auto");
                //vertical center the image
                var padtop = (190 - $(this).height())/2;
                $(this).css("margin-top",padtop + "px");
            }else{
                $(this).css("margin-top","20px");
            }
        }else{
            $(this).css("width","210px");
            if($(this).height()>150){
                $(this).css("height","150px");
                $(this).css("width","auto");
                $(this).css("margin-top","20px");
            }else{
                //vertical center the image
                var padtop = (190 - $(this).height())/2;
                $(this).css("margin-top",padtop + "px");
            }
        }
    });
}

function nextslide(){
    $("#fcs-ad .logo_item:eq(" + image_num_slide_show + ")").slideUp(slide_show_transition,function(){
        image_num_slide_show++;
        if(image_num_slide_show>=$("#fcs-ad .logo_item").size()){
            image_num_slide_show = 0;
        }
        
        $("#fcs-ad .logo_item:eq(" + image_num_slide_show + ")").slideDown(slide_show_transition);
        
        //set the slideshow in motion
        window.setTimeout(nextslide,slide_show_speed);
    });
}

function loadProduct(val){
    $.fancybox.showActivity();
    if(/apple/gi.test(navigator.userAgent)){
        $.get("product.asp?ajax=true&id=" + val,"",function(dd){
            $("body").animate({scrollTop: 0}, 500,function(){
                $("#sub_right_col").html("<div id=\"fader\">" + $("#sub_right_col").html() + "</div>");
                $("#fader").fadeOut(500,function(){
                    $("#fader").remove();
                    $("#sub_right_col").html(dd);
                    $("#other_prods").append(related_page_products);
                    $("#other_prods").change(function(){
                        loadProduct($("#other_prods").val());
                    });
                    init_product(); 
                    $.fancybox.hideActivity();
                });
            });              
        });
    }else{
        $.get("product.asp?ajax=true&id=" + val,"",function(dd){
            $("html").animate({scrollTop: 0}, 500,function(){
                $("#sub_right_col").html("<div id=\"fader\">" + $("#sub_right_col").html() + "</div>");
                $("#fader").fadeOut(500,function(){
                    $("#fader").remove();
                    $("#sub_right_col").html(dd);
                    $("#other_prods").append(related_page_products);
                    $("#other_prods").change(function(){
                        loadProduct($("#other_prods").val());
                    });
                    init_product(); 
                    $.fancybox.hideActivity();
                });
            });              
        });
    }
}

function init_product(){
    //populate other products
    $("#other_products").show();
    
    //change h3 to ul li a tags
    $("#product_tabs").prepend("<ul class=\"main_tabs\"></ul>");
    $("#product_tabs h3").each(function(i){
        $("#product_tabs ul.main_tabs").append("<li><a href=\"JavaScript:toggle_product_tab(" + i + ");\">" + $(this).html() + "</a></li>");
        $(this).next("div").addClass("tab_box");
        if(i!=0){
            $(this).next("div").hide();
        }
        $(this).remove();
    });
    $("#product_tabs ul.main_tabs li a").each(function(ival){
            if(ival==0){
                $(this).css("color","#9D1B30");
            }else{
                $(this).css("color","#555555");
            }
        });
    
    //add fancy box to gallery
    $("a.fancyGallery").fancybox({
        'titlePosition':"inside",
        'titleFormat':formatTitle,
        overlayOpacity:'0.7'
    });
}

//fancybox format
function formatTitle(title, currentArray, currentIndex, currentOpts) {
    return '<div align="left" style="font-size:12px;"><b>' + title + '</b><br /><font style="font-size:10px;">Image ' + (currentIndex + 1) + ' of ' + currentArray.length + '</font></div>';
}

function toggle_product_tab(val){
    $("#product_tabs ul.main_tabs li a").each(function(ival){
            if(ival==val){
                $(this).css("color","#9D1B30");
            }else{
                $(this).css("color","#555555");
            }
        });
    switch(val){
        case 0:
            $("#product_tabs ul.main_tabs").css("background-position","left top");
            $("#tech_info").hide();
            $("#features").show();
            break;
        case 1:
            $("#product_tabs ul.main_tabs").css("background-position","left -44px");
            $("#features").hide();
            $("#tech_info").show();
            break;
    }
}
