// JavaScript Document

$(document).ready(function () {
    /* pulldown for the language selection */
    $('TD.lang').click(function () {
        $(".languagePulldown").slideDown('fast');
    });
    $(".languagePulldown").mouseleave(function () {
        $(this).slideUp('fast');
    });

    /* jquery cycle caroussel */
    $('#caroussel').append('<ul id="pager">')
    $('.slides').cycle({
        fx: 'fade',
        pager: '#pager',
        pause: 1,
        timeout: 5000,

        pagerAnchorBuilder: function (idx, slide) {
            return '<li><a href="#"></a></li>';
        }
    });

    /* hover function on widgets on home- & search-page */
    $('.widget').hover(function () {
        $('IMG.bg', this).fadeIn();
    }, function () {
        $('IMG.bg', this).fadeOut();
    });

    /* fancybox trigger */
    $(".fancyboxVid").fancybox({
        'titlePosition': 'inside',
        'transitionIn': 'none',
        'transitionOut': 'none',
        'overlayColor': '#584a44',
        'overlayOpacity': '0.7'
    });
    $("a.album_image").fancybox({
        'transitionIn': 'none',
        'transitionOut': 'none',
        'speedIn': 600,
        'speedOut': 200,
        'overlayShow': true
    });
    
    // replace alt parameter outside the image when in sidebar div
    $('.sidebar IMG[alt], .news .mainContent IMG[alt]').each(function () {
        $(this).wrap('<div class="imgPlaceholder"></div>').after('<p>' + $(this).attr('alt') + '</p>');
    });
	
	// loop through all the anchors
	$("A").each(function() {
		// match only the ones going to apple.com
		var href = $(this).attr("href");
		if (href != null && href.match(/apple.com/) != null) {
			// catch the clicks on those
			$(this).click(function (e) { 
				// when clicked track the event to the analytics
				_gaq.push(["_trackEvent", "external links", "apple.com", href]); 
			
			});
		}
	});

    // give images in the content margins when they are left or right aligned
    $('.contentContent IMG[align]').each(function () {
        $(this).addClass($(this).attr('align'));
    });

    // execute the function queue 
    if (initQ != null) {
        for (var i = 0; i < initQ.length; i++) {
            initQ[i].call();
        }
    }
});


