// winning pitch js

$(document).ready(function() {

	$('#videolist').jScrollPane({scrollbarWidth:20,showArrows:false});
	//get links from videolist and make them update the video without clickthough
	$('#videolist a').live('click',vidlistclick);
	
	// modify success factor menu link to open in new window
	$('#nav a').each(function(){
		if($(this).attr('href') == '/success/'){
			$(this).attr('target','_blank');
		}
	});
	
	$("#fromtwo").tweet({
		avatar_size: 40,
		count: 3,
		username: "winningpitch",
		loading_text: "searching twitter..."
	});
	

	
});



function vidlistclick(event){
	if($("#gotoLink").length>0){
		return;
	}
	event.preventDefault();
	$('#vidlistSelected').attr('id','');
	//get parent element
	var liBlock=$(event.target).closest("li");
	liBlock.attr('id','vidlistSelected');
	//liBlock.css('border','1px solid red');
	var img=liBlock.find('img');
	var imgpath=img.first().attr('src');
	imgpath=imgpath.replace(/\.jpg\/rs-[0-9]+x[0-9]+[a-z]+\.jpg\//i,'');
	//remove url for ie
	imgpath=imgpath.replace(/http:\/\/[^\/]+/i,'');
	if($('video').length>0){
		$('video').first().attr('src',imgpath+".flv");
		$('video').first().attr('poster',imgpath+".jpg");
	}
	
	
	var blockHTML=$("#videoblock").html();
	$("#videoblock").html(blockHTML.replace(/\?mn=[^'"]+/gi,"?mn="+imgpath));
	
	//reapply to fix any issue	
	$('#videolist').jScrollPane({scrollbarWidth:20,showArrows:true});
	//$('#videolist a').live('click',vidlistclick);
	if($("#swapcontent").length>0){
		vidlistSwapContent(event);
	}
	
}

function vidlistSwapContent(event){
	var clickLink=$(event.target).closest("a");
	var clickpath=clickLink.attr('href');
    $.post( "/duocms/wp/wptvGetPage/",{ path: clickpath },function(json){
		var cols=json.info.html.split('<!--PARTBREAK-->');
		$('#wptvtextwrapper').html(cols['6']);
		$('#vbleft').html(cols['2']);
		$('#vbright').html(cols['3']);
    },"json");
}


function frontNews(id,dir){
	var elem = elem = document.getElementById(id);
	if(elem){
		if(dir > 0){
			elem.className='active';
			if(id != 'activefnli'){
				document.getElementById('activefnli').className='';
			}
		} else {
			if(id != 'activefnli'){ elem.className=''; }
			document.getElementById('activefnli').className='active';
		}
	}
}


function flashfix () {
	theObjects = document.getElementsByTagName("object");
		for (var i = 0; i < theObjects.length; i++) {
			theObjects[i].parentNode.innerHTML = theObjects[i].parentNode.innerHTML;
		}
} 

if (window.attachEvent){
	window.attachEvent("onload", flashfix);
}
window.onload=fixsafari;

function fixsafari(){

	// check autocomplete for login boxes - winning pitch
	if(document.getElementById('editor_name')){
		if(document.getElementById('editor_name').value != ''){ document.getElementById('editor_name').style.backgroundPosition = 'left bottom'; }
		if(document.getElementById('duoPasswordent').value != ''){ document.getElementById('duoPasswordent').style.backgroundPosition = 'left bottom'; }
	}


	if(document.getElementById('redirectTreeParent0')){
		loadSiteTreeBranch('0');
	}

	//bookmark setup
	if(document.getElementById('fpull_bookmark')){
		if (window.external) {
		 	document.getElementById('fpull_bookmark').innerHTML='<a href = "javascript:bookmark()");"></a>'; 
		  } else  if (window.sidebar) {
		  	document.getElementById('fpull_bookmark').innerHTML='<a href ="javascript:bookmark()");"></a>'; 
		 } else if (window.opera && window.print) {	
		 	document.getElementById('fpull_bookmark').innerHTML='<a href = "javascript:bookmark()");"></a>';
		 }else{
		 	document.getElementById('fpull_bookmark').innerHTML='<a href ="javascript:void(0);" onclick="alert(\'ctrl+d to bookmark page\');"></a>';
		 }
	}	
	var agt=navigator.userAgent.toLowerCase();
	if (agt.indexOf("safari") != -1){ 
		theObjects = document.getElementsByTagName("object");
		for (var i = 0; i < theObjects.length; i++) {
			if(theObjects[i].className == "duoflash"){	
				for (j=0;j<theObjects[i].childNodes.length;j++){
					if(theObjects[i].childNodes[j].name=="Src"){
						var data=theObjects[i].childNodes[j].value;
					}
				}
			var height=theObjects[i].height;
			var width=theObjects[i].width;
			var classid=theObjects[i].classid;
			var codebase=theObjects[i].codebase;
			var htmlData = theObjects[i].parentNode.innerHTML;
			var newHtml='<object class="duoflash" data="'+data+'" width="'+width+'" height="'+height+'" classid="'+classid+'" codebase="'+codebase+'" >  <param name="AllowFullScreen" value="true" /> <param name="WMode" value="Opaque" /></object>';
			var newObject=theObjects[i].parentNode;
			newObject.innerHTML=newHtml;
			}
		}
	}
	flashfix();
}

function bookmark(){
	 
	 url = window.location;
		if (window.sidebar) { // Mozilla Firefox Bookmark
			window.sidebar.addPanel(title, url,"");
		} else if( window.external ) { // IE Favorite
			window.external.AddFavorite( url, title); 
		}else if(window.opera && window.print) { // Opera Hotlist
			return true; 
		}

}




function mailThisUrl() {
	var initialsubj="I thought you may find this interesting";
	var initialmsg="I saw this and thought you may find it interesting: "+window.location	
	window.location = "mailto:?subject="+initialsubj+"&body="+initialmsg
}




/*--------------------------------------------------------------------------------
  Site Tree Loader
--------------------------------------------------------------------------------*/
function loadSiteTreeBranch(parent,treeName){
	if(!parent){var parent='0';}
	if(!treeName){var treeName='redirect';}
	var holder=document.getElementById(treeName+'TreeParent'+parent);
	if(holder.innerHTML.length>5){
		holder.innerHTML='';
		if(item){item.className="branchPlus";}
	}else{
		var item=document.getElementById(treeName+'TreeId'+parent);
		if(item){item.className="branchMinus";}
		var myConn = new XHConn();
		if (myConn){
			myConn.connect("/duocms/files/listFiles/"+parent+"/"+treeName+"/", "POST", "", res_loadSiteTreeBranch);
		}
	}
}

function res_loadSiteTreeBranch(data){
	//alert(data.responseText);
	var data=eval('('+data.responseText+')');
	var treeName=data.info.treeName;
	if(!treeName){var treeName='file';}
	if(data && data.error){
		alert(data.error);
		return;
	}
	if(data && data.info){
		var holder=document.getElementById(treeName+'TreeParent'+data.info.parent);
		var treeHTML='';
		for (var Row in data.info){
			var branchTitle= data.info[Row]['title'];
			if(!branchTitle && data.info[Row]['path']){
				branchTitle=xtractFile(data.info[Row]['path']).file;			
			}
			if(branchTitle){
				if(data.info[Row]['children']!="0"){
					treeHTML+='<a class="branchPlus" id="'+treeName+'TreeId'+data.info[Row]['id']+'" href="javascript:loadSiteTreeBranch('+data.info[Row]['id']+',\''+treeName+'\');setLinkLine(\''+data.info[Row]['path']+'\',\''+data.info[Row]['id']+'\');">'+branchTitle+'</a><div class="branch" id="'+treeName+'TreeParent'+data.info[Row]['id']+'"></div>';
				}else{
					treeHTML+='<a class="branchNone" id="id'+data.info[Row]['id']+'" href="javascript:setLinkLine(\''+data.info[Row]['path']+'\',\''+data.info[Row]['id']+'\');">'+branchTitle+'</a>';
				}
			}
		}
		holder.innerHTML=holder.innerHTML+treeHTML;
	} 
}

function setLinkLine(path,id){
//	setFormValue(te_linkline,path);
//	if(te_linkline=='imgprops_link'){//update link on image on change
//		te_applyImageProps();		
//	}
	if(document.getElementById('redirectPath')){
		document.getElementById('redirectPath').value=path;
	}
} 

function xtractFile(data){
	data=data.substring(0,(data.length-1));//remove ending slash
	var m = data.match(/(.*)[\/\\]([^\/\\]+)$/);
	if (m == null) {
		alert("The data string does not fit the pattern of location.pathname.\nPlease click Pathname or type a new string.");
		return {path: "", file: ""};
	}else {
		return {path: m[1], file: m[2]}
	}
}


