 $(document).ready(function(){
	BindEvents();
	/*if(promo == "true"){
		CheckPromoState();
	 }else{
		$("#promorow").addClass("hide"); 
	}*/
	bindFilterEvents();
 });
 
// show Hide function
$.fn.showToggle = function() {
	if(this.is(':visible')){
		this.hide();
	} else {
		this.show();
	}	
	return this;
}

function getURLParam(strParamName){
  var strReturn = "";
  var strHref = window.location.href;
  if ( strHref.indexOf("?") > -1 ){
    var strQueryString = strHref.substr(strHref.indexOf("?")).toLowerCase();
    var aQueryString = strQueryString.split("&");
    for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
      if ( 
aQueryString[iParam].indexOf(strParamName.toLowerCase() + "=") > -1 ){
        var aParam = aQueryString[iParam].split("=");
        strReturn = aParam[1];
        break;
      }
    }
  }
  return unescape(strReturn);
} 

function popup(url,winname,w,h,feat)
{
	if (!(isNaN(w) || isNaN(h)))
	{
		var x=parseInt((screen.width-w)/2);
		var y=parseInt((screen.height-h)/2);
		if (x<0)	x=0;
		if (y<0)	y=0;
		
		if (feat!=null && feat!="")
		{
			feat=","+feat;
		}
		else
		{
			feat="";
		}
		feat="left="+x+",top="+y+",width="+w+",height="+h+feat;
	}
	window.open(url,winname,feat);
}


function helpMe() {
	popup(dotdotpath+"help","",900,700,"");
}

//news promo functions  
function CheckPromoState(){
	var x = get_cookie ("promostate");
	if(x == "open"){
		$("#promorow").show();
	}else if(x == "closed"){
		$("#promorow").hide();
	}else{
		showPromo();
	}
}

function BindEvents(){
 /*$(".promoclose a").click(function(){
		$("#promorow").slideUp("slow");	
		set_cookie("promostate","closed","/","00","");		
	 });*/
 
 $(".account #seller_btn a").click(function(){
		$(".account .seller_details").toggleClass("js_hide");									
 	});
 
// phone open close
var phoneisvisible = false;

$('#phone_lnk').click(function(){
	if(phoneisvisible == false){
	phoneisvisible = true;
	$('#phone_box').showToggle();
	loadSifrLite();
	}
	else{
	}
});

$('.alertclose').click(function(){
	$(this).parent().parent().parent().showToggle();
});

$('#phone_box_close').click(function(){
	$('#phone_box').showToggle();
	phoneisvisible = false;
});

// seo gutter
$('#seo_slide').hide();
$('#seo_trigger').click(function(){
	if($(this).hasClass('active')){
		$(this).removeClass('active');
	} else {
		$(this).addClass('active')
	}
	$('#seo_slide').showToggle();
	return false;
})
 
}


/*$('#btn_change').hide();
$('#profileImage').hover(function(){
	$('#btn_change').show();
	return false;
});*/

/*$('#imagetochange').hover(function(){
	$('#imagechangelink').showToggle();
});*/


function showPromo(){
	$("#promorow").slideDown("slow");
	set_cookie("promostate","open","/");
}

//overlay functions
function bindOverlayBehavior(){
 $("a#select_product").click(function(){
    	loadcontent("select");
		return false
  });
}
 
