function dropdownValue(mySel){
	var myVal;
	myVal = mySel.options[mySel.selectedIndex].value;
	if(myVal=='')
	{
		myVal = '0';
	}
	return myVal;
}

function goToPage(url,name,replace){
	if (name=='order'){
		window.location.href = url+'?ordby='+dropdownValue(document.getElementById('ordby'));
		return
	}
	if(!(url=='')){
		if(replace){
			window.open(url, name, 'left=100,top=100,width=500,height=500,toolbar=1,resizable=1,scrollbars=1,location=1', false);
		}else{
			window.location.href = url;
		}
	}
}

function goToNextPage(url,dropdown,page,tag){
	var sortId	
	if (dropdown.options === null || dropdown.options === undefined) {	   
		sortId = dropdown;
	}else{
		sortId = dropdownValue(dropdown);
	}	
	var _url = url+'?sortId='+sortId+'&_tags='+tag+'&_page='+page;		
	if(!(_url=='')){					
		window.location.href = _url;			
	}
}

function getpage(url,dropdown,text){
	if(dropdown !=''){
		var ddl = document.getElementById(dropdown);
		var catvalue = dropdownValue(ddl);
		window.open(url+catvalue,'_self','',true);
	}else{
		var textvalue = document.getElementById(text).value;
		window.open(url+textvalue,'_self','',true);
	}
	return true;
}

//SETTING UP OUR POPUP
var popupStatus = 0;//0 means disabled; 1 means enabled;
var bodydivcont; // =============== the content of the floating window
var bodydiv; // =============== the id for content div of the floating window
var jqobj;// =============== the jquery class the has most of the methods
var frm;// ======================= the dummy form to be sent
var postid;// ================= id of post in list of assosiated company
var jqnamespace; // =============== the jquery object that has been created
var clickedpost;// ============================ id of post that has been clicked by user so that the floating window can appear
var button ;//  thi id of the image tha will be clicked so as to show the floating window
var popupContact ; // the floating window that has the information
var popupContactClose ;// id of button that cloases the floating window
var backgroundPopup ; //id of the smoky background of the floating window

function setupevents(bt, pc, bp, hidden){
	if(typeof jqnamespace === "undefined") return false;
	if(typeof jqobj === "undefined") return false;
	button = bt;
	buttonm = bt+'m';
	popupContact = pc;
	backgroundPopup = bp;
	if(button.indexOf("#abt") != -1){
		clickedpost = button.replace("#abt", "");
	}else if(button.indexOf("#hbt") != -1){
		clickedpost = button.replace("#hbt", "");
	}else{
		clickedpost = button.replace("#bt", "");
	}
	var tt = jqnamespace(hidden).val();
	if(tt=='true'){
		jqobj.setaction();
		jqnamespace(hidden).val(false);
	}
	return true;
}

function setupclosseevent(pcc, hidden){
	popupContactClose = pcc;
	var tt = jqnamespace(hidden).val();
	if(tt=='true'){
		jqobj.setclose();
		jqnamespace(hidden).val(false);
	}
}

function setupclosseevent2(pcc){
	popupContactClose = pcc;
	jqnamespace(popupContactClose).unbind('click');
	jqobj.setclose();
}

function getpostcontent(id){
	postid = id;// set the id of the clicked post in list of assosiated company
	frm.submit();
	return false;
}

function getpostcontent2(id,clickid,hidden){
	postid = id;// set the id of the clicked post in list of assosiated company
	//var tt = jqnamespace(hidden).val();
	jqnamespace(clickid).unbind('click');
	jqobj.setopen(clickid);
	//if(tt=='true'){
		
		//jqnamespace(hidden).val(false);
	//}
	return false;
}

