/* This function will remove the cookie 'carousel_state before the current exist.  
    This is clear the cookie for the next page with video carousel to display properly */
	function removeCookie()
	{
		jQuery.cookie('carousel_state', null);
		//alert('cookie'+jQuery.cookie('carousel_state'))
	}


/* This function will update the carousel state by retreiving the carousel_state cookie and increase*/
	function updateCState(elm, disp)
	{	
	
	
		// elm : carousel position
		// disp : this is the display type 4 or 8 icons	

        var options = { path: '/', expires: 3600 };
		var arrCState = new Array();
		var cState = jQuery.cookie('carousel_state');
		var arrCState = cState.split('|');
		var arrCStateDetail = arrCState[elm].split(',');
			
		// update the display type
		if (disp != '')
		{
			arrCStateDetail[1] = disp;
		}
		// update the page num
		arrCStateDetail[2] = pageJSON.on_page[elm].on_page;
		// merge it back
		var _arrCStateDetail=arrCStateDetail.join(',')
		////console.log(_arrCStateDetail)		
		// restore back to cookie array
		arrCState[elm] = _arrCStateDetail;
		////console.log(arrCState)
		_arrCState = arrCState.join('|');
		////console.log('final:'+_arrCState)
		
		var date = new Date();
		date.setTime(date.getTime() + (60 * 60 * 1000));
		jQuery.cookie('carousel_state', _arrCState, { path: document.location.pathname, expires: date });
		
////console.log('AT THE END:'+jQuery.cookie('carousel_state'))
	}
	
	function nextPage(page_num, disp, theElement, videoSection, play) {
		if(page_num == "") { page_num = on_page; }
		on_page = page_num;
		
		pageJSON.on_page[theElement].on_page=page_num;
		
		
		
//console.log('jason:'+pageJSON.on_page[theElement].on_page)
		
		formElement = eval("document.form"+theElement);
		
		search_elements = formElement.searchEl.value;
		return_elements = formElement.returnEl.value;
		
		formElement.pageNum.value = page_num;





		theElementID = "vid_feeds_parent" + theElement + "";

		new Ajax.Updater(
			{success: theElementID}, 
			'/sitecommon/includes/video/2009_carousel_data_multiple.php', {
			method: 'post', 
			parameters: {pg:page_num, repub:'yes', displayType:disp, search_elements:search_elements, return_elements:return_elements, carouselId:theElement, vs:videoSection, play:play}, 
			insertion: Insertion.Top,
			onSuccess: function (oXHR, oJson) {
				$(theElementID).update();
				paginate(disp, theElement, videoSection, play);
				updateCState(theElement, disp);
			}
			}
		
	)
	function paginate(disp, theElement2, videoSection, play) {

		theElementID2 = "pagination" + theElement2 + "";
		
		formElement = eval("document.form"+theElement2);
		
		search_elements = formElement.searchEl.value;
		return_elements = formElement.returnEl.value;

		new Ajax.Updater(
			{success: theElementID2}, 
			'/sitecommon/includes/video/2009_carousel_data_multiple.php', {
			method: 'post', 
			parameters: {pg:page_num, paginate:'yes', displayType:disp, search_elements:search_elements, return_elements:return_elements, carouselId:theElement2, vs:videoSection, play:play}, 
			insertion: Insertion.Top,
			onSuccess: function (oXHR, oJson) {
				$(theElementID2).update();
			}
			}
		)
		}
	}
	function changeLayout(disp, carouselId, videoSection, play) {
		
		
		
				
		
		var arrCState = new Array();
		var cState = jQuery.cookie('carousel_state');
		var arrCState = cState.split('|');
		var arrCStateDetail = arrCState[carouselId].split(',');
	
////console.log(cState)
////console.log("cookie page num:"+arrCStateDetail[2]);


		
		
		if(disp == "fourby") { nextPage(Math.ceil(((arrCStateDetail[2] * 4)-3) / 8), 'fourby', carouselId, videoSection, play); }
		if(disp == "twoby") { nextPage(Math.ceil(((arrCStateDetail[2] * 8)-7) / 4), 'twoby', carouselId, videoSection, play); }
	
		
		
		
		
		
		
		
		
		
		//if(disp == "fourby") { nextPage(Math.ceil(((on_page * 4)-3) / 8), 'fourby', carouselId, videoSection, play); }
		//if(disp == "twoby") { nextPage(Math.ceil(((on_page * 8)-7) / 4), 'twoby', carouselId, videoSection, play); }
	}