$(document).ready(function() {
	$("#frontYrityksille a#nostoLeftButton").click(function(event) {
		event.preventDefault();
		$("#frontBlueTab2Yrityksille").hide();
		$("#frontBlueTab1Yrityksille").show();
	});
	$("#frontYrityksille a#nostoRightButton").click(function(event) {
		event.preventDefault();
		$("#frontBlueTab1Yrityksille").hide();
		$("#frontBlueTab2Yrityksille").show();
	});
	$("#frontYksityisille a#nostoLeftButton").click(function(event) {
		event.preventDefault();
		$("#frontBlueTab2Yksityisille").hide();
		$("#frontBlueTab1Yksityisille").show();
	});
	$("#frontYksityisille a#nostoRightButton").click(function(event) {
		event.preventDefault();
		$("#frontBlueTab1Yksityisille").hide();
		$("#frontBlueTab2Yksityisille").show();
	});
	
	// section selector linkit + cookielaitto
	$("a#sectionYksityisille").click(function(event) {
		event.preventDefault();
		
		var href = $(this).attr('href');
		var pageId = href.substring(href.lastIndexOf('=')+1);
		
		$.get("setCookie.php", { page: pageId },
		function(data){
			document.location = href;	
			// alert('Yksityisille: ' + data);
		});
	});
	// section selector linkit + cookielaitto
	$("a#sectionYrityksille").click(function(event) {
		event.preventDefault();
		
		var href = $(this).attr('href');
		var pageId = href.substring(href.lastIndexOf('=')+1);
		
		$.get("setCookie.php", { page: pageId },
		function(data){
			document.location = href;
			// alert('Yrityksille: ' + data);
		});
	});
    //$('#contentContainer').css('height', $('#content_1').height());
    //$('#content_1').css('left', '0');
	//$('#tabs a').click(tabSlide);
	
	//MAC SAFARI FIX
	setTimeout("$('#contentContainer').css('height', $('#content_1').height()); $('#content_1').css('left', '0'); $('#tabs a').click(tabSlide);", 1000);
	
	
	var $infoButton = $('a.openInfo');
    var $closeButton = $('a.closeInfo');
    var $loginButton = $('a.openLogin');
    var $loginInfoButton = $('a.openLoginInfo');
    
    $infoButton.click(function() {
        $(this).next('.infoBox').css("display","block");
    });
    
    $loginInfoButton.click(function() {
        $(this).next('.infoBox').css("display","block");
    });
    
    $loginButton.click(function() {
        $(this).next('.infoBoxLogin').css("display","block");
    });
    
    $closeButton.click(function() {
        $(this).parent().parent().parent('.infoBox').css("display","none");
        $(this).parent().parent().parent('.infoBoxLogin').css("display","none");
    }); 
    
    $(".infoBox").hover('', 
      function () {
        $(this).css("display","none");
      }
    );

	$("a.delete").click(function () {
      $(this).parent().parent('.nimip_row').remove();
    });
    
    $("a.nimipReset").click(function () {
        $('#nameRepo').html("");
        //return false;
    });
    
    $("#search_suggestBox").hover('', 
      function () {
        $(this).css("display","none");
      }
    );
    
    var $openBtn = $('a.control_showAll');
    
    $openBtn.toggle(function(){

        $(this).parent().find('.notes').css("display","block");
        $(this).removeClass("control_showAll");
        $(this).addClass("control_hide");
    }, function () {
        $(this).parent().find('.notes').css("display","none");
        $(this).removeClass("control_hide");
        $(this).addClass("control_showAll");
    });
    
    var $editBtn = $('a.control_edit');
    $editBtn.toggle(function(){
        $(this).parent().find('.editDate').css("display","block");
    }, function () {
        $(this).parent().find('.editDate').css("display","none");
    });
    
    $('input.lahjakoodiInput').focus(function () {
        $(this).val("");
    });
    
    //Avaa hakudropparit
    $('.drop_head').click(function(){
        if ($(this).next('.drop_content').is(":animated")) { 
                return false; 
            }
        else
        {
            if ($(this).hasClass('active')) {
                
                $(this).parent().find('.drop_content').css("display","none");
                $(this).removeClass("active");
            }
            else
            {
                
                $(this).addClass("active");
                $(this).parent().find('.drop_content').slideDown("fast");
            }
        }
    });
    
    $('.drop_content a').click(function () {
        var links = $('li a');
        links.removeClass("selected");
        $(this).addClass("selected");
        $(this).parent().parent().parent().parent().find('.label').html($(this).html() );        
        $('.drop_head').removeClass("active");
      $('.drop_content').css("display","none");
    });
    
    //Avaa lisähakuehdot
    $('a.searchMore').toggle(function(){
        $('.searchHidden').css("display","none");
        $('a.searchMore').html("Lis&auml;&auml; hakuehtoja");
    }, function () {
        $('.searchHidden').css("display","block");
        $('a.searchMore').html("Piilota lis&auml;hakuehdot");
    });
    
    //Nollataan hakulomake
    $('a.searchReset').click(function () {
        $('div.s_kenelle').html("Kuka on saajana?");
        $('div.s_location').html("Missä han asuu?");
        $('div.s_productgroup').html("Kategoria?");
        $('div.s_pricegroup').html("Hintaluokka?");
        $('div.s_tyyppi').html("Lahjakortin tyyppi?");
    });
    
    $('input.lahjakoodiInput, [name=s_freetext]').focus(function () {
        $(this).val("");
    }); 
    
    
    
});

function setValue(name, value)
{
    $('[name='+name+']').val(value);
}

function confirmMerkkipDel(form) {
	var answer = confirm("Haluatko varmasti poistaa merkkipäivän listalta");
	if (answer){
	   form.submit();
	}
}

function tabSlide(){
    var prevCont = $('#tabs div.active').attr('id').split('_')[1];
      if($(this).parents('div:eq(1)').attr('class')=='active'){return;}
      var tabNum = $(this).attr('class').split('_')[1];
      var height = $('#content_'+tabNum).height();
      $('#tabs div').removeClass('active');
      $(this).parents('div:eq(1)').addClass('active');
      $('#contentContainer').css('height', height+'px');
      $('#content_'+prevCont).css('left','-3000px');
      $('#content_'+tabNum).css('left','0');     
}

    
    function showAddress(address) {
       var geocoder = new GClientGeocoder();
      if (geocoder) 
      {
        geocoder.getLatLng(address, function(point) 
        {
            if (!point) 
            {
              //alert(address + " not found");
              point = new GLatLng(64.105485, 26.718750);
              map.openInfoWindowHtml(point, 'Kauppiaan antamaa osoitetta ei l&ouml;ytynyt.<br />'+ address);
            } 
            else 
            {
              map.setCenter(point, 13);
              var marker = new GMarker(point);
              map.addOverlay(marker);
                map.openInfoWindowHtml(point, '<h2>Osoite:</h2>' + address + '<br /><br />Reittiohjeet:<br /><a target="_blank" href="http://maps.google.fi/maps?daddr=' + address + '">T&auml;nne</a> | <a target="_blank" href="http://maps.google.fi/maps?&saddr=' + address + '">T&auml;&auml;lt&auml;</a>');
            }
          }
        );
      }
    }
    
function popupHO(url) {
	window.open(url, 'Hakuohjeet', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=500,height=450');
}
function popupK(url) {
	window.open(url, 'Kilpailu', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=720,height=675');
}