function initOverlay(w){
	if(typeof(w) != undefined){
	  var w = w;
	}else{
	  var w="700px";
	}


	$().bind('cbox_complete', function() { 
	    $().unbind("keydown.cbox_arrows"); 
	}); 
 
    $(".colorbox").colorbox({title:true,close:"",keyboard:false}, function(){
		var div = $(this).parent().parent().parent().parent().parent().parent().parent().find(".opts_panel");
		var parentguid = div.find(".voisseclipguid").html();		
		var topicid = getURLParam("totdid");
		embedflash("VoisseLoader",'','',topicid,0);
	});
   
    $(".colorbox2").colorbox({title:true}, function(){
		var div = $(this)
		var guid = div.find(".previewguid").html();
		embedflash("players/visual/_level0",guid,'','',0);
	});
	 
    $(".colorbox2a").colorbox({title:true}, function(){
		var div = $(this)
		var guid = div.find(".previewguid").html();
		embedflash("players/podcast/_level0",guid,'','',0);
	});
	 
	$(".colorbox3").colorbox({title:true,close:""}, function(){
		var div = $(this).parent().parent().parent().parent().parent().find(".opts_panel");
		var guid = div.find(".voisseclipguid").html();
		embedflash("VoisseLoader",guid,'','',0);
	}); 
	 
	$(".colorbox4").colorbox({title:true,close:""}, function(){
		var div = $(this).parent().parent().parent().parent();
		var guid = div.find(".relatedvoisseclipguid").html();
		embedflash("VoisseLoader",guid,'','',0);
	}); 
  
    $(".colorbox5").colorbox({title:true,close:""}, function(){
		embedflash("VoisseLoader",'','','',window.multiModeB);
	});
  
    $(".colorbox6").colorbox({title:true,close:""}, function(){
		var div = $(this).parent().parent().parent().parent().parent().parent().parent().find(".opts_panel");
		var parentguid = div.find(".voisseclipguid").html();
		
		var topicid = getURLParam("totdid");
		embedflash("VoisseLoader",'',parentguid,topicid,0);
	});
	
	$(".colorbox7").colorbox({title:true,close:""}, function(){
		var div = $(this).parent().parent().parent().find(".opts_panel");
		var guid = div.find(".voisseclipguid").html();
		embedflash("VoisseLoader",guid,'','',0);
	}); 
	
	$().bind('cbox_cleanup', function(){
			resetMultiMode();
	}); 
}

function resetMultiMode () {
	window.multiModeB = 0;
}

function embedflash(swf,guid,parentguid,topicid,multiModeBool){
	// var hasReqestedVersion = DetectFlashVer(requiredMajorVersion, requiredMinorVersion, requiredRevision);
	if(document.getElementById('multivoisseeditordiv') != null){		
			//function unloadeditor(){}
			$('.voiseeditor,.spcr').fadeOut();
	closeeditorfromflash();
	}
if (multiModeBool == 1){
		multiModeBool = 1;
	}else{
		multiModeBool = 0;
	}
	if(swf=="VoisseLoader"){
		
		var h = 350; 
		var w = 550;
		var cssid ="voisseRecorder";
		if (guid != '')
		{
			swfobject.embedSWF(HttpsURL+"flash/"+swf+".swf", ""+cssid+"", ""+ w +"", ""+ h +"", "10",false,{swfToLoad:HttpsURL+"flash/SingleVoisseEditor.swf",voisseId:guid,xmlPath:HttpsURL+"mediaservices/geteditstreaminfo.cfm",uploadPath:HttpsURL+"my-voisses/uploadmp3.cfm",updateVoissePath:HttpsURL+"mediaservices/updatevoisse.cfm",uploadImagePath:HttpsURL+"mediaservices/uploadImage.cfm"},{wmode:"opaque"});
		} else {
			swfobject.embedSWF(HttpsURL+"flash/"+swf+".swf", ""+cssid+"", ""+ w +"", ""+ h +"", "10",false,{swfToLoad:HttpsURL+"flash/SingleVoisseRecorder.swf",recordStreamPath:HttpsURL+"mediaservices/getrecordstream.cfm?nocache="+Math.round(Math.random() * 1000000000),saveVoissePath:HttpsURL+"mediaservices/savenewvoisse.cfm",updateVoissePath:HttpsURL+"mediaservices/updatevoisse.cfm",parentVoisseId:parentguid,uploadPath:HttpsURL+"my-voisses/uploadmp3.cfm",uploadImagePath:HttpsURL+"mediaservices/uploadImage.cfm",multimode:multiModeBool,topicId:topicid},{wmode:"opaque"});
		}
	}
	
	
	if(swf=="players/visual/_level0"){
		var h = 598; 
		var w = 950;
		var cssid ="voissepreview";
		
		swfobject.embedSWF(HttpsURL+"flash/"+swf+".swf", ""+cssid+"", ""+ w +"", ""+ h +"", "10",false,{voisseid:guid,checkPromoCode:HttpsURL+"mediaservices/checkpromocode.cfm",configxml:HttpsURL+"mediaservices/visualvoisseplayerconfig.cfm?nocache="+Math.round(Math.random() * 1000000000)},{wmode:"opaque",allowFullScreen:"true",scale:"noScale"});
	}
	if(swf=="players/podcast/_level0"){
		var h = 496; 
		var w = 545;
		var cssid ="voissepreview";
		
		swfobject.embedSWF(HttpsURL+"flash/"+swf+".swf", ""+cssid+"", ""+ w +"", ""+ h +"", "10",false,{voisseid:guid,checkPromoCode:HttpsURL+"mediaservices/checkpromocode.cfm",configxml:HttpsURL+"mediaservices/visualpodcastplayerconfig.cfm?nocache="+Math.round(Math.random() * 1000000000)},{wmode:"opaque",allowFullScreen:"true",scale:"noScale"});
	}


	//alert("[common.js] embedFlash()\n\tswf=" + swf + "\n\tguid=" + guid + "\n\tparentguid=" + parentguid + "\n\ttopicid=" + topicid + "\n\tmultimodebool=" + multiModeBool);
}




