
// Prepare container for GA events
_gaq = [];
_gaq.push(['_setAccount', 'UA-1693297-1']);
ga_virtual_url = '';

// Setup main nav
$(document).ready(function() { 

  // See if Eloqua needs to update a form
  if (typeof(GetElqCustomerGUID) != "undefined") {
    $('#elqCustomerGUID').val(GetElqCustomerGUID());
  }

  // EULA Retrofit
  if ($("p.question:contains('EULA')").length > 0) {
    var q = $("p.question:contains('EULA')");
    q.html(q.html().replace(/EULA/ig, '<a target="_blank" href="/Usage/Master-EULA?iframe=true&width=800&height=600">EULA</a>')); 
  } 

  // Setup Main Nav
  $('ul.nav-main-sf').superfish({
    delay: 500,
    animation: {
      opacity: 'show',
      height: 'show'
    },
    speed: 'fast',
    autoArrows: false,
    dropShadows: false
  });

   // Setup connect icons
  $('#header-connect').qtip({
    content: $('#header .connect-options'),
    position: { adjust: { x: 50, y: 100 } },
    hide: {
      fixed: true,
      delay: 250
    },
    position: {
      corner: {
        target: 'bottomLeft',
        tooltip: 'topRight'
      },
      adjust: {screen: true}
    },
    style: {
      width: {
        max: 500
      },
      border: {
        radius: 8
      },
      name: 'light', 
      tip: true
    }
  });

   // Add qtip to imagemaps
   $('area').each(function() {
      $(this).qtip({
         content: $(this).attr('alt'), // Use the ALT attribute of the area map
		    position: {
		      corner: {
		        target: 'topRight',
		        tooltip: 'bottomLeft'
		      },
		      adjust: {
			     screen: true,
			     x: -48, 
			     y: -10
			   }
		    },
		    hide: {
		      fixed: true,
		      delay: 250
		    },
		    style: {
		      width: {
		        max: 500
		      },
		      border: {
		        radius: 8
		      },
		      name: 'light', 
		      tip: true
		    }
      });
   });

  // Setup pagecurl
  $("#pagecurl").hover(function() { //On hover...
    $("#pagecurl img , #pagecurl .msg").stop()
      .animate({ //Animate and expand the image and the msg (Width + height)
        width: '300px',
        height: '300px'
      }, 500);
    } , function() {
    $("#pagecurl img").stop() //On hover out, go back to original size 50x50
      .animate({
        width: '50px',
        height: '50px'
      }, 220);
    $("#pagecurl .msg").stop() //On hover out, go back to original size 50x50
      .animate({
        width: '50px',
        height: '50px'
      }, 200); //Note this one retracts a bit faster (to prevent glitching in IE)
  });

  // Setup Image Zooming
  $("a[rel^='prettyPhoto']").prettyPhoto();

  // Setup Feature Matrixes
  $(".feature").each(function() {
      $(this).qtip({
          hide: {
            fixed: true,
            delay: 250
          },
          content: $('#desc-' + this.id),
          position: {
            corner: {
              target: 'rightMiddle',
              tooltip: 'leftMiddle'
             }
          },
          style: {
            width: {
              max: 500
              },
            border: {
              radius: 6,
              color: '#9CC1DE'
              },
            name: 'light', 
            tip: true
          }
      });
  });


  // Setup super footer connect links 
  $('#footer-links a[title]').qtip({
    position: {
      corner: {
        target: 'topLeft',
        tooltip: 'bottomRight'
      },
      adjust: {screen: true}
    },
    style: {
      textAlign: 'center',
      width: {
        max: 270
      },
      border: {
        radius: 8
      },
      name: 'light', 
      tip: true
    }

  });

  // Setup Uniform
  if ($('.uni-form').length > 0) {
    $(".uni-form select, .uni-form input[type='checkbox'], .uni-form input[type='radio'], .uni-form input[type='file'], .uni-form input[type='text'], .uni-form input[type='button'], .uni-form input[type='submit'], .uni-form input[type='reset'], .uni-form textarea").uniform();
    $(".justify").each(function() {
      maxi = 0
      $(this).find("label").each(function() { 
          width = $(this).width();
          if (width > maxi) { 
            maxi = width;
          } 
        });
      maxi = maxi * 1.1;
      $(this).find("label").width(maxi);
    });
  }


  // Setup expandable definition lists
  $('dl.expandable dt').live('click',function(){
    var dd = $(this).next();
    var w = dd.parent().width() * 0.95;
    dd.css({ width : w });
    if(!dd.is(':animated')){
      dd.slideToggle(750);
      $(this).toggleClass('opened');
    }
  });

  $('a.expand-all').click(function(){
    if($(this).hasClass('collapse')){
      $('dl.expandable dt.opened').click();
    }
    else $('dl.expandable dt:not(.opened)').click();
    $(this).toggleClass('expand collapse');
    return false;
  });


  // Toggel vissibility of items
  cur_ref = $('.displayed');
  $('a.show[rel]').click(function() {
    var target = $('#' + $(this).attr('rel'));
    if (!$(target).hasClass('displayed')){
     $('#' + cur_ref.attr('id') + '-btn').toggleClass('current');
     $('#' + target.attr('id') + '-btn').toggleClass('current');
     $(cur_ref).fadeOut(350, function() {
      $(cur_ref).toggleClass('displayed');
       $(target).fadeIn(350, function() {
         $(target).toggleClass('displayed');
         cur_ref = $(target);
       });
     });
    }
    return false;
  });


  /* Checkout method */
  if ($("input[name$='payment_type']").length > 0) {

			if ($("#payment_type_cc").attr('checked')){
				$("#method-cc").show();
			} else {
				$("#method-cc").hide();
			}

			if ($("#payment_type_pp").attr('checked')){
				$("#method-pp").show();
			} else {
				$("#method-pp").hide();
			}

			$("input[name$='payment_type']").click(function() {
					var payment_type = $(this).val();
					if (payment_type == 'cc') {
						$("#method-pp").slideUp('fast', function() {
									$("#method-cc").slideDown('fast');
								});
					} else if (payment_type == 'pp') {
						$("#method-cc").slideUp('fast', function() {
									$("#method-pp").slideDown('fast');
								});

					}
			});

	}

  /*  
  if ($("input[name$='payment_type']").length > 0) {

	
			if ($("#payment_type_cc").attr('checked')){
				$("#method-cc").show();
			} else {
				$("#method-cc").hide();
			}
			
			if ($("#payment_type_pp").attr('checked')){
				$("#method-pp").show();
			} else {
				$("#method-pp").hide();
			}
	
			if ($("#payment_type_inv").attr('checked')){
				$("#method-inv").show();
			} else {
				$("#method-inv").hide();
			}
	
			$("input[name$='payment_type']").click(function() {
					var payment_type = $(this).val();
					if (payment_type == 'cc') {
						document.checkoutForm.action = '/shop/checkout';
						$("#method-pp").slideUp('fast', function() {
						$("#method-inv").slideUp('fast', function () {
                     $("#method-cc").slideDown('fast');
					    }) });
					} else if (payment_type == 'pp') {
						document.checkoutForm.action = '/pp/expresscheckout';
						$("#method-cc").slideUp('fast', function() {
						$("#method-inv").slideUp('fast', function () {
						$("#method-pp").slideDown('fast');
						}) });
					} else if (payment_type == 'inv') {
						document.checkoutForm.action = '/shop/checkout';
						$("#method-cc").slideUp('fast', function() {
						$("#method-pp").slideUp('fast', function () {
						$("#method-inv").slideDown('fast');
						}) });
					}
			});

	}
	*/

  // Update the sidebar in the books
  if ($('.book #sidebar').length > 0) {
    // Add Trial and Demo links
     // Add the demo links
     $('.ss-book-mcookbook #sidebar, .ss-book-mvnref #sidebar, .ss-book-mvnex #sidebar, .ss-book-nxbook #sidebar, .ss-book-m2eclipse #sidebar, .ss-book-tychobook #sidebar').append('<a class="gbtn gb-sad"  href="/Request/Live-Demo/Live-Sonatype-Pro-Suite-Demo" title="Access a live demo of the Sonatype Pro Suite"><span>Access a live demo of the Sonatype Pro Suite</span></a> <a class="gbtn gb-tiff" href="/Request/Free-Trial/Free-Sonatype-Pro-Suite-Trial" title="Install a trial version of the Sonatype Pro Suite on your own machine"><span>Install a trial version of the Sonatype Pro Suite on your own machine</span></a> <a class="gbtn gb-bn"   href="/Products/Sonatype-Pro-Suite/Purchase" title="Purchase a license for the Sonatype Pro Suite"><span>Purchase a license for the Sonatype Pro Suite</span></a>');

    // Add the feedburner recent blog posts
    $('.book #sidebar').append('<h3>Recent Sonatype Posts</h3><div id="recent-posts"></div>');
    // Add ticker element first then do this
    //$('.book #sidebar some-element')
     $('.book #sidebar #recent-posts').rssfeed('http://www.sonatype.com/people/feed/',{
        limit: 5,
      header: false,
      titletag: 'h4',
      date: false,
      content: false,
      snippet: true,
      showerror: false,
      errormsg: '',
      key: null,
      ssl: false,
      linktarget: '_self'
       }, function(e) {
       $(e).find('div.rssBody').vTicker({
        speed: 700,
        pause: 6000,
        showItems: 5,
        animation: '',
        mousePause: true,
        isPaused: false
       });
    });


     // Add the connect links
     $('.book #sidebar').append('<h3>Connect With Sonatype</h3><ul class="connect-options"><li><a class="bgi30 i30-blog" href="/people" title="Tips, Tricks, and news on all things Maven, Hudson, Nexus, and Central!">Follow the Sonatype Blog</a></li><li><a class="bgi30 i30-newsletter" href="/Request/Newsletter" title="So you prefer email? No problem! Sign-up and we\'ll keep you current.">Get the Sonatype Newsletter</a></li><li><a class="bgi30 i30-twitter" target="_blank" href="http://twitter.com/SonatypeCM" title="Follow Sonatype on Twitter!">Follow Sonatype on Twitter</a></li><li><a class="bgi30 i30-youtube" target="_blank" href="http://www.youtube.com/user/sonatype" title="Watch Sonatype\'s YouTube Channel!">Tune in on YouTube</a></li><li><a class="bgi30 i30-linkedin" target="_blank" href="http://www.linkedin.com/company/sonatype" title="Follow us on LinkedIn!">Connect on LinkedIn</a></li></ul>');

  }




  if ($('.youtube-player').length > 0) {
    var ytp_config =  {
     width: 640,                     // player width (integer or string)
     height: 360,                    // player height (integer or string)
           showPlaylist: 0,                // show playlist on plugin init (boolean)
           showTime: 0,                    // show current time and duration in toolbar (boolean)
           showTitleOverlay: 0,            // show video title overlay text (boolean)
           repeat: 0,                      // repeat videos (boolean)
           chromeless: 0,                  // chromeless player (boolean)
           highDef: 1,                     // high definition quality or normal quality (boolean)
           showToolbar: 0,                 // show or hide the custom toolbar (boolean)
     playlist: {
         title: 'Random videos',
         videos: $("a.video-trigger").map(function() {
                                                return {
                                                        id: $(this).attr('href').replace(/^[^v]+v.(.{11}).*/, '$1'),
                                                        title: $(this).html()
                                                      };
                    })
               }
    };

    player = $('.youtube-player').player(ytp_config);
    p = player.player('plugin');

    function playYTVideo(vid, vtitle) {
        p.loadVideo({
            id: vid,
            title: vtitle
        });
        p.playVideo();
    }

    $("a.video-trigger").click(function() {
        playYTVideo($(this).attr('href').replace(/^[^v]+v.(.{11}).*/, '$1'), $(this).html());
        return false;
    });
  }


});




// Swap in Flash banner
/*

// This should only happen on the homepage

var flashvars = {};
var params = {wmode:"transparent"};
var attributes = {};
swfobject.embedSWF("../flash/bnr-home-flash.swf", "bnr-home-flash", "996", "240", "9.0.0", "../javascripts/swfobject/expressInstall.swf", flashvars, params, attributes);
*/

