$(document).ready(function() {

	custform.initSelect();
	//setBar();
	
	var ie = false;
	if ($.browser.msie) {
		ie = true;
	}
	
	/*
	$(window).resize(function() {
		setBar();
	});*/
	
   $("a#login-link").click(function(e){
	   $("#heckle").submit();
	   e.preventDefault();
   });
   
	var resetSearch = true;
	$("#design-search input").focus(function() {
	  if (resetSearch) {
		$(this).val("");
		resetSearch = false;
	  }
	});
	
	/* Create button active state for IE */
	var activeClass = "";
	$("input:submit").mousedown(function() {
		activeClass = $(this).attr("id") + "-active";
		if (ie) {
			$(this).addClass(activeClass);
		}
	}).mouseup(function() {
		if (ie) {
			$(this).removeClass(activeClass);
		}
	});
	
	/**
	$("#email-submit").click(function() {
	  $("#email-capture-window").css({"top":$(this).offset().top+40,"left":$(this).offset().left-180}).slideDown();
	});
	
	$("#email-capture-enter, #email-window-close").click(function() {
	  $("#email-capture-window").slideUp();
	});
	
	$("#email-window-close").mouseover(function() {
	  $(this).css("cursor","pointer");
	});
	**/

	$(".window-print").click(function(e) {
	  window.print();
	  e.preventDefault();
	});

	$(".close-button").live("click", function(e) {
	  cancelPopup();
	  e.preventDefault();
	});
	
	if ( $("#footer-promo-content .promo").length > 0 ) {
	  addPromoLink();
	}
	
	//pngForIE();
});

$(window).load(function() {
	setBar();
});

$(window).unload(function() {
	closeLightBox();
	closeWaitIcon();
});

var custform = {
	/**
	 * Initialize selection dropdown
	 */
	initSelect: function() {
		// Create dropdown box
		$(".custom-select").each(function() {
		  $(this).before('<span class="select"></span>');
			var selectOverLay = $(this).prev(".select");
			selectOverLay.append('<span class="select-down"></span>');
			var displayText = $(this).find(":selected").text();
			var thisWidth = parseInt($(this).css("width"));
			selectOverLay.css("width",thisWidth+13).append('<div class="select-text">'+displayText+'</div>');
			selectOverLay.after('<ul class="select-options"></ul>');
			var optionsBox = selectOverLay.next("ul.select-options");
			optionsBox.css({"min-width":thisWidth+3});
			var options = $(this).find("option");
			$.each(options, function(i) {
			  if ($(this).is(":selected")) {
				optionsBox.append('<li class="opt-active">'+$(this).text()+'</li>');
			  } else {
				optionsBox.append('<li>'+$(this).text()+'</li>');
			  }
			});
		});
		
		$("#cpicker-content, .select-text").disableSelection();

		// When user clicks anywhere on the page, dropdown folds up
		$(document).bind("mousedown",function(e) {
		  var target = $(e.target);
			if (!target.parents().hasClass("select") && !target.hasClass("select-options") && !target.parents().hasClass("select-options")) {
		    $("ul.select-options").slideUp(100);
			}
		});
		custform.actionSelect();
		custform.makeSelection();
		custform.selectFocus();
		custform.selectBlur();
	},
	/**
	 * Action of toggling dropdown box
	 */
	actionSelect: function() {
		$(".select").click(function() {
		  $("ul.select-options").not($(this).next()).slideUp(100);
		  $(this).next("ul.select-options").css({"top":$(this).position().top+30,"left":$(this).position().left+3}).toggle(100);		
		});
	},
	/**
	 * Action of selecting a dropdown item
	 */
	makeSelection: function() {
		$("ul.select-options li").click(function() {
			$(this).siblings("li").removeClass("opt-active");
			$(this).addClass("opt-active");
		    var nthSelect = $("ul.select-options").index($(this).parent());
			//alert(nthSelect);
			var selectIndex = $("ul.select-options:eq("+nthSelect+") li").index(this);
			var selectObj = $(this).parent().next();
			selectObj.find("option:eq("+selectIndex+")").attr("selected",true);
			$("ul.select-options").slideUp(100);
			var displayText = selectObj.find("option:selected").text();
			//alert(displayText);
			$(".select-text:eq("+nthSelect+")").text(displayText);
			selectObj.change();
		});
	},
	selectFocus: function() {
		$("select").focus(function() {
			$(this).prev().prev(".select").addClass("select-focus");
			$(this).prev().prev(".select").find(".select-down").addClass("select-down-focus");
			custform.selectByKey($(this));
		});
	},
	selectBlur: function() {
		$("select").blur(function() {
			$(this).prev().prev(".select").removeClass("select-focus");
			$(this).prev().prev(".select").find(".select-down").removeClass("select-down-focus");
		});
	},
	/**
	 * Selection made by keyboard
	 */
	selectByKey: function(o) {
		var elemSelect = o.prev().prev(".select");
		o.keyup(function() {
			var nSelect = o.attr("selectedIndex");
			var displayText = o.find(":selected").text();
			//alert(nSelect + ", " + displayText);
			elemSelect.find(".select-text").text(displayText);
			o.prev("ul.select-options").find("li").removeClass("opt-active").eq(nSelect).addClass("opt-active");
		});
	}
}