(
	function(popmanager){
		popmanagerObj = 
			{
				init : function(){
						//popmanager( "#tabz" ).tabs({ tabTemplate: '<ul><li><a href="#{href}">{label}</a></li></ul>' });
						//popmanager( "#tabz" ).tabs({ panelTemplate: '<div></div>' });
						/*popmanager( "#tabz" ).tabs(
							{
								selected: 0 //default tab
							}
						);*/

						if(popmanager("#tabz").html() !== null){
							popmanager("ul.portfolio_tabs").tabs("div.tab_panes > div");
						}
                                                
						jqobj = popmanagerObj;
						jqnamespace = popmanager;
						frm = popmanager("#portwin form");
						frm.submit(function(){
							popmanagerObj.ajaxGetPost(this);
							return false;
						});
						return true;				
					},
				setclose :function(){
								
					//CLOSING POPUP
					//Click the x event!
					popmanager(popupContactClose).click(function(){
						popmanager(bodydiv).html(bodydivcont);
						popmanagerObj.unload();
					});
					return true;
				},
				setopen :function(clickid){
								
					//CLOSING POPUP
					//Click the x event!
					popmanager(clickid).click(function(){
						frm.submit();// get content from servre
						//centering with css
						popmanagerObj.center();
						//load popup
						popmanagerObj.load();
					});
					return true;
				},
				setaction : function(){
					//LOADING POPUP
					//Click the button event!
					popmanager(button).click(function(){
						if(button.indexOf("#abt") != -1){
							clickedpost = button.replace("#abt", "");
						}else if(button.indexOf("#hbt") != -1){
							clickedpost = button.replace("#hbt", "");
						}else{
							clickedpost = button.replace("#bt", "");
						}
						//centering with css
						popmanagerObj.center();
						//load popup
						popmanagerObj.load();
					});
					//Click the more button event!
					popmanager(buttonm).click(function(){
						//centering with css
						popmanagerObj.center();
						//load popup
						popmanagerObj.load();
					});
					
					//Click out event!
					popmanager(backgroundPopup).click(function(){
						popmanager(bodydiv).html(bodydivcont);
						popmanagerObj.unload();
					});
					//Press Escape event!
					popmanager(document).keypress(function(e){
						if(e.keyCode==27 && popupStatus==1){
							popmanager(bodydiv).html(bodydivcont);
							popmanagerObj.unload();
						}
						});
						return true;				
					},
				//loading popup with jQuery magic!
				load : function(){
						//loads popup only if it is disabled
						if(popupStatus==0){
							popmanager(backgroundPopup).css({
								"opacity": "0.5"
							});
							popmanager(backgroundPopup).fadeIn("slow");
							popmanager(popupContact).fadeIn("slow");
							popupStatus = 1;
						}
						return true;				
					},
				ajaxGetPost : function(f){
					var hidf = ''; //clickedpost is set when a logo is clicked
					if(button.indexOf("#abt") != -1){
						hidf = '#ahds'+clickedpost;
					}else if(button.indexOf("#hbt") != -1){
						hidf = '#hhds'+clickedpost;
					}else{
						hidf = '#hds'+clickedpost;
					}
					
					var halfbodydiv = '';
					if(button.indexOf("#abt") != -1){
						halfbodydiv = 'apc'+clickedpost;
					}else if(button.indexOf("#hbt") != -1){
						halfbodydiv = 'hpc'+clickedpost;
					}else{
						halfbodydiv = 'pc'+clickedpost;
					}
					
					
					if(button.indexOf("#abt") != -1){
						bodydiv = '#apc'+clickedpost; //clickedpost is set when a logo is clicked
					}else if(button.indexOf("#hbt") != -1){
						bodydiv = '#hpc'+clickedpost; //clickedpost is set when a logo is clicked
					}else{
						bodydiv = '#pc'+clickedpost; //clickedpost is set when a logo is clicked
					}
					popmanager('#thepostid').val(postid); //postid is set in the getpostcontent  function
					//popmanager('#bodydiv').val(halfbodydiv);

                                        f=popmanager(f);			
					var data=f.serialize();
                                        popmanager.ajax({
						url: '/wp-admin/admin-ajax.php',
						type: 'GET',
						data: data,
						dataType: 'html',
						beforeSend: function() {
							if(popmanager(hidf).val()=='true'){ // check whether we need to store current the content as the original
								bodydivcont = popmanager(bodydiv).html();
							}
							popmanager(bodydiv).html("<img style='margin-top:30px; width:20px; height:20px;' src='/wp-content/plugins/Extreme-Custom-Posts/images/loading.gif' align='middle'/> Getting Content ... ");
						},
						success: function(result, status, XMLHttpRequest){
							var r=result.replace(/&#8203;/gi,'');
							popmanager(bodydiv).html(r);
							return false;
						},
						error:function(x,e){
							if(x.status==0){
								alert('You are offline!!\n Please Check Your Network.');
							}else if(x.status==404){
								alert('Requested URL not found.');
							}else if(x.status==500){
								alert('Internel Server Error.');
							}else if(e=='parsererror'){
								alert('Error.\nParsing JSON Request failed.');
							}else if(e=='timeout'){
								alert('Request Time out.');
							}else {
								alert('Unknow Error.\n'+x.responseText);
							}
							return false;
						}								
					});
				},
				//disabling popup with jQuery magic!
				unload : function(){
					
						//disables popup only if it is enabled
						if(popupStatus==1){
							popmanager(backgroundPopup).fadeOut("slow");
							popmanager(popupContact).fadeOut("slow");
							popupStatus = 0;
						}
						return true;				
					},
				//centering popup
				center : function(){
					
						//request data for centering
						var windowWidth = document.documentElement.clientWidth;
						var windowHeight = document.documentElement.clientHeight;
						var popupHeight = popmanager(popupContact).height();
						var popupWidth = popmanager(popupContact).width();
						//centering
						popmanager(popupContact).css({
							"position": "absolute",
							"top": windowHeight/2-popupHeight/2,
							"left": windowWidth/2-popupWidth/2
						});
						//only need force for IE6
						
						popmanager(backgroundPopup).css({
							"height": windowHeight
						});
						return true;				
					}
					
			};		
		popmanager(document).ready(function(){
			popmanagerObj.init()
		})
	}
)(jQuery);

