$(document).ready(function() {

  $("div.warning").fadeIn(2000).animate({opacity: 1.0}, 3000).fadeOut(1000, function() { $(this).remove(); } ); 
  $(".fadeIn").hide().fadeIn(1000);
  $(".fadeInSlow").hide().fadeIn(2000);

// --------------------------------------------------------
// Forgot password reset
// --------------------------------------------------------

function finishAjax(id, response) {
  $('#loading').hide();
  $('#'+id).html(unescape(response));
  $('#'+id).fadeIn();
}


	$('#usernameLoading').hide();
	$('#fld_email_work').blur(function(){
	  $('#usernameLoading').show();
      $.post("/pub/check_email.php", {
        email: $('#fld_email_work').val()
      }, function(response){
        $('#usernameResult').fadeOut();
        setTimeout("finishAjax('usernameResult', '"+escape(response)+"')", 600);
      });
    	return false;
	});


// --------------------------------------------------------
// popup 
// --------------------------------------------------------
var popupStatus = 0;

$.fn.loadPopup = function(){
		$("#background_popup").css({ "opacity": "0.7" });
		$("#background_popup").fadeIn("slow");
		$("#popup_content").fadeIn("slow");
		popupStatus = 1;

}

$.fn.disablePopup = function(){
  $("#background_popup").fadeOut("slow");
  $("#popup_content").fadeOut("slow");
  popupStatus = 0;
}

$.fn.centerPopup = function(){
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#popup_content").height();
	var popupWidth = $("#popup_content").width();
	$("#popup_content").css({
		"position": "absolute",
		"top": windowHeight/2-popupHeight/2,
		"left": windowWidth/2-popupWidth/2
	});
	//only need force for IE6	
	$("#background_popup").css({ "height": windowHeight });
	
}


$("#product_diagram").click(function(){
    $(this).centerPopup();
    $(this).loadPopup();
});


  $("#popup_close").click(function(){
    $(this).disablePopup();
  });

  $("#background_popup").click(function(){
    $(this).disablePopup();
  });

  $(document).keypress(function(e){
    if(e.keyCode == 27){
      $(this).disablePopup();
    }
  });

// --------------------------------------------------------
// show hid div 
// --------------------------------------------------------

  $('#toggle').click(function(){
   $('div.showhide').slideToggle();
  });

// --------------------------------------------------------
// slideshow (interior) 
// --------------------------------------------------------

  $('#slideshow_interior').cycle({
      speed:9000,
      speedIn:3000,
      speedOut:3000,
      timeout:2000,
      sync: 1,
      delay:1000
    });    
    
  $('#tip_slideshow').cycle({
  
next:   '#next2', 
prev:   '#prev2'

    });     

// --------------------------------------------------------
// photo gallery (inline) 
// --------------------------------------------------------

    
    $('.photo_gallery').after('<div id="gallery_nav"><ul id="pager">').cycle({
      pager: '#pager',
      fastOnEvent: true,      
      sync: 1,
      // callback fn that creates a thumbnail to use as pager anchor 
      pagerAnchorBuilder: function(idx, slide) { 
        var imgalt = $(slide).children().eq(0).attr("alt");
        if (imgalt == null ) { imgalt = slide.alt; }
        return '<li id="slide_0' + idx + '" ><a href="#">' + imgalt + '</a></li>'; 
      },
//      after: onAfter
    }); 

    var pager = $('#pager');
    var elementCount = $('#pager > li').length;
    var elementWidth = (elementCount * 13);
    var elementWidthString = elementWidth + "px";

    pager.css("width",elementWidthString);



  function onAfter(curr,next,opts) {
//    $("#gallery_nav > ul > li").removeClass("activeSlide"); 
//    $("#slide_0"+(opts.currSlide + 1)+"").addClass("activeSlide"); 
  } 




// --------------------------------------------------------
// accordion navigation
// --------------------------------------------------------
  
 $("#acc1").accordion({
    active: '.selected',
    alwaysOpen: false,
    autoheight: false,
    header: 'a.acc1',
    clearStyle: true
  });
  
  $("#acc2").accordion({
    active: '.selected',
    alwaysOpen: false,
    autoheight: false,
    header: 'a.acc2',
    clearStyle: true
  }); 

  $("#acc_left").accordion({
    active: '.selected',
    alwaysOpen: false,
    autoheight: false,
    header: 'a.acc2',
    clearStyle: true
  });  

  $("#acc_right").accordion({
    active: '.selected',
    alwaysOpen: false,
    autoheight: false,
    header: 'a.acc2',
    clearStyle: true
  });  

  $("#acc_stage").accordion({
    active: '.selected',
    alwaysOpen: false,
    autoheight: false,
    header: 'a.acc2',
    clearStyle: true
  });  
  
// --------------------------------------------------------
// tooltips 
// --------------------------------------------------------

  $("[title]").mbTooltip({          // also $([domElement])..mbTooltip  >>  in this case only children element are involved
    opacity : .97,                  //opacity
    wait:1200,                      //before show
    cssClass:"default",             // default = default
    timePerWord:70,                 //time to show in milliseconds per word
    hasArrow:true,                  // if you whant a little arrow on the corner
    imgPath:"/img/tooltips/",
    ancor:"mouse",                  //"parent"  you can ancor the tooltip to the mouse position or at the bottom of the element
    shadowColor:"black"             //the color of the shadow
  });

// --------------------------------------------------------
// corner rounding
// --------------------------------------------------------
  
  $("div.round").corner("6px");
  $("div.round_bottom").corner("bottom 6px");
  $("div.round_small").corner("top 3px");

// --------------------------------------------------------
//  Form Validation + AJAX Submission
// --------------------------------------------------------
    var options = { 
        target:        '#results'   // target element(s) to be updated with server response 
    }; 
  
  // validate form
  $(".validateMe").validate({
   submitHandler: function(form) {
      $(".form_wrapper").hide();
      $(form).ajaxSubmit(options);
   }
  }); 

// --------------------------------------------------------
// file uploader (uploadify)
// --------------------------------------------------------

	$("#uploadify").uploadify({
		'uploader'       : '/js/uploadify/uploadify.swf',
		'script'         : '/js/uploadify/uploadify.php',
		'cancelImg'      : '/img/buttons/bttn_cancel_2.png',
		'folder'         : '/assets/xml/products/',
		'queueID'        : 'fileQueue',
		'auto'           : true,
		'multi'          : true,
		'scriptAccess'   : 'always'
	});






   
});