function lightBox() {
	if ( $("#lightbox-bg").length == 0 ) {
		$("body").append('<div id="lightbox-bg"></div>');
		$("#lightbox-bg").css({"width":$(document).width(),"height":$(document).height(),"top":"0","left":"0"}).show();	
	}
}

function resetLightBox() {
	if ($("#lightbox-bg").length > 0) {
		$("#lightbox-bg").css({"width":$(document).width(),"height":$(document).height(),"top":"0","left":"0"}).show();
	}
}

function closeLightBox() {
	$("#lightbox-bg, .wait-icon").remove();
}

function setBar() {
	$("#body-wrapper").addClass("body-wrapper-border").height($(document).height()-10);
}

function updateSelect(what) {
	var overlayObj = what.prev();
	var objSelectedIndex = what.attr("selectedIndex");
	var overlayObjSelected = $(overlayObj).find("li").eq(objSelectedIndex);
	$(overlayObj).find("li").removeClass("opt-active");
	overlayObjSelected.addClass("opt-active");
	overlayObj.prev().find(".select-text").text(overlayObjSelected.text());
}

// For SFV
var params = {wmode: "opaque", allowscriptaccess: "always"};

function face(templateId) {
	 return eval("face"+templateId);
}

function closeRelatedLightBox() {
	 parent.closeSFDWin();
     var newUrl = parent.location.href;
     parent.location = newUrl;
}

function closeSFV() {
	 closeLightBox();
	 $("#sfv-window").fadeOut();
}

function cancelPopup() {
	closeLightBox();
	$("iframe.pop-window-body").attr("src","/sfd/plain.html");
	$(".pop-window-body").empty();
	$(".med-pop-window, .dyn-pop-window, #acct-ae-window, .popwin").hide();
}

function waitIcon() {
  $("body").append('<div class="wait-icon"></div>');
  $(".wait-icon").css({"top":$(window).height()/2+$(document).scrollTop()-16,"left":$(document).width()/2-16});
  lightBox();
}

function closeWaitIcon() {
	closeLightBox();
	$(".wait-icon").remove();
}

function readyPopup() {
	if ( $(".popwin").length == 0) {
      $.get("/includes/popup_win.jsp",function(data) {
        $(data).appendTo("body");
      });
    } else {
  	  $(".popwin .pwin-elem *").remove();
    }
}

function loadPopup(content) {
	$(".popwin .pwin-elem").html(content);
	if ( $(".popwin .pwin-elem img").length > 0 ) {
	    $(".popwin .pwin-elem img").load(function() {
		  var popWidth = $(".popwin").width();
	      var popHeight = $(".popwin").height();
	      sizePopup(popWidth,popHeight);
	    });
	} else if ( $(".popwin .pwin-elem object").length > 0 ) {
		  var popWidth = parseInt($(".popwin .pwin-elem object").attr("width"))+30;
	      var popHeight = parseInt($(".popwin .pwin-elem object").attr("height"))+60;
	      sizePopup(popWidth,popHeight);

	} else {
		var popWidth = $(".popwin").width();
	    var popHeight = $(".popwin").height();
	    sizePopup(popWidth,popHeight);
	}
}

function addPromoLink() {
	var promo = $("#footer-promo-content .promo:eq(2)");
	$("#footer-promo-content .promo:eq(2)").find("img").wrap('<a href="mailto:sales@opensoftdev.com" />');
}

function sizePopup(w,h) {
	$(".popwin").css({"top":($(window).height()-h)/2+$(document).scrollTop(),"left":($(document).width()-w)/2}).show();
}

function pngForIE() {
   var arVersion = navigator.appVersion.split("MSIE");
   var version = parseFloat(arVersion[1]);
   if ((version >= 5.5) && (document.body.filters)) {
      for(var i=0; i<document.images.length; i++) {
         var img = document.images[i];
         var imgName = img.src.toUpperCase();
         if (imgName.substring(imgName.length-3, imgName.length) == "PNG") {
            var imgID = (img.id) ? "id='" + img.id + "' " : ""
            var imgClass = (img.className) ? "class='" + img.className + "' " : ""
            var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
            var imgStyle = "display:inline-block;" + img.style.cssText 
            if (img.align == "left") imgStyle = "float:left;" + imgStyle
            if (img.align == "right") imgStyle = "float:right;" + imgStyle
            if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
            var strNewHTML = "<span " + imgID + imgClass + imgTitle
            + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
            + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
            + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>" 
            img.outerHTML = strNewHTML;
            i = i-1;
         }
      }
   } 
}

// Plugin: Prevent text from selected
jQuery.fn.extend({ 
    disableSelection : function() { 
            this.each(function() { 
                    this.onselectstart = function() { return false; }; 
                    this.unselectable = "on"; 
                    jQuery(this).css('-moz-user-select', 'none'); 
            }); 
    } 
});

