$(function(){
	
	// Flash
	$('#flash').flash(null, { version: 8 }, function(htmlOptions) {
        var $this = $(this);
        var params = $this.children().attr('rel').split('|');
        htmlOptions.src = params[0];
        htmlOptions.width = params[1];
        htmlOptions.height = params[2];
        this.innerHTML = '<div class="alt">'+this.innerHTML+'</div>';
        $this.addClass('flash-replaced').prepend($.fn.flash.transform(htmlOptions));
    });
		
		// 'Poslovnice' dropdown
		$('#office-switch').addClass('js');
		$('#office-switch ul').hide();
		$('#office-switch h2').after('<div>'+phrasebook.choose_office+'</div>');
		var officeSwitchSlideStatus='close';
		$('#office-switch div').click(function(){
			if ( $(this).next().is(':hidden') ) {
				$(this).next().slideDown(300,function(){officeSwitchSlideStatus='open'});
			} else {
				$(this).next().slideUp(300,function(){officeSwitchSlideStatus='close'});
			}
		});
		$().click(function(){
			if ( officeSwitchSlideStatus=='open' ) {
				$('#office-switch ul').slideUp(300,function(){officeSwitchSlideStatus='close'});
			}
		});
		
		// 'Filter' dropdown
		
    $('.switcher').addClass('switcherJs');
		
		/*$('#filterType').hide();
		$('#filterType').before('<div id="filterTypeTitle"><a rel="all">Poslovnice i bankomati</a></div>');
		var filterTypeSlideStatus='close';
		$('#filterTypeTitle').click(function(){		  
			if ( $(this).next().is(':hidden') ) {
				$(this).next().slideDown(300,function(){filterTypeSlideStatus='open'});
			} else {
				$(this).next().slideUp(300,function(){filterTypeSlideStatus='close'});
			}
		});
		$().click(function(){
			if ( filterTypeSlideStatus=='open' ) {
				$('#filterType').slideUp(300,function(){filterTypeSlideStatus='close'});
			}
		});*/
		
		$('#filterCity').hide();
		$('#filterCity').before('<div id="filterCityTitle"><a rel="all">'+phrasebook.all_places+'</a></div>');
		var filterCitySlideStatus='close';
		$('#filterCityTitle').click(function(){
			if ( $(this).next().is(':hidden') ) {
				$(this).next().slideDown(300,function(){filterCitySlideStatus='open'});
			} else {
				$(this).next().slideUp(300,function(){filterCitySlideStatus='close'});
			}
		});
		$().click(function(){
			if ( filterCitySlideStatus=='open' ) {
				$('#filterCity').slideUp(300,function(){filterCitySlideStatus='close'});
			}
		});
		
		//$('#filterTypeTitle,#filterType').wrapAll("<div class='switcherWrap'></div>");
		$('#filterCityTitle,#filterCity').wrapAll("<div class='switcherWrap'></div>");
		
		$('.switcher div ul li').click(function(){
        var txtValue = $(this).text();
        var relValue = $(this).attr("rel");
        
        var searchOrder = $(this).parents(".switcherWrap").prevAll().length;
        
        $(this).parents("ul").prev("div").find("a").text(txtValue);
        $(this).parents("ul").prev("div").find("a").attr("rel",relValue);
        
        //var searchValue1 = $("#filterTypeTitle a").attr("rel");
        var searchValue2 = $("#filterCityTitle a").attr("rel");
        
        /*if(searchValue1=="all"&&searchValue2=="all"){ $(".office-list > li").show(); }
        else {
            if(searchValue1=="all"){
                $(".office-list > li").show();
                for (i=0;i<$(".office-list > li").length;i++){
                   if($(".office-list > li:eq("+i+")").attr("class").search(searchValue2)==-1){
                        $(".office-list > li:eq("+i+")").hide();
                   }
                }
            } else {
                  if(searchValue2=="all"){
                      $(".office-list > li").show();
                      for (i=0;i<$(".office-list > li").length;i++){
                         if($(".office-list > li:eq("+i+")").attr("class").search(searchValue1)==-1){
                              $(".office-list > li:eq("+i+")").hide();
                         }
                      }
                  }
                  else {
                      $(".office-list > li").show();
                      for (i=0;i<$(".office-list > li").length;i++){
                         if($(".office-list > li:eq("+i+")").attr("class").search(searchValue1)==-1){
                              $(".office-list > li:eq("+i+")").hide();
                         }
                      }
                      for (i=0;i<$(".office-list > li").length;i++){
                         if($(".office-list > li:eq("+i+")").attr("class").search(searchValue2)==-1){
                              $(".office-list > li:eq("+i+")").hide();
                         }
                      }
                  }
            }
        }*/
        if(searchValue2=="all"){ $(".office-list > li").show(); }
        else {
            $(".office-list > li").show();
            for (i=0;i<$(".office-list > li").length;i++){
               if($(".office-list > li:eq("+i+")").attr("class").search(searchValue2)==-1){
                    $(".office-list > li:eq("+i+")").hide();
               }
            }
        }
        $(".office-list > li").removeClass("bkg");
        $(".office-list > li:visible:even").addClass("bkg");         
    });
    
    $('.office-list .more').click(function(){
        $("#map-wrapper iframe").attr("src",$(this).attr("href"));
        $('html, body').animate({scrollTop:0}, 500);
        return false;
    });
    
		// Rounded Corners
		$("#sidenav > li:first-child a").corner("tr 8px");
		$("#sidenav > li:last-child a").corner("br 8px");
	
});
