﻿ITVM=window.ITVM||{};ITVM.MyITV=(function($)
{function init()
{createEventListeners();}
function createEventListeners()
{var myItvCache=$('#my_itv_result');$("#delete_myitv").click(function()
{var userId=$("#web_user_id_hiddenfield").val();var ids=[];myItvCache.find('.result-group :checked').each(function(index)
{ids.push($(this).val());});sendCommand("action=delete&ids="+ids+"&userid="+userId,ids);});myItvCache.find('a.moveup').click(ITVM.MyITV.moveUp);myItvCache.find('a.movedown').click(ITVM.MyITV.moveDown);}
function moveUp(e)
{moveRow(e,this,'up');e.preventDefault();}
function moveDown(e)
{moveRow(e,this,'down');e.preventDefault();}
function moveRow(e,node,dir)
{var row=$(node).closest('tr').get(0);moveRowNode(row,dir);var userId=$('#web_user_id_hiddenfield').val();var ids=[];$('#my_itv_result').find('.result-group :checkbox').each(function()
{ids.push($(this).val());});sendCommand({action:'reorder',ids:ids.join(','),userid:userId});}
function moveRowNode(rowNode,dir)
{var first,second;var prevNode,nextNode;if(rowNode)
{switch(dir)
{case'up':first=rowNode;prevNode=$(rowNode).prev();if(prevNode.length)
{second=prevNode.get(0);if($(second).hasClass('immovable')||$(second).children('th').length>0)
{second=null;}}
break;case'down':second=rowNode;nextNode=$(rowNode).next();if(nextNode.length)
{first=nextNode.get(0);if($(first).hasClass('immovable'))
{first=null;}}
break;}
if(first&&second)
{firsttd=$(first).find('td:first-child');secondtd=$(second).find('td:first-child');if(secondtd.hasClass('first'))
{firsttd.addClass('first');secondtd.removeClass('first');}
if(firsttd.hasClass('last'))
{secondtd.addClass('last');firsttd.removeClass('last');}
$(first).insertBefore(second);}}}
function sendCommand(options,ids)
{$.ajax({data:options,dataType:'json',success:handleSuccess(ids),type:'POST',url:'/common/template/usercontrols/handlers/MyITVHandler.ashx'});}
function handleSuccess(ids)
{if(ids!==undefined&&ids!==null)
{for(var i=0;i<ids.length;i++)
{$("#my_itv_result .result-group :checkbox").each(function(index)
{if($(this).val()==ids[i])
{$(this).parent().parent().remove();}});}}}
return{init:init,moveUp:moveUp,moveDown:moveDown};})(jQuery);jQuery(ITVM.MyITV.init);
