var quickSearch = {
	elemquick:null,
	home:'http://kavun.mynet.com',
	url:'/quickSearch.php',
	albumKapakPath:'http://g.mynet.com/kavun/album',
	artistProfil:'http://g.mynet.com/kavun/profil',
	query:0,
	json:null,
	as:[],
	suggestID:0,
	suggestTimeout:500,
	searchText:null,
	submit:function(form){
		ara(cname);	
	},
	qclearTimeout:function(){
		this.suggestID ? window.clearTimeout(this.suggestID):1;
	},
	send:function(form){
		if(this.query){ return false; }
		this.qclearTimeout();	
		this.suggestID = window.setTimeout( function(){ quickSearch.request(form); },quickSearch.suggestTimeout);
		return false;
	},
	request:function(search){
		this.searchText = search.toLowerCase();
		var data = 'search='+this.searchText; 			
			//this.query = 1;
			jQuery.ajax({
			   cache:true,
			   dataType: "json",	
			   type: "POST",
			   url: this.home+this.url,
			   data: data,
			   timeout: 5000,
			   success: function(msg){
				 quickSearch.html(msg);
				 quickSearch.query = 0;
			   },
			   error:function(){
				  quickSearch.query = 0;	
			   }
			 });
	},
	html:function(json){
		this.json = json;
		
		if(json.artist.length<1 && json.album.length<1 && json.sound.length<1){
			return false;
		}
		
		var html = '<div style="z-index: 1;" class="results">';
			html += '<a href="javascript:quickSearch.reset()" class="btnClose">Kapat</a>';
			html += '<div class="arrowSearch"></div>';
			html += this.chtm(json.artist,'artist');
			html += this.chtm(json.album,'album');	
			html += this.chtm(json.sound,'sound');	
					
			html +='</div>'
			jQuery("#quicksearchhtml").html(html);
	},
	chtm:function(data,type){
		
		if( data.length < 1){ return ''; }
		
		_title = type ==='sound' ? '\u015Eark\u0131lar':type;
		_title = type ==='artist' ? 'Sanat\u00E7\u0131lar':_title;
		_title = type ==='album' ? 'Alb\u00FCmler':_title;
		
		var html  ='<div class="clr">';
			html +='<span >'+_title+'</span>';
			html +='<a rel="muzikhistory" href="#'+quickSearch.searchText+' |aratumu/'+type+';'+quickSearch.searchText+'" class="fRight"><span>Daha fazla sonu\u00E7</span></a>';
			html +='</div>';
			
			html +='<ul>';
			for(var i in data){
				

				
				
				if( type ==='sound' ){
					var name = data[i].artist_name +" - "+ data[i].sound_name;
				}else if( type ==='artist' ){
					var name = data[i].artist_name;
				}else{
					var name = data[i].artist_name +" - "+ data[i].album_name;
				}
				name = name.replace('&',"-");
				name = name.replace('"',"");
				name = name.replace('?',"");
				var hid = data[i].album_id;
				var img = type === 'artist' ?  data[i].artist_img.toLowerCase():data[i].album_img;
				var artist_id 	= data[i].artist_id;
				var img_artist_id 	= data[i].artist_id;
				if (data[i].sound_id) {
					var artist_id = data[i].sound_id;
				}
				
				html +='<li><b><a rel="muzikhistory" href="#'+this.href(type,hid,artist_id,name)+'"  title="'+name+'">';	
				html +='<img width="40" height="40" align="middle" src="'+this.gimg(img,img_artist_id,type)+'" border="0" onerror="quickSearch.errorIMG(this)"/>';
				html +=name+'</a></b></li>';
				
			}
			
			html +='</ul>';
			return html;
			
	},
	errorIMG:function(elem){
		elem.src='http://kavun.mynet.com/gorsel/profil/profil_b.jpg';
	},
	
	gimg:function(album_img,artist_id,type){
		if( type === 'artist' ){
			rt = this.artistProfil+'/'+album_img;
		}else{
			rt = this.albumKapakPath+'/'+artist_id+'/'+album_img;
		}

		return rt;
	},
	href:function(type,album_id,artist_id,name){
		//type = type === 'sound' ? 'album':type;
		name = name.replace('&',"-");
		name = name.replace('"',"");
		
		if( isNaN(parseInt(album_id)) || type === 'sound' ){
			return name+' - - |'+type+'/'+artist_id;
		}else{
			return name+' - - |'+type+'/'+album_id+';'+artist_id;
		}
		
	},
	git:function(url){
		var url = url.title+'-  - '+url.href;
		Pageload(url);
	},
	reset:function(){
		jQuery("#quicksearchhtml").html('');
	}

}