function loadcontent(file){
	$("#ajax_content").load(""+file+".cfm");	
}


function rate(voisseclipguid,rating,colour)
{
	$.post(dotdotpath + "_include/ssi/voisse/rate.cfm", {voisseclipguid: voisseclipguid, rating:rating, colour:colour}, function(data){
		var div = document.getElementById("ratebox"+voisseclipguid);																	
		
		if (div){
			div.innerHTML = data;
		}
	});
	
	$.post(dotdotpath + "_include/ssi/voisse/fe_rate.cfm", {voisseclipguid: voisseclipguid, rating:rating, colour:colour}, function(data){
		var div2 = document.getElementById("fe_ratebox"+voisseclipguid);
		
		if (div2){
			div2.innerHTML = data;
		}
	});
	
	$.post(dotdotpath + "_include/ssi/voisse/ratetext.cfm", {voisseclipguid: voisseclipguid}, function(data){
		var div3 = document.getElementById("votetext"+voisseclipguid);
		var div4 = document.getElementById("fe_votetext"+voisseclipguid);
		
		if (div3){
			div3.innerHTML = data;
		}
		
		if (div4){
			div4.innerHTML = data;
		}
	});
}

function publishvoisse(voisseclipguid,publish)
{
	$.post(dotdotpath + "_include/ssi/voisse/_publish.cfm", {voisseclipguid: voisseclipguid, publish: publish}, function(data){
		var div = document.getElementById("publishbox"+voisseclipguid);																	
		div.innerHTML = data;
	});
}

function embedSWF(voisseclipguid,image,id){
	swfobject.embedSWF(HttpsURL + "flash/singlevoisseplayer.swf", "featuredvoissediv" , "186", "186", "9",false,{sessionid: id, voisseid: voisseclipguid, image: image, wmode:"transparent",allowScriptAccess:"always"},{/*id:"featuredvoise" + voissediv*/});
}


function embedSlideShow(voisseclipguid,image,id,type){
	var o=document.getElementById("featuredvoissediv");
		o.innerHTML = "<div class=\"row\">";
		o.innerHTML += "<div class=\"rel\">";
		if (type == 2) {
			o.innerHTML += "<a href=\""+dotdotpath+"preview/preview.cfm\" class=\"colorbox2 abs btn-sldShw_large\"><span class=\"hide previewguid\">" + voisseclipguid + "</span></a>";
			if (image != "") {
				o.innerHTML += "<img src=\""+image+"\" width=\"186\" height=\"186\" alt=\"play Slideshow\" />";
			} else {
				o.innerHTML += "<img src=\""+dotdotpath+"images/voisseplayer_large.jpg\" width=\"186\" height=\"186\" alt=\"play Slideshow\" />";					
			}
		} else {
			o.innerHTML += "<a href=\""+dotdotpath+"preview/preview_pc.cfm\" class=\"colorbox2a abs btn-sldShw_large\"><span class=\"hide previewguid\">" + voisseclipguid + "</span></a>";
			if (image != "") {
				o.innerHTML += "<img src=\""+image+"\" width=\"186\" height=\"186\" alt=\"play Podcast\" />";
			} else {
				o.innerHTML += "<img src=\""+dotdotpath+"images/voisseplayer_large.jpg\" width=\"186\" height=\"186\" alt=\"play Podcast\" />";					
			}
		} 
		o.innerHTML += "</div>";
		o.innerHTML += "</div>";
	//swfobject.embedSWF(HttpsURL + "flash/singlevoisseplayer.swf", "featuredvoissediv" , "186", "186", "9",false,{sessionid: id, voisseid: voisseclipguid, image: image, wmode:"transparent",allowScriptAccess:"always"},{/*id:"featuredvoise" + voissediv*/});
	initOverlay();
}

