(function($) {
    var debugMode = false;
    $.fn.extend({
        // We assume there could be multiple sets of tabs on a page, so,
        // the unique id for each invididual tab's heading is identified with params q and r (e.g., id="accessibletabscontent0-2")
        getUniqueId: function(p, q, r){
            if (r===undefined) {r='';} else {r='-'+r;}
            return p + q + r;
        },
        accessibleTabs: function(config) {
            var defaults = {
                wrapperClass: 'content', // Classname to apply to the div that is wrapped around the original Markup
                currentClass: 'current', // Classname to apply to the LI of the selected Tab
                tabhead: 'h4', // Tag or valid Query Selector of the Elements to Transform the Tabs-Navigation from (originals are removed)
                tabheadClass: 'tabhead', // Classname to apply to the target heading element for each tab div
                tabbody: '.tabbody', // Tag or valid Query Selector of the Elements to be treated as the Tab Body
                fx:'show', // can be "fadeIn", "slideDown", "show"
                fxspeed: 'normal', // speed (String|Number): "slow", "normal", or "fast") or the number of milliseconds to run the animation
                currentInfoText: 'current tab: ', // text to indicate for screenreaders which tab is the current one
                currentInfoPosition: 'prepend', // Definition where to insert the Info Text. Can be either "prepend" or "append"
                currentInfoClass: 'current-info', // Class to apply to the span wrapping the CurrentInfoText
                tabsListClass:'tabs-list', // Class to apply to the generated list of tabs above the content
                syncheights:false, // syncs the heights of the tab contents when the SyncHeight plugin is available http://blog.ginader.de/dev/jquery/syncheight/index.php
                syncHeightMethodName:'syncHeight', // set the Method name of the plugin you want to use to sync the tab contents. Defaults to the SyncHeight plugin: http://github.com/ginader/syncHeight
                cssClassAvailable:false, // Enable individual css classes for tabs. Gets the appropriate class name of a tabhead element and apply it to the tab list element. Boolean value
                saveState:false, // save the selected tab into a cookie so it stays selected after a reload. This requires that the wrapping div needs to have an ID (so we know which tab we're saving)
                autoAnchor:false // will move over any existing id of a headline in tabs markup so it can be linked to it
            };
            var keyCodes = {
                37 : -1, //LEFT
                38 : -1, //UP
                39 : +1, //RIGHT 
                40 : +1 //DOWN
            };
            this.options = $.extend(defaults, config);
            var o = this;
            return this.each(function(t) {
                var el = $(this);
                var list = '';
                var tabCount = 0;

                $(el).wrapInner('<div class="'+o.options.wrapperClass+'"></div>');

                $(el).find(o.options.tabhead).each(function(i){
                    var id = '';
                    elId = $(this).attr('id');
                    if(elId){
                        id =' id="'+elId+'"';
                    }
                    var tabId = o.getUniqueId('accessibletabscontent', t, i);//get a unique id to assign to this tab's heading
                    $(this).attr({"id": tabId, "class": o.options.tabheadClass, "tabindex": "-1"});//assign the unique id and the tabheadClass class name to this tab's heading
                    if(o.options.cssClassAvailable === true) {
                        var cssClass = '';
                        if($(el).attr('class')) {
                            cssClass = $(this).attr('class');
                            cssClass = ' class="'+cssClass+'"';
                            list += '<li><a'+id+''+cssClass+' href="#'+tabId+'">'+$(this).html()+'</a></li>';
                        }
                    } else {
                        list += '<li><a'+id+' href="#'+tabId+'">'+$(this).html()+'</a></li>';
                    }
                    tabCount++;
                });

                $(el).prepend('<ul class="clearfix '+o.options.tabsListClass+' tabamount'+tabCount+'">'+list+'</ul>');
                $(el).find(o.options.tabbody).hide();
                $(el).find(o.options.tabbody+':first').show();
                $(el).find("ul>li:first").addClass(o.options.currentClass)
                .find('a')[o.options.currentInfoPosition]('<span class="'+o.options.currentInfoClass+'">'+o.options.currentInfoText+'</span>');

                if (o.options.syncheights && $.fn[o.options.syncHeightMethodName]) {
                    $(el).find(o.options.tabbody)[o.options.syncHeightMethodName]();
                    $(window).resize(function(){ 
                        $(el).find(o.options.tabbody)[o.options.syncHeightMethodName]();
                    });
                }

                $(el).find('ul.'+o.options.tabsListClass+'>li>a').each(function(i){
                    $(this).click(function(event){
                        event.preventDefault();
                        if(o.options.saveState && $.cookie){//fixed typo: "savestate"-->"saveState"
                            $.cookie('accessibletab_'+el.attr('id')+'_active',i);
                        }
                        $(el).find('ul>li.'+o.options.currentClass).removeClass(o.options.currentClass)
                        .find("span."+o.options.currentInfoClass).remove();
                        $(this).blur();
                        $(el).find(o.options.tabbody+':visible').hide();
                        $(el).find(o.options.tabbody).eq(i)[o.options.fx](o.options.fxspeed);
                        $(this)[o.options.currentInfoPosition]('<span class="'+o.options.currentInfoClass+'">'+o.options.currentInfoText+'</span>')
                        .parent().addClass(o.options.currentClass);
                        //now, only after writing the currentInfoText span to the tab list link, set focus to the tab's heading
                        $($(this).attr("href")).focus().keyup(function(event){
                            if(keyCodes[event.keyCode]){
                                o.showAccessibleTab(i+keyCodes[event.keyCode]);
                                $(this).unbind( "keyup" );
                            }
                        });

                        // $(el).find('.accessibletabsanchor').keyup(function(event){
                        //     if(keyCodes[event.keyCode]){
                        //         o.showAccessibleTab(i+keyCodes[event.keyCode]);
                        //     }
                        // });
                        
                        
                    });

                    $(this).focus(function(event){
                        $(document).keyup(function(event){
                            if(keyCodes[event.keyCode]){
                                o.showAccessibleTab(i+keyCodes[event.keyCode]);
                            }
                        });
                    });
                    $(this).blur(function(event){
                        $(document).unbind( "keyup" );
                    });
                
                    
                });

                if(o.options.saveState && $.cookie){//fixed typo: "savestate"-->"saveState"
                    var savedState = $.cookie('accessibletab_'+el.attr('id')+'_active');
                    debug($.cookie('accessibletab_'+el.attr('id')+'_active'));
                    if(savedState != null){
                        o.showAccessibleTab(savedState,el.attr('id'));
                    }
                };
                
                if(o.options.autoAnchor && window.location.hash){
                    var anchorTab = $('.'+o.options.tabsListClass).find(window.location.hash);
                    if(anchorTab.size()){
                        anchorTab.click();
                    }
                }
            });
        },
        showAccessibleTab: function(index,id){
            debug('showAccessibleTab');
            var o = this;
            if(id){
                var el = $('#'+id);
                var links = el.find('ul.'+o.options.tabsListClass+'>li>a');
                links.eq(index).click();
            }else{
                return this.each(function() {
                    var el = $(this);
                    var links = el.find('ul.'+o.options.tabsListClass+'>li>a');
                    links.eq(index).click();
                });
            }
        }
    });
    // private Methods
    function debug(msg,info){
        if(debugMode && window.console && window.console.log){
            if(info){
                window.console.log(info+': ',msg);
            }else{
                window.console.log(msg);
            }
        }
    }
})(jQuery);
function goto_page(el){
	if(el.options[el.selectedIndex].value != '-') {
		document.location.href = el.options[el.selectedIndex].value;
	}
}
$(document).ready(function(){
/* 	if($('#main-prop-search #search-country').val() != 4) {
		$('#main-prop-search #search-nor').attr('disabled', 'disabled');
		$('#main-prop-search #search-nor').val('');
	}
	$('#main-prop-search #search-country').change(function() {
		if($('#main-prop-search #search-country').val() == 4) {
			$('#main-prop-search #search-nor').attr('disabled', '');
		} else $('#main-prop-search #search-nor').attr('disabled', 'disabled');
	});*/

$('#more-stuff').before('<p id=""rmf><a id="rm" href="#">Read More &raquo</a></p>').css('display','none');
$('#rm').click(function(){$('#more-stuff').show("slow");$('#rm').css('display','none');$('#more-stuff').before('<h2>More Information</h2>');return false;});
function parseGetVars() {
var getVars = new Array();
var qString = unescape(top.location.search.substring(1));
var pairs = qString.split(/\&/);
for (var i in pairs) {
var nameVal = pairs[i].split(/\=/);
getVars[nameVal[0]] = nameVal[1];
}
return getVars;
}

	$('#main-prop-search #search-country').change(function() { 
		var dd = $("#prop_area");												   
		if($('#main-prop-search #search-country').val() == 4) {
			$("#sf-area").append('<div style="text-align:center"><img src="/assets/images/v5structure/8-0.gif" /></div>');
			var foo = parseGetVars();
			$.ajax({
				type: "GET",
				url: '/a_/?f=createAreadropdown&n='+foo['prop_area'],
				data: '',
				error: function(obj, msg){
					// Die silently? noooo
				},
				success: function(msg){
					$("#sf-area").empty();
					$("#sf-area").append(msg);
					$("#sf-area").show('slow');
				}
			});
			
			return false;
		} else $("#sf-area").empty();
	});
	$('#advanced-search #search-country').change(function() { 
		var dd = $("#prop_area");												   
		if($('#advanced-search #search-country').val() == 4) {
			$("#adv-area-target").append('<div style="text-align:center"><img src="/assets/images/v5structure/8-0.gif" /></div>');
			var foo = parseGetVars();
			$.ajax({
				type: "GET",
				url: '/a_/?f=createAreadropdown&n='+foo['prop_area'],
				data: '',
				error: function(obj, msg){
					// Die silently? noooo
				},
				success: function(msg){
					$("#adv-area-target").empty();
					$("#adv-area-target").append(msg);
					$("#adv-area-target").show('slow');
					$("#area-label").show('slow');
				}
			});
			
			return false;
		} else $("#adv-area-target").empty();
	});

$(".jquery_tabs").accessibleTabs({tabhead:'h3'});
		if($('#doss_tree').length > 0) {
			$('#doss_tree').treeview({
				persist: 'location'
			});
		}

});
