var loginapi = null;
var login = new bjLogin({});

function debug($msg) {
          if (window.console && window.console.log)
            window.console.log($msg);
      //    else alert($msg);
        };


function getDomain()
{
    var href = document.location.href;
    var sec = 'https:';

    var url = '//www.bocajava.com/';
    var d = document.domain;
debug(d)
    if(d=='slidemo.bocajava.com' || d=='test.bocajava.com') url = "//qa.bocajava.com/";
    else if(d=='qa-catalog.bocajava.com') url = "//qa.bocajava.com/";
    else if(d=='localhost') url= "//localhost/";
    else if(d=='catalog') url= "//localhost/";
    else if(d.match(/10\.1\.1\.147/)!=null) url = "//10.1.1.147/";
    else if(d=='catalog.bocajava.com') url='//www.bocajava.com/';
     else if(d=='qa.bocajava.com') url='//qa.bocajava.com/';
    //url = document.location.protocol + url;

    url = sec+url;


    return url;
}

function addNotice(message)
{


$.pnotify({
					pnotify_title: "Notice:",
					pnotify_text: message,
					pnotify_delay: 20000,
					pnotify_stack: false,
                                        pnotify_delay:3000,
                                        pnotify_opacity: 1,
                                        pnotify_notice_icon: 'ui-icon ui-icon-circle-check',
					pnotify_before_open: function(pnotify){
						// Position this notice in the center of the screen.
						pnotify.css({
							"top": ($(window).height() / 2) - (pnotify.height() / 2),
							"left": ($(window).width() / 2) - (pnotify.width() / 2)
						});
						// Make a modal screen overlay.

					}
				});


  noticeAdd({text:message});

/**
*	jQuery.noticeAdd() and jQuery.noticeRemove()
*	These functions create and remove growl-like notices
*
*   Copyright (c) 2009 Tim Benniks
*
*	Permission is hereby granted, free of charge, to any person obtaining a copy
*	of this software and associated documentation files (the "Software"), to deal
*	in the Software without restriction, including without limitation the rights
*	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
*	copies of the Software, and to permit persons to whom the Software is
*	furnished to do so, subject to the following conditions:
*
*	The above copyright notice and this permission notice shall be included in
*	all copies or substantial portions of the Software.
*
*	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
*	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
*	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
*	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
*	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
*	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
*	THE SOFTWARE.
*
*	@author 	Tim Benniks <tim@timbenniks.com>
* 	@copyright  2009 timbenniks.com
*	@version    $Id: jquery.notice.js 1 2009-01-24 12:24:18Z timbenniks $
**/

		function noticeAdd(options)
		{
			var defaults = {
				inEffect: 			{opacity: 'show'},	// in effect
				inEffectDuration: 	600,				// in effect duration in miliseconds
				stayTime: 			6000,				// time in miliseconds before the item has to disappear
				text: 				'',					// content of the item
				stay: 				false,				// should the notice item stay or not?
				type: 				'notice' 			// could also be error, succes
			}

			// declare varaibles
			var options, noticeWrapAll, noticeItemOuter, noticeItemInner, noticeItemClose;

			options 		= jQuery.extend({}, defaults, options);
			noticeWrapAll	= (!jQuery('.notice-wrap').length) ? jQuery('<div></div>').addClass('notice-wrap').appendTo('body') : jQuery('.notice-wrap');
			noticeItemOuter	= jQuery('<div></div>').addClass('notice-item-wrapper');
			noticeItemInner	= jQuery('<div></div>').hide().addClass('notice-item ' + options.type).appendTo(noticeWrapAll).html('<p>'+options.text+'</p><div class="notice-bg"></div>').animate(options.inEffect, options.inEffectDuration).wrap(noticeItemOuter);
			noticeItemClose	= jQuery('<div></div>').addClass('notice-item-close').prependTo(noticeItemInner).html('x').click(function() { noticeRemove(noticeItemInner) });

			// hmmmz, zucht
            var nw = noticeWrapAll.width();
            var nh = noticeWrapAll.height();

            var ww = $(window).width();
            var wh = $(window).height();

            w = ww/2 - nw/2;
            h = wh/2 - nh/2;
            if($.browser.msie) h = h + $(document).scrollTop();

            noticeWrapAll.css({left: w+'px',top:h+'px'});

			if(navigator.userAgent.match(/MSIE 6/i))
			{
		    	noticeWrapAll.css({top: document.documentElement.scrollTop + h});
		    }

			if(!options.stay)
			{
				setTimeout(function()
				{
					noticeRemove(noticeItemInner);
				},
				options.stayTime);
			}
		}

		function noticeRemove(obj)
		{
			obj.animate({opacity: '0'}, 600, function()
			{
				obj.parent().animate({height: '0px'}, 300, function()
				{
					obj.parent().remove();
				});
			});
		}


}