function featuredvoisse(voisseclipguid,image,id,type)
{
	$.post(dotdotpath + "_include/ssi/_featured_voisses.cfm", {voisseclipguid: voisseclipguid}, function(data){
		var div = document.getElementById("featuredvoisse_block");
		div.innerHTML = data;
		loadSifr();
		if (type == 1)
		{
			embedSWF(voisseclipguid,image,id);
		} else {
			embedSlideShow(voisseclipguid,image,id,type);
		}
	});
}

function deletevoisse(voisseclipguid)
{
	if (confirm("Are you sure you want to delete this voisse ?")) {
		$.post(dotdotpath + "_include/ssi/voisse/delete.cfm", {voisseclipguid: voisseclipguid}, function(data){
			location.href=dotdotpath+"my-voisses/";
		});
	}
}

function closepromo(promostripid)
{
	$.post(dotdotpath + "_include/ssi/voisse/readalert.cfm", {promostripid: promostripid});
	$("#promorow").slideUp("slow");	
}

function closealert(voisseclipguid)
{
	$.post(dotdotpath + "_include/ssi/voisse/readalert.cfm", {voisseclipguid: voisseclipguid});
	$(this).parent().parent().parent().showToggle();
}

function commentpost(voisseclipguid,comment)
{ 
	document.forms["cb"+voisseclipguid].comment.value="";
	var totalcomments=0;
	
	$.post(dotdotpath + "_include/ssi/voisse/commentpost.cfm", {voisseclipguid: voisseclipguid, comment:comment }, function(data){
		setpage("comments",1,data,voisseclipguid);
	});
	
	$.post(dotdotpath + "_include/ssi/voisse/commenttotal.cfm", {voisseclipguid: voisseclipguid }, function(data){
		setCommentTotal(data,voisseclipguid);
	});																
}


function setCommentTotal(totalcomments,voisseclipguid)
{
	document.getElementById("commentstotal"+voisseclipguid).innerHTML="Comments ("+totalcomments+")";
}

// filter nav functions
function bindFilterEvents(){
	$(".filter_nav a.togglebtn").each(function(){
	    $(this).click(function(){
		 	toggleList($(this));				   
		});
	});
}

function toggleList(div){
	//alert(div);
	div.parent().find("ul").toggleClass("js_hide");

}

// cookies
function set_cookie ( name, value, path, exp_y, exp_m, exp_d,  domain, secure )
{
  var cookie_string = name + "=" + escape ( value );

  if ( exp_y )
  {
    var expires = new Date ( exp_y, exp_m, exp_d );
    cookie_string += "; expires=" + expires.toGMTString();
  }

  if ( path )
        cookie_string += "; path=" + escape ( path );

  if ( domain )
        cookie_string += "; domain=" + escape ( domain );
  
  if ( secure )
        cookie_string += "; secure";
  
  document.cookie = cookie_string;
}



function get_cookie ( cookie_name )
{
  var results = document.cookie.match ( '(^|;) ?' + cookie_name + '=([^;]*)(;|$)' );

  if ( results )
    return ( unescape ( results[2] ) );
  else
    return null;
}


function setmarketsearch (area)
{
	if (area == "people")
	{
		document.searchbox.searcharea.value = area;
		document.searchbox.action = dotdotpath + "people/index.cfm";
	} else {
		document.searchbox.searcharea.value = area;
		document.searchbox.action = dotdotpath + "marketplace/index.cfm";
	}
	document.searchbox.submit();
}

function setsearch (area)
{
	document.searchbox.searcharea.value = area;
	document.searchbox.submit();
}

