// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
$(function(){

	$(".popup_link_tag").live("click",function(e){
		pop_name = $(this).attr("name");
		pop_name = pop_name!="" ? pop_name : "page_popup";
		url = $(this).attr("href");
		js_on = stristr( url, "?" ) ? "&js_on=true" : "?js_on=true";
		//js_on = "?js_on=true";
		e.preventDefault();
		openModal({
			ajax: url+js_on,
			name: pop_name
		 });
		return false;
	});
	$(".toggle_section_switch").each(function(){
		prepend = true;
		if($(this).attr("prepend")=="false"){
			prepend = false;
		}
		if(prepend){
			if($(this).attr("toggle_sec")!=undefined){
				var toggle_sec = $(this).attr("toggle_sec");
				pm = ($(toggle_sec).is(":visible") ? "-" : "+");
				$(this).prepend("[ <span class=\"plus_minus\">"+pm+"</span> ]&nbsp;&nbsp;");
			}else{
				pm = ($(this).next(".toggle_section").is(":visible") ? "-" : "+");
				$(this).prepend("[ <span class=\"plus_minus\">"+pm+"</span> ]&nbsp;&nbsp;");
			}
		}
	});
	
	$(".toggle_section_switch").live("click",function(e){
		e.preventDefault();
		var opposite = "";
		var hide_effect = $(this).attr("hide_effect")!=undefined ? $(this).attr("hide_effect") : "hide";
		var show_effect = $(this).attr("show_effect")!=undefined ? $(this).attr("show_effect") : "show";
		if($(this).attr("opposite")!=undefined){
			opposite = $(this).attr("opposite");
		}
		if($(this).attr("toggle_sec")!=undefined){
			var toggle_sec = $(this).attr("toggle_sec");
			if($(toggle_sec).is(':visible')){
				eval("$(toggle_sec)."+hide_effect+"();");
				$(".plus_minus",this).html("+");
				if(opposite!=""){$(opposite).show();}
			}else{
				eval("$(toggle_sec)."+show_effect+"();")
				$(".plus_minus",this).html("-");
				if(opposite!=""){$(opposite).hide();}
			}
		}else{
			if($(this).next(".toggle_section").is(':visible')){
				eval("$(this).next(\".toggle_section\")."+hide_effect+"();")
				$(".plus_minus",this).html("+");
				if(opposite!=""){$(opposite).show();}
			}else{
				eval("$(this).next(\".toggle_section\")."+show_effect+"();")
				$(".plus_minus",this).html("-");
				if(opposite!=""){$(opposite).hide();}
			}
		}
		return false;
	});
	
	$(".close_popup").live("click",function(e){
		e.preventDefault();
		closeFacebox($(this).attr("popup_name"));
		return false;
	});
	
	$(".popup_image_tag").click(function(e){
		e.preventDefault();
		openModal({
			text: "<div align=\"center\"><img src=\""+$(this).attr("href")+"\" height=\"350px\" /><br /><b>"+$(this).attr("label")+"</b></div>",
			name: "image_view"
		 });
		return false;
	});
	
	//$('.lightbox').lightbox();
});

function openModal(o){
	var s =  {
		name : "facebox",
		ajax : null,
		image : null,
		div : null
	};
	
	var field = this;
	
	if(o){
		jQuery.extend(s,o);
	}
	
	jQuery.facebox(s);
}

$.fn.hoverInfo = function(o) { 
	
	var s =  {
		width : 100,
		top : "-100px",
		left : "-100px"
	};
		
	if(o){
		jQuery.extend(s,o);
	}
	
	var box = this;
	
	var image = $(box).attr("hv_image");
	var text = $(box).attr("hv_text");
	//var link = $(box).attr("hv-link");

	alert("<div align=\"center\"><img src=\""+image+"\" width=\""+s.width+"\" /><br /><b>"+text+"</b></div>");

	hover_dat = "<div align=\"center\"><img src=\""+image+"\" width=\""+s.width+"\" /><br /><b>"+text+"</b></div>";
	//$(hover_dat).attr("id","hover_img");
	//$(hover_dat).attr("width","100");
	//$(hover_dat).removeAttr("height");

	$(this).append("<div id=\"hover_wrapper\" style=\"position:relative;\">"+
					"<div class=\"hover_row\" style=\"position:absolute;top:"+s.top+";left:"+s.left+";\">"+
					"</div></div>");
	$("#hover_wrapper > .hover_row").html(hover_dat);
}

function stristr( haystack, needle, bool ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   bugfxied by: Onno Marsman
    // *     example 1: stristr('Kevin van Zonneveld', 'Van');
    // *     returns 1: 'van Zonneveld'
    // *     example 2: stristr('Kevin van Zonneveld', 'VAN', true);
    // *     returns 2: 'Kevin '
 
    var pos = 0;
 
    haystack += '';
    pos = haystack.toLowerCase().indexOf( (needle+'').toLowerCase() );
    if( pos == -1 ){
        return false;
    } else{
        if( bool ){
            return haystack.substr( 0, pos );
        } else{
            return haystack.slice( pos );
        }
    }
}

function urlencode( str ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Philip Peterson
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: AJ
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // %          note: info on what encoding functions to use from: http://xkr.us/articles/javascript/encode-compare/
    // *     example 1: urlencode('Kevin van Zonneveld!');
    // *     returns 1: 'Kevin+van+Zonneveld%21'
    // *     example 2: urlencode('http://kevin.vanzonneveld.net/');
    // *     returns 2: 'http%3A%2F%2Fkevin.vanzonneveld.net%2F'
    // *     example 3: urlencode('http://www.google.nl/search?q=php.js&ie=utf-8&oe=utf-8&aq=t&rls=com.ubuntu:en-US:unofficial&client=firefox-a');
    // *     returns 3: 'http%3A%2F%2Fwww.google.nl%2Fsearch%3Fq%3Dphp.js%26ie%3Dutf-8%26oe%3Dutf-8%26aq%3Dt%26rls%3Dcom.ubuntu%3Aen-US%3Aunofficial%26client%3Dfirefox-a'
                                     
    var histogram = {}, histogram_r = {}, code = 0, tmp_arr = [];
    var ret = str.toString();
    
    var replacer = function(search, replace, str) {
        var tmp_arr = [];
        tmp_arr = str.split(search);
        return tmp_arr.join(replace);
    };
    
    // The histogram is identical to the one in urldecode.
    histogram['!']   = '%21';
    histogram['%20'] = '+';
    
    // Begin with encodeURIComponent, which most resembles PHP's encoding functions
    ret = encodeURIComponent(ret);
    
    for (search in histogram) {
        replace = histogram[search];
        ret = replacer(search, replace, ret) // Custom replace. No regexing
    }
    
    // Uppercase for full PHP compatibility
    return ret.replace(/(\%([a-z0-9]{2}))/g, function(full, m1, m2) {
        return "%"+m2.toUpperCase();
    });
    
    return ret;
}