function makeTooltip(el)
	{
		 $(el).bt({
                            fill: '#ffffff',
                              strokeStyle: '#666666',
                              shadow: true,
                            shadowOffsetX: 1,
                            shadowOffsetY: 1,
                            shadowBlur: 3,
                            shadowColor: 'rgba(0,0,0,.9)',
                            shadowOverlap: false,
                            noShadowOpts: {strokeStyle: '#B7B7B7', strokeWidth: 2},
                            positions: ['top', 'bottom'],
                              spikeLength: 10,
                              spikeGirth: 10,
                              padding: 8,
                              cornerRadius: 2,
                              cssStyles: {
                                fontFamily: '"lucida grande",tahoma,verdana,arial,sans-serif',
                                fontSize: '11px'},
                              trigger: 'hover',
                              preShow: function(box){


                              },
                              contentSelector: function(){

                                  var content = $(this).attr('title');
                                  if(content==null || content=="") content = $(this).attr('alt');
                                  return content;
                              },
                              showTip:function(box){
                                $(box).fadeIn(500);
                              },
                               hideTip: function(box, callback) {
                                $(box).fadeOut(500);

                        },
                              width: 200
});
	}

	function makeTooltipSecondary(el, newtop)
	{
		makeTooltip(el);
	}


    function makeBasicOmnitip(el, tag)
    {
         $(el).bt({
                            fill: '#ffffff',
                              strokeStyle: '#666666',
                              shadow: true,
                            shadowOffsetX: 1,
                            shadowOffsetY: 1,
                            shadowBlur: 3,
                            shadowColor: 'rgba(0,0,0,.9)',
                            shadowOverlap: false,
                            noShadowOpts: {strokeStyle: '#B7B7B7', strokeWidth: 2},
                            positions: ['top', 'bottom'],
                              spikeLength: 10,
                              spikeGirth: 10,
                              padding: 8,
                              cornerRadius: 2,
                              cssStyles: {
                                fontFamily: '"lucida grande",tahoma,verdana,arial,sans-serif',
                                fontSize: '11px'},
                              trigger: 'hover',
                              preShow: function(box){


                              },
                              contentSelector: function(){

                                  return $(this).attr(tag);
                              },
                              showTip:function(box){
                                $(box).fadeIn(500);
                              },
                               hideTip: function(box, callback) {
                                $(box).fadeOut(500);

                        },
                              width: 200
});
    }

    function makeErrorOmnitip(el, tag)
    {
        makeBasicOmnitip(el,tag);
    }





    function ajaxLogin(el)
    {

    }


    function doBilling(id)
    {
         if($("#f_billingId").val()==null) $("#mainform").prepend('<input type="hidden" class="hidden" value="" name="billingId" id="f_billingId" />');
         $("#f_billingId").val(id);
       //  console.log(b.disableValidation());
         b.disableValidation();
    }

    function doShipping(id)
    {
         if($("#f_shippingId").val()==null) $("#mainform").prepend('<input type="hidden" class="hidden" value="" name="shippingId" id="f_shippingId" />');
         $("#f_shippingId").val(id);
         a.disableValidation();
    }

    function ajaxShippingBilling()
    {


    }

   function insertAddress(item, data)
   {

   }
