var SAPCareerHome = window.SAPCareerHome || {};

(function($){

SAPCareerHome.Media = (function() {
	return { //public members
		closeOverlay:function (){
            $('#videoModalOverlay').dialog('close');
        },
        popOverlay:function (type, guid){
            embedSlideShow(type, guid);
           $('#videoModalOverlay').dialog('open');
        }
	}
})();

function embedSlideShow(type, guid){
    var swfFile;
    var flashVars;
    if(type== "slideshow"){ 
   	      swfobject.embedSWF('/Assets/swf/slideShow.swf', 

	                           'testVideo', 

	                           '696', 

	                           '442', 

	                           '9.0.0', 

	                           '/Assets/swf/expressinstall.swf', 
    	                      
	                           {xmlPath: '/webservices/overlay.svc/slideshows/'+guid , closeFunc: 'SAPCareerHome.Media.closeOverlay', shareFunc: 'addThisFlash'}, 

	                           {bgcolor: '#FFFFFF' , wmode:'transparent'}, 

	                           {id: 'testVideo'});
      }else if(type=="movies"){
        swfobject.embedSWF('/Assets/swf/videoPlayer.swf', 

	                           'testVideo', 

	                           '696', 

	                           '442', 

	                           '9.0.0', 

	                           '/Assets/swf/expressinstall.swf', 
    	                      
	                           {xmlPath: '/webservices/overlay.svc/movies/'+ guid, closeFunc: 'SAPCareerHome.Media.closeOverlay', shareFunc: 'addThisFlash'},

	                           { bgcolor: '#000', wmode: 'transparent' }, 

	                           {id: 'testVideo'});
      }
}

SAPCareerHome.Carousels = (function() {
	var _this=this;
	var _order;
	var _id;

    var _heroScroll1_itemList = [];
    var _heroScroll2_itemList = [];
    var _heroScroll3_itemList = [];
    
	var carouselfirst = {
		
		heroScroll_initCallback: function(carousel){
			$('#mycarousel-next1').click(function() {
				carousel.next();
				return false;
			});

			$('#mycarousel-prev1').click(function() {
				carousel.prev();
				return false;
			});
		},
		heroScroll_getItemHTML: function(item)
		{
		    return '<img src="' + item.image + '" width="371" height="273" alt="' + item.title + '" />';
		},
		heroScroll_itemVisibleInCallback: function(carousel, item, i, state, evt)
		{
		    // The index() method calculates the index from a
		    // given index who is out of the actual item range.
		    var idx = carousel.index(i, _heroScroll1_itemList.length);
		    carousel.add(i, _this.heroScroll_getItemHTML(_heroScroll1_itemList[idx - 1]));
		    var mediaType = _heroScroll1_itemList[idx - 1].mediaShowType;
		    
		    if(mediaType == null) {
				console.info('mediaArray = null');
				$('#caption1').html('<a href="'+ _heroScroll1_itemList[idx - 1].url + '" class="captionTitle">' + _heroScroll1_itemList[idx - 1].title + '</a><br/>' + _heroScroll1_itemList[idx - 1].desc + '<br/>' + '<a href="'+ _heroScroll1_itemList[idx - 1].url + '">' + 'MORE &gt;' + '</a>');
			}
			else if(mediaType == 'slideshow'){
				$('#caption1').html('<a href="'+ _heroScroll1_itemList[idx - 1].url + '" class="captionTitle">' + _heroScroll1_itemList[idx - 1].title + '</a><br/>' + _heroScroll1_itemList[idx - 1].desc + '<br/>' + '<a href="javascript:SAPCareerHome.Media.popOverlay(\'slideshow\',\''+ _heroScroll1_itemList[idx - 1].mediaShowId +'\')">' + 'VIEW SLIDESHOW &gt;' + '</a>');
			}
			else if(mediaType =='movie'){
				$('#caption1').html('<a href="'+ _heroScroll1_itemList[idx - 1].url + '" class="captionTitle">' + _heroScroll1_itemList[idx - 1].title + '</a><br/>' + _heroScroll1_itemList[idx - 1].desc + '<br/>' + '<a href="javascript:SAPCareerHome.Media.popOverlay(\'movies\',\''+ _heroScroll1_itemList[idx - 1].mediaShowId +'\')">' + 'WATCH VIDEO &gt;' + '</a>');
			}
			//if($('.jcarousel-container-horizontal')){ $('.carouseldemo > .jcarousel-skin-tango > .jcarousel-container-horizontal > .overlayPanel').remove(); }
			//$('.carouseldemo > .jcarousel-skin-tango > .jcarousel-container-horizontal').append('<div class="overlayPanel">' + heroScroll_itemList[idx - 1].title + '</div>');
			if(_heroScroll1_itemList.length <= 1){
				$('#mycarousel-next1').hide();
				$('#mycarousel-prev1').hide();
			}else{
				$('#mycarousel-next1').show();
				$('#mycarousel-prev1').show();
			}
		},
		heroScroll_itemVisibleOutCallback: function(carousel, item, i, state, evt)
		{
		    carousel.remove(i);
		},		
		heroScroll_reloadCallback:function(carousel) {
			carousel.reset();
		},	
		init: function(id, index){
			_this = this;
			jQuery(id).jcarousel({
			    start:index,
				scroll:1,
				visible:1,
				animation:300,
				wrap:"circular",
				initCallback: _this.heroScroll_initCallback,
				buttonNextHTML:null,
		        buttonPrevHTML:null,
				itemVisibleInCallback: {onBeforeAnimation: _this.heroScroll_itemVisibleInCallback},
				itemVisibleOutCallback: {onAfterAnimation: _this.heroScroll_itemVisibleOutCallback},
				reloadCallback: _this.heroScroll_reloadCallback

			});		
		}
	};
	var carouselsecond = {
		
		heroScroll_initCallback: function(carousel){
			$('#mycarousel-next2').click(function() {
				carousel.next();
				return false;
			});

			$('#mycarousel-prev2').click(function() {
				carousel.prev();
				return false;
			});
		},
		heroScroll_getItemHTML: function(item)
		{
		    return '<img src="' + item.image + '" width="371" height="273" alt="' + item.title + '" />';
		},
		heroScroll_itemVisibleInCallback: function(carousel, item, i, state, evt)
		{
		    // The index() method calculates the index from a
		    // given index who is out of the actual item range.
		    var idx = carousel.index(i, _heroScroll2_itemList.length);
		    carousel.add(i, _this.heroScroll_getItemHTML(_heroScroll2_itemList[idx - 1]));
		    var mediaType = _heroScroll2_itemList[idx - 1].mediaShowType;
		    
		    if(mediaType == null) {
				console.info('mediaArray = null');
				$('#caption2').html('<a href="'+ _heroScroll2_itemList[idx - 1].url + '" class="captionTitle">' + _heroScroll2_itemList[idx - 1].title + '</a><br/>' + _heroScroll2_itemList[idx - 1].desc + '<br/>' + '<a href="'+ _heroScroll2_itemList[idx - 1].url + '">' + 'MORE &gt;' + '</a>');
			}
			else if(mediaType == 'slideshow'){
				$('#caption2').html('<a href="'+ _heroScroll2_itemList[idx - 1].url + '" class="captionTitle">' + _heroScroll2_itemList[idx - 1].title + '</a><br/>' + _heroScroll2_itemList[idx - 1].desc + '<br/>' + '<a href="javascript:SAPCareerHome.Media.popOverlay(\'slideshow\',\''+ _heroScroll2_itemList[idx - 1].mediaShowId +'\')">' + 'VIEW SLIDESHOW &gt;' + '</a>');
			}
			else if(mediaType =='movie'){
				$('#caption2').html('<a href="'+ _heroScroll2_itemList[idx - 1].url + '" class="captionTitle">' + _heroScroll2_itemList[idx - 1].title + '</a><br/>' + _heroScroll2_itemList[idx - 1].desc + '<br/>' + '<a href="javascript:SAPCareerHome.Media.popOverlay(\'movies\',\''+ _heroScroll2_itemList[idx - 1].mediaShowId +'\')">' + 'WATCH VIDEO &gt;' + '</a>');
			}

			//if($('.jcarousel-container-horizontal')){ $('.carouseldemo > .jcarousel-skin-tango > .jcarousel-container-horizontal > .overlayPanel').remove(); }
			//$('.carouseldemo > .jcarousel-skin-tango > .jcarousel-container-horizontal').append('<div class="overlayPanel">' + heroScroll_itemList[idx - 1].title + '</div>');
			if(_heroScroll2_itemList.length <= 1){
				$('#mycarousel-next2').hide();
				$('#mycarousel-prev2').hide();
			}else{
				$('#mycarousel-next2').show();
				$('#mycarousel-prev2').show();
			}
		},
		heroScroll_itemVisibleOutCallback: function(carousel, item, i, state, evt)
		{
		    carousel.remove(i);
		},		
		heroScroll_reloadCallback:function(carousel) {
			carousel.reset();
		},	
		init: function(id,index){
			_this = this;
			jQuery(id).jcarousel({
				start:index,
				scroll:1,
				visible:1,
				animation:300,
				wrap:"circular",
				initCallback: _this.heroScroll_initCallback,
				buttonNextHTML:null,
		        buttonPrevHTML:null,
				itemVisibleInCallback: {onBeforeAnimation: _this.heroScroll_itemVisibleInCallback},
				itemVisibleOutCallback: {onAfterAnimation: _this.heroScroll_itemVisibleOutCallback},
				reloadCallback: _this.heroScroll_reloadCallback
			});		
		}
	};
	var carouselthird = {
		
		heroScroll_initCallback: function(carousel){
			$('#mycarousel-next3').click(function() {
				carousel.next();
				return false;
			});

			$('#mycarousel-prev3').click(function() {
				carousel.prev();
				return false;
			});
		},
		heroScroll_getItemHTML: function(item)
		{
		    return '<img src="' + item.image + '" width="371" height="273" alt="' + item.title + '" />';
		},
		heroScroll_itemVisibleInCallback: function(carousel, item, i, state, evt)
		{
		    // The index() method calculates the index from a
		    // given index who is out of the actual item range.
		    var idx = carousel.index(i, _heroScroll3_itemList.length);
		    carousel.add(i, _this.heroScroll_getItemHTML(_heroScroll3_itemList[idx - 1]));
		    var mediaType = _heroScroll3_itemList[idx - 1].mediaShowType;
		    
		    if(mediaType == null) {
				console.info('mediaArray = null');
				$('#caption3').html('<a href="'+ _heroScroll3_itemList[idx - 1].url + '" class="captionTitle">' + _heroScroll3_itemList[idx - 1].title + '</a><br/>' + _heroScroll3_itemList[idx - 1].desc + '<br/>' + '<a href="'+ _heroScroll3_itemList[idx - 1].url + '">' + 'MORE &gt;' + '</a>');
			}
			else if(mediaType == 'slideshow'){
				$('#caption3').html('<a href="'+ _heroScroll3_itemList[idx - 1].url + '" class="captionTitle">' + _heroScroll3_itemList[idx - 1].title + '</a><br/>' + _heroScroll3_itemList[idx - 1].desc + '<br/>' + '<a href="javascript:SAPCareerHome.Media.popOverlay(\'slideshow\',\''+ _heroScroll3_itemList[idx - 1].mediaShowId +'\')">' + 'VIEW SLIDESHOW &gt;' + '</a>');
			}
			else if(mediaType =='movie'){
				$('#caption3').html('<a href="'+ _heroScroll3_itemList[idx - 1].url + '" class="captionTitle">' + _heroScroll3_itemList[idx - 1].title + '</a><br/>' + _heroScroll3_itemList[idx - 1].desc + '<br/>' + '<a href="javascript:SAPCareerHome.Media.popOverlay(\'movies\',\''+ _heroScroll3_itemList[idx - 1].mediaShowId +'\')">' + 'WATCH VIDEO &gt;' + '</a>');
			}

			//if($('.jcarousel-container-horizontal')){ $('.carouseldemo > .jcarousel-skin-tango > .jcarousel-container-horizontal > .overlayPanel').remove(); }
			//$('.carouseldemo > .jcarousel-skin-tango > .jcarousel-container-horizontal').append('<div class="overlayPanel">' + heroScroll_itemList[idx - 1].title + '</div>');
			if(_heroScroll3_itemList.length <= 1){
				$('#mycarousel-next3').hide();
				$('#mycarousel-prev3').hide();
			}else{
				$('#mycarousel-next3').show();
				$('#mycarousel-prev3').show();
			}
		},
		heroScroll_itemVisibleOutCallback: function(carousel, item, i, state, evt)
		{
		    carousel.remove(i);
		},		
		heroScroll_reloadCallback:function(carousel) {
			carousel.reset();
		},	
		init: function(id,index){
			_this = this;
			jQuery(id).jcarousel({
				start:index,
				scroll:1,
				visible:1,
				animation:300,
				wrap:"circular",
				initCallback: _this.heroScroll_initCallback,
				buttonNextHTML:null,
		        buttonPrevHTML:null,
				itemVisibleInCallback: {onBeforeAnimation: _this.heroScroll_itemVisibleInCallback},
				itemVisibleOutCallback: {onAfterAnimation: _this.heroScroll_itemVisibleOutCallback},
				reloadCallback: _this.heroScroll_reloadCallback

			});		
		}
	};
	return { //public members
		init:function() {
			if( $('#toprightCarousel').length > 0 ) { carouselfirst.init('#toprightCarousel', getAutoAdvCookie('autoAdv_toprightCarousel')); }
			if( $('#lowerleftCarousel').length > 0 ) { carouselsecond.init('#lowerleftCarousel', getAutoAdvCookie('autoAdv_lowerleftCarousel')); }
			if( $('#lowerrightCarousel').length > 0 ) { carouselthird.init('#lowerrightCarousel', getAutoAdvCookie('autoAdv_lowerrightCarousel')); }
		},
		AssignHeroScrollVar1:function(scroll_list){
		    _heroScroll1_itemList=scroll_list;
		},
		AssignHeroScrollVar2:function(scroll_list){
		    _heroScroll2_itemList=scroll_list;
		},
		AssignHeroScrollVar3:function(scroll_list){
		    _heroScroll3_itemList=scroll_list;
		}
	}
})();



var options = { path: '/', expires: 10 };
 
function processAutoAdvCookie(cookieArray){
    $.each(cookieArray, function(key, value) { 
       if( $('#' + cookieArray[key]).val() == 1){
            if(getAutoAdvCookie(cookieArray[key]) != null){
                var startIndex = parseInt(getAutoAdvCookie(cookieArray[key])) +1;
                var listOrder = key+1;
                var listName = 'heroScroll' + listOrder +'_itemList';
                if(startIndex > listName.length){
                    startIndex = 1;
                }
                setAutoAdvCookie(cookieArray[key], startIndex );
            }else{
                setAutoAdvCookie(cookieArray[key], 1 );
            }
        }
        else{
            setAutoAdvCookie(cookieArray[key], 1 );
        }
    });
   
}
     
function setAutoAdvCookie(name, value){
            var COOKIE_NAME = name;
            $.cookie(COOKIE_NAME, value, options);
}
 
function getAutoAdvCookie(name){
            return $.cookie(name);
}


$(document).ready(function() {
    var cookieArray = ['autoAdv_toprightCarousel','autoAdv_lowerleftCarousel','autoAdv_lowerrightCarousel'];
    processAutoAdvCookie(cookieArray);
	SAPCareerHome.Carousels.init();
	
	var $videoOverlay = $('#videoModalOverlay');
	$videoOverlay.dialog({
	    autoOpen:false,
	    modal:true,
	    resizable:false,
	    width:696,
	    height:442,
	    position:['center', '100'],
	    dialogClass:'videoOverlay' 
	});

});

}(jQuery));