function setformpage (page)
{
	document.searchbox.page.value = page;
	document.searchbox.submit();
}

function setformssearch (searchvalue)
{
	document.searchbox.keyword.value = searchvalue.toString();
	document.searchbox.submit();
}

function updateplaycount(voisseid,count)
{
	currentplaysbox=document.getElementById("totalplaysof"+voisseid);
	currentplaysbox.innerHTML=count; 
}

function showhide(o,st)
{
	if (typeof(o)=="string")
	{
		o=document.getElementById(o);
	}
	if (!o)
	{
		return;
	}
	if (typeof(st)!="boolean")
	{
		st=(o.style.display=="none");
	}
	o.style.display=(st) ? "" : "none";
	return st;
}

// overlays
function openoverlay(element) {
	$.fn.colorbox({inline:true,open:true,opacity:0.85,href:element});
}

// overlays
function open_vv(guid,type) {
		
	//podcast
	if(type==3)
	{
		$(".create_btns li.create_pc a").attr({disabled: "disabled"}).addClass("active");
		$(".create_btns li.create_ss a").attr({disabled: "disabled"}).addClass("disabled").end();
		$(".create_btns li.n_v a").attr({disabled: "disabled"}).addClass("disabled").end();
	}
	//slideshow
	else if(type==2)
	{
		$(".create_btns li.create_ss a").attr({disabled: "disabled"}).addClass("active");
		$(".create_btns li.create_pc a").attr({disabled: "disabled"}).addClass("disabled").end();
		$(".create_btns li.n_v a").attr({disabled: "disabled"}).addClass("disabled").end();

	}
	//catch
	else
	{
		$(".create_btns li.create_ss a").attr({disabled: "disabled"}).addClass("active");
		$(".create_btns li.create_pc a").attr({disabled: "disabled"}).addClass("disabled").end();
		$(".create_btns li.n_v a").attr({disabled: "disabled"}).addClass("disabled").end();
	}
	
	if (guid != "")
	{
		loadeditoredit(guid,type);
		$(".dashboard .voiseeditoredit").show();
	} 
	else
	{
		loadeditor(guid,type);
		$(".dashboard .voiseeditor").show();
	}
	$(".spcr").show();
}


var multiModeB = 0;

function openNewVoisseEditor () {
	multiModeB = 1;
	//document.getElementById("flashcallanchor").click();
	$(".flashcallanchor").click();
}

function forceVoisseListRefresh(guid) {
	$.fn.colorbox.close();
	if (guid != ""){
		document.getElementById("visualVoisseEditor").forceVoisseListRefresh(guid);
	}
	multiModeB = 0;
	
}

function getElementByIdSafe(id) {
	if (navigator.appName.indexOf("Microsoft") != -1) {
		return window[id];
	} else {
		return document[id];
	}
}

function closeeditorfromflash()
{
	unloadeditor();
	unloadeditoredit();
	$(".create_btns li.create_pc a").removeClass("active");
	$(".create_btns li.create_ss a").removeClass("active");
	$(".create_btns li.create_ss a").removeAttr("disabled").removeClass("disabled").end();
	$(".create_btns li.n_v a").removeAttr("disabled").removeClass("disabled").end();
	$(".create_btns li.create_pc a").removeAttr("disabled").removeClass("disabled").end();
	$(".dashboard .voiseeditor").hide();
	$(".dashboard .voiseeditoredit").hide();
	$(".spcr").hide();
	$(".pcinfo").hide();
	$(".ssinfo").hide();
}

function closerecorderfromflash() {
	$.fn.colorbox.close();
}


function GetFileName(fileName){
	fullName = fileName;
	shortName = fullName.match(/[^\/\\]+$/);
	if (shortName == null || shortName == ""){
		document.getElementById("FileName").innerHTML = "No file selected. Press the Browse button to find it.";
	}
	else{
		document.getElementById("FileName").innerHTML = shortName;
	}
}

function addToDownload(itemCode){
	openoverlay('#addtoDownloadOverlay');
	var loadPath = dotdotpath+'_include/ssi/voisse/singledownload.cfm?voisseclipguid='+itemCode;
	$('#addedToDownload').load(loadPath);

	//alert(itemCode);
}

function showspinner(){
	$('#uploader').show();
}