/*
   function doOverlays()  //handles new overlay style
   {

        $(".clickmodal").overlay({finish:{top:'center',left:'center',absolute:true},
       expose: {
            color: '#000',
            opacity: 0.3,
            closeSpeed: 1000
        },
       onLoad: function(){
            if ($.browser.msie && parseInt($.browser.version)<=6) //IE MUST DIE!
            {
                $("#bd :input").hide();
            }
       },
       onClose: function(){
           if ($.browser.msie && parseInt($.browser.version)<=6) //IE MUST DIE!
            {
                $("#bd :input").show();

            }



       }
       });

       loginapi = $("#modal_login").unbind().overlay({api:true,finish:{top:'center',left:'center',absolute:true},
               expose: {
                    color: '#000',
                    opacity: 0.3,
                    closeSpeed: 1000
                },
               onLoad: function(){

                    login.run("#modal_login");

                    if ($.browser.msie && parseInt($.browser.version)<=6) //IE MUST DIE!
                    {
                        $("#bd :input").hide();
                    }
               },
               onClose: function(){
                   if ($.browser.msie && parseInt($.browser.version)<=6) //IE MUST DIE!
                    {
                        $("#bd :input").show();
                    }
               }
               });


     ajaxmodalapi = $("#modal_ajax").unbind().overlay({api:true,finish:{top:'center',left:'center',absolute:true},
               expose: {
                    color: '#000',
                    opacity: 0.3,
                    closeSpeed: 1000
                },
               onLoad: function(){




                    if ($.browser.msie && parseInt($.browser.version)<=6) //IE MUST DIE!
                    {
                        $("#bd :input").hide();
                    }
               },
               onClose: function(){
                   if ($.browser.msie && parseInt($.browser.version)<=6) //IE MUST DIE!
                    {
                        $("#bd :input").show();
                        $("#modal_ajax .loading").show();
                        $("#modal_ajax .loading").next().remove();
                    }
                    if(modalButtonHref.match(/termCondition/)){
                          var category = 'TERMS TRACKING';
                          var action = 'close terms';
                          var label = $("#f_email").val();
                          if(label==undefined) label = 'Existing Member';
                          var value = startstop();
                         gaPagetracker(category, action, label, value);
                    }
               }
               });

      resetModalButtons();

   }


   var modalButtonHref ='';
   function resetModalButtons()
   {
        $('.doModal').unbind().click(function(){
          var id = $(this).attr('rel');
          modalButtonHref = $(this).attr('href');
          $("#"+id).overlay().load();
          return false;
       });

   }
*/


    function runDialog(el){

       var relative = $(el).attr('rel');
          var title="";
          var html = "";
          if(relative==null) relative="";
          var path = $(el).attr('href');
          if(relative!="") title = $("#"+relative).attr('title');
          if(title=="" || title==null) title = $(el).attr('title');
          if(title=="" || title==null) title = $(el).text();
          //remove validation from the div we are pulling the text from
          if(relative!="") $("#"+relative+" :input").addClass('disabled');



          if(relative!="") html = $("#"+relative).html();


          $("#bj_dialog").dialog('option','title',title);

          $("#bj_dialog").dialog('option','open',function(event,ui){

         var docHeight = parseInt($(document).height());
         var docWidth = parseInt($(window).width());

          if(!$.support.leadingWhitespace) {


          var pos = $("#bj_dialog").position();
          var dw = parseInt($("#bj_dialog").width());
          var dh = parseInt($("#bj_dialog").height());
          var w = parseInt($(window).width());

          var left = w/2 - 400;
          var top = 50;
          $(window).scrollTop(0);
           var posA = [left,top];

          $("#bj_dialog").dialog('option','position',posA);
          }

        //  var left = parseInt(pos.left);



              $(".ui-widget-overlay").height(docHeight).width(docWidth).click(function(){
                $(".ui-dialog-titlebar-close").trigger('click');
            })

              if(relative.match(/ajax/)==null && relative!="")
              {
               $("#bj_dialog .loading").after('<div id="dialogContainer">'+html+'</div>');


               $('#dialogContainer :input').each(function(){

          })

               $("#bj_dialog .loading").hide();
               //add back validation
               $("#bj_dialog :input").removeClass('disabled');


              }
              else
              {

                  dialogAjaxContent(path);
              }
          });
          $("#bj_dialog").dialog('open');


   }



   function prepDialog() //prepares a dialog box to use for dialog popups
   {
       $("#bj_dialog").dialog({
        autoOpen:false,
        modal:true,
        width:800,
        height:400,
        resizable:false,
        open:function(event,ui){



             $(".ui-dialog-content .loading").after();
             $(".ui-dialog-content .loading").hide();
             $(".ui-widget-overlay").click(function(){
                $(".ui-dialog-titlebar-close").trigger('click');
            })

        },
        close: function(event,ui){
            $("#bj_dialog :input").addClass('disabled');
            runValidate();
            $("#bj_dialog .loading").show();
            $("#bj_dialog #dialogContainer").remove();
        }
    });
    startDialog();
   }

   function startDialog()
   {

      $('.openDialog').click(function(){
          runDialog(this);
          return false;
       });

       $(".doDialog,.doModal").click(function(){

          var relative = $(this).attr('rel');
          var path = $(this).attr('href');
          var title = $("#"+relative).attr('title');
          //remove validation from the div we are pulling the text from
          $("#"+relative+" :input").addClass('disabled');



          var html = $("#"+relative).html();




          $("#bj_dialog").dialog('option','title',title);
          $("#bj_dialog").dialog('option','open',function(event,ui){

              $(".ui-widget-overlay").click(function(){
                $(".ui-dialog-titlebar-close").trigger('click');
            })

              if(relative.match(/ajax/)==null)
              {
               $("#bj_dialog .loading").after('<div id="dialogContainer">'+html+'</div>');


               $('#dialogContainer :input').each(function(){
              if($(this).attr('xmlns')!=null) $(this).remove()
          })

               $("#bj_dialog .loading").hide();
               //add back validation
               $("#bj_dialog :input").removeClass('disabled');
               runValidate('',true);
               if(relative.match(/login/)!=null){login.run("#"+relative);}
               if(relative.match(/addnew/)!=null){addAddress();}
               if(relative.match(/editaddress/)!=null){editAddress();}
              }
              else
              {
                  dialogAjaxContent(path);
              }
          });
          $("#bj_dialog").dialog('open');

          $(".bj_buynow").each(function(){
              $(this).btOff(); //turn off any buy now tooltips
          })

          return false;
       });
   }


   function dialogAjaxContent(path)
   {

       var thisData = '';
       var $this = this;

       if(path.match(/#/)!=null) {

                    var route = path.split('#');
                    var uriString = route[1].split('?');
                    var uri = uriString[0];
                    var hash = uriString[1];
                    var keyVal = hash.split('=');

                    var p = {};
                    p[keyVal[0]] = keyVal[1];
                    var d = getDomain();

                    $.ajax({
                        data: p,
                        type:'POST',
                        dataType:'html',
                        url: d+uri,
                        success: function(data){

                           $("#bj_dialog .loading").after('<div id="dialogContainer">'+data+'</div>');
                           $("#bj_dialog .loading").hide();
                           //add back validation
                           $("#bj_dialog :input").removeClass('disabled');




                           runValidate();
                        }
                    });

                     var category = 'TERMS TRACKING';
                          var action = 'close terms';
                          var label = $("#f_email").val();
                          if(label==undefined) label = 'Existing Member';
                          var value = startstop();
                         gaPagetracker(category, action, label, value);
       } else {
          $("#bj_dialog .loading").after('<div id="dialogContainer"></div>');
          $('#dialogContainer').load(path,function(){
             // fixThemes($.Jookie.Get('theme','href'))
            //  createSelectMenu();
             // styleForms();


             /* i hate putting single page code into js, but since its only needed for a month, i can live with it */
                           if(document.location.href.match(/countdown/)!=null){
                               if($("#f_email_a").val()!=''){
                                   $("#f_email").val($("#f_email_a").val());
                                   $(".acform").hide();
                               }
                           }


              if(path.match(/login/)!=null){

                               var login = new bjLogin({});

                               login.run("#logindiv");}

              $("#bj_dialog .loading").hide();
          });
       }
   }







	function addressBookMayhem()
	{
		var addressValue = $('#f_address1').val();
		if(addressValue!=undefined)
		{
		if(addressValue.match(/p\.?o\.? box/gi)!=null)
		{
			$("#f_addressLocationCd").val('B');
		}

		$("#f_address1").keyup(function(){
			var addressValue = $(this).val();
			if(addressValue.match(/p\.?o\.? box/gi)!=null)
			{
			$("#f_addressLocationCd").val('B');
			}
			else $("#f_addressLocationCd").val('R');
		});
		}
	}











	 /*** FAQ OPENER **/

	 function enable_faq()
	 {

	 	$(".faq_box:even").each(function(){
	 		$(this).addClass('stripe_even');
	 	});

	 	$(".faqquestion a").toggle(
	      function () {
	      //	console.log('hello')
	      	$(this).attr('class','faq_active');
	        $(this).parent().next().slideDown('slow');
	        return false;
	      },
	      function () {
	      	$(this).attr('class','');
	         $(this).parent().next().slideUp('slow');
	        return false;
	      }
	    );

	 }













