﻿
	function show_gold_price_list(bool) {		
		if(bool) {
			$(".gold_price_list").show(); // showslideDown()
		} else {
			$(".gold_price_list").hide(); // slideUp()
		}		
	}
	
	
	function show_email_frame(bool) {		
		if(bool) {
			$(".email_frame").show(); // slideDown
		} else {
			$(".email_frame").hide(); // slideUp
		}		
	}	
	
	
	function getPosition(who){
		var T= 0,L= 0;
		while(who){
			//alert(L + " " + T);
			L+= who.offsetLeft;
			T+= who.offsetTop;
			who= who.offsetParent;
			//alert(who.id);
			if(who == document.getElementById("page")) break;
		}
		return [L,T];    
	}
	/*
	function getPos(el) {
		//alert(el.id);
		// yay readability
		for (var lx=0, ly=0;
			 el != null;
			 lx += el.offsetLeft, ly += el.offsetTop, el = el.offsetParent);
		//alert(lx + " " + ly);
		return {x: lx,y: ly};
	}
	*/
	
	
  // Resolve a string identifier to an object
  // ========================================
  function getObject(s) {
    if (document.getElementById && document.getElementById(s)!=null) {
      return document.getElementById(s);
    }
    else if (document.all && document.all[s]!=null) {
      return document.all[s];
    }
    else if (document.anchors && document.anchors.length && document.anchors.length>0 && document.anchors[0].x) {
      for (var i=0; i<document.anchors.length; i++) {
        if (document.anchors[i].name==s) { 
          return document.anchors[i]
        }
      }
    }
  }	

	
	//==================== handle add post title to lightbox ===================
  
	var interv = 0;
	function change_cbox_text(post_title) {
		//alert("change = " + post_title);
		//alert($("#cboxTitle").text() + "   " + interv );	
		//var post_title = "<?php /*echo get_the_title()*/ ?>";
		if($("#cboxOverlay").css('display') == 'none') { // lightbox is not active anymore
			clearInterval(interv);	
		} else { // lightbox is active
			if(!contains($("#cboxTitle").html(), post_title)) { // add post title
				var new_text = "<b>" + $("#cboxTitle").html() + "</b>" + "</br>" + post_title;
				$("#cboxTitle").html(new_text);
				//alert(new_text);
			}
		}
	}	

	function check_cbox_text(post_title) {
		//alert("check = " + post_title);
		interv = setInterval("change_cbox_text('" + post_title+ "');", 500); 
	}  
  
  
