// *************** begin scrolling functions *********************************
jQuery.noConflict();

(function($){                     
            $.fn.entryMain = function(options){
                        options = $.extend({
                                    interval: 2000,
                                    auto: true          
                        }, options);
 
                        var _mainContainer = $(this);
                        var _totalImages = options.mainImages.length;
						var _totalsubs = options.subImages.length;
                        var _images;
 
                        var _interval = options.interval;
                        var _autoPlay = options.auto;
                        var _current = 0;
                        var _timer;
 
                        function _configureImages(){
                                    var imageContent = [];
                                    for(x=0;x<_totalImages;x++){
										imageContent.push('<a href="'+options.mainLinks[x]+'"><img class="mainimgs" id="img'+x+'" src="'+options.mainImages[x]+'" border="0"/></a>');
                                    }
                                    _mainContainer.append(imageContent.join(''));
                                    _images = $('img.mainimgs');
                        }

						function _configuresubs(){
                                    var subContent = [];
									subContent.push('<div id="subAd-background"><div id="subAds">');
                                    for(x=0;x<_totalsubs;x++){
										subContent.push('<div class="subAd"><a href="'+options.subLinks[x]+'"><img src="'+options.subImages[x]+'"/></a></div>');
                                    }
									subContent.push('</div></div>');
									subContent.push('<div id="downarrow"><img src="'+options.downArrow+'" title="PRESS DOWN"</div>');
                                    _mainContainer.append(subContent.join(''));
                        }
 
                        function _setFadeIn(){ //need to change
									_images.hide();
									_images.eq(_current).show();
                                    _mainContainer.fadeIn(500);
                        }
 
                        function _show(index){
                                    while(index < 0)
                                                index += _totalImages;
                                    while(index >= _totalImages)
                                                index -= _totalImages;
                                    _images.eq(_current).fadeOut(500);
                                    _images.eq(index).fadeIn(500);
                                    _current = index;            
                                    if(_autoPlay)
                                                _start();
                        }
 
                        function _start(){
                                    _stop();
                                    _autoPlay = true;
                                    _timer = setTimeout(_next, _interval); 
                        }
 
                        function _stop(){
                                    _autoPlay = false;
                                    clearTimeout(_timer);
                        }
 
                        function _previous(){return _show(_current - 1);}
                        function _next(){return _show(_current +1);}
 
                        _configureImages();
						_configuresubs();
                        _setFadeIn();
 
                        return this.each(function(){
                                    function show(index){return _show(index);}
                                    function previous(){return _previous();}
                                    function next(){return _next();}
                                    
                                    _mainContainer.bind('prev', previous).bind('next', next).bind('goto', function(e, index){
                                                show(index);
                                    }).bind('stop', _stop).bind('start',_start);
                                    if(_autoPlay && _totalImages > 1)
                                                _start();
                        });
            };          


})(jQuery);

(function($){	
	$(document).ready(function() {
		if(isiPad){
			 var imageContainer = $('#js-main');
			 imageContainer.entryMain({
					mainImages: mainImages,
					mainLinks: mainLinksNoFlash,
					subImages: subImages,
					subLinks: subLinksNoFlash,
					downArrow: downArrow,
					interval: 3000,
					auto: true
			}); 
			var slider = $('#subAds');
			var dnarrow = $('#downarrow');	
			dnarrow.hide();
			dnarrow.css('top', '445px'); 
			slider.hide();
			var scrollDirection = "-"; // direction must be either "+" or "-"
			var scrollDuration = 800;  // determines the speed
			var initscrollDuration = 2000;  // determines the speed	
			var animating = false;
			var atTheTop = 0;
			var numloaded = 0;
			var stackBottom = slider.outerHeight(true);
			$('#subAds img').load(function(){
				numloaded++;
				if (numloaded == subImages.length){
					stackBottom = slider.outerHeight(true);
					}
				});
			
			//init 
			slider.show();
			slider.css('top', "-"+slider.height());
			slider.animate({top: 0}, initscrollDuration);
			dnarrow.fadeIn('slow');
			dnarrow.click(function() {
				doScroll(scrollDirection);
			});
		}

		function doScroll(direction){	
			if(!animating){
				animating = true;
				var scrollDistance = $(".subAd:eq("+atTheTop+")").outerHeight(true);
				var nextStop = 	direction+'='+scrollDistance;
				slider.animate({top: nextStop}, scrollDuration, afterScroll);
			}
		}

		function afterScroll() {
			$(".subAd:eq("+atTheTop+")").css("top", function(index,val){
				return stackBottom + "px";
			});
			atTheTop++;
			if(atTheTop == $('div.subAd').length){
				$(".subAd").css('top', 0);
				slider.css('top', 0);
				atTheTop = 0;
			}
			animating = false;
		}
		
		if($('#main-slider')){
			var position = $("#slide-img").width() + 10;
			$("#main-slider").css("left",-position);
			$('#close-slide').css("opacity","0").click(function() {
										$(this).hide();
										$('#main-slider').animate({opacity: 'toggle'},'slow');
										return false;
									});
			position = position+10;
			setTimeout(function(){$("#main-slider").animate({"left": "+="+position,opacity: 'toggle'},400,function(){$('#close-slide').show();});}, 600);
		}
	});
})(jQuery);
// *************** end scrolling functions *********************************

/**
 * Display iPad application promotion on an overlay for domestic users that have not 
 * viewed the overlay for the past 90 days - assuming cookies are accepted and not 
 * deleted by the user.
 */

(function($){
	
	if ( !$ ) return;
	
	$(function(){		
		var cookie = getCookie('sks_ipdwlcm'),
			isiPad = window.navigator.userAgent.match(/iPad/i) != null;
		
		if ( !( isiPad && cookie != 'skip' && countryCode && countryCode == 'US' ) ) return;		
		 		
		createCookie('sks_ipdwlcm', 'skip', 90);
		
		sksGlobalAjax.ajaxRequest({
			url: '/static/html/iPad_Welcome_Layer.html',
			dataType: 'html',
			success: function (html) {
				$( 'html, body' ).css({ height: '100%', width: '100%' });
				document.addEventListener('touchstart', function(){}, false);
				window.addEventListener('orientationchange', function(){ saksModal.refresh() }, false)										
				saksModal.viewInModal(html, 728, 629);				
			}
		});
	});

})(jQuery);