quickSearch.init = function(event,elem){
	var KEY = {ENTER:13,ESC:27};
	var data= elem.value;
	switch(event.keyCode) {
		case KEY.ENTER:
			quickSearch.submit();		
		break;
		
		case KEY.ESC:
			quickSearch.reset();		
		break;
		
		default:
		    if( (data.length>=3 && event.keyCode>45) || (event.keyCode === 8 && data.length>=3 ) ){
				quickSearch.send(data);
			}else if(data.length<3){
				quickSearch.reset();
			}			
		break;
	}
}

 
 
 var myFriends = {
	page: 1,
	thread_id: 0,
	ajaxPid:0,
	getMessages: function(operation,page,thread_id,username,elem){
		if(page){ this.page = page };
		if(thread_id){ this.thread_id = thread_id };
		jQuery("#m_loading").show();
		
		if( operation == "acc" || operation == "di" ){
			myLogin.setCookie("GetOutMsgCount", "", 0 , "/", ".mynet.com", false);
			this.getFriendCount();
		}
		
		if( typeof this.ajaxPid !== 'number'  )	{
			this.ajaxPid.abort();
		}
		
		this.ajaxPid =  jQuery.post("/friends/friends.php", { o: operation, p: this.page, tid: this.thread_id,tousername:username },
		  function(data){
		  	jQuery("#m_loading").hide();
		    if(data != "-1"){
		    	if(operation != "di" && operation != "do" && operation != "rj"  && operation != "acc"){
		    		elem = window.location.hash.indexOf('profile') >0 ? 'rightSmallContain':elem;
					jQuery(document.getElementById(elem)).html(data);
		    	} else {
					
		    		myFriends.count = myFriends.count - 1;
					
		    		if(myFriends.count == 0){
		    			myFriends.getMessages("g"+operation.substr(1),1,false,'','m_content');
		    		} else {
						if(operation =='acc' ){
							jQuery("#msg_"+myFriends.thread_id+" .messageButton").html("Arkada\u015F listenize eklendi");
						}else if ( operation == 'di' ){
							jQuery("#msg_"+myFriends.thread_id+" .messageButton").html("Arkada\u015F iste\u011Fi reddedildi");
						}else{
							jQuery("#msg_"+myFriends.thread_id).remove();
						}
		    		}
		    	}
		    } else {
		    	//alert(data);
		    }
		  });

	},
	getFriendCount:function(){
		var elemcount = jQuery('#m_counts');
		var mcount = parseInt(elemcount.html().replace(/[^0-9]/g,''))-1;
		if( mcount > -1 ){
			elemcount.html(' ( '+mcount+' ) ');
		}
		
	},
	handleResponse: function(data){
		alert(data);
	},
	errorAvatar:function(elem){
		elem.src='http://s.mynet.com/eksn1/DefaultImages/avatar-u2.gif';
	}
	
}


function ShowMessageDialog(){

	 if(IsAuthenticated){

			showDialog('Mesaj G\u00F6nder','<iframe height="330" frameborder="0" align="middle" width="550" scrolling="no" src="http://eksenim.mynet.com/ep/NewMsg.aspx?u='+ RequestedUser + '" ></iframe>','warning','419','602');

	 }else{

			eksenAlertMessage("Mesaj g\u00F6nderebilmek i\u00E7in, \u00DCye giri\u015Fi yapmal\u0131s\u0131n\u0131z.","login");

	 }

}


function showDialog(title,message,type,popupheight,popupwidth) {

  var dialogheader;
  var dialogclose;
  var dialogtitle;
  var dialogcontent;
  var dialogmask;
  var htmlcontent;
  
  
  if(!document.getElementById('dialogMask')) {
    
	dialogmask = document.createElement('div');
    dialogmask.id = 'dialogMask';
	dialogrelative = document.createElement('div');
    dialogrelative.id = 'dialogRelative';
    dialogheader = document.createElement('div');
    dialogheader.id = 'dialogHeader';
    dialogcontent = document.createElement('div');
    dialogcontent.id = 'dialogContent';
    
	htmlcontent = document.createElement('div');
    htmlcontent.id = 'htmlContent';
	
	
	dialogtitle = document.createElement('div');
    dialogtitle.id = 'dialogTitle';
	dialogclose = document.createElement('div');
    dialogclose.id = 'dialogClose';

    document.body.appendChild(dialogmask);
	dialogmask.appendChild(dialogrelative);
	dialogrelative.appendChild(dialogcontent);
	dialogcontent.appendChild(dialogheader);
	dialogcontent.appendChild(htmlcontent);


    dialogheader.appendChild(dialogtitle);
    dialogheader.appendChild(dialogclose);

    dialogclose.setAttribute('onclick','hideDialog()');
    dialogclose.onclick = hideDialog;

	//dialogmask.onclick = hideDialog;

  } else {
	
    dialogmask = document.getElementById('dialogMask');
    dialogheader = document.getElementById('dialogHeader');
    dialogtitle = document.getElementById('dialogTitle');
    dialogclose = document.getElementById('dialogClose');
    dialogcontent = document.getElementById('dialogContent');
	htmlcontent = document.getElementById('htmlContent');
    dialogmask.style.visibility = "visible";
  }


	dialogheader.className = type + "header";
	dialogtitle.innerHTML = title;
	dialogcontent.className ="clear";
	htmlcontent.className ="clear";


	var width = pageWidth();
	var height = pageHeight();
	var left = leftPosition();
	var top = topPosition();

   var content = document.getElementById(WRAPPER);
   dialogmask.style.height = content.offsetHeight + 'px';
	
	jQuery('#htmlContent').html(message);
    //htmlcontent.innerHTML = message;
    
	var dialogwidth;
	var dialogheight;
	var topposition;
	var leftposition;


	if(popupwidth==null || popupwidth=='' || popupwidth==undefined){
		dialogwidth = dialogcontent.offsetWidth;
	}else{
		dialogwidth = popupwidth;
	}

	if(popupheight==null || popupheight=='' || popupheight==undefined){
		dialogheight = dialogcontent.offsetHeight;
	}else{
		dialogheight = popupheight;
	}

	topposition=height-dialogheight;
	topposition=topposition/2;
	dialogcontent.style.top=topposition+"px";

	leftposition=width-dialogwidth;
	leftposition=leftposition/2;
	dialogcontent.style.left=leftposition+"px";

	dialogmask.style.display = "block"; 
	dialogmask.style.visibility = "visible";
}