$(document).ready(function(){
	
	// open price list small popup above the clicked link
	$("a.gold_price_list_link").click(function(event) {
		
		var obj = getObject($(this).attr('id'));		
		var pos = getPosition(obj);
		
		//var new_top = pos['y'] - 288;
		//var new_left = pos['x'] - 156 - 65;
		var new_top = pos[1] - 288;
		var new_left = pos[0] - 156 + 30; // - 65;
		
		// left: event.pageX-338 + "px"
		// top: event.pageY-316 + "px"
		$(".gold_price_list").css( {top: new_top + "px", left: new_left + "px", position: "absolute", zIndex:100} );
		
		//$(".gold_price_list").offset({ top: new_top, left: new_left });
		//alert($(".gold_price_list").offset().left);
		show_gold_price_list(true);
		//alert($(".gold_price_list").offset().left);
	});	
	
	
	// un-click show email link	
	$("body:not(.show_email a.show_email)").click(function() {
		show_email_frame(false);
	});
	
	
	// click show email link
	$(".show_email a.show_email").click(function() {
		// find expert ad id (expert_ad_<post_id>)
		var parent_obj = $(this).parents(".expert_record_wrapper");
		var post_id = parent_obj.attr("id").substr("expert_ad_".length);
		//alert(post_id);
		//var show_email_span = $(this).parents(".show_email");
		//var show_email_frame_text = $(this).parents(".expert_record_left").find(".show_email_frame").find(".text");
		var show_email_frame_text = $(".email_frame").find(".text");
		
		
		var obj = getObject($(this).attr('id'));		
		var pos = getPosition(obj);
		
		// get the email from db
		var data = {
			action: 'get_email_action',			
			post_id: post_id
		};		
		// ajaxurl is defined in expert_inc.php
		jQuery.post(ajaxurl, data, function(response) {			
			var email = response;
			//var subject = encodeURIComponent("פנייה מאינדקס המקצוענים של UXI");
			var subject = encodeURI("פנייה ממדריך המומחים של UXI");
			show_email_frame_text.html("<a href='mailto:" + email + "?subject=" + subject + "'>" + email + "</a>");
			//show_email_frame_text.css( {display:'block'}) ;
			$(".email_frame").css( {display:'block'}) ; // without this all the widths are 0
			
			// 343 294
			//alert(pos[1] + " " + pos[0]);
			var new_top = pos[1] + 25;
			var new_left = pos[0] - ($(".email_frame .main").width() / 2) + 10; 
			
			$(".email_frame .main").css( {top: new_top + "px", left: new_left + "px", position: "absolute", zIndex:100} );
			$(".email_frame .top").css( {top: new_top-7 + "px", left: pos[0]+5 + "px", position: "absolute", zIndex:101} );			
			show_email_frame(true);
		});				
		
	});

	
	// click ad right part (should open/close 'more details')
	$(".expert_record_right").click(function() {
		$(this).next(".expert_record_mid").find(".extra_details a").click();	
	});
	
	// click ad middle part (should open/close 'more details')
	$(".expert_record_mid").click(function(e) {		
		if(e.target.nodeName != "A") {// user clicked a link inside that div, so ignore the DIV click	(prevent bubbling)
			$(this).find(".extra_details a").click();			
		}
	});
	
	// click ad 'more details' link
	$(".extra_details a").toggle(function() {
			$(this).parents(".expert_record_wrapper").children(".more_details").removeClass('hide');			
			$(this).parent().addClass("opened");
			$(this).text("הסתר פרטים");
		}, function() {
			$(this).parents(".expert_record_wrapper").children(".more_details").addClass('hide');	
			$(this).parent().removeClass("opened");			
			$(this).text("פרטים נוספים");
	});
	
	// click ad tab title
	$("li[class^='inner_tab_title_view_']").click(function() {
		//alert("click ad tab title");
		//	$(this).parents(".expert_record_wrapper").children(".more_details").removeClass('hide');
		
		// get the clicked tab num
		var str = $(this).attr('class');	
		var inner_tab_num_to_show = str.charAt(str.length-1);		
		
		// the tabs are already exist. just show the correct one		
		$(this).parents(".expert_record_wrapper").children(".more_details").children(".white_wrapper").children('.inner_tab_view_' + inner_tab_num_to_show).removeClass('hide');
		$(this).parents(".expert_record_wrapper").children(".more_details").children(".white_wrapper").children('.inner_tab_view_' + inner_tab_num_to_show).siblings("DIV[class^='inner_tab_view_']").addClass('hide');

		// make the title selected
		$(this).addClass('selected');
		$(this).siblings().removeClass('selected');			
			
	});	
	
	// hover "all classifications" in taxonomy page
	$(".results_header .change_classification").hover(function() {		
		$(".results_header .all_sub_classification").slideDown(200);
	}, function() {	
		$(".results_header .all_sub_classification").slideUp(200);
	});		
	
	
	$(".personal_menu ul li").click(function(event) {		
		if(event.target.nodeName == "LI") {			
			$(this).find("a").click(); // this click event is catched later
		}
	});	
	
	
	//==================== handle add post title to lightbox ===================
	// click an image to open lightbox 
	$("a[rel^='lightbox']").click(function(event) {		
		var post_title = $(this).parents(".expert_record_wrapper").find(".expert_record_mid").find("h3").text();				
		if(post_title != "") {
			//alert("before check_cbox_text");
			check_cbox_text(post_title);		
		}		
	});		
	



});




	// when page loads, select the first ad tab
	/*
	function click_first_ad_tab() {
		$("li[class^='inner_tab_title_view_']:first").click();
	}
	*/

