// extension specific variables
var gridTimer, timerObj; 
var current;

/**
 * jquery extensions
 */
jQuery.fn.carat = function(color, colorDuration, position1, position1Duration, position2, position2Duration) 
{
	var c = (color) ? color : '#000000';
	var cD = (colorDuration) ? colorDuration : 250;
	var p1 = (position1) ? position1 : '(5px 5px)';
	var p1D = (position1Duration) ? position1Duration : 100;
	var p2 = (position2) ? position2 : '(3px 5px)';
	var p2D = (position2Duration) ? position2Duration : 150;
	$(this).stop(true)
	       .animate({color: c}, {queue:false, duration: cD})
	       .animate({backgroundPosition: p1}, p1D)
           .animate({backgroundPosition: p2}, p2D);	
}
	
jQuery.fn.bgColorSwap = function(color1, color2)
{
	$(this).hover(
				  function(){
					  $(this).stop().animate({'backgroundColor': color1}, {duration: 150});
				  },
				  function(){
					  $(this).stop().animate({'backgroundColor': color2}, {duration: 150});
				  }
				  );
}
		
jQuery.fn.zoomIn = function()
{
	$(this).stop(true)
	.animate({'margin-top':'0px'}, {duration: 250})
	.find('img').stop(true)
	.animate({'height':'63px', 'width':'111px'}, {duration: 250});
	return this;
}

jQuery.fn.zoomOut = function()
{
	$(this).stop(true)
	.animate({'margin-top':'13px'}, {duration: 250})
	.find('img').stop(true)
	.animate({'height':'50px', 'width':'89px'}, {duration: 250});
	return this;
}

jQuery.fn.zoomOut = function()
{
	$(this).stop(true)
	.animate({'margin-top':'13px'}, {duration: 250})
	.find('img').stop(true)
	.animate({'height':'50px', 'width':'89px'}, {duration: 250});
	return this;
}

/**
 * URL Query string parser
 * @param fragment - query string fragment from url without "?"
 * @returns query string dictionary(map of key=value pair)
 */
jQuery.fn.querystring = function(fragment)
{
    var qsDict = {};
 
    var qs = decodeURI(fragment);
 
    if (!qs) {
        return {};
    }
    //Querystring should be passed without "?"
    //qs = qs.substring(1);
 
    var pairs = qs.split("&");
    for (var i = 0; i < pairs.length; i++) {
        var keyValuePair = pairs[i].split("=");
        qsDict[keyValuePair[0]]
                = keyValuePair[1];
    }
 
    qsDict.toString = function() {
        if (qsDict.length == 0) {
            return "";
        }
        var toString = "?";
        for (var key in qsDict) {
            toString += key + "=" +
                qsDict[key];
        }
        return toString;
    };

    return qsDict;
}
	
jQuery.fn.collapseTout = function(subsection)
{
	var w = (subsection == 'landing') ? '202px' : '245px';
	var options = ($(this).hasClass('last')) ? {width: w, marginLeft: '0px'} : {width: w};
	$(this).unbind('mouseleave').unbind('mouseenter').stop(true).animate(options, 500).removeClass('expanded');
	return this;
}

/**
 * carousel
 */
jQuery.fn.activateCarousel = function(){

	var zoom = $(current).parent().hasClass('zoom');

    var isRich = typeof(mediaContainer) != 'undefined' ? true : false;

    if(current && current[0] != this[0]){

        newIndex = $(touts).index(this);

        var carouselContentUrl = carouselContent.eq(newIndex).find(".hero-media").eq(0).text();

        var mediaType =  carouselContentUrl.substring(carouselContentUrl.length - 3).toLowerCase();
        $('#blackout').stop(true, true).show()
            .animate(
                {'opacity':'1'}
                , {
                    duration : 500
                    , complete: function() {
                	
                        if(isRich && swfObjectLoaded && 'clear' in mediaContainer){
                        	mediaContainer.clear();
                            mediaContainer.setContent( carouselContentUrl );

                            if (mediaType == 'swf' || mediaType == 'flv'){

                                carouselContent.eq(newIndex).find(".hp-hero-links").eq(0).hide();
                            }

                            if (mediaType == 'flv'){

                                clearInterval(carouselAutoPlay);

                            }


                        }

                        carouselContent.hide();
                        carouselContent.eq(newIndex).show();

                        $(this).animate(
                           {'opacity':'0'}
                           , {
                               duration: 500
                               , complete: function() {
                                   $(this).hide();
                               } 
                           }
                        );

                    }
                }
            )
        ;
        if(zoom){$(current).find('.tout-thumbnail').zoomOut();}
    }
    current = this;
    if(zoom){current.find('.tout-thumbnail').zoomIn();}
    return current;
}

