/**
 *
 * @author Marcel Tuch <marcel.tuch@gmail.com>
 * @desc dynamic_stuff.js : all function to drag and drop boxes
 * @version $ $
$Log: dynamic_stuff.js,v $
Revision 1.12  2010-02-10 11:22:55  jnimmich
- MV-3817 stop big player if voting player is started

Revision 1.11  2010-01-26 15:42:36  jnimmich
- MV-3728 fixed voting in www.myvideo.de/echo for ie

Revision 1.10  2010-01-26 14:36:35  jnimmich
- changed fixed player-width and -height into relative at show_player()

Revision 1.9  2010-01-25 11:58:43  jnimmich
- MV-3728 fixed voting max_votes_per_user

Revision 1.8  2010-01-21 14:23:54  jnimmich
- MV-3728 added param bpmvsm= in votings

Revision 1.7  2009-11-11 15:08:40  tmuehlberg
- add allowScriptAccess always for voting player

Revision 1.6  2009-09-17 16:06:07  tmuehlberg
- fix for not working thx_for_vote_show_ranking link

Revision 1.5  2009/09/16 16:44:04  tmuehlberg
- fix for not working thx_for_vote_show_ranking link

Revision 1.4  2009/09/03 09:35:01  tmuehlberg
- remove scrollTo from inline login

Revision 1.3  2009/07/28 10:47:50  jnimmich
- fixed player_fullscreen in voting
*/
var drag_list_1;
var drag_list_2;
var i = 0;
var stored_css;
var open_player = '';
var vt_cookie_alive = false;
vt_block_vote = false;
var vote_first_place_only = false;
var user_has_vote = false;
var user_votes = 0;
var first_place_not_set = false;
var thx_for_vote = '';
function voting_dynamic(init_name,init_rank_offset) {
  this.ref = HO(init_name); // @fetch html object
  this.children = new Array(); // @id's of children of html object
  this.child_count = 0; // @amount of childs of html object
  this.rank_offset = init_rank_offset; //offset for ranking display
 this.setChildren = function() {
		//save child nodes
		this.children=new Array();
		for(i=0;i<this.ref.childNodes.length;i++) {
			if(this.ref.childNodes[i].nodeType==1) {
				if(this.ref.childNodes[i].id.match('voting_')) {
					this.children[this.children.length]=this.ref.childNodes[i].id;
				}
			}
		}
		this.child_count=this.children.length;
	}
	this.getActChildCount = function() {
		//get actual count for checking against excpected count in this.child_count
		child_count=0;
		for(i=0;i<this.ref.childNodes.length;i++) {
			if(this.ref.childNodes[i].nodeType==1) {
				if(this.ref.childNodes[i].id.match('voting_')) {
					child_count++;
				}
			}
		}
		return child_count;
	}
	this.addElem_bottom = function(elem) {
		//adds element at bottom
		this.ref.appendChild($(elem));
		this.setChildren();
	}
	this.addElem_top = function(elem) {
		//adds element at top
    this.ref.insertBefore($(elem), this.ref.firstChild);
		this.setChildren();
	}
	this.addElem_pos = function(elem,pos) {
		//inserts element elem at position pos
		ins_before_elem="";
		if(pos>=this.children.length) {
			//nothing found to insert before, we have to insert last element
			this.ref.appendChild($(elem));
		}
		else {
			//not last element ... insert before element @ position
			for(i=0;i<this.children.length && i <= pos;i++) {
				if(i==pos) {
					ins_before_elem=$(this.children[i]);
				}
			}
			this.ref.insertBefore($(elem), ins_before_elem);
		}

		this.setChildren();
	}
	this.getFirstChild = function() {
		//returns first child
		return this.ref.firstChild;
	}
	this.getLastChild = function() {
		//returns last child
		//return this.ref.lastChild;
		for(i = this.children.length-1; i >= 0; i--) {
			if(this.children[i].match('voting_')) {
				return $(this.children[i]);
			}
		}
	}
	this.isInchildren = function(elem_id) {
		//returns true if elem is part of this list's children, else false
		for(i = 0;i < this.children.length;i++) {
			if(elem_id == this.children[i]) {
				return true;
			}
		}
		return false;
	}
	this.getRelativePosition = function(elem_id) {
		//returns the positon of the element within this list
		for(i=0;i < this.children.length;i++) {
			if(this.children[i] == elem_id) {
				return i;
			}
		}
	}

	this.changeBackground = function(i) {
		// change class of background
		if (i % 2 == 0) {
		  rank_li.className = 'vtListEle vtDark';
		}	else {
		  rank_li.className = 'vtListEle vtLight';
		}
	}
	this.setRankings = function() {
		//set the ranks according to this.rank_offset
		for(i = 0;i < this.children.length; i++) {
			rank_id = this.children[i].replace("voting_","act_pos_");
			rank_li =  HO(this.children[i]);
			$(rank_id).innerHTML = ((vote_first_place_only == true && i + this.rank_offset > 1) || first_place_not_set == true ? '' : i + this.rank_offset);
		  if (this.rank_offset == 1) {
		  	// change class for first place
		  	if (i == 0) {
		  	  $(rank_id).className = 'vtActPos vtFirst';
		    } else {
		  	  $(rank_id).className = 'vtActPos';
		    }
		    this.changeBackground(i);
		    stored_css = i;
		  } else {
		    $(rank_id).className = 'vtActPos';
		    if (stored_css % 2 == 0) {
		    	this.changeBackground(i+1);
		    } else {
		    	this.changeBackground(i);
		    }
		  }
		}
	}
	this.initRankings = function() {
		//set inital old ranks
		for(i=0;i < this.children.length;i++) {
			rank_id = this.children[i].replace("voting_","old_pos_");
			$(rank_id).innerHTML = i + this.rank_offset;
		}
	}
	this.setChildren();
}
function vt_status(state) {
	if (state == false) {
	  HO("vtBtnDiv").className = HO("vtBtnDiv").className.replace("vtBtnActive","vtBtnInactive");
	} else {
		HO("vtBtnDiv").className = HO("vtBtnDiv").className.replace("vtBtnInactive","vtBtnActive");
	}
}
function vt_check_cookie() {
	// if cookie isset block voting
	if(document.cookie) {
	  var cookie_string = document.cookie.split(" ").join("");
	  var cookie_array = cookie_string.split(";");
	  for (var k = 0; k < cookie_array.length; k++) {
      if (cookie_array[k].split("=")[0] == 'vt_' + page_id + '_' + user_id) {
      	cookie_values = cookie_array[k].split("=")[1].split(",");
      	//alert(cookie_values[1]);//pid
      	if(cookie_values[1] == page_id && cookie_values[2] == user_id) {
      		//vt_cookie_alive = true;
      	}
      	if (cookie_values[3] != '') user_votes = cookie_values[3];
      	break;
      }
    }
    if (vt_cookie_alive == true) {
    	vt_status(false);
    } else {
    	vt_status(true);
    }
	}
}
function vt_set_cookie(response) {
	// @set cookie after succeed voting
	response_values = response.split(",");
	if (response_values[3] == '') response += user_votes;
	if (response_values[3] > 0) {
		datum = new Date();
		kill_cookie = new Date(datum.getFullYear() + 1, datum.getMonth(), datum.getDate());
	}
	//document.cookie = "vt_" + page_id + "_" + user_id + "=" + response + "; expires=" + kill_cookie + ";";
	document.cookie = "vt_" + page_id + "_" + user_id + "=" + response + ";";
	//document.cookie = "vt=votet; expires=Fri, 20 Aug 2007 14:41:00 GMT;";
	vt_check_cookie();
}
function vt_vote(page) {
	// @ ajax call to vote
	var list_order = '';
	//if (vt_cookie_alive == false && vt_block_vote != true) {
	if (vt_block_vote != true) {
    var url = '/dynamic/dynamic_voting.php?page='+page;
    user_votes++;
    if (!big_voting) {
    	list_order = Sortable.serialize('drag_list_1') + '&' + Sortable.serialize('drag_list_2');
    } else {
    	list_order = "drag_list_1[]=" + remember_last_movie;
    }
    if (window.XMLHttpRequest) {
    	// Mozilla, Safari,...
      http_request = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
    	// IE
      try
      {
        http_request = new ActiveXObject("Msxml2.XMLHTTP");
      }
      catch (e) {
        try {
          http_request = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch (e) {}
      }
    }
    if (!http_request) {
      ajax_error='create';
      return false;
    }
    try {
      if(http_request.setRequestHeader)
        http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    }
    catch (e){}
    http_request.onreadystatechange = vt_receiveResponse;
    http_request.open('POST', url, true);
    try {
      http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    }
    catch (e){}
    http_request.send(list_order);
	} else {
	}
}
function vt_receiveResponse() {
	// @set cookie and block voting if voting succeed
	if (http_request.readyState == 4 && http_request.status == 200) {
		if (http_request.responseText.substr(0,7) == 'already') {
			HO("vt_thx").innerHTML = thx_already;
		} else {
			HO("vt_thx").innerHTML = thx_for_vote;
		}
		if (http_request.responseText.substr(0,7) == 'already' || http_request.responseText.substr(0,7) == 'success') {
			response = http_request.responseText;
			vt_set_cookie(response);
		}
		vt_status(false);
	}
}
function init_sortable_list() {
	// @dynamic voting
	if (arguments[0] == 2) {
		drag_list_1 = new voting_dynamic('drag_list_1', 1);
		drag_list_2 = new voting_dynamic('drag_list_2', drag_list_1.child_count + 1);
		Sortable.create('drag_list_1',{handle:'drag',containment:['drag_list_1','drag_list_2'],constraint:false,onChange:adjust_list});
		Sortable.create('drag_list_2',{handle:'drag',containment:['drag_list_1','drag_list_2'],constraint:false,onChange:adjust_list});
		if (arguments[2] == 'no_ranks') first_place_not_set = true;
		set_ranks();
		vt_cookie_alive = false;
		vt_check_cookie();
	}
	if (arguments[0] == "no") {
		drag_list_1 = new voting_dynamic('drag_list_1', 1);
		drag_list_2 = new voting_dynamic('drag_list_2', drag_list_1.child_count + 1);
		if (arguments[2] == 'no_ranks') first_place_not_set = true;
		recalc_ranks();
	}
	if (arguments[0] == "big_voting") {
		vt_cookie_alive = false;
		vt_check_cookie();
	}
	first_place_not_set = false;
}
function adjust_list() {
	if(drag_list_1.child_count>drag_list_1.getActChildCount()) {
		//first list has less elements, move first from second list to bottom of first list
		first_child = drag_list_2.getFirstChild();
		drag_list_1.addElem_bottom(first_child.id);
	} else if(drag_list_2.child_count>drag_list_2.getActChildCount()) {
		//second list has less elements, move lat from first list to top of second list
		last_child = drag_list_1.getLastChild();
		drag_list_2.addElem_top(last_child);
	}
	drag_list_1.setChildren();
	drag_list_2.setChildren();
	recalc_ranks();
}
function move_up(m_id) {
	if(drag_list_1.isInchildren("voting_" + m_id)) {
		//element is in first list
		pos=drag_list_1.getRelativePosition("voting_" + m_id);
		if(pos>0) {
			//not first element, can be brough once up
			drag_list_1.addElem_pos($("voting_"+m_id),pos-1);
			recalc_ranks();
		}
	}
	else {
		//element is in second list
		pos = drag_list_2.getRelativePosition("voting_" + m_id);
		if(pos == 0) {
			//first element, move to first list, move last from first list to second list
			last_child = drag_list_1.getLastChild();
			drag_list_1.addElem_bottom($("voting_" + m_id));
			drag_list_2.addElem_top($(last_child.id));
			drag_list_1.setChildren();
			recalc_ranks();
		}
		else {
			//not first element, can be brough once up
			drag_list_2.addElem_pos($("voting_"+m_id),pos - 1);
			recalc_ranks();
		}
	}
}
function move_down(m_id) {
	if(drag_list_1.isInchildren("voting_" + m_id)) {
		//element is in first list
		pos = drag_list_1.getRelativePosition("voting_" + m_id);
		if(pos >= drag_list_1.children.length-1) {
			//move to second list, move first from second list to first list's bottom
			first_child = drag_list_2.getFirstChild();
			drag_list_2.addElem_top($("voting_" + m_id));
			drag_list_1.addElem_bottom(first_child.id);
			drag_list_2.setChildren();
			recalc_ranks();
		}
		else {
			pos = pos + 2;
			drag_list_1.addElem_pos($("voting_"+m_id),pos);
			recalc_ranks();
		}
	}
	else {
		//element is in second list
		pos = drag_list_2.getRelativePosition("voting_" + m_id);
		if(pos < drag_list_2.children.length-1) {
			//not last element, can be pushed down once
			pos =pos + 2;
			drag_list_2.addElem_pos($("voting_"+m_id),pos);
			recalc_ranks();
		}
	}
}
function move_top(m_id) {
	if(drag_list_1.isInchildren("voting_" + m_id)) {
		//element is in first list, just move to top
		drag_list_1.addElem_top($("voting_" + m_id));
	}
	else {
		//element is in second list, move to top and move last element of first list to second list
		drag_list_1.addElem_top($("voting_" + m_id));
		drag_list_2.addElem_top(drag_list_1.getLastChild());
		drag_list_1.setChildren();
		drag_list_2.setChildren();
	}
	recalc_ranks();
}
function recalc_ranks() {
	//set the ranks of the movies and remove the player, if open
	drag_list_1.setRankings();
	drag_list_2.setRankings();
	user_has_vote = true;
}
function set_ranks() {
	// @init and set rankings for both shown ids
	drag_list_1.setRankings();
	drag_list_2.setRankings();
	drag_list_1.initRankings();
	drag_list_2.initRankings();
}
function reset_player() {
	//resets the player if open
	nMC();
	m_id = open_player;
	if(parseInt(m_id) > 0) {
		$('vt_drag_div_'+m_id).className = $('vt_drag_div_'+m_id).className.replace(" vtIeMove","");
		$('vt_img_'+m_id).style.display = "inline";
		$('vt_player_'+m_id).style.display = "none";
		$('vt_player_close_'+m_id).style.display = "none";
		$('vt_player_' + m_id).innerHTML = "";
		open_player = "";
	}
}
function drag_close_player(m_id) {
	//rests the player on drag and drop if the id with the open player is being moved
	if(open_player == m_id) {
		reset_player();
	}
}
function show_player(m_id,post) {
	//display the player
	if (typeof(player_to_stop) != "undefined") HO(player_to_stop).pressPAUSE();
	if(open_player != "") this.reset_player();
	$('vt_img_' + m_id).style.display = "none";
	$('vt_player_' + m_id).style.display = "block";
	$('vt_drag_div_' + m_id).className = $('vt_drag_div_' + m_id).className + " vtIeMove";
	$('vt_player_close_' + m_id).style.display = "inline";
	so = new SWFObject("/movie/"+m_id+post, "vt_instance_"+m_id, "100%", "100%", "9", "#FFFFFF");
	so.addVariable('_VARS','POSTACTION');
	so.addVariable('POSTACTION','REWIND');
	so.addParam("allowFullScreen", "true");
	so.addParam("allowScriptAccess", "always");
	if (saf) {
		so.addParam("wmode", "opaque");
		$('vt_player_'+m_id).style.position = "absolute";
		$('vt_player_'+m_id).style.zIndex = "1000";
		$('vt_player_close_'+m_id).style.position = "relative";
	}
	so.write("vt_player_"+m_id);
	open_player = m_id;
}
function vote_result_link() {
	var winloc=''+window.location+'';
	var winlink=winloc+(winloc.indexOf('?')>-1?'&':'?')+'shres=1';
	winlink=winlink.replace(/#/g,'');
	window.location.href=winlink;
}