var postURL = '/js/';
var loadMessage = '<img src="/images/overlayLoad.gif" />';

jQuery(document).ready(function($){

	var mainTab = $(".primaryTabs li a");
	var fileGroup = $(".tabContentWrap");
	var answersWrap = $(".answersWrap");

	$(mainTab).click(function(){

		var num = (mainTab.index(this));
		$(mainTab).removeClass("active");
		$(mainTab[num]).addClass("active");

		$(fileGroup).removeClass("show").addClass("hide");
		$(fileGroup[num]).removeClass("hide").addClass("show");

		$(answersWrap).removeClass("show").addClass("hide");
		$(answersWrap[num]).removeClass("hide").addClass("show");

		return false;

	});

	$('#downloadWrap .downloadBtn').click(function(){
		s.tl(this,'d','Cloudy Downloads');
	});

	$('#semaCar').css({display: 'block', left:'-7000px'}).animate({left:'35px'},1000);
	$('#semaCarFan').css({display: 'block', left:'-7000px'}).animate({left:'118px'},1000);

	$("#snippet-nav .snippet-sec .trigger:first").addClass("highlight")
	$(".snippet-content:first").stop().slideToggle("normal");

    $("#snippet-nav .snippet-sec .trigger").click(function(){

		$(".snippet-content:visible").slideToggle("normal");

		$(this).toggleClass("highlight");

       	$(this).parent().find('.snippet-content').stop().slideToggle('normal', function(){

			$('.snippet-content:hidden').parent().find('.trigger').removeClass('highlight');

		});

        return false;

    });

    /*
	$('#subject').change(function(){handleSubject();});
	$('#brand').change(function(){handleBrandChange();});
    */

	//OVERLAY CODE
	if(navigator.userAgent.indexOf("MSIE 6") != -1){
        $("body").children().wrapAll("<div id='overlayWrap'></div>");
    }

    $("body").append("<div id='cover' class='beGone'></div><div id='boxWrap' class='beGone'><div id='box'><a class='beGone' id='closeOverlay' href='#'><span>CLOSE WINDOW</span></a><div id=\"overlayReturn\"></div></div></div>");
    var showIt = $(".overlayTrigger");

    var topMargin = 20;
    var overlayFadeSpeed = 500;
    var boxFadeSpeed = 500;
    var isIE6 = (navigator.userAgent.indexOf("MSIE 6") != -1) ? true : false;
    var ie6FullHeight = true;
    var closeDisable = true;
	var clicked;

    showIt.click(function(){

		$('#overlayReturn').html('');

		clicked = $(this);

        this.blur();
        closeDisable = true;

        if(isIE6){
            $("#cover, #boxWrap").css({position: "absolute"});
            if($("body").css("height") == "auto"){
                ie6FullHeight = false;
                $("html, body").css({height: "100%"});
            }
            $("#overlayWrap").css({height: "100%", overflow: "auto"});
            $("html").css({overflow: "hidden"});
        }

        $("#cover").addClass("overlay").fadeTo(1, 0.7).fadeIn(overlayFadeSpeed, function(){
        	showBox();
        });

        return false;
    });

    function ie6Reset(){
        if(isIE6 && !ie6FullHeight){
	        $("html, body").css({height: "auto"});
	        $("html").css({"overflow-y": "auto"});
	        ie6FullHeight = true;
        }
    }

    function showBox(){
         closeDisable = false;
         $("#box").addClass("panel").css({"margin-top": topMargin}).fadeTo(1, 1).fadeIn(boxFadeSpeed, function(){
             action();
             if(isIE6){
            	 $('select').hide();
             }
         });
    }

    function action(){

		$('#overlayReturn').html(loadMessage);

		$.post(postURL,{ type: 'showOverlay', template: clicked.attr('rel')},function(data){

			$('#overlayReturn').html(data);

			$(".beGone").click(function(event){

				if(event.target == this && !closeDisable){

		             this.blur();

		             $("#cover, #box").stop().fadeOut(boxFadeSpeed, function(){ie6Reset();});

		             if(isIE6){
		            	 $('select').show();
		             }
		        }

			    return false;

			});

		});
    }
});

function clearText(field) {
	if(field.defaultValue == field.value) { field.value = "" }
}
function replaceText(field) {
	if(field.value == "") { field.value = field.defaultValue }
}

function toggle(o){
	var e = document.getElementById(o);
	e.style.display = (e.style.display == 'block') ? 'none' : 'block';
}
/*
function handleSubject(){
	var brand = document.getElementById('brand');
	var products = document.getElementById('products');
	var index = $("#subject option:selected").val();

	if(index <= 3 && index != ''){
		brand.disabled = false;
		$('#upc_label,#upc_fields,#lot_label,#lot_field').show();
	}
	else{
		$('#upc_label,#upc_fields,#lot_label,#lot_field').hide();
		brand.disabled = true;
		brand.selectedIndex = 0;
		products.innerHTML = '<select name="product" id="product" disabled><option value="">Choose One...</option></select>';
	}
}

function handleBrandChange(){
	var brand = $("#brand option:selected").val();
	getHTML('products','type=products&brand='+brand);
}
*/

function getHTML(div,params){
	$.ajax({
	type: "POST",
	url: postURL,
	data: params,
	dataType: "html",
	success:
		function(html){
			document.getElementById(div).innerHTML = html;
		}
	});
